diff --git a/src/patterns.py b/src/patterns.py index bee21e5..7afe788 100644 --- a/src/patterns.py +++ b/src/patterns.py @@ -2,15 +2,11 @@ from machine import Pin from neopixel import NeoPixel import utime import random +from patterns_base import PatternsBase -class Patterns: +class Patterns(PatternsBase): def __init__(self, pin, num_leds, color1=(0,0,0), color2=(0,0,0), brightness=127, selected="rainbow_cycle", delay=100): - self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds) - self.num_leds = num_leds - self.pattern_step = 0 - self.last_update = utime.ticks_ms() - self.delay = delay - self.brightness = brightness + super().__init__(pin, num_leds, color1, color2, brightness, selected, delay) self.patterns = { "off": self.off, "on" : self.on, @@ -24,186 +20,6 @@ class Patterns: "bidirectional_scanner": self.bidirectional_scanner_step, # New: Bidirectional scanner "external": None } - self.selected = selected - # Ensure colors list always starts with at least two for robust transition handling - self.colors = [color1, color2] if color1 != color2 else [color1, (255, 255, 255)] # Fallback if initial colors are same - if not self.colors: # Ensure at least one color exists - self.colors = [(0, 0, 0)] - - self.transition_duration = delay * 50 # Default transition duration - self.hold_duration = delay * 10 # Default hold duration at each color - self.transition_step = 0 # Current step in the transition - self.current_color_idx = 0 # Index of the color currently being held/transitioned from - self.current_color = self.colors[self.current_color_idx] # The actual blended color - - self.hold_start_time = utime.ticks_ms() # Time when the current color hold started - - # New attributes for scanner patterns - self.scanner_direction = 1 # 1 for forward, -1 for backward - self.scanner_tail_length = 3 # Number of trailing pixels - - def sync(self): - self.pattern_step=0 - self.last_update = utime.ticks_ms() - self.delay - if self.selected == "color_transition": - self.transition_step = 0 - self.current_color_idx = 0 - self.current_color = self.colors[self.current_color_idx] - self.hold_start_time = utime.ticks_ms() # Reset hold time - # Reset scanner specific variables - self.scanner_direction = 1 - self.tick() - - def set_pattern_step(self, 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): - self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds) - self.num_leds = num_leds - self.pattern_step = 0 - - def set_delay(self, delay): - self.delay = delay - # Update transition duration and hold duration when delay changes - self.transition_duration = self.delay * 50 - self.hold_duration = self.delay * 10 - - - def set_brightness(self, brightness): - self.brightness = brightness - - def set_color1(self, color): - if len(self.colors) > 0: - self.colors[0] = color - if self.selected == "color_transition": - # If the first color is changed, potentially reset transition - # to start from this new color if we were about to transition from it - if self.current_color_idx == 0: - self.transition_step = 0 - self.current_color = self.colors[0] - self.hold_start_time = utime.ticks_ms() - else: - self.colors.append(color) - - - def set_color2(self, color): - if len(self.colors) > 1: - self.colors[1] = color - elif len(self.colors) == 1: - self.colors.append(color) - else: # List is empty - self.colors.append((0,0,0)) # Dummy color - self.colors.append(color) - - - def set_colors(self, colors): - if colors and len(colors) >= 2: - self.colors = colors - if self.selected == "color_transition": - self.sync() # Reset transition if new color list is provided - elif colors and len(colors) == 1: - self.colors = [colors[0], (255,255,255)] # Add a default second color - if self.selected == "color_transition": - print("Warning: 'color_transition' requires at least two colors. Adding a default second color.") - self.sync() - else: - print("Error: set_colors requires a list of at least one color.") - self.colors = [(0,0,0), (255,255,255)] # Fallback - if self.selected == "color_transition": - self.sync() - - def set_color(self, num, color): - # Changed: More robust index check - if 0 <= num < len(self.colors): - self.colors[num] = color - # If the changed color is part of the current or next transition, - # restart the transition for smoother updates - if self.selected == "color_transition": - current_from_idx = self.current_color_idx - current_to_idx = (self.current_color_idx + 1) % len(self.colors) - if num == current_from_idx or num == current_to_idx: - # If we change a color involved in the current transition, - # it's best to restart the transition state for smoothness. - self.transition_step = 0 - self.current_color_idx = current_from_idx # Stay at the current starting color - self.current_color = self.colors[self.current_color_idx] - self.hold_start_time = utime.ticks_ms() # Reset hold - return True - elif num == len(self.colors): # Allow setting a new color at the end - self.colors.append(color) - return True - return False - - def add_color(self, color): - self.colors.append(color) - if self.selected == "color_transition" and len(self.colors) == 2: - # If we just added the second color needed for transition - self.sync() - - - def del_color(self, num): - # Changed: More robust index check and using del for lists - if 0 <= num < len(self.colors): - del self.colors[num] - # If the color being deleted was part of the current transition, - # re-evaluate the current_color_idx - if self.selected == "color_transition": - if len(self.colors) < 2: # Need at least two colors for transition - print("Warning: Not enough colors for 'color_transition'. Switching to 'on'.") - self.select("on") # Or some other default - else: - # Adjust index if it's out of bounds after deletion or was the one transitioning from - self.current_color_idx %= len(self.colors) - self.transition_step = 0 - self.current_color = self.colors[self.current_color_idx] - self.hold_start_time = utime.ticks_ms() - return True - return False - - def apply_brightness(self, color, brightness_override=None): - effective_brightness = brightness_override if brightness_override is not None else self.brightness - return tuple(int(c * effective_brightness / 255) for c in color) - - def select(self, pattern): - if pattern in self.patterns: - self.selected = pattern - self.sync() # Reset pattern state when selecting a new pattern - if pattern == "color_transition": - if len(self.colors) < 2: - print("Warning: 'color_transition' requires at least two colors. Switching to 'on'.") - self.selected = "on" # Fallback if not enough colors - self.sync() # Re-sync for the new pattern - else: - self.transition_step = 0 - self.current_color_idx = 0 # Start from the first color in the list - self.current_color = self.colors[self.current_color_idx] - self.hold_start_time = utime.ticks_ms() # Reset hold timer - self.transition_duration = self.delay * 50 # Initialize transition duration - self.hold_duration = self.delay * 10 # Initialize hold duration - return True - return False - - def set(self, i, color): - self.n[i] = color - - def write(self): - self.n.write() - - def fill(self, color=None): - fill_color = color if color is not None else self.colors[0] - for i in range(self.num_leds): - self.n[i] = fill_color - self.n.write() - - def off(self): - self.fill((0, 0, 0)) - - def on(self): - self.fill(self.apply_brightness(self.colors[0])) def color_wipe_step(self): color = self.apply_brightness(self.colors[0]) diff --git a/src/patterns_base.py b/src/patterns_base.py new file mode 100644 index 0000000..a0e9178 --- /dev/null +++ b/src/patterns_base.py @@ -0,0 +1,195 @@ +from machine import Pin +from neopixel import NeoPixel +import utime + +class PatternsBase: + def __init__(self, pin, num_leds, color1=(0,0,0), color2=(0,0,0), brightness=127, selected="rainbow_cycle", delay=100): + + self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds) + self.num_leds = num_leds + self.pattern_step = 0 + self.last_update = utime.ticks_ms() + self.delay = delay + self.brightness = brightness + self. run = True + self.selected = selected + # Ensure colors list always starts with at least two for robust transition handling + self.colors = [color1, color2] if color1 != color2 else [color1, (255, 255, 255)] # Fallback if initial colors are same + if not self.colors: # Ensure at least one color exists + self.colors = [(0, 0, 0)] + + self.transition_duration = delay * 50 # Default transition duration + self.hold_duration = delay * 10 # Default hold duration at each color + self.transition_step = 0 # Current step in the transition + self.current_color_idx = 0 # Index of the color currently being held/transitioned from + self.current_color = self.colors[self.current_color_idx] # The actual blended color + + self.hold_start_time = utime.ticks_ms() # Time when the current color hold started + + # New attributes for scanner patterns + self.scanner_direction = 1 # 1 for forward, -1 for backward + self.scanner_tail_length = 3 # Number of trailing pixels + self.patterns = {} + self.run = True + + def sync(self): + self.pattern_step=0 + self.last_update = utime.ticks_ms() - self.delay + if self.selected == "color_transition": + self.transition_step = 0 + self.current_color_idx = 0 + self.current_color = self.colors[self.current_color_idx] + self.hold_start_time = utime.ticks_ms() # Reset hold time + # Reset scanner specific variables + self.scanner_direction = 1 + self.tick() + + def set_pattern_step(self, 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): + self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds) + self.num_leds = num_leds + self.pattern_step = 0 + + def set_delay(self, delay): + self.delay = delay + # Update transition duration and hold duration when delay changes + self.transition_duration = self.delay * 50 + self.hold_duration = self.delay * 10 + + def set_brightness(self, brightness): + self.brightness = brightness + + def set_color1(self, color): + if len(self.colors) > 0: + self.colors[0] = color + if self.selected == "color_transition": + # If the first color is changed, potentially reset transition + # to start from this new color if we were about to transition from it + if self.current_color_idx == 0: + self.transition_step = 0 + self.current_color = self.colors[0] + self.hold_start_time = utime.ticks_ms() + else: + self.colors.append(color) + + + def set_color2(self, color): + if len(self.colors) > 1: + self.colors[1] = color + elif len(self.colors) == 1: + self.colors.append(color) + else: # List is empty + self.colors.append((0,0,0)) # Dummy color + self.colors.append(color) + + + def set_colors(self, colors): + if colors and len(colors) >= 2: + self.colors = colors + if self.selected == "color_transition": + self.sync() # Reset transition if new color list is provided + elif colors and len(colors) == 1: + self.colors = [colors[0], (255,255,255)] # Add a default second color + if self.selected == "color_transition": + print("Warning: 'color_transition' requires at least two colors. Adding a default second color.") + self.sync() + else: + print("Error: set_colors requires a list of at least one color.") + self.colors = [(0,0,0), (255,255,255)] # Fallback + if self.selected == "color_transition": + self.sync() + + def set_color(self, num, color): + # Changed: More robust index check + if 0 <= num < len(self.colors): + self.colors[num] = color + # If the changed color is part of the current or next transition, + # restart the transition for smoother updates + if self.selected == "color_transition": + current_from_idx = self.current_color_idx + current_to_idx = (self.current_color_idx + 1) % len(self.colors) + if num == current_from_idx or num == current_to_idx: + # If we change a color involved in the current transition, + # it's best to restart the transition state for smoothness. + self.transition_step = 0 + self.current_color_idx = current_from_idx # Stay at the current starting color + self.current_color = self.colors[self.current_color_idx] + self.hold_start_time = utime.ticks_ms() # Reset hold + return True + elif num == len(self.colors): # Allow setting a new color at the end + self.colors.append(color) + return True + return False + + def add_color(self, color): + self.colors.append(color) + if self.selected == "color_transition" and len(self.colors) == 2: + # If we just added the second color needed for transition + self.sync() + + + def del_color(self, num): + # Changed: More robust index check and using del for lists + if 0 <= num < len(self.colors): + del self.colors[num] + # If the color being deleted was part of the current transition, + # re-evaluate the current_color_idx + if self.selected == "color_transition": + if len(self.colors) < 2: # Need at least two colors for transition + print("Warning: Not enough colors for 'color_transition'. Switching to 'on'.") + self.select("on") # Or some other default + else: + # Adjust index if it's out of bounds after deletion or was the one transitioning from + self.current_color_idx %= len(self.colors) + self.transition_step = 0 + self.current_color = self.colors[self.current_color_idx] + self.hold_start_time = utime.ticks_ms() + return True + return False + + def apply_brightness(self, color, brightness_override=None): + effective_brightness = brightness_override if brightness_override is not None else self.brightness + return tuple(int(c * effective_brightness / 255) for c in color) + + def select(self, pattern): + if pattern in self.patterns: + self.selected = pattern + self.sync() # Reset pattern state when selecting a new pattern + if pattern == "color_transition": + if len(self.colors) < 2: + print("Warning: 'color_transition' requires at least two colors. Switching to 'on'.") + self.selected = "on" # Fallback if not enough colors + self.sync() # Re-sync for the new pattern + else: + self.transition_step = 0 + self.current_color_idx = 0 # Start from the first color in the list + self.current_color = self.colors[self.current_color_idx] + self.hold_start_time = utime.ticks_ms() # Reset hold timer + self.transition_duration = self.delay * 50 # Initialize transition duration + self.hold_duration = self.delay * 10 # Initialize hold duration + return True + return False + + def set(self, i, color): + self.n[i] = color + + def write(self): + self.n.write() + + def fill(self, color=None): + fill_color = color if color is not None else self.colors[0] + for i in range(self.num_leds): + self.n[i] = fill_color + self.n.write() + + def off(self): + self.fill((0, 0, 0)) + + def on(self): + self.fill(self.apply_brightness(self.colors[0]))