Switch to using itterators

This commit is contained in:
2025-08-30 21:31:24 +12:00
parent 9b96a6d4a9
commit 90e1511651
2 changed files with 186 additions and 161 deletions

View File

@@ -8,20 +8,20 @@ class Patterns(PatternsBase):
def __init__(self, pin, num_leds, color1=(0,0,0), color2=(0,0,0), brightness=127, selected="rainbow_cycle", delay=100): def __init__(self, pin, num_leds, color1=(0,0,0), color2=(0,0,0), brightness=127, selected="rainbow_cycle", delay=100):
super().__init__(pin, num_leds, color1, color2, brightness, selected, delay) super().__init__(pin, num_leds, color1, color2, brightness, selected, delay)
self.patterns = { self.patterns = {
"off": self.off, "off": self.off(),
"on" : self.on, "on" : self.on(),
"color_wipe": self.color_wipe_step, "color_wipe": self.color_wipe_step(),
"rainbow_cycle": self.rainbow_cycle_step, "rainbow_cycle": self.rainbow_cycle_step(),
"theater_chase": self.theater_chase_step, "theater_chase": self.theater_chase_step(),
"blink": self.blink_step, "blink": self.blink_step(),
"color_transition": self.color_transition_step, # Added new pattern "color_transition": self.color_transition_step(), # Added new pattern
"flicker": self.flicker_step, "flicker": self.flicker_step(),
"scanner": self.scanner_step, # New: Single direction scanner "scanner": self.scanner_step(), # New: Single direction scanner
"bidirectional_scanner": self.bidirectional_scanner_step, # New: Bidirectional scanner "bidirectional_scanner": self.bidirectional_scanner_step(), # New: Bidirectional scanner
"external": None
} }
def color_wipe_step(self): def color_wipe_step(self):
while True:
color = self.apply_brightness(self.colors[0]) color = self.apply_brightness(self.colors[0])
current_time = utime.ticks_ms() current_time = utime.ticks_ms()
if utime.ticks_diff(current_time, self.last_update) >= self.delay: if utime.ticks_diff(current_time, self.last_update) >= self.delay:
@@ -34,8 +34,10 @@ class Patterns(PatternsBase):
else: else:
self.pattern_step = 0 self.pattern_step = 0
self.last_update = current_time self.last_update = current_time
yield
def rainbow_cycle_step(self): def rainbow_cycle_step(self):
while True:
current_time = utime.ticks_ms() current_time = utime.ticks_ms()
if utime.ticks_diff(current_time, self.last_update) >= self.delay/5: if utime.ticks_diff(current_time, self.last_update) >= self.delay/5:
def wheel(pos): def wheel(pos):
@@ -54,8 +56,10 @@ class Patterns(PatternsBase):
self.n.write() self.n.write()
self.pattern_step = (self.pattern_step + 1) % 256 self.pattern_step = (self.pattern_step + 1) % 256
self.last_update = current_time self.last_update = current_time
yield
def theater_chase_step(self): def theater_chase_step(self):
while True:
current_time = utime.ticks_ms() current_time = utime.ticks_ms()
if utime.ticks_diff(current_time, self.last_update) >= self.delay: if utime.ticks_diff(current_time, self.last_update) >= self.delay:
for i in range(self.num_leds): for i in range(self.num_leds):
@@ -66,8 +70,10 @@ class Patterns(PatternsBase):
self.n.write() self.n.write()
self.pattern_step = (self.pattern_step + 1) % 3 self.pattern_step = (self.pattern_step + 1) % 3
self.last_update = current_time self.last_update = current_time
yield
def blink_step(self): def blink_step(self):
while True:
current_time = utime.ticks_ms() current_time = utime.ticks_ms()
if utime.ticks_diff(current_time, self.last_update) >= self.delay: if utime.ticks_diff(current_time, self.last_update) >= self.delay:
if self.pattern_step % 2 == 0: if self.pattern_step % 2 == 0:
@@ -76,8 +82,10 @@ class Patterns(PatternsBase):
self.fill((0, 0, 0)) self.fill((0, 0, 0))
self.pattern_step = (self.pattern_step + 1) % 2 self.pattern_step = (self.pattern_step + 1) % 2
self.last_update = current_time self.last_update = current_time
yield
def color_transition_step(self): def color_transition_step(self):
while True:
current_time = utime.ticks_ms() current_time = utime.ticks_ms()
# Check for hold duration first # Check for hold duration first
@@ -85,7 +93,7 @@ class Patterns(PatternsBase):
# Still in hold phase, just display the current solid color # Still in hold phase, just display the current solid color
self.fill(self.apply_brightness(self.current_color)) self.fill(self.apply_brightness(self.current_color))
self.last_update = current_time # Keep updating last_update to avoid skipping frames self.last_update = current_time # Keep updating last_update to avoid skipping frames
return yield
# If hold duration is over, proceed with transition # If hold duration is over, proceed with transition
if utime.ticks_diff(current_time, self.last_update) >= self.delay: if utime.ticks_diff(current_time, self.last_update) >= self.delay:
@@ -93,7 +101,7 @@ class Patterns(PatternsBase):
if num_colors < 2: if num_colors < 2:
# Should not happen if select handles it, but as a safeguard # Should not happen if select handles it, but as a safeguard
self.select("on") self.select("on")
return yield
from_color = self.colors[self.current_color_idx] from_color = self.colors[self.current_color_idx]
to_color_idx = (self.current_color_idx + 1) % num_colors to_color_idx = (self.current_color_idx + 1) % num_colors
@@ -124,8 +132,10 @@ class Patterns(PatternsBase):
self.hold_start_time = current_time # Start hold phase for the new color self.hold_start_time = current_time # Start hold phase for the new color
self.last_update = current_time self.last_update = current_time
yield
def flicker_step(self): def flicker_step(self):
while True:
current_time = utime.ticks_ms() current_time = utime.ticks_ms()
if utime.ticks_diff(current_time, self.last_update) >= self.delay/5: if utime.ticks_diff(current_time, self.last_update) >= self.delay/5:
base_color = self.colors[0] base_color = self.colors[0]
@@ -137,11 +147,13 @@ class Patterns(PatternsBase):
flicker_color = self.apply_brightness(base_color, brightness_override=flicker_brightness) flicker_color = self.apply_brightness(base_color, brightness_override=flicker_brightness)
self.fill(flicker_color) self.fill(flicker_color)
self.last_update = current_time self.last_update = current_time
yield
def scanner_step(self): def scanner_step(self):
""" """
Mimics a 'Knight Rider' style scanner, moving in one direction. Mimics a 'Knight Rider' style scanner, moving in one direction.
""" """
while True:
current_time = utime.ticks_ms() current_time = utime.ticks_ms()
if utime.ticks_diff(current_time, self.last_update) >= self.delay: if utime.ticks_diff(current_time, self.last_update) >= self.delay:
self.fill((0, 0, 0)) # Clear all LEDs self.fill((0, 0, 0)) # Clear all LEDs
@@ -171,11 +183,13 @@ class Patterns(PatternsBase):
self.pattern_step = 0 # Reset to start self.pattern_step = 0 # Reset to start
self.last_update = current_time self.last_update = current_time
yield
def bidirectional_scanner_step(self): def bidirectional_scanner_step(self):
""" """
Mimics a 'Knight Rider' style scanner, moving back and forth. Mimics a 'Knight Rider' style scanner, moving back and forth.
""" """
while True:
current_time = utime.ticks_ms() current_time = utime.ticks_ms()
if utime.ticks_diff(current_time, self.last_update) >= self.delay/100: if utime.ticks_diff(current_time, self.last_update) >= self.delay/100:
self.fill((0, 0, 0)) # Clear all LEDs self.fill((0, 0, 0)) # Clear all LEDs
@@ -210,3 +224,4 @@ class Patterns(PatternsBase):
self.pattern_step = 0 # Start moving forward from the first LED self.pattern_step = 0 # Start moving forward from the first LED
self.last_update = current_time self.last_update = current_time
yield

