Created
June 30, 2024 00:39
-
-
Save feelfreetofee/88f6d9fa8053e60fcc9384ef524579d3 to your computer and use it in GitHub Desktop.
WiringPi Lua GPIO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local uv = require('uv') | |
| local gpio = require('./wiringPi.lua') | |
| gpio.wiringPiSetup() | |
| local led = 2 | |
| local led_state = gpio.digitalRead(led) | |
| gpio.pinMode(led, 1) -- OUTPUT | |
| while true do | |
| led_state = led_state == 1 and 0 or 1 | |
| gpio.digitalWrite(led, led_state) | |
| uv.sleep(500) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local uv = require('uv') | |
| local gpio = require('./wiringPi.lua') | |
| gpio.wiringPiSetup() | |
| local switch = 0 | |
| local switch_state = 1 | |
| gpio.pinMode(switch, 0) -- INPUT | |
| gpio.pullUpDnControl(switch, 2) -- PULLED UP | |
| function cb() | |
| print(('Button %s'):format(switch_state == 1 and 'released' or 'pressed')) | |
| end | |
| while true do | |
| if gpio.digitalRead(switch) ~= switch_state then | |
| switch_state = switch_state == 1 and 0 or 1 | |
| cb() | |
| uv.sleep(300) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local uv = require('uv') | |
| local gpio = require('./wiringPi.lua') | |
| gpio.wiringPiSetup() | |
| local switch = 0 | |
| local switch_state = 1 | |
| gpio.pinMode(switch, 0) -- INPUT | |
| gpio.pullUpDnControl(switch, 2) -- PULLED UP | |
| local led = 2 | |
| local led_state = gpio.digitalRead(led) | |
| gpio.pinMode(led, 1) -- OUTPUT | |
| function cb() | |
| if switch_state == 1 then | |
| led_state = led_state == 1 and 0 or 1 | |
| gpio.digitalWrite(led, led_state) | |
| end | |
| end | |
| while true do | |
| if gpio.digitalRead(switch) ~= switch_state then | |
| switch_state = switch_state == 1 and 0 or 1 | |
| cb() | |
| uv.sleep(300) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local ffi = require('ffi') | |
| ffi.cdef[[ | |
| // Core wiringPi functions | |
| enum WPIPinType { | |
| WPI_PIN_BCM = 1, | |
| WPI_PIN_WPI, | |
| WPI_PIN_PHYS, | |
| }; | |
| void wiringPiVersion (int *major, int *minor); | |
| int wiringPiGlobalMemoryAccess(void); // Interface V3.3 | |
| int wiringPiUserLevelAccess (void); | |
| int wiringPiSetup (void); | |
| int wiringPiSetupSys (void); | |
| int wiringPiSetupGpio (void); | |
| int wiringPiSetupPhys (void); | |
| int wiringPiSetupPinType (enum WPIPinType pinType); // Interface V3.3 | |
| int wiringPiSetupGpioDevice(enum WPIPinType pinType); // Interface V3.3 | |
| enum WPIPinAlt { | |
| WPI_ALT_UNKNOWN = -1, | |
| WPI_ALT_INPUT = 0, | |
| WPI_ALT_OUTPUT, | |
| WPI_ALT5, | |
| WPI_ALT4, | |
| WPI_ALT0, | |
| WPI_ALT1, | |
| WPI_ALT2, | |
| WPI_ALT3, | |
| WPI_ALT6, | |
| WPI_ALT7, | |
| WPI_ALT8, | |
| WPI_ALT9, | |
| WPI_NONE = 0x1F, // Pi5 default | |
| }; | |
| int wiringPiGpioDeviceGetFd(); // Interface V3.3 | |
| void pinModeAlt(int pin, int mode); | |
| enum WPIPinAlt getPinModeAlt(int pin); // Interface V3.5, same as getAlt but wie enum | |
| void pinMode(int pin, int mode); | |
| void pullUpDnControl(int pin, int pud); | |
| int digitalRead(int pin); | |
| void digitalWrite(int pin, int value); | |
| unsigned int digitalRead8(int pin); | |
| void digitalWrite8(int pin, int value); | |
| void pwmWrite(int pin, int value); | |
| int analogRead(int pin); | |
| void analogWrite(int pin, int value); | |
| // On-Board Raspberry Pi hardware specific stuff | |
| int piGpioLayout(void); | |
| int piBoardRev(void); // Deprecated, but does the same as piGpioLayout | |
| void piBoardId(int *model, int *rev, int *mem, int *maker, int *overVolted); | |
| int wpiPinToGpio(int wpiPin); | |
| int physPinToGpio(int physPin); | |
| void setPadDrive(int group, int value); | |
| void setPadDrivePin(int pin, int value); // Interface V3.0 | |
| int getAlt(int pin); | |
| void pwmToneWrite(int pin, int freq); | |
| void pwmSetMode(int mode); | |
| void pwmSetRange(unsigned int range); | |
| void pwmSetClock(int divisor); | |
| void gpioClockSet(int pin, int freq); | |
| unsigned int digitalReadByte(void); | |
| unsigned int digitalReadByte2(void); | |
| void digitalWriteByte(int value); | |
| void digitalWriteByte2(int value); | |
| // Interrupts | |
| // (Also Pi hardware specific) | |
| int waitForInterrupt(int pin, int mS); | |
| int wiringPiISR(int pin, int mode, void (*function)(void)); | |
| int wiringPiISRStop(int pin); // V3.2 | |
| int waitForInterruptClose(int pin); // V3.2 | |
| // Threads | |
| int piThreadCreate(void *(*fn)(void *)); | |
| void piLock(int key); | |
| void piUnlock(int key); | |
| // Schedulling priority | |
| int piHiPri(const int pri); | |
| ]] | |
| return ffi.load('wiringPi.so') | |
| -- local definitions { | |
| -- -- wiringPi modes | |
| -- WPI_MODE_PINS = 0, | |
| -- WPI_MODE_GPIO = 1, | |
| -- WPI_MODE_GPIO_SYS = 2, -- deprecated since 3.2 | |
| -- WPI_MODE_PHYS = 3, | |
| -- WPI_MODE_PIFACE = 4, | |
| -- WPI_MODE_GPIO_DEVICE_BCM = 5, -- BCM pin numbers like WPI_MODE_GPIO | |
| -- WPI_MODE_GPIO_DEVICE_WPI = 6, -- WiringPi pin numbers like WPI_MODE_PINS | |
| -- WPI_MODE_GPIO_DEVICE_PHYS = 7, -- Physic pin numbers like WPI_MODE_PHYS | |
| -- WPI_MODE_UNINITIALISED = -1, | |
| -- -- Pin modes | |
| -- INPUT = 0, | |
| -- OUTPUT = 1, | |
| -- PWM_OUTPUT = 2, | |
| -- GPIO_CLOCK = 3, | |
| -- SOFT_PWM_OUTPUT = 4, | |
| -- SOFT_TONE_OUTPUT = 5, | |
| -- PWM_TONE_OUTPUT = 6, | |
| -- PM_OFF = 7, -- to input / release line | |
| -- LOW = 0, | |
| -- HIGH = 1, | |
| -- -- Pull up/down/none | |
| -- PUD_OFF = 0, | |
| -- PUD_DOWN = 1, | |
| -- PUD_UP = 2, | |
| -- -- PWM | |
| -- PWM_MODE_MS = 0, | |
| -- PWM_MODE_BAL = 1, | |
| -- Interrupt levels | |
| -- INT_EDGE_SETUP = 0, | |
| -- INT_EDGE_FALLING = 1, | |
| -- INT_EDGE_RISING = 2, | |
| -- INT_EDGE_BOTH = 3 | |
| -- } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment