Files
led-hoop/pico/src/patterns/flare.py
Jimmy 52a5f0f8c4 Add Pico presets engine, patterns, and tests.
Wire the Pico to UART-driven preset selection, add pattern modules and presets data, remove old p2p/settings code, and update tests and LED driver.

Made-with: Cursor
2026-03-03 19:28:11 +13:00

64 lines
2.0 KiB
Python

import utime
class Flare:
def __init__(self, driver):
self.driver = driver
def run(self, preset):
"""
Flare: on the strip used by the first roll head,
make the strip fade up to brightness over the delay time.
- c[0]: color1 for the first n1 LEDs
- c[1]: color2 for the rest of the strip
- n1: number of LEDs from the start of the strip that use color1
- d: fade-in duration in ms (time to reach full preset brightness b)
"""
strips = self.driver.strips
# Which strip to flare: last roll head, clamped to valid range
strip_idx = getattr(self.driver, "last_roll_head", 0)
if strip_idx < 0 or strip_idx >= len(strips):
strip_idx = 0
strip = strips[strip_idx]
n = strip.num_leds
colors = preset.c
base_c1 = colors[0] if len(colors) > 0 else (255, 255, 255)
base_c2 = colors[1] if len(colors) > 1 else (0, 0, 0)
count_c1 = max(0, min(int(preset.n1), n))
fade_ms = max(1, int(preset.d) or 1)
target_b = int(preset.b) if hasattr(preset, "b") else 255
start_time = utime.ticks_ms()
done = False
while True:
now = utime.ticks_ms()
elapsed = utime.ticks_diff(now, start_time)
if not done:
if elapsed >= fade_ms:
factor = 1.0
done = True
else:
factor = elapsed / fade_ms if fade_ms > 0 else 1.0
else:
factor = 1.0
# Effective per-preset brightness scaled over time
current_b = int(target_b * factor)
# Apply global + local brightness to both colors
c1 = self.driver.apply_brightness(base_c1, current_b)
c2 = self.driver.apply_brightness(base_c2, current_b)
for i in range(n):
strip.set(i, c1 if i < count_c1 else c2)
strip.show()
yield