58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
import utime
|
|
|
|
|
|
class Transition:
|
|
def __init__(self, driver):
|
|
self.driver = driver
|
|
|
|
def run(self, preset):
|
|
"""Transition between colors, blending over `delay` ms."""
|
|
colors = preset.c
|
|
if not colors:
|
|
self.driver.off()
|
|
yield
|
|
return
|
|
|
|
# Only one color: just keep it on
|
|
if len(colors) == 1:
|
|
while True:
|
|
self.driver.fill(self.driver.apply_brightness(colors[0], preset.b))
|
|
yield
|
|
return
|
|
|
|
color_index = 0
|
|
start_time = utime.ticks_ms()
|
|
|
|
while True:
|
|
if not colors:
|
|
break
|
|
|
|
# Get current and next color based on live list
|
|
c1 = colors[color_index % len(colors)]
|
|
c2 = colors[(color_index + 1) % len(colors)]
|
|
|
|
duration = max(10, int(preset.d)) # At least 10ms
|
|
now = utime.ticks_ms()
|
|
elapsed = utime.ticks_diff(now, start_time)
|
|
|
|
if elapsed >= duration:
|
|
# End of this transition step
|
|
if not preset.a:
|
|
# One-shot: transition from first to second color only
|
|
self.driver.fill(self.driver.apply_brightness(c2, preset.b))
|
|
break
|
|
# Auto: move to next pair
|
|
color_index = (color_index + 1) % len(colors)
|
|
start_time = now
|
|
yield
|
|
continue
|
|
|
|
# Interpolate between c1 and c2
|
|
factor = elapsed / duration
|
|
interpolated = tuple(
|
|
int(c1[i] + (c2[i] - c1[i]) * factor) for i in range(3)
|
|
)
|
|
self.driver.fill(self.driver.apply_brightness(interpolated, preset.b))
|
|
|
|
yield
|