Skip to content

Commit

Permalink
Added new RGB LED lua scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
zyren committed Dec 6, 2023
1 parent 98902e7 commit 053619a
Show file tree
Hide file tree
Showing 12 changed files with 522 additions and 0 deletions.
61 changes: 61 additions & 0 deletions sdcard/bw128x64/SCRIPTS/RGBLED/Bback.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
local function init()
colorChangeTime = getTime() -- Initialize time
phase = 0
currentLed = 0 -- Current lit LED position
scroll_oldtime = getTime() -- Initialize scroll_oldtime
scroll_cycle = 0 -- Initialize scroll_cycle
end

-- Function to generate smooth cyclic colors
local function getColor(phase, length)
local position = (phase % length) / length
local r, g, b = 0, 0, 0
local maxBrightness = 255 -- Maximum brightness value

-- RGB color transition: red -> green -> blue -> red
if position < 1/3 then
-- From red to green
r = maxBrightness * (1 - 3 * position)
g = maxBrightness * (3 * position)
elseif position < 2/3 then
-- From green to blue
position = position - 1/3
g = maxBrightness * (1 - 3 * position)
b = maxBrightness * (3 * position)
else
-- From blue to red
position = position - 2/3
b = maxBrightness * (1 - 3 * position)
r = maxBrightness * (3 * position)
end

return r, g, b
end

local colorPhase = 0 -- Initialize color phase

local function run()
for i=LED_STRIP_LENGTH - 1, 0, -1 do -- Reverse iteration
if (i == scroll_cycle) then
local r, g, b = getColor(colorPhase, 255)
setRGBLedColor(i, r, g, b)
else
setRGBLedColor(i, 0, 0, 50)
end
end
if ((getTime() - scroll_oldtime) > 8) then
scroll_oldtime = getTime()
scroll_cycle = scroll_cycle - 1 -- Decrement scroll_cycle
if (scroll_cycle < 0) then
scroll_cycle = LED_STRIP_LENGTH - 1 -- Reset scroll_cycle to the end of the strip
end
end
colorPhase = (colorPhase + 1) % 255 -- Update color phase
applyRGBLedColors()
end

local function background()
-- Called periodically while the Special Function switch is off
end

return { run=run, background=background, init=init }
61 changes: 61 additions & 0 deletions sdcard/bw128x64/SCRIPTS/RGBLED/Bfwrd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
local function init()
colorChangeTime = getTime() -- Initialize time
phase = 0
currentLed = 0 -- Current lit LED position
scroll_oldtime = getTime() -- Initialize scroll_oldtime
scroll_cycle = 0 -- Initialize scroll_cycle
end

-- Function to generate smooth cyclic colors
local function getColor(phase, length)
local position = (phase % length) / length
local r, g, b = 0, 0, 0
local maxBrightness = 255 -- Maximum brightness value

-- RGB color transition: red -> green -> blue -> red
if position < 1/3 then
-- From red to green
r = maxBrightness * (1 - 3 * position)
g = maxBrightness * (3 * position)
elseif position < 2/3 then
-- From green to blue
position = position - 1/3
g = maxBrightness * (1 - 3 * position)
b = maxBrightness * (3 * position)
else
-- From blue to red
position = position - 2/3
b = maxBrightness * (1 - 3 * position)
r = maxBrightness * (3 * position)
end

return r, g, b
end

local colorPhase = 0 -- Initialize color phase

local function run()
for i=0, LED_STRIP_LENGTH - 1, 1 do
if (i == scroll_cycle) then
local r, g, b = getColor(colorPhase, 255)
setRGBLedColor(i, r, g, b)
else
setRGBLedColor(i, 0, 0, 50)
end
end
if ((getTime() - scroll_oldtime) > 8) then
scroll_oldtime = getTime()
scroll_cycle = scroll_cycle + 1
if (scroll_cycle >= LED_STRIP_LENGTH) then
scroll_cycle = 0
end
end
colorPhase = (colorPhase + 1) % 255 -- Update color phase
applyRGBLedColors()
end

