Skip to content

Instantly share code, notes, and snippets.

@feelfreetofee
Created May 5, 2024 20:08
Show Gist options
  • Select an option

  • Save feelfreetofee/a3abc2aa01eaa76aee2789e9e8ed872d to your computer and use it in GitHub Desktop.

Select an option

Save feelfreetofee/a3abc2aa01eaa76aee2789e9e8ed872d to your computer and use it in GitHub Desktop.
Raspberry Pi GPIO with Lua
local ffi = require('ffi')
local lib = ffi.load('libpilwgpio.so') -- https://github.com/besp9510/pi_lw_gpio
-- GPIO operation function prototypes:
ffi.cdef[[
int gpio_set(int p);
int gpio_clear(int p);
int gpio_read_level(int p);
int gpio_set_mode(int mode, int p);
int gpio_read_mode(int p);
]]
-- GPIO mode aliases:
local GPIO_INPUT = 0x00
local GPIO_OUTPUT = 0x01
-- Error numbers:
local ENOPIVER = 140 -- Could not get PI board revision
-- Test:
function read_gpio()
-- Store values and modes:
local read_gpio_values = {}
local read_gpio_modes = {}
-- Read all pins and save to revert later:
for i = 0, 29 do
table.insert(read_gpio_values, lib.gpio_read_level(i))
table.insert(read_gpio_modes, lib.gpio_read_mode(i))
end
-- Display the results in a table:
print('+---+---+----++----+---+---+\n| M | V | GPIO Pin | V | M |\n+---+---+----++----+---+---+')
for i = 1, 30, 2 do
print(('| %s | %d | %02d || %02d | %d | %s |'):format(
read_gpio_modes[i] == GPIO_INPUT and '-' or '+',
read_gpio_values[i], i, i + 1, read_gpio_values[i + 1],
read_gpio_modes[i + 1] == GPIO_INPUT and '-' or '+'
))
end
print('+---+---+----++----+---+---+\n| M | V | GPIO Pin | V | M |\n+---+---+----++----+---+---+')
end
-- Simple GPIO test script
function test()
-- Store values and modes:
local read_gpio_values = {}
local read_gpio_modes = {}
-- Read all pins:
for i = 0, 29 do
table.insert(read_gpio_values, lib.gpio_read_level(i))
table.insert(read_gpio_modes, lib.gpio_read_mode(i))
end
-- Begin by reading modes and levels:
print('Reading all pins')
-- This will prints results to the screen in a table:
read_gpio()
-- Next by setting all to output:
print('All pins to output')
-- Set all pins to output:
for i = 0, 29 do
lib.gpio_set_mode(GPIO_OUTPUT, i)
end
read_gpio()
-- Then set (V = 1):
print('All pins set')
-- Set all pins:
for i = 0, 29 do
lib.gpio_set(i)
end
read_gpio()
-- Next clear (V = 0):
print('All pins cleared')
-- Clear all pins:
for i = 0, 29 do
lib.gpio_clear(i)
end
read_gpio()
-- Lastly, set to input:
print('All pins to input')
-- Set all pins to input
for i = 0, 29 do
lib.gpio_set_mode(GPIO_INPUT, i)
end
read_gpio()
-- Even more lastly, set to modes and levels back to where they were:
print('All pins reverted')
-- Set all pins to output
for i = 0, 29 do
-- The read modes will map directly to GPIO_INPUT and GPIO_OUTPUT:
lib.gpio_set_mode(read_gpio_modes[i + 1], i)
-- Set or clear depending on last read value:
lib[read_gpio_values[i + 1] == 1 and 'gpio_set' or 'gpio_clear'](i)
end
read_gpio()
end
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment