-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledbar.lua
81 lines (64 loc) · 1.49 KB
/
ledbar.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
local PIXELS = 30*5
local BYTES_PER_LED = 3
--global LED buffer for all animations and PIXELS
local wsbuf = pixbuf.newBuffer(PIXELS, BYTES_PER_LED)
local POST = node.task.post
local PRIO = node.task.LOW_PRIORITY
local W = ws2812.write
local animFunc, animParams, animNumParams
local lbartmr = tmr.create()
local function off()
wsbuf:fill(0, 0, 0)
W(wsbuf)
tmr.wdclr()
end
local function stop()
lbartmr:unregister()
end
local timerTick
local function animate()
if animFunc then
local delay = animFunc(wsbuf, unpack(animParams, 1, animNumParams))
W(wsbuf)
if delay then
lbartmr:interval(delay)
lbartmr:start()
return
end
end
stop()
end
timerTick = function()
POST(PRIO, animate)
end
local function init()
ws2812.init()
end
local function start()
lbartmr:register(100, tmr.ALARM_SEMI, timerTick)
return lbartmr:start()
end
local function setFunction(f, ...)
animFunc = f
animParams = { ... }
animNumParams = select("#", ...)
end
local function ison()
return lbartmr:state()
end
local function info()
local on, mode = lbartmr:state()
local s = "tmrOn: " .. tostring(on) .. ", tmrMode: " .. tostring(mode) .. ", func: " .. tostring(animFunc)
print(s)
return s
end
return {
off = off,
start = start,
stop = stop,
init = init,
setFunction = setFunction,
ison = ison,
info = info,
wsbuf = wsbuf,
}