41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
import random
|
|
import utime
|
|
|
|
|
|
class Flicker:
|
|
def __init__(self, driver):
|
|
self.driver = driver
|
|
|
|
def run(self, preset):
|
|
"""Random brightness between n1 (min) and b (max); delay d ms between updates."""
|
|
colors = preset.c if preset.c else [(255, 255, 255)]
|
|
color_index = 0
|
|
last_update = utime.ticks_ms()
|
|
|
|
def brightness_bounds():
|
|
lo = max(0, min(255, int(preset.n1)))
|
|
hi = max(0, min(255, int(preset.b)))
|
|
if lo > hi:
|
|
lo, hi = hi, lo
|
|
return lo, hi
|
|
|
|
if not preset.a:
|
|
lo, hi = brightness_bounds()
|
|
level = random.randint(lo, hi)
|
|
base = colors[color_index % len(colors)]
|
|
self.driver.fill(self.driver.apply_brightness(base, level))
|
|
yield
|
|
return
|
|
|
|
while True:
|
|
current_time = utime.ticks_ms()
|
|
delay_ms = max(1, int(preset.d))
|
|
lo, hi = brightness_bounds()
|
|
if utime.ticks_diff(current_time, last_update) >= delay_ms:
|
|
level = random.randint(lo, hi)
|
|
base = colors[color_index % len(colors)]
|
|
self.driver.fill(self.driver.apply_brightness(base, level))
|
|
color_index += 1
|
|
last_update = utime.ticks_add(last_update, delay_ms)
|
|
yield
|