Check-in [123e544590]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added demo scripts showing two ways to get at GPIO pins from Lua.
Timelines: family | ancestors | descendants | both | scared
Files: files | file ages | folders
SHA1: 123e5445905a84305c96c856600a9bcbbee82b4d
User & Date: rberteig 2015-06-06 01:03:07
Context
2015-06-09
01:12
Use Roberto's struct to build the AU file header, and include the (empty) optional comment string at its minimum length. check-in: 1d21a5874d user: rberteig tags: scared
2015-06-06
01:03
Added demo scripts showing two ways to get at GPIO pins from Lua. check-in: 123e544590 user: rberteig tags: scared
2015-05-24
06:48
Very simple configuraton from scared.rc which must be a Lua chunk that returns a table containing configuration parameters. The only known parametner is address. check-in: 9f2b594b39 user: rberteig tags: scared
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added Scared/pinmmap.lua.































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
local GPIO=require"GPIO"

GPIO.setmode(GPIO.BOARD)

--[[
table.foreach(GPIO,function(k,v)
  if type(v)=="string" or type(v)=="number" then print(k,v) end
end
)
--]]

i2c={sda=3,scl=5}
--GPIO.setup(i2c.sda,GPIO.I2C)
--GPIO.setup(i2c.scl,GPIO.I2C)

uart={tx=8,rx=10}
--GPIO.setup(uart.tx,GPIO.SERIAL)
--GPIO.setup(uart.rx,GPIO.SERIAL)

spi={mosi=19,miso=21,sclk=23}
--GIO.setup(spi.mosi, GPIO.SPI)
--GIO.setup(spi.miso, GPIO.SPI)
--GIO.setup(spi.sclk, GPIO.SPI)

--[[
pins={11,12,13,15,16,18,22,24,26}
for _,p in ipairs(pins) do 
  GPIO.setup(p,GPIO.IN)
end
for _,p in ipairs(pins) do 
  print(p, GPIO.input(p))
end
--]]

local b = false
--GPIO.setup(26,GPIO.IN,GPIO.PUD_UP)
GPIO.setup(26,GPIO.IN)
while(true) do
  --GPIO.wait_for_edge(26, GPIO.BOTH)
  local b0 = GPIO.input(26)
  if b0 ~= b then
    print(b0)
  end
  b = b0
end

local pinfuncs={}
pinfuncs[GPIO.IN] = "in"
pinfuncs[GPIO.OUT] = "out"
pinfuncs[GPIO.SPI] = "spi"
pinfuncs[GPIO.I2C] = "i2c"
pinfuncs[GPIO.PWM] = "pwm"
pinfuncs[GPIO.SERIAL] = "ser"
pinfuncs[GPIO.UNKNOWN] = "unk"
setmetatable(pinfuncs,{__index=function(k) return tostring(k) end})

pins={3,5,7,8,10}

for i=1,#pins do
 print(i, pins[i], pinfuncs[GPIO.gpio_function(pins[i])])
end

GPIO.cleanup()

Added Scared/pinsysfs.lua.























































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
local P = require "posix"

-- write str to the named file
function putfile(name,str)
  local f = assert(io.open(name,"w"))
  f:write(str)
  f:close()
end

-- read the named file and return its content
function getfile(name)
  local f = assert(io.open(name,"r"))
  local str f:read"*a"
  f:close()
  return str
end

-- transform a list into a set for quick lookup
function SET(t)
  local s = {}
  for _,v in ipairs(t) do
    s[v] = true
  end
  return s
end


-- container for GPIO functions
gpio={
  pins1_1 = SET{ -- Original RPi A, B
     0,  1,          4,          7,
     8,  9, 10, 11,         14, 15,
        17, 18,         21, 22, 23,
    24, 25,       },

  pins1_2 = SET{ -- Rev 2 A, B
             2,  3,  4,          7,
     8,  9, 10, 11,         14, 15,
        17, 18,             22, 23,
    24, 25,     27},

  pins2_0 = SET{ -- A+, B+, 2A, 2B
             2,  3,  4,  5,  6,  7,
     8,  9, 10, 11, 12, 13, 14, 15,
    16, 17, 18, 19, 20, 21, 22, 23,
    24, 25, 26, 27},
}

-- default to RPi B+ board
-- TODO: Read /proc/cpuinfo and get this right
gpio.pins = gpio.pins2_0

-- export a sysfs pin to userland
function gpio.export(pin)
  assert(gpio.pins[pin], "Can't use pin")
  putfile("/sys/class/gpio/export",tostring(pin))
end

-- return a sysfs pin to the kernel
function gpio.unexport(pin)
  assert(gpio.pins[pin], "Can't use pin")
  putfile("/sys/class/gpio/unexport",tostring(pin))
end

-- userland path to a specific pin's atribute file
function gpio.pinpath(pin,attr)
  return ("/sys/class/gpio/gpio%d/%s"):format(pin,attr)
end

-- set pin direction
function gpio.direction(pin,dir)
  local path = gpio.pinpath(pin, "direction")
print("Setting "..path.." to "..dir)
  putfile(path, dir)
end

-- set pin edge detector
function gpio.edge(pin,edge)
  local path = gpio.pinpath(pin, "edge")
print("Setting "..path.." to "..edge)
  putfile(path, edge)
end

-- blocking wait with timeout for an edge on a pin
function gpio.wait(pin)
  assert(gpio.pins[pin], "Can't use pin")
  local path = gpio.pinpath(pin, "value")
  if not gpio.pinfd then 
    gpio.pinfd = P.open(path, P.O_RDONLY)
  end
  local p = gpio.pinfd
  if not gpio.fds then gpio.fds = {} end
  if not gpio.fds[p] then gpio.fds[p] = {events={}, revents={}} end
  gpio.fds[p].events.PRI=true
  local ret = P.poll(gpio.fds, 10000)
  if ret == 0 then return 0 end
  P.lseek(p,0,P.SEEK_SET)
  ret = tonumber(P.read(p,8))
  --P.close(p)
  --gpio.pinfd = nil
  return ret
end 


-- Use pin P1-28 aka GPIO7 for SPLear alerts
gpio.export(7)
gpio.direction(7,"in")
gpio.edge(7,"rising")

while(true) do
  local r = gpio.wait(7)
  if r == 0 then
    --print"timeout"
  elseif r==1 then
    local tim = P.gettimeofday()
    print("eek "..tim.sec..'.'..tim.usec)
  else
    print("???", r)
  end
end

if gpio.pinfd then P.close(gpio.pinfd) end