Add n_chase
This commit is contained in:
@@ -37,6 +37,7 @@ class Patterns(PatternsBase):
|
|||||||
"rainbow": self.rainbow,
|
"rainbow": self.rainbow,
|
||||||
"pulse": self.pulse,
|
"pulse": self.pulse,
|
||||||
"transition": self.transition,
|
"transition": self.transition,
|
||||||
|
"n_chase": self.n_chase,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -78,7 +79,7 @@ class Patterns(PatternsBase):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Auto is True: run continuously
|
# Auto is True: run continuously
|
||||||
sleep_ms = max(1, int(self.delay / 5))
|
sleep_ms = max(1, int(self.delay))
|
||||||
last_update = utime.ticks_ms()
|
last_update = utime.ticks_ms()
|
||||||
|
|
||||||
while self.running:
|
while self.running:
|
||||||
@@ -100,10 +101,10 @@ class Patterns(PatternsBase):
|
|||||||
self.running = True
|
self.running = True
|
||||||
self.off()
|
self.off()
|
||||||
|
|
||||||
# Get timing parameters with defaults if not set
|
# Get timing parameters, ensure non-negative
|
||||||
attack_ms = getattr(self, 'n1', 200) # Attack time in ms
|
attack_ms = max(0, int(self.n1)) # Attack time in ms
|
||||||
hold_ms = getattr(self, 'n2', 200) # Hold time in ms
|
hold_ms = max(0, int(self.n2)) # Hold time in ms
|
||||||
decay_ms = getattr(self, 'n3', 200) # Decay time in ms
|
decay_ms = max(0, int(self.n3)) # Decay time in ms
|
||||||
|
|
||||||
# Ensure we have at least one color
|
# Ensure we have at least one color
|
||||||
if not self.colors:
|
if not self.colors:
|
||||||
@@ -271,3 +272,69 @@ class Patterns(PatternsBase):
|
|||||||
|
|
||||||
self.running = False
|
self.running = False
|
||||||
self.stopped = True
|
self.stopped = True
|
||||||
|
|
||||||
|
def n_chase(self):
|
||||||
|
"""N-chase pattern: n1 LEDs of color0, n2 LEDs of color1, repeating.
|
||||||
|
Moves by n3 on even steps, n4 on odd steps (n3/n4 can be positive or negative)"""
|
||||||
|
self.stopped = False
|
||||||
|
self.running = True
|
||||||
|
|
||||||
|
if len(self.colors) < 2:
|
||||||
|
# Need at least 2 colors
|
||||||
|
self.running = False
|
||||||
|
self.stopped = True
|
||||||
|
return
|
||||||
|
|
||||||
|
n1 = max(1, int(self.n1)) # LEDs of color 0
|
||||||
|
n2 = max(1, int(self.n2)) # LEDs of color 1
|
||||||
|
n3 = int(self.n3) # Step movement on odd steps (can be negative)
|
||||||
|
n4 = int(self.n4) # Step movement on even steps (can be negative)
|
||||||
|
|
||||||
|
segment_length = n1 + n2
|
||||||
|
position = 0 # Current position offset
|
||||||
|
step_count = 0 # Track which step we're on
|
||||||
|
|
||||||
|
color0 = self.apply_brightness(self.colors[0])
|
||||||
|
color1 = self.apply_brightness(self.colors[1])
|
||||||
|
|
||||||
|
transition_duration = max(10, self.delay)
|
||||||
|
last_update = utime.ticks_ms()
|
||||||
|
|
||||||
|
while self.running:
|
||||||
|
current_time = utime.ticks_ms()
|
||||||
|
if utime.ticks_diff(current_time, last_update) >= transition_duration:
|
||||||
|
# Clear all LEDs
|
||||||
|
self.n.fill((0, 0, 0))
|
||||||
|
|
||||||
|
# Draw repeating pattern starting at position
|
||||||
|
for i in range(self.num_leds):
|
||||||
|
# Calculate position in the repeating segment
|
||||||
|
relative_pos = (i - position) % segment_length
|
||||||
|
if relative_pos < 0:
|
||||||
|
relative_pos = (relative_pos + segment_length) % segment_length
|
||||||
|
|
||||||
|
# Determine which color based on position in segment
|
||||||
|
if relative_pos < n1:
|
||||||
|
self.n[i] = color0
|
||||||
|
else:
|
||||||
|
self.n[i] = color1
|
||||||
|
|
||||||
|
self.n.write()
|
||||||
|
|
||||||
|
# Move position by n3 or n4 on alternate steps
|
||||||
|
if step_count % 2 == 0:
|
||||||
|
position = position + n3
|
||||||
|
else:
|
||||||
|
position = position + n4
|
||||||
|
|
||||||
|
# Wrap position to keep it reasonable
|
||||||
|
max_pos = self.num_leds + segment_length
|
||||||
|
position = position % max_pos
|
||||||
|
if position < 0:
|
||||||
|
position += max_pos
|
||||||
|
|
||||||
|
step_count += 1
|
||||||
|
last_update = current_time
|
||||||
|
|
||||||
|
self.running = False
|
||||||
|
self.stopped = True
|
||||||
Reference in New Issue
Block a user