Add flicker and n_chase patterns, increase test brightness to 255, add test suites

This commit is contained in:
2025-10-26 18:49:45 +13:00
parent 83cb34d6a8
commit b13ec01561
30 changed files with 1695 additions and 32 deletions

View File

@@ -26,6 +26,9 @@ class Patterns(PatternBase): # Inherit from PatternBase
"bl": self.blink,
"cl": self.circle_loading,
"sb": self.sine_brightness,
"rb": self.rainbow,
"fl": self.flicker,
"nc": self.n_chase,
}
self.step = 0
self.run = True
@@ -185,6 +188,155 @@ class Patterns(PatternBase): # Inherit from PatternBase
self.run = False
self.running = False
def rainbow(self):
"""Rainbow pattern - delay = cycle time, n1 = number of nodes"""
self.run = True
# Calculate timing
cycle_time = max(100, self.delay) # delay = total cycle time in ms
num_nodes = max(1, int(self.n1)) # n1 = number of rainbow nodes/segments
steps_per_cycle = 360 # 360 steps for full cycle
step_delay = cycle_time // steps_per_cycle # ms per step
last_update = utime.ticks_ms()
while self.run:
self.wdt.feed()
current_time = utime.ticks_ms()
# Update rainbow every step_delay ms
if utime.ticks_diff(current_time, last_update) >= step_delay:
# Clear all LEDs
self.n.fill((0, 0, 0))
# Create rainbow effect with n1 nodes
for i in range(self.num_leds):
# Calculate hue based on LED position, number of nodes, and time
hue_per_node = 360 // num_nodes
node_index = i * num_nodes // self.num_leds
base_hue = node_index * hue_per_node
hue = (base_hue + self.step) % 360
# Convert HSV to RGB
rgb = self.hsv_to_rgb(hue, 255, self.brightness)
self.n[i] = rgb
self.n.write()
self.step = (self.step + 1) % 360 # Increment step for animation
last_update = current_time
self.run = False
self.running = False
def hsv_to_rgb(self, h, s, v):
"""Convert HSV to RGB"""
h = h % 360
s = min(255, max(0, s))
v = min(255, max(0, v))
c = v * s // 255
x = c * (60 - abs((h % 120) - 60)) // 60
m = v - c
if h < 60:
r, g, b = c, x, 0
elif h < 120:
r, g, b = x, c, 0
elif h < 180:
r, g, b = 0, c, x
elif h < 240:
r, g, b = 0, x, c
elif h < 300:
r, g, b = x, 0, c
else:
r, g, b = c, 0, x
return (r + m, g + m, b + m)
def flicker(self):
"""Flicker pattern - random brightness variation on base color"""
self.run = True
base_color = self.colors[0]
# n1 = minimum brightness (default to 10 if not set)
min_brightness = max(0, int(self.n1)) if hasattr(self, 'n1') and self.n1 > 0 else 10
# Calculate update rate from delay (n3 is not used, delay controls speed)
update_delay = max(10, int(self.delay)) # At least 10ms to avoid too fast updates
last_update = utime.ticks_ms()
while self.run:
self.wdt.feed()
current_time = utime.ticks_ms()
# Update flicker every update_delay ms
if utime.ticks_diff(current_time, last_update) >= update_delay:
# Calculate random brightness variation
# Flicker between min_brightness and full brightness
max_flicker = self.brightness - min_brightness
flicker_offset = random.randint(0, max(max_flicker, 1))
flicker_brightness = min_brightness + flicker_offset
# Apply brightness to color
flicker_color = (
int(base_color[0] * flicker_brightness / 255),
int(base_color[1] * flicker_brightness / 255),
int(base_color[2] * flicker_brightness / 255)
)
self.fill(flicker_color)
self.n.write()
last_update = current_time
utime.sleep_ms(1)
self.run = False
self.running = False
def n_chase(self):
"""Chase pattern - n1 LEDs on, n2 LEDs off, moving forward"""
self.run = True
# n1 = on width, n2 = off width
on_width = max(1, int(self.n1))
off_width = max(0, int(self.n2))
segment_length = on_width + off_width
if segment_length == 0:
segment_length = 1
# Calculate timing from delay
step_delay = max(10, int(self.delay)) # At least 10ms
step = 0
last_update = utime.ticks_ms()
color = self.apply_brightness(self.colors[0])
while self.run:
self.wdt.feed()
current_time = utime.ticks_ms()
# Move forward every step_delay ms
if utime.ticks_diff(current_time, last_update) >= step_delay:
# Clear all LEDs
self.n.fill((0, 0, 0))
# Draw the chase pattern - repeating segments across all LEDs
for i in range(self.num_leds):
pos_in_segment = (i + step) % segment_length
if pos_in_segment < on_width:
self.n[i] = color
self.n.write()
step = (step + 1) % segment_length
last_update = current_time
utime.sleep_ms(1)
self.run = False
self.running = False
# def flicker(self):
# current_time = utime.ticks_ms()
# base_color = self.colors[0]