Add circle loading pattern

This commit is contained in:
2025-10-25 23:28:01 +13:00
parent 059bd41a59
commit c9449a6d86
2 changed files with 202 additions and 0 deletions

View File

@@ -23,6 +23,7 @@ class Patterns(PatternBase): # Inherit from PatternBase
"o": self.off,
"on": self.on,
"bl": self.blink,
"cl": self.circle_loading,
}
self.step = 0
self.run = True
@@ -64,6 +65,79 @@ class Patterns(PatternBase): # Inherit from PatternBase
self.run = False
self.running = False
def circle_loading(self):
"""Circle loading pattern - grows to n2, then tail moves forward at n3 until min length n4"""
self.run = 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.run:
self.wdt.feed()
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
phase = "growing"
self.n.write()
self.run = False
self.running = False
# def flicker(self):
# current_time = utime.ticks_ms()
# base_color = self.colors[0]