Align pattern background rendering to use preset.background_or(...) and update pulse/radiate single-step behaviour to preserve visible frames and step progression.
42 lines
1.7 KiB
Python
42 lines
1.7 KiB
Python
import random
|
|
import utime
|
|
|
|
|
|
class RainDrops:
|
|
def __init__(self, driver):
|
|
self.driver = driver
|
|
|
|
def run(self, preset):
|
|
colors = preset.c if preset.c else [(120, 180, 255)]
|
|
rate = max(1, int(preset.n1) if int(preset.n1) > 0 else 32)
|
|
width = max(1, int(preset.n2) if int(preset.n2) > 0 else 3)
|
|
drops = []
|
|
last = utime.ticks_ms()
|
|
while True:
|
|
d = max(1, int(preset.d))
|
|
now = utime.ticks_ms()
|
|
if utime.ticks_diff(now, last) >= d:
|
|
bg_color = self.driver.apply_brightness(preset.background_or(colors), preset.b)
|
|
for i in range(self.driver.num_leds):
|
|
self.driver.n[i] = bg_color
|
|
if random.randint(0, 255) < rate:
|
|
drops.append([random.randint(0, max(0, self.driver.num_leds - 1)), 0])
|
|
nd = []
|
|
for pos, age in drops:
|
|
for off in range(-width, width + 1):
|
|
idx = pos + off
|
|
if 0 <= idx < self.driver.num_leds:
|
|
s = 255 - min(255, abs(off) * 255 // max(1, width + 1) + age * 40)
|
|
base = self.driver.apply_brightness(colors[age % len(colors)], preset.b)
|
|
self.driver.n[idx] = ((base[0]*s)//255, (base[1]*s)//255, (base[2]*s)//255)
|
|
age += 1
|
|
if age < 8:
|
|
nd.append([pos, age])
|
|
drops = nd
|
|
self.driver.n.write()
|
|
last = utime.ticks_add(last, d)
|
|
if not preset.a:
|
|
yield
|
|
return
|
|
yield
|