local function background()
-- Called periodically while the Special Function switch is off
end

return { run=run, background=background, init=init }
61 changes: 61 additions & 0 deletions sdcard/bw128x64/SCRIPTS/RGBLED/Pback.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
local function init()
colorChangeTime = getTime() -- Initialize time
phase = 0
currentLed = 0 -- Current lit LED position
scroll_oldtime = getTime() -- Initialize scroll_oldtime
scroll_cycle = 0 -- Initialize scroll_cycle
end

-- Function to generate smooth cyclic colors
local function getColor(phase, length)
local position = (phase % length) / length
local r, g, b = 0, 0, 0
local maxBrightness = 255 -- Maximum brightness value

-- RGB color transition: red -> green -> blue -> red
if position < 1/3 then
-- From red to green
r = maxBrightness * (1 - 3 * position)
g = maxBrightness * (3 * position)
elseif position < 2/3 then
-- From green to blue
position = position - 1/3
g = maxBrightness * (1 - 3 * position)
b = maxBrightness * (3 * position)
else
-- From blue to red
position = position - 2/3
b = maxBrightness * (1 - 3 * position)
r = maxBrightness * (3 * position)
end

return r, g, b
end

local colorPhase = 0 -- Initialize color phase

local function run()
for i=LED_STRIP_LENGTH - 1, 0, -1 do -- Reverse iteration
if (i == scroll_cycle) then
local r, g, b = getColor(colorPhase, 255)
setRGBLedColor(i, r, g, b)
else
setRGBLedColor(i, 6, 0, 6)
end
end
if ((getTime() - scroll_oldtime) > 8) then
scroll_oldtime = getTime()
scroll_cycle = scroll_cycle - 1 -- Decrement scroll_cycle
if (scroll_cycle < 0) then
scroll_cycle = LED_STRIP_LENGTH - 1 -- Reset scroll_cycle to the end of the strip
end
end
colorPhase = (colorPhase + 1) % 255 -- Update color phase
applyRGBLedColors()
end

local function background()
-- Called periodically while the Special Function switch is off
end

return { run=run, background=background, init=init }
61 changes: 61 additions & 0 deletions sdcard/bw128x64/SCRIPTS/RGBLED/Pfwrd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
local function init()
colorChangeTime = getTime() -- Initialize time
phase = 0
currentLed = 0 -- Current lit LED position
scroll_oldtime = getTime() -- Initialize scroll_oldtime
scroll_cycle = 0 -- Initialize scroll_cycle
end

-- Function to generate smooth cyclic colors
local function getColor(phase, length)
local position = (phase % length) / length
local r, g, b = 0, 0, 0
local maxBrightness = 255 -- Maximum brightness value

-- RGB color transition: red -> green -> blue -> red
if position < 1/3 then
-- From red to green
r = maxBrightness * (1 - 3 * position)
g = maxBrightness * (3 * position)
elseif position < 2/3 then
-- From green to blue
position = position - 1/3
g = maxBrightness * (1 - 3 * position)
b = maxBrightness * (3 * position)
else
-- From blue to red
position = position - 2/3
b = maxBrightness * (1 - 3 * position)
r = maxBrightness * (3 * position)
end

return r, g, b
end

local colorPhase = 0 -- Initialize color phase

local function run()
for i=0, LED_STRIP_LENGTH - 1, 1 do
if (i == scroll_cycle) then
local r, g, b = getColor(colorPhase, 255)
setRGBLedColor(i, r, g, b)
else
setRGBLedColor(i, 5, 0, 5)
end
end
if ((getTime() - scroll_oldtime) > 8) then
scroll_oldtime = getTime()
scroll_cycle = scroll_cycle + 1
if (scroll_cycle >= LED_STRIP_LENGTH) then
scroll_cycle = 0
end
end
colorPhase = (colorPhase + 1) % 255 -- Update color phase
applyRGBLedColors()
end

