Basic GPIO driver

This commit is contained in:
Łukasz Magiera 2016-02-12 12:21:22 +01:00
parent 21624f310c
commit 1204809ac6
4 changed files with 78 additions and 0 deletions

View File

@ -6,6 +6,7 @@ extern char lua_computer[];
extern char lua_debug[];
extern char lua_eeprom[];
extern char lua_filesystem[];
extern char lua_gpio[];
extern char lua_init[];
extern char lua_internet[];
extern char lua_sandbox[];

View File

@ -16,6 +16,7 @@ void setup_modules(lua_State *L) {
pushstuple(L, "debug", lua_debug);
pushstuple(L, "eeprom", lua_eeprom);
pushstuple(L, "filesystem", lua_filesystem);
pushstuple(L, "gpio", lua_gpio);
pushstuple(L, "internet", lua_internet);
pushstuple(L, "sandbox", lua_sandbox);
pushstuple(L, "textgpu", lua_textgpu);

74
src/lua/core/gpio.lua Normal file
View File

@ -0,0 +1,74 @@
local gpio = {}
local fopen = native.fs_open
local fread = native.fs_read
local fwrite = native.fs_write
local fclose = native.fs_close
local fexists = native.fs_exists
local function _read(file)
local fd = fopen(file, "r")
if not fd then
return nil, "Can't open " .. file .. " for reading"
end
local v = fread(fd, 1024)
fclose(fd)
return v
end
local function _write(file, value)
local fd = fopen(file, "w")
if not fd then
return false, "Can't open " .. file .. " for writing"
end
fwrite(fd, value)
fclose(fd)
return true
end
gpio.register = function ()
if not fexists("/sys/class/gpio") then
return
end
local component = {}
function component.pinMode(pin, mode) --TODO: return current mode if no new is specified
checkArg(1, pin, "number")
checkArg(2, mode, "string")
pin = tostring(math.floor(pin))
if mode ~= "in" and mode ~= "out" then
return false, "Invalid mode string"
end
if not fexists("/sys/class/gpio/gpio" .. pin) then
_write("/sys/class/gpio/export", pin)
end
if not _write("/sys/class/gpio/gpio" .. pin .. "/direction", mode) then
return false, "Couldn't set pin mode"
end
return mode
end
function component.write(pin, value)
checkArg(1, pin, "number")
checkArg(2, pin, "boolean", "number", "nil")
pin = tostring(math.floor(pin))
value = (value == true or value > 0) and "1" or "0"
if not fexists("/sys/class/gpio/gpio" .. pin) then
return false, "Set pin mode first"
end
if not _write("/sys/class/gpio/gpio" .. pin .. "/value", value) then
return false, "Couldn't set pin value"
end
end
function component.read(pin)
checkArg(1, pin, "number")
pin = tostring(math.floor(pin))
if not fexists("/sys/class/gpio/gpio" .. pin) then
return false, "Set pin mode first"
end
return _read("/sys/class/gpio/gpio" .. pin .. "/value")
end
return modules.component.api.register(uuid, "gpio", component)
end
return gpio

View File

@ -61,6 +61,7 @@ function main()
--Components
loadModule("eeprom")
loadModule("gpio")
loadModule("textgpu")
loadModule("filesystem")
loadModule("internet")
@ -75,6 +76,7 @@ function main()
_G.pushEvent = modules.computer.api.pushSignal
modules.eeprom.register()
modules.gpio.register()
modules.internet.start()
modules.filesystem.register("root")
if native.debug then