-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblinkled.lua
103 lines (90 loc) · 1.77 KB
/
blinkled.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
local modname = ...
local M =
{
}
_G[modname] = M
local LED = nil
local MaxBlinkFreq = 10
local Blink = false
local BlinkFreq = 0
local BlinkCycle = 1
local BlinkCycle2 = 2
local BlinkCount = 0
local PrevR = 0
local PrevG = 0
local PrevB = 0
local CurrR = 0
local CurrG = 0
local CurrB = 0
local function Update()
if Blink then
if BlinkCount == 0 then
LED.color(CurrR, CurrG, CurrB)
PrevR = CurrR
PrevG = CurrG
PrevB = CurrB
elseif BlinkCount == BlinkCycle then
LED.color(0, 0, 0)
PrevR = 0
PrevG = 0
PrevB = 0
end
if BlinkCount < BlinkCycle2 then
BlinkCount = BlinkCount + 1
else
BlinkCount = 0
end
else
if PrevR ~= CurrR or PrevG ~= CurrG or PrevB ~= CurrB then
PrevR = CurrR
PrevG = CurrG
PrevR = CurrB
LED.color(CurrR, CurrG, CurrB)
end
end
end
function M.SetFreq(freq)
if freq == BlinlFreq then return end
BlinkFreq = freq
if freq == 0 then
Blink = false
return
end
if freq > MaxBlinkFreq then freq = MaxBlinkFreq end
BlinkCycle2 = math.ceil(MaxBlinkFreq * 2 / freq)
BlinkCycle = math.ceil (BlinkCycle2 / 2)
Blink = true
end
function M.SetColor(r, g, b)
CurrR = r
CurrG = g
CurrB = b
Update()
end
local _timer = nil
function M.init(led, maxFreq)
LED = assert(led, "led can't be nil")
if maxFreq then
if maxFreq > 10 then
assert(false, "maxFreq must be not more than 10")
end
MaxBlinkFreq = maxFreq
end
if not _timer then
LED.color(0,0,0)
PrevR = 0
PrevG = 0
PrevB = 0
Blink = false
_timer = tmr.create()
_timer:alarm(math.ceil(1000/MaxBlinkFreq/2), tmr.ALARM_AUTO, Update)
end
end
function M.close()
if _timer then
_timer:stop()
_timer:unregister()
_timer = nil
end
end
return M