-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws2812.lua
92 lines (76 loc) · 2.38 KB
/
ws2812.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
shift_direction = 1
function reverse_shift()
--print('stop shifting...')
local delay_target = 1000
local interval = delay_ms
local multiplier = 0.5
-- slows
tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()
interval = interval / multiplier
tmr.interval(0, interval)
--print('interval', interval)
if multiplier > 1 and interval <= delay_target
or multiplier < 1 and interval >= delay_target then
--print('shifting reversed')
shift_direction = shift_direction * -1
tmr.interval(0, delay_ms)
tmr.unregister(1)
end
end)
end
--
ws2812.init()
local i, buffer = 0, ws2812.newBuffer(led_count, 3)
method = random_method()
h, s, l = math.random(), saturation, brightness
buffer:fill(0, 0, 0)
ws2812.write(buffer)
-- main cycle
tmr.alarm(0, 15, tmr.ALARM_AUTO, function()
buffer:shift(shift_direction, ws2812.SHIFT_CIRCULAR)
ws2812.write(buffer)
local pos = i % led_count
-- full strip cycle
if pos == 0 then
method = random_method()
print('method: ', method)
--print_power(buffer)
ws2812.write(buffer)
color_random_cycle = math.random(1, 255) / 255
end
-- fill method
if method == 0 then
-- rainbow
color = i % 255 / 255
elseif method == 1 then
-- waterfall
color = math.random(1, 255) / 255
else
-- solid
color = color_random_cycle
end
-- add pixel for fill shifted
buffer:set(1, hslToRgb(color, s, l, 1))
-- dead pixel after each led strip full cycle
if pos == 0 then
buffer:set(1, 0, 0, 0)
end
-- reverse flow effect
if reverse_chance > 0 and math.random(0, led_count / reverse_chance) == 1 then
reverse_shift()
end
-- dead pixel effect
if dead_picel_chance > 0 and math.random(0, led_count / dead_picel_chance) == 1 then
local color_pixel
if method == 1 then
-- черный пиксель, иначе будет не видно
color_pixel = {0, 0, 0}
else
-- рандомный цвет пикселя (потемнее, чтобы видно было)
color_pixel = {hslToRgb(math.random(1, 255) / 255 , s , l / 2, 1)}
end
buffer:set(1, color_pixel)
--print('random dead pixel')
end
i = i + 1
end)