View File

@@ -47,10 +47,6 @@ class PatternsBase:
def set_pattern_step(self, step): def set_pattern_step(self, step):
self.pattern_step = step self.pattern_step = step
def tick(self):
if self.patterns[self.selected]:
self.patterns[self.selected]()
def update_num_leds(self, pin, num_leds): def update_num_leds(self, pin, num_leds):
self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds) self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds)
self.num_leds = num_leds self.num_leds = num_leds
@@ -158,8 +154,9 @@ class PatternsBase:
return tuple(int(c * effective_brightness / 255) for c in color) return tuple(int(c * effective_brightness / 255) for c in color)
def select(self, pattern): def select(self, pattern):
if pattern in self.patterns: if pattern in self.patterns and hasattr(self.patterns[pattern], "__next__"):
self.selected = pattern self.selected = pattern
self.run = True
self.sync() # Reset pattern state when selecting a new pattern self.sync() # Reset pattern state when selecting a new pattern
if pattern == "color_transition": if pattern == "color_transition":
if len(self.colors) < 2: if len(self.colors) < 2:
@@ -176,6 +173,13 @@ class PatternsBase:
return True return True
return False return False
def tick(self):
if self.run:
try:
next(self.patterns[self.selected])
except StopIteration:
self.run = False
def set(self, i, color): def set(self, i, color):
self.n[i] = color self.n[i] = color
@@ -189,7 +193,13 @@ class PatternsBase:
self.n.write() self.n.write()
def off(self): def off(self):
while True:
self.fill((0, 0, 0)) self.fill((0, 0, 0))
self.run = False
yield
def on(self): def on(self):
while True:
self.fill(self.apply_brightness(self.colors[0])) self.fill(self.apply_brightness(self.colors[0]))
self.run = False
yield