forked from jmechnich/esp8266-leds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.lua
67 lines (57 loc) · 1.4 KB
/
led.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
local P={}
led = P
P.pin = 2
P.nled = 5
require('ledbuffer')
P.buffer = ledbuffer.new{}
function P.clear()
for i,pix in pairs(P.buffer) do
P.buffer[i] = nil
end
end
function P.dump ()
for i,pix in pairs(P.buffer) do
print(i,unpack(pix))
end
end
local function getrange(i,max)
if i < max then
return string.char(unpack(P.buffer[i]))..getrange(i+1,max)
end
return string.char(unpack(P.buffer[max]))
end
function P.show_off(offset)
if offset == nil then offset = 0 end
if offset > 0 then return ws2812.writergb( P.pin, getrange(P.nled-offset+1,P.nled)..getrange(1,P.nled-offset)) end
if offset < 0 then return ws2812.writergb( P.pin, getrange(-offset+1,P.nled)..getrange(1,-offset)) end
return ws2812.writergb( P.pin, getrange(1,P.nled))
end
function P.show()
local buf = ""
for i=1,P.nled do
buf = buf..string.char(unpack(P.buffer[i]))
end
return ws2812.writergb( P.pin, buf)
end
function P.setUniColor(r,g,b)
if r == nil then r = 0 end
if g == nil then g = 0 end
if b == nil then b = 0 end
for i=1,P.nled do
P.buffer[i] = {r,g,b}
end
end
function P.setRainbow(offset)
require('colorsys')
if offset == nil then offset = 0 end
for i=1,P.nled do
local frac = (i-1+offset)%P.nled
local r,g,b = hsvToRgb(frac/P.nled, 1, 0.1)
P.buffer[i] = { r,g,b }
end
end
function P.off()
P.clear()
P.show()
end
return led