feat(patterns): merge pattern styles and add mode support
Consolidate legacy pattern ids into meteor, particles, sparkle, chase, and colour_cycle with n6/mode style selection; add pattern_modes helper, self-contained tests/all.py, and preset mode alias on wire. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
794f1a2841
commit
55a97ac51c
44 files changed
+998
-1539
No files matched your search
+69
-5
@@ -1,12 +1,16 @@
|
||||
import math
|
||||
import utime
|
||||
|
||||
from patterns.pattern_modes import style_mode
|
||||
|
||||
_LEGACY = {"northern_wave": 1}
|
||||
|
||||
|
||||
class Aurora:
|
||||
def __init__(self, driver):
|
||||
self.driver = driver
|
||||
|
||||
def run(self, preset):
|
||||
colors = preset.c if preset.c else [(40, 200, 140), (80, 120, 255), (160, 80, 220)]
|
||||
def _run_bands(self, preset, colors):
|
||||
bands = max(1, int(preset.n1) if int(preset.n1) > 0 else 3)
|
||||
shimmer = max(0, min(255, int(preset.n2) if int(preset.n2) > 0 else 40))
|
||||
phase = self.driver.step % 256
|
||||
@@ -16,11 +20,17 @@ class Aurora:
|
||||
now = utime.ticks_ms()
|
||||
if utime.ticks_diff(now, last) >= d:
|
||||
for i in range(self.driver.num_leds):
|
||||
idx = ((i * bands) // max(1, self.driver.num_leds) + (phase // 32)) % len(colors)
|
||||
idx = (
|
||||
(i * bands) // max(1, self.driver.num_leds) + (phase // 32)
|
||||
) % len(colors)
|
||||
c = self.driver.apply_brightness(colors[idx], preset.b)
|
||||
w = (255 - abs(128 - ((i * 8 + phase) & 255)) * 2)
|
||||
w = 255 - abs(128 - ((i * 8 + phase) & 255)) * 2
|
||||
w = max(0, min(255, w + shimmer))
|
||||
self.driver.n[i] = ((c[0]*w)//255, (c[1]*w)//255, (c[2]*w)//255)
|
||||
self.driver.n[i] = (
|
||||
(c[0] * w) // 255,
|
||||
(c[1] * w) // 255,
|
||||
(c[2] * w) // 255,
|
||||
)
|
||||
self.driver.n.write()
|
||||
phase = (phase + 1) & 255
|
||||
self.driver.step = phase
|
||||
@@ -29,3 +39,57 @@ class Aurora:
|
||||
yield
|
||||
return
|
||||
yield
|
||||
|
||||
def _run_northern(self, preset, colors):
|
||||
period = max(4, int(preset.n1) if int(preset.n1) > 0 else 20)
|
||||
contrast = max(1, min(255, int(preset.n2) if int(preset.n2) > 0 else 200))
|
||||
drift = max(1, int(preset.n3) if int(preset.n3) > 0 else 2)
|
||||
phase = 0
|
||||
last = utime.ticks_ms()
|
||||
ncols = len(colors)
|
||||
if ncols < 2:
|
||||
colors = list(colors) + [(120, 180, 255)]
|
||||
ncols = len(colors)
|
||||
twopi = 6.2831853
|
||||
|
||||
def lerp3(a, b, f):
|
||||
return (
|
||||
a[0] + ((b[0] - a[0]) * f) // 255,
|
||||
a[1] + ((b[1] - a[1]) * f) // 255,
|
||||
a[2] + ((b[2] - a[2]) * f) // 255,
|
||||
)
|
||||
|
||||
while True:
|
||||
d = max(1, int(preset.d))
|
||||
now = utime.ticks_ms()
|
||||
if utime.ticks_diff(now, last) >= d:
|
||||
bg = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
||||
for i in range(self.driver.num_leds):
|
||||
t = (i * twopi / period) + (phase * twopi / 256.0)
|
||||
w = (math.sin(t) + 1.0) * 0.5
|
||||
u = w * (ncols - 1) * 256.0
|
||||
fi = int(u) >> 8
|
||||
frac = int(u) & 255
|
||||
if fi >= ncols - 1:
|
||||
fi = ncols - 2
|
||||
frac = 255
|
||||
peak = lerp3(colors[fi], colors[fi + 1], frac)
|
||||
peak = self.driver.apply_brightness(peak, preset.b)
|
||||
mixf = min(255, int(w * contrast * 2) >> 1)
|
||||
self.driver.n[i] = lerp3(bg, peak, mixf)
|
||||
self.driver.n.write()
|
||||
phase = (phase + drift) % 256
|
||||
last = utime.ticks_add(last, d)
|
||||
if not preset.a:
|
||||
yield
|
||||
return
|
||||
yield
|
||||
|
||||
def run(self, preset):
|
||||
"""Aurora bands (n6=0) or sine northern wave (n6=1, legacy northern_wave)."""
|
||||
colors = preset.c if preset.c else [(40, 200, 140), (80, 120, 255), (160, 80, 220)]
|
||||
if style_mode(preset, 0, _LEGACY) == 1:
|
||||
colors = preset.c if preset.c else [(20, 55, 120), (60, 140, 220), (180, 220, 255)]
|
||||
yield from self._run_northern(preset, colors)
|
||||
return
|
||||
yield from self._run_bands(preset, colors)
|
||||
Reference in new issue
Block a user