Skip to content

Commit aed1d44

Browse files
committed
Initial commit
1 parent 134bee8 commit aed1d44

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
# rpio.lua
22
Pure Lua Raspberry Pi GPIO library
3+
4+
```lua
5+
local rpio = require 'rpio'
6+
local gpio = rpio(12)
7+
8+
gpio.set_direction('out')
9+
gpio.write(1)
10+
11+
gpio.set_direction('in')
12+
print(gpio.read())
13+
```
14+

rockspecs/gpio-git-0.rockspec

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package = 'rpio'
2+
version = 'git-0'
3+
source = {
4+
url = 'git://github.com/ryanplusplus/rpio.lua.git'
5+
}
6+
description = {
7+
summary = 'Pure Lua Raspberry Pi GPIO library',
8+
homepage = 'https://github.com/ryanplusplus/rpio.lua/',
9+
license = 'MIT <http://opensource.org/licenses/MIT>'
10+
}
11+
dependencies = {
12+
'lua >= 5.1'
13+
}
14+
build = {
15+
type = 'builtin',
16+
modules = {
17+
['rpio'] = 'rpio.lua'
18+
}
19+
}
20+

rockspecs/rpio-1.0-0.rockspec

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package = 'rpio'
2+
version = '1.0-0'
3+
source = {
4+
url = 'https://github.com/ryanplusplus/rpio.lua/archive/v1.0-0.tar.gz',
5+
dir = 'rpio.lua-1.0-0/src'
6+
}
7+
description = {
8+
summary = 'Pure Lua Raspberry Pi GPIO library',
9+
homepage = 'https://github.com/ryanplusplus/rpio.lua/',
10+
license = 'MIT <http://opensource.org/licenses/MIT>'
11+
}
12+
dependencies = {
13+
'lua >= 5.1'
14+
}
15+
build = {
16+
type = 'builtin',
17+
modules = {
18+
['rpio'] = 'rpio.lua'
19+
}
20+
}
21+

src/rpio.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
local gpio_subsystem = '/sys/class/gpio/'
2+
3+
local function write(path, value)
4+
local f = io.open(path, 'a+')
5+
f:write(tostring(value))
6+
f:close()
7+
end
8+
9+
local function read(path)
10+
local f = io.popen('cat ' .. path, 'r')
11+
local value = f:read('*a')
12+
f:close()
13+
return value
14+
end
15+
16+
local function exists(path)
17+
local f = io.open(path)
18+
if f then
19+
f:close()
20+
return true
21+
else
22+
return false
23+
end
24+
end
25+
26+
local function ready(device)
27+
return pcall(function()
28+
write(device .. 'value', 0)
29+
end)
30+
end
31+
32+
return function(which)
33+
local device = gpio_subsystem .. 'gpio' .. which .. '/'
34+
35+
if exists(device) then
36+
write(gpio_subsystem .. 'unexport', which)
37+
end
38+
39+
write(gpio_subsystem .. 'export', which)
40+
41+
while not ready(device) do
42+
os.execute('sleep 0.001')
43+
end
44+
45+
return {
46+
set_direction = function(direction)
47+
write(device .. 'direction', direction)
48+
end,
49+
50+
write = function(value)
51+
write(device .. 'value', value)
52+
end,
53+
54+
read = function()
55+
return read(device .. 'value')
56+
end
57+
}
58+
end
59+

0 commit comments

Comments
 (0)