local function background()
-- Called periodically while the Special Function switch is off
end

return { run=run, background=background, init=init }
53 changes: 53 additions & 0 deletions sdcard/bw128x64/SCRIPTS/RGBLED/cycle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local function init()
cycleTime = getTime() -- Initialize time
phase = 0
end

-- Function to generate smooth cyclic colors
local function getColor(phase, length)
local position = (phase % length) / length
local r, g, b = 5, 5, 5
local maxBrightness = 50 -- Maximum brightness value
local minBrightness = 20 -- Minimum brightness value

-- RGB color transition: red -> green -> blue -> red
if position < 1/3 then
-- From red to green
r = minBrightness + (maxBrightness - minBrightness) * (1 - 3 * position)
g = maxBrightness * (3 * position)
b = minBrightness
elseif position < 2/3 then
-- From green to blue
position = position - 1/3
g = minBrightness + (maxBrightness - minBrightness) * (1 - 3 * position)
b = maxBrightness * (3 * position)
r = minBrightness
else
-- From blue to red
position = position - 2/3
b = minBrightness + (maxBrightness - minBrightness) * (1 - 3 * position)
r = maxBrightness * (3 * position)
g = minBrightness
end

return math.max(0, math.min(r, maxBrightness)), math.max(0, math.min(g, maxBrightness)), math.max(0, math.min(b, maxBrightness))
end

local function run()
if ((getTime() - cycleTime) > 8) then -- Use an interval of 8 time units
cycleTime = getTime()
phase = phase + 1 -- Update color phase
end

for i = 0, LED_STRIP_LENGTH - 1, 1 do
local r, g, b = getColor(phase, 255)
setRGBLedColor(i, r, g, b)
end
applyRGBLedColors()
end

local function background()
-- Called periodically while the Special Function switch is off
end

return { run=run, background=background, init=init }
49 changes: 49 additions & 0 deletions sdcard/bw128x64/SCRIPTS/RGBLED/flow.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local function init()
colorChangeTime = getTime() -- 初始化时间
phase = 0
end

-- 生成平滑循环颜色的函数
local function getColor(phase, length)
local position = (phase % length) / length
local r, g, b = 30, 30, 30
local maxBrightness = 50 -- 最大亮度值

-- RGB颜色过渡:红 -> 绿 -> 蓝 -> 红
if position < 1/3 then
-- 从红到绿
r = maxBrightness * (1 - 3 * position)
g = maxBrightness * (3 * position)
elseif position < 2/3 then
-- 从绿到蓝
position = position - 1/3
g = maxBrightness * (1 - 3 * position)
b = maxBrightness * (3 * position)
else
-- 从蓝到红
position = position - 2/3
b = maxBrightness * (1 - 3 * position)
r = maxBrightness * (3 * position)
end

return math.max(0, math.min(r, maxBrightness)), math.max(0, math.min(g, maxBrightness)), math.max(0, math.min(b, maxBrightness))
end

local function run()
if ((getTime() - colorChangeTime) > 16) then -- 将时间间隔增加到16个时间单位
colorChangeTime = getTime()
phase = phase + 1 -- 更新颜色相位
end

for i = 0, LED_STRIP_LENGTH - 1, 1 do
local r, g, b = getColor(phase, 255)
setRGBLedColor(i, r, g, b)
end
applyRGBLedColors()
end

local function background()
-- Called periodically while the Special Function switch is off
end

return { run=run, background=background, init=init }
16 changes: 16 additions & 0 deletions sdcard/bw128x64/SCRIPTS/RGBLED/purple.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local function init()
end

local function run()
for i=0, LED_STRIP_LENGTH - 1, 1
do
setRGBLedColor(i, 30, 0, 30) -- Set to purple color
end
applyRGBLedColors()
end

local function background()
-- Called periodically while the Special Function switch is off
end

return { run=run, background=background, init=init }
Loading

0 comments on commit 053619a

Please sign in to comment.