Add circle pattern
This commit is contained in:
@@ -26,7 +26,7 @@ param_mapping = {
|
||||
}
|
||||
|
||||
class Patterns(PatternsBase):
|
||||
def __init__(self, pin, num_leds, color1=(0,0,0), color2=(0,0,0), brightness=127, selected="rainbow_cycle", delay=100):
|
||||
def __init__(self, pin, num_leds, color1=(0,0,0), color2=(0,0,0), brightness=127, selected="off", delay=100):
|
||||
super().__init__(pin, num_leds, color1, color2, brightness, selected, delay)
|
||||
self.auto = True
|
||||
self.step = 0
|
||||
@@ -38,6 +38,7 @@ class Patterns(PatternsBase):
|
||||
"pulse": self.pulse,
|
||||
"transition": self.transition,
|
||||
"n_chase": self.n_chase,
|
||||
"circle": self.circle,
|
||||
}
|
||||
|
||||
|
||||
@@ -337,4 +338,80 @@ class Patterns(PatternsBase):
|
||||
last_update = current_time
|
||||
|
||||
self.running = False
|
||||
self.stopped = True
|
||||
self.stopped = True
|
||||
|
||||
def circle(self):
|
||||
"""Circle loading pattern - grows to n2, then tail moves forward at n3 until min length n4"""
|
||||
self.stopped = False
|
||||
self.running = True
|
||||
head = 0
|
||||
tail = 0
|
||||
|
||||
# Calculate timing
|
||||
head_rate = max(1, int(self.n1)) # n1 = head moves per second
|
||||
tail_rate = max(1, int(self.n3)) # n3 = tail moves per second
|
||||
max_length = max(1, int(self.n2)) # n2 = max length
|
||||
min_length = max(0, int(self.n4)) # n4 = min length
|
||||
|
||||
head_delay = 1000 // head_rate # ms between head movements
|
||||
tail_delay = 1000 // tail_rate # ms between tail movements
|
||||
|
||||
last_head_move = utime.ticks_ms()
|
||||
last_tail_move = utime.ticks_ms()
|
||||
|
||||
phase = "growing" # "growing", "shrinking", or "off"
|
||||
|
||||
while self.running:
|
||||
current_time = utime.ticks_ms()
|
||||
|
||||
# Clear all LEDs
|
||||
self.n.fill((0, 0, 0))
|
||||
|
||||
# Calculate segment length
|
||||
segment_length = (head - tail) % self.num_leds
|
||||
if segment_length == 0 and head != tail:
|
||||
segment_length = self.num_leds
|
||||
|
||||
# Draw segment from tail to head
|
||||
color = self.apply_brightness(self.colors[0])
|
||||
for i in range(segment_length + 1):
|
||||
led_pos = (tail + i) % self.num_leds
|
||||
self.n[led_pos] = color
|
||||
|
||||
# Move head continuously at n1 LEDs per second
|
||||
if utime.ticks_diff(current_time, last_head_move) >= head_delay:
|
||||
head = (head + 1) % self.num_leds
|
||||
last_head_move = current_time
|
||||
|
||||
# Tail behavior based on phase
|
||||
if phase == "growing":
|
||||
# Growing phase: tail stays at 0 until max length reached
|
||||
if segment_length >= max_length:
|
||||
phase = "shrinking"
|
||||
elif phase == "shrinking":
|
||||
# Shrinking phase: move tail forward at n3 LEDs per second
|
||||
if utime.ticks_diff(current_time, last_tail_move) >= tail_delay:
|
||||
tail = (tail + 1) % self.num_leds
|
||||
last_tail_move = current_time
|
||||
|
||||
# Check if we've reached min length
|
||||
current_length = (head - tail) % self.num_leds
|
||||
if current_length == 0 and head != tail:
|
||||
current_length = self.num_leds
|
||||
|
||||
# For min_length = 0, we need at least 1 LED (the head)
|
||||
if min_length == 0 and current_length <= 1:
|
||||
phase = "off" # All LEDs off for 1 step
|
||||
elif min_length > 0 and current_length <= min_length:
|
||||
phase = "growing" # Cycle repeats
|
||||
else: # phase == "off"
|
||||
# Off phase: all LEDs off for 1 step, then restart
|
||||
tail = head # Reset tail to head position to start fresh
|
||||
phase = "growing"
|
||||
|
||||
self.n.write()
|
||||
|
||||
self.running = False
|
||||
self.stopped = True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user