Remove patterns_base.py after merging into patterns.py
- Merge Patterns_Base and Patterns into single patterns.py file - Consolidate pattern management logic - Simplify codebase structure
This commit is contained in:
@@ -1,176 +0,0 @@
|
|||||||
from machine import Pin
|
|
||||||
from neopixel import NeoPixel
|
|
||||||
import utime
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Short-key parameter mapping for convenience setters
|
|
||||||
param_mapping = {
|
|
||||||
"pt": "selected",
|
|
||||||
"pa": "selected",
|
|
||||||
"cl": "colors",
|
|
||||||
"br": "brightness",
|
|
||||||
"dl": "delay",
|
|
||||||
"nl": "num_leds",
|
|
||||||
"co": "color_order",
|
|
||||||
"lp": "led_pin",
|
|
||||||
"n1": "n1",
|
|
||||||
"n2": "n2",
|
|
||||||
"n3": "n3",
|
|
||||||
"n4": "n4",
|
|
||||||
"n5": "n5",
|
|
||||||
"n6": "n6",
|
|
||||||
"auto": "auto",
|
|
||||||
}
|
|
||||||
|
|
||||||
class Preset:
|
|
||||||
def __init__(self, data):
|
|
||||||
# Set default values for all preset attributes
|
|
||||||
self.pattern = "off"
|
|
||||||
self.delay = 100
|
|
||||||
self.brightness = 127
|
|
||||||
self.colors = [(255, 255, 255)]
|
|
||||||
self.auto = True
|
|
||||||
self.n1 = 0
|
|
||||||
self.n2 = 0
|
|
||||||
self.n3 = 0
|
|
||||||
self.n4 = 0
|
|
||||||
self.n5 = 0
|
|
||||||
self.n6 = 0
|
|
||||||
|
|
||||||
# Override defaults with provided data
|
|
||||||
self.edit(data)
|
|
||||||
|
|
||||||
def edit(self, data=None):
|
|
||||||
if not data:
|
|
||||||
return False
|
|
||||||
for key, value in data.items():
|
|
||||||
setattr(self, key, value)
|
|
||||||
return True
|
|
||||||
|
|
||||||
class Patterns_Base:
|
|
||||||
def __init__(self, pin, num_leds, color1=(0,0,0), color2=(0,0,0), brightness=127, selected="off", delay=100):
|
|
||||||
self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds)
|
|
||||||
self.num_leds = num_leds
|
|
||||||
self.delay = delay
|
|
||||||
self.brightness = brightness
|
|
||||||
self.auto = False
|
|
||||||
self.patterns = {}
|
|
||||||
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.n1 = 0
|
|
||||||
self.n2 = 0
|
|
||||||
self.n3 = 0
|
|
||||||
self.n4 = 0
|
|
||||||
self.n5 = 0
|
|
||||||
self.n6 = 0
|
|
||||||
|
|
||||||
self.generator = None
|
|
||||||
self.presets = {}
|
|
||||||
self.select(self.selected)
|
|
||||||
|
|
||||||
def edit(self, name, data):
|
|
||||||
if name in self.presets:
|
|
||||||
self.presets[name].edit(data)
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def add(self, name, data):
|
|
||||||
self.presets[name] = Preset(data)
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def delete(self, name):
|
|
||||||
if name in self.presets:
|
|
||||||
del self.presets[name]
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def tick(self):
|
|
||||||
if self.generator is None:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
next(self.generator)
|
|
||||||
except StopIteration:
|
|
||||||
self.generator = None
|
|
||||||
|
|
||||||
def select(self, preset_name):
|
|
||||||
if preset_name in self.presets:
|
|
||||||
preset = self.presets[preset_name]
|
|
||||||
if preset.pattern in self.patterns:
|
|
||||||
self.generator = self.patterns[preset.pattern](preset)
|
|
||||||
self.selected = preset_name # Store the preset name, not the object
|
|
||||||
return True
|
|
||||||
# If preset doesn't exist or pattern not found, default to "off"
|
|
||||||
return False
|
|
||||||
|
|
||||||
def set_param(self, key, value):
|
|
||||||
if key in param_mapping:
|
|
||||||
setattr(self, param_mapping[key], value)
|
|
||||||
return True
|
|
||||||
print(f"Invalid parameter: {key}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
def update_num_leds(self, pin, num_leds):
|
|
||||||
self.n = NeoPixel(Pin(pin, Pin.OUT), num_leds)
|
|
||||||
self.num_leds = num_leds
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
return True
|
|
||||||
elif num == len(self.colors): # Allow setting a new color at the end
|
|
||||||
self.colors.append(color)
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
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]
|
|
||||||
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 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, preset=None):
|
|
||||||
self.fill((0, 0, 0))
|
|
||||||
|
|
||||||
def on(self, preset):
|
|
||||||
colors = preset.colors
|
|
||||||
color = colors[0] if colors else (255, 255, 255)
|
|
||||||
self.fill(self.apply_brightness(color, preset.brightness))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def wheel(self, pos):
|
|
||||||
if pos < 85:
|
|
||||||
return (pos * 3, 255 - pos * 3, 0)
|
|
||||||
elif pos < 170:
|
|
||||||
pos -= 85
|
|
||||||
return (255 - pos * 3, 0, pos * 3)
|
|
||||||
else:
|
|
||||||
pos -= 170
|
|
||||||
return (0, pos * 3, 255 - pos * 3)
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user