fix(patterns): remove blocking sleeps from pattern loops

Replace sleep-based timing in pattern generators with non-blocking tick checks so long delays do not block the main loop and risk watchdog resets.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-04 22:37:33 +12:00
parent 2fcaf2f064
commit a79c6f4dd3
4 changed files with 73 additions and 35 deletions

View File

@@ -7,18 +7,38 @@ class StrobeBurst:
def run(self, preset):
colors = preset.c if preset.c else [(255, 255, 255)]
count = max(1, int(preset.n1) if int(preset.n1) > 0 else 3)
gap = max(1, int(preset.n2) if int(preset.n2) > 0 else 60)
cooldown = max(1, int(preset.n3) if int(preset.n3) > 0 else 400)
c = self.driver.apply_brightness(colors[0], preset.b)
state = "flash_on"
flash_idx = 0
state_start = utime.ticks_ms()
while True:
for _ in range(count):
count = max(1, int(preset.n1) if int(preset.n1) > 0 else 3)
gap = max(1, int(preset.n2) if int(preset.n2) > 0 else 60)
cooldown = max(1, int(preset.n3) if int(preset.n3) > 0 else 400)
on_ms = max(1, int(preset.d) // 2)
c = self.driver.apply_brightness(colors[0], preset.b)
now = utime.ticks_ms()
if state == "flash_on":
self.driver.fill(c)
utime.sleep_ms(max(1, int(preset.d)//2))
if utime.ticks_diff(now, state_start) >= on_ms:
state = "flash_off"
state_start = utime.ticks_add(state_start, on_ms)
elif state == "flash_off":
self.driver.fill((0, 0, 0))
utime.sleep_ms(gap)
yield
utime.sleep_ms(cooldown)
if utime.ticks_diff(now, state_start) >= gap:
flash_idx += 1
if flash_idx >= count:
if not preset.a:
return
state = "cooldown"
flash_idx = 0
state_start = utime.ticks_add(state_start, gap)
else:
state = "flash_on"
state_start = utime.ticks_add(state_start, gap)
else:
self.driver.fill((0, 0, 0))
if utime.ticks_diff(now, state_start) >= cooldown:
state = "flash_on"
state_start = utime.ticks_add(state_start, cooldown)
yield
if not preset.a:
return