Switch to list for colors
This commit is contained in:
parent
a19b1e86f2
commit
1989f6f5c9
107
src/patterns.py
107
src/patterns.py
|
@ -26,11 +26,13 @@ class Patterns:
|
||||||
"external": None
|
"external": None
|
||||||
}
|
}
|
||||||
self.selected = selected
|
self.selected = selected
|
||||||
self.color1 = color1
|
self.colors = [color1, color2]
|
||||||
self.color2 = color2
|
|
||||||
self.transition_duration = delay * 10 # Default transition duration is 10 times the delay
|
self.transition_duration = delay * 10 # Default transition duration is 10 times the delay
|
||||||
self.transition_step = 0
|
self.transition_step = 0
|
||||||
self.current_color = self.color1
|
self.current_color = self.colors[0]
|
||||||
|
|
||||||
|
# New: Track the current index for color transitions
|
||||||
|
self.current_color_idx = 0
|
||||||
|
|
||||||
def sync(self):
|
def sync(self):
|
||||||
self.pattern_step=0
|
self.pattern_step=0
|
||||||
|
@ -57,18 +59,62 @@ class Patterns:
|
||||||
self.brightness = brightness
|
self.brightness = brightness
|
||||||
|
|
||||||
def set_color1(self, color):
|
def set_color1(self, color):
|
||||||
self.color1 = color
|
self.colors[0] = color
|
||||||
if self.selected == "color_transition":
|
if self.selected == "color_transition":
|
||||||
|
# Restart transition if color 0 (start color) is changed
|
||||||
self.transition_step = 0
|
self.transition_step = 0
|
||||||
self.current_color = self.color1
|
self.current_color_idx = 0 # Ensure we start from the new color[0]
|
||||||
|
self.current_color = self.colors[0]
|
||||||
|
|
||||||
|
|
||||||
def set_color2(self, color):
|
def set_color2(self, color):
|
||||||
self.color2 = color
|
self.colors[1] = color
|
||||||
if self.selected == "color_transition":
|
if self.selected == "color_transition":
|
||||||
self.transition_step = 0
|
# No direct effect on current_color here, but transition will eventually use it
|
||||||
self.current_color = self.color1
|
pass
|
||||||
|
|
||||||
|
def set_colors(self, colors):
|
||||||
|
self.colors = colors
|
||||||
|
|
||||||
|
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:
|
||||||
|
self.transition_step = 0
|
||||||
|
# Optionally reset current_color_idx if num is the start color
|
||||||
|
if num == current_from_idx:
|
||||||
|
self.current_color_idx = num
|
||||||
|
self.current_color = self.colors[num]
|
||||||
|
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)
|
||||||
|
|
||||||
|
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
|
||||||
|
self.select("off") # Or some other default
|
||||||
|
else:
|
||||||
|
self.current_color_idx %= len(self.colors) # Adjust index if it's out of bounds
|
||||||
|
self.transition_step = 0
|
||||||
|
self.current_color = self.colors[self.current_color_idx]
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def apply_brightness(self, color):
|
def apply_brightness(self, color):
|
||||||
return tuple(int(c * self.brightness / 255) for c in color)
|
return tuple(int(c * self.brightness / 255) for c in color)
|
||||||
|
@ -78,9 +124,15 @@ class Patterns:
|
||||||
self.selected = pattern
|
self.selected = pattern
|
||||||
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":
|
||||||
self.transition_step = 0
|
if len(self.colors) < 2:
|
||||||
self.current_color = self.color1
|
print("Warning: 'color_transition' requires at least two colors. Switching to 'on'.")
|
||||||
self.transition_duration = self.delay * 10 # Initialize transition duration
|
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.transition_duration = self.delay * 10 # Initialize transition duration
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -91,7 +143,7 @@ class Patterns:
|
||||||
self.n.write()
|
self.n.write()
|
||||||
|
|
||||||
def fill(self, color=None):
|
def fill(self, color=None):
|
||||||
fill_color = color if color is not None else self.color1
|
fill_color = color if color is not None else self.colors[0]
|
||||||
for i in range(self.num_leds):
|
for i in range(self.num_leds):
|
||||||
self.n[i] = fill_color
|
self.n[i] = fill_color
|
||||||
self.n.write()
|
self.n.write()
|
||||||
|
@ -100,11 +152,11 @@ class Patterns:
|
||||||
self.fill((0, 0, 0))
|
self.fill((0, 0, 0))
|
||||||
|
|
||||||
def on(self):
|
def on(self):
|
||||||
self.fill(self.apply_brightness(self.color1))
|
self.fill(self.apply_brightness(self.colors[0]))
|
||||||
|
|
||||||
|
|
||||||
def color_wipe_step(self):
|
def color_wipe_step(self):
|
||||||
color = self.apply_brightness(self.color1)
|
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:
|
||||||
if self.pattern_step < self.num_leds:
|
if self.pattern_step < self.num_leds:
|
||||||
|
@ -142,7 +194,7 @@ class Patterns:
|
||||||
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):
|
||||||
if (i + self.pattern_step) % 3 == 0:
|
if (i + self.pattern_step) % 3 == 0:
|
||||||
self.n[i] = self.apply_brightness(self.color1)
|
self.n[i] = self.apply_brightness(self.colors[0])
|
||||||
else:
|
else:
|
||||||
self.n[i] = (0, 0, 0)
|
self.n[i] = (0, 0, 0)
|
||||||
self.n.write()
|
self.n.write()
|
||||||
|
@ -153,7 +205,7 @@ class Patterns:
|
||||||
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:
|
||||||
self.fill(self.apply_brightness(self.color1))
|
self.fill(self.apply_brightness(self.colors[0]))
|
||||||
else:
|
else:
|
||||||
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
|
||||||
|
@ -224,21 +276,30 @@ class Patterns:
|
||||||
|
|
||||||
def color_transition_step(self):
|
def color_transition_step(self):
|
||||||
current_time = utime.ticks_ms()
|
current_time = utime.ticks_ms()
|
||||||
# Use delay for how often to update the transition, not for the duration
|
|
||||||
|
if len(self.colors) < 2:
|
||||||
|
# Not enough colors to transition, possibly switch to 'on' or 'off'
|
||||||
|
self.fill(self.apply_brightness(self.colors[0]))
|
||||||
|
return # Exit if there aren't enough colors
|
||||||
|
|
||||||
if utime.ticks_diff(current_time, self.last_update) >= 1: # Update frequently for smooth transition
|
if utime.ticks_diff(current_time, self.last_update) >= 1: # Update frequently for smooth transition
|
||||||
self.transition_step += utime.ticks_diff(current_time, self.last_update)
|
self.transition_step += utime.ticks_diff(current_time, self.last_update)
|
||||||
self.last_update = current_time
|
self.last_update = current_time
|
||||||
|
|
||||||
|
color_from = self.colors[self.current_color_idx]
|
||||||
|
color_to_idx = (self.current_color_idx + 1) % len(self.colors)
|
||||||
|
color_to = self.colors[color_to_idx]
|
||||||
|
|
||||||
if self.transition_step >= self.transition_duration:
|
if self.transition_step >= self.transition_duration:
|
||||||
# Transition complete, swap colors and restart
|
# Transition complete to the next color
|
||||||
self.color1, self.color2 = self.color2, self.color1
|
self.current_color_idx = color_to_idx # Move to the next color in the sequence
|
||||||
self.transition_step = 0
|
self.transition_step = 0 # Reset transition step
|
||||||
|
|
||||||
# Calculate the interpolation factor (0 to 1)
|
# Calculate the interpolation factor (0 to 1)
|
||||||
factor = self.transition_step / self.transition_duration
|
factor = self.transition_step / self.transition_duration
|
||||||
|
|
||||||
# Get the interpolated color and apply brightness
|
# Get the interpolated color and apply brightness
|
||||||
interpolated_color = self.interpolate_color(self.color1, self.color2, factor)
|
interpolated_color = self.interpolate_color(color_from, color_to, factor)
|
||||||
self.current_color = self.apply_brightness(interpolated_color)
|
self.current_color = self.apply_brightness(interpolated_color)
|
||||||
|
|
||||||
# Fill the LEDs with the current interpolated color
|
# Fill the LEDs with the current interpolated color
|
||||||
|
@ -248,7 +309,9 @@ class Patterns:
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
p = Patterns(4, 180)
|
p = Patterns(4, 180)
|
||||||
p.set_color1((255,0,0))
|
p.set_color1((255,0,0))
|
||||||
p.set_color2((0,255,0))
|
p.set_color2((0,0,255)) # Blue
|
||||||
|
p.add_color((0,255,0)) # Green
|
||||||
|
p.add_color((255,255,0)) # Yellow
|
||||||
#p.set_delay(10)
|
#p.set_delay(10)
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
|
|
|
@ -54,7 +54,12 @@ class Settings(dict):
|
||||||
print(data)
|
print(data)
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
print(key, value)
|
print(key, value)
|
||||||
if key == "color1":
|
if key == "colors":
|
||||||
|
buff = []
|
||||||
|
for color in value:
|
||||||
|
buff.append(tuple(int(color[i:i+2], 16) for i in self.color_order))
|
||||||
|
patterns.set_colors(buff)
|
||||||
|
elif key == "color1":
|
||||||
patterns.set_color1(tuple(int(value[i:i+2], 16) for i in self.color_order)) # Convert hex to RGB
|
patterns.set_color1(tuple(int(value[i:i+2], 16) for i in self.color_order)) # Convert hex to RGB
|
||||||
elif key == "color2":
|
elif key == "color2":
|
||||||
patterns.set_color2(tuple(int(value[i:i+2], 16) for i in self.color_order)) # Convert hex to RGB
|
patterns.set_color2(tuple(int(value[i:i+2], 16) for i in self.color_order)) # Convert hex to RGB
|
||||||
|
@ -84,6 +89,7 @@ class Settings(dict):
|
||||||
else:
|
else:
|
||||||
return "Invalid key", 400
|
return "Invalid key", 400
|
||||||
self[key] = value
|
self[key] = value
|
||||||
|
#print(self)
|
||||||
patterns.sync()
|
patterns.sync()
|
||||||
if save:
|
if save:
|
||||||
self.save()
|
self.save()
|
||||||
|
|
Loading…
Reference in New Issue