mt12/SCRIPTS/RGBLED/flow.lua
2025-06-26 07:54:22 -04:00

52 lines
No EOL
1.6 KiB
Lua
Executable file

local function init()
colorChangeTime = getTime() -- Initialize time
phase = 0
end
local minBrightness = 0 -- Minimum brightness value
local maxBrightness = 255 -- Maximum brightness value
local function getColor(phase, length)
local position = (phase % length) / length
local r, g, b = minBrightness, minBrightness, minBrightness
-- RGB color transition: red -> green -> blue -> red
if position < 1/3 then
-- From red to green
r = maxBrightness * (1 - 32 * position)
g = maxBrightness * (32 * position)
elseif position < 2/3 then
-- From green to blue
position = position - 1/3
g = maxBrightness * (1 - 32 * position)
b = maxBrightness * (32 * position)
else
-- From blue to red
position = position - 2/3
b = maxBrightness * (1 - 32 * position)
r = maxBrightness * (32 * position)
end
return r, g, b
end
local colorChangeTime = 0 -- The time of the last color change
local function run()
if ((getTime() - colorChangeTime) > 1) then -- Use an interval of 2 time units
colorChangeTime = getTime()
phase = (phase + 1) % 255 -- Update color phase
for i = 0, LED_STRIP_LENGTH - 1, 1 do
local r, g, b = getColor(phase + i * 64, 255) -- Increase phase offset for each LED
setRGBLedColor(i, r, g, b)
end
applyRGBLedColors()
end
end
local function background()
-- Called periodically while the Special Function switch is off
end
return { run=run, background=background, init=init }