38 lines
1.4 KiB
Python
38 lines
1.4 KiB
Python
import random
|
|
import utime
|
|
|
|
|
|
class Snowfall:
|
|
def __init__(self, driver):
|
|
self.driver = driver
|
|
|
|
def run(self, preset):
|
|
colors = preset.c if preset.c else [(255, 255, 255), (180, 220, 255)]
|
|
density = max(1, int(preset.n1) if int(preset.n1) > 0 else 20)
|
|
speed = max(1, int(preset.n2) if int(preset.n2) > 0 else 1)
|
|
flakes = []
|
|
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(colors[-1], preset.b)
|
|
if random.randint(0, 255) < density:
|
|
flakes.append([self.driver.num_leds - 1, random.randint(0, len(colors)-1)])
|
|
for i in range(self.driver.num_leds):
|
|
self.driver.n[i] = bg_color
|
|
nf = []
|
|
for pos, ci in flakes:
|
|
if 0 <= pos < self.driver.num_leds:
|
|
self.driver.n[pos] = self.driver.apply_brightness(colors[ci], preset.b)
|
|
pos -= speed
|
|
if pos >= -1:
|
|
nf.append([pos, ci])
|
|
flakes = nf
|
|
self.driver.n.write()
|
|
last = utime.ticks_add(last, d)
|
|
if not preset.a:
|
|
yield
|
|
return
|
|
yield
|