-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.lua
41 lines (35 loc) · 1.17 KB
/
input.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
input = {}
local inputs = { [constants.OYO.ios[1].inputPin] = 0, [constants.OYO.ios[2].inputPin] = 0, [constants.OYO.ios[3].inputPin] = 0, [constants.OYO.ios[4].inputPin] = 0 }
local subscriber = { notify = nil }
function input.initialize()
for pin, level in pairs(inputs) do
gpio.mode(pin, gpio.INPUT)
gpio.write(pin, level)
detectChanges()
end
end
function detectChanges()
tmr.alarm(6, 500, tmr.ALARM_AUTO, function()
for pin, level in pairs(inputs) do
if inputs[pin] ~= gpio.read(pin) then
inputs[pin] = gpio.read(pin)
if subscriber.notify then
subscriber.notify({ event = constants.events.INPUT_CHANGE, inputPin = pin, inputLevel = gpio.read(pin) })
end
end
end
end)
end
function input.subscribe(callback)
subscriber.notify = callback
end
function input.unsubscribe()
subscriber.notify = nil
end
function input.getState()
local state = {}
for pin, level in pairs(inputs) do
table.insert(state, { inputLevel = gpio.read(pin), inputPin = pin })
end
return { event = constants.events.INITIAL, ios = state }
end