You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
4.5 KiB

dofile("trigonometric.lua")
dofile("lookup.lua")
dofile("colorutils.lua")
local LED_COUNT = length
ws2812.init()
strip = ws2812.newBuffer(LED_COUNT, 3)
function colorSet(r, g, b)
strip:fill(lup(g),lup(r), lup(b))
ws2812.write(strip)
end
function allOff()
colorSet(0,0,0)
end
function colorSetPartial(r, g, b, percentage)
percentage = percentage % 100
for i=1,math.floor(percentage/100*strip:size())-1 do
strip:set(i,lup(g),lup(r), lup(b))
end
for i=math.floor(percentage/100*strip:size()),strip:size() do
strip:set(i,lup(math.floor(g/3)),lup(math.floor(r/3)), lup(math.floor(b/3)))
end
ws2812.write(strip)
end
function rainbowSetPartial(rotation, percentage)
rotation = rotation % strip:size()
percentage = percentage % 100
local offset = rotation/strip:size()
if math.floor(percentage/100*strip:size()) > 0 then
for i=1,math.floor(percentage/100*strip:size()) do
h_value = (i/strip:size() + offset) % 1
r,g,b = hsvToRgb(h_value, 1, .8, 1)
strip:set(i, lup(g), lup(r), lup(b))
end
end
for i=math.floor(percentage/100*strip:size()),strip:size() do
if i == 0 then
i = 1
end
h_value = (i/strip:size() + offset) % 1
r,g,b = hsvToRgb(h_value, 1, .3, 1)
strip:set(i, lup(g), lup(r), lup(b))
end
ws2812.write(strip)
end
function colorWipe(r, g, b)
allOff()
local i = 1
local t = tmr.create()
t:register(20, tmr.ALARM_AUTO, function()
strip:set(i, lup(g),lup(r), lup(b))
ws2812.write(strip)
i = i+1
if ( i == LED_COUNT) then
t:unregister()
end
end)
t:start()
end
function colorStrobe(r, g, b, strobes)
local j=1
local t = tmr.create()
local lr = lup(r)
local lg = lup(g)
local lb = lup(b)
t:register(70, tmr.ALARM_AUTO, function()
if j%10 == 1 or j%10 == 3 then
strip:fill(lg,lr,lb)
ws2812.write(strip)
else
strip:fill(0,0,0)
ws2812.write(strip)
end
j = j+1
if ( j == 10*strobes) then
t:unregister()
end
end)
t:start()
end
function colorPulse(r, g, b, iterations)
h, s, v = color_utils.grb2hsv(g, r, b)
local steps = 100
local step = 1
local t = tmr.create()
t:register(15, tmr.ALARM_AUTO, function()
local f = 0.5-0.5*fcos(360/steps*step)
g,r,b = color_utils.hsv2grb(h, s, math.floor(f*v))
strip:fill(g,r,b)
ws2812.write(strip)
step = step + 1
if (step > steps*iterations) then
t:unregister()
end
end)
t:start()
end
function colorChase(r, g, b, iterations)
allOff()
local segments = 6
local segment_length = LED_COUNT/segments
local t = tmr.create()
for i=1,LED_COUNT do
if (math.floor(i/segment_length)%3==0) then
strip:set(i,lup(g),lup(r), lup(b))
end
end
local step = 1
local steps = 3
t:register(100, tmr.ALARM_AUTO, function()
ws2812.write(strip)
strip:shift(segment_length, ws2812.SHIFT_CIRCULAR)
step = step+1
if (step > steps*iterations) then
t:unregister()
allOff()
end
end)
t:start()
end
function roundWipe(r, g, b, rounds)
allOff()
local segments = 6
local segment_length = LED_COUNT/segments
local step = math.floor(segment_length/3)
for i=1,segment_length do
strip:set(i, lup(g),lup(r), lup(b))
end
local j=1
local t = tmr.create()
t:register(24, tmr.ALARM_AUTO, function()
ws2812.write(strip)
strip:shift(step, ws2812.SHIFT_CIRCULAR)
j = j+step
if (j > LED_COUNT*rounds) then
t:unregister()
allOff()
end
end)
t:start()
end
function colorRand(rounds)
allOff()
local j = 1
local t = tmr.create()
t:register(24, tmr.ALARM_AUTO, function()
strip:fill(0,0,0)
ws2812.write(strip)
local r=node.random(255)
local g=node.random(255)
local b=node.random(255)
local i=node.random(LED_COUNT)
strip:set(i, g,r,b)
ws2812.write(strip)
j = j+1
if (j > 24*rounds) then
t:unregister()
allOff()
end
end)
t:start()
end
function clamp(val, lower, upper)
return math.max(lower, math.min(upper, val))
end
function activateLight(data)
local s = data["effect"]
local r = clamp(0, data['r'], 255)
local g = clamp(0, data['g'], 255)
local b = clamp(0, data['b'], 255)
local i = clamp(0, data['iterations'], 100)
if s =='color' then
colorSet(r, g, b)
elseif s == 'wipe' then
colorWipe(r, g, b)
elseif s == 'chase' then
colorChase(r, g, b, i)
elseif s == 'strobe' then
colorStrobe(r, g, b, i)
elseif s == 'pulse' then
colorPulse(r, g, b, i)
elseif s == 'round' then
roundWipe(r, g, b, i)
elseif s == 'rand' then
colorRand(i)
elseif s == 'partial' then
colorSetPartial(r, g, b, i)
elseif s == 'rainbow' then
rainbowSetPartial(r, i)
elseif s == 'off' then
allOff()
end
end