Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanplusplus committed May 27, 2016
1 parent 134bee8 commit aed1d44
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# rpio.lua
Pure Lua Raspberry Pi GPIO library

```lua
local rpio = require 'rpio'
local gpio = rpio(12)

gpio.set_direction('out')
gpio.write(1)

gpio.set_direction('in')
print(gpio.read())
```

20 changes: 20 additions & 0 deletions rockspecs/gpio-git-0.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package = 'rpio'
version = 'git-0'
source = {
url = 'git://github.com/ryanplusplus/rpio.lua.git'
}
description = {
summary = 'Pure Lua Raspberry Pi GPIO library',
homepage = 'https://github.com/ryanplusplus/rpio.lua/',
license = 'MIT <http://opensource.org/licenses/MIT>'
}
dependencies = {
'lua >= 5.1'
}
build = {
type = 'builtin',
modules = {
['rpio'] = 'rpio.lua'
}
}

21 changes: 21 additions & 0 deletions rockspecs/rpio-1.0-0.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package = 'rpio'
version = '1.0-0'
source = {
url = 'https://github.com/ryanplusplus/rpio.lua/archive/v1.0-0.tar.gz',
dir = 'rpio.lua-1.0-0/src'
}
description = {
summary = 'Pure Lua Raspberry Pi GPIO library',
homepage = 'https://github.com/ryanplusplus/rpio.lua/',
license = 'MIT <http://opensource.org/licenses/MIT>'
}
dependencies = {
'lua >= 5.1'
}
build = {
type = 'builtin',
modules = {
['rpio'] = 'rpio.lua'
}
}

59 changes: 59 additions & 0 deletions src/rpio.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local gpio_subsystem = '/sys/class/gpio/'

local function write(path, value)
local f = io.open(path, 'a+')
f:write(tostring(value))
f:close()
end

local function read(path)
local f = io.popen('cat ' .. path, 'r')
local value = f:read('*a')
f:close()
return value
end

local function exists(path)
local f = io.open(path)
if f then
f:close()
return true
else
return false
end
end

local function ready(device)
return pcall(function()
write(device .. 'value', 0)
end)
end

return function(which)
local device = gpio_subsystem .. 'gpio' .. which .. '/'

if exists(device) then
write(gpio_subsystem .. 'unexport', which)
end

write(gpio_subsystem .. 'export', which)

while not ready(device) do
os.execute('sleep 0.001')
end

return {
set_direction = function(direction)
write(device .. 'direction', direction)
end,

write = function(value)
write(device .. 'value', value)
end,

read = function()
return read(device .. 'value')
end
}
end

0 comments on commit aed1d44

Please sign in to comment.