From 1204809ac63fcbfc3a0c32cf067a4b04df044f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 12 Feb 2016 12:21:22 +0100 Subject: [PATCH] Basic GPIO driver --- include/luares.h | 1 + src/c/modules.c | 1 + src/lua/core/gpio.lua | 74 +++++++++++++++++++++++++++++++++++++++++++ src/lua/core/init.lua | 2 ++ 4 files changed, 78 insertions(+) create mode 100644 src/lua/core/gpio.lua diff --git a/include/luares.h b/include/luares.h index 15287d3..d47a749 100644 --- a/include/luares.h +++ b/include/luares.h @@ -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[]; diff --git a/src/c/modules.c b/src/c/modules.c index 8b371c4..82a64db 100644 --- a/src/c/modules.c +++ b/src/c/modules.c @@ -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); diff --git a/src/lua/core/gpio.lua b/src/lua/core/gpio.lua new file mode 100644 index 0000000..85abc38 --- /dev/null +++ b/src/lua/core/gpio.lua @@ -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 diff --git a/src/lua/core/init.lua b/src/lua/core/init.lua index b66abf6..caba669 100644 --- a/src/lua/core/init.lua +++ b/src/lua/core/init.lua @@ -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