Switch chase to double-buffered ring chase
Made-with: Cursor
This commit is contained in:
@@ -33,27 +33,28 @@ for ws in strips[:-1]:
|
||||
cumulative_leds.append(cumulative_leds[-1] + ws.num_leds)
|
||||
total_ring_leds = cumulative_leds[-1] + strips[-1].num_leds
|
||||
|
||||
# Chase: trail length (0 = single LED), color (R,G,B)
|
||||
TRAIL_LEN = 8
|
||||
CHASE_COLOR = (0, 255, 100) # cyan-green
|
||||
# Chase: color1 n1 long, then color2 n2 long, stepping n3 pixels
|
||||
COLOR1 = (255, 0, 0) # red
|
||||
COLOR2 = (0, 0, 255) # blue
|
||||
N1 = 24 # length of color1 segment
|
||||
N2 = 24 # length of color2 segment
|
||||
STEP = 1 # step size in pixels per frame
|
||||
|
||||
|
||||
def make_chase_double(num_leds, cumulative_leds, total_ring_leds, color, trail_len=0):
|
||||
"""Pregenerate strip double buffer: when head shows index b first, that pixel is at
|
||||
distance (2*cumulative_leds - b) % total_ring_leds from chase head. GRB order."""
|
||||
def make_chase_double(num_leds, cumulative_leds, total_ring_leds, color1, color2, n1, n2):
|
||||
"""Pregenerate strip double buffer with repeating segments:
|
||||
color1 for n1 pixels, then color2 for n2 pixels, around the full ring. GRB order."""
|
||||
n = 2 * num_leds
|
||||
buf = bytearray(n * 3)
|
||||
pattern_len = n1 + n2
|
||||
for b in range(n):
|
||||
dist = (2 * cumulative_leds - b) % total_ring_leds
|
||||
if dist == 0:
|
||||
r, grn, b_ = color[0], color[1], color[2]
|
||||
elif trail_len and 0 < dist <= trail_len:
|
||||
fade = 1.0 - (dist / (trail_len + 1))
|
||||
r = int(color[0] * fade)
|
||||
grn = int(color[1] * fade)
|
||||
b_ = int(color[2] * fade)
|
||||
# Position of this pixel along the logical ring
|
||||
pos = (2 * cumulative_leds - b) % total_ring_leds
|
||||
seg_pos = pos % pattern_len
|
||||
if seg_pos < n1:
|
||||
r, grn, b_ = color1[0], color1[1], color1[2]
|
||||
else:
|
||||
r = grn = b_ = 0
|
||||
r, grn, b_ = color2[0], color2[1], color2[2]
|
||||
o = b * 3
|
||||
buf[o] = grn
|
||||
buf[o + 1] = r
|
||||
@@ -63,7 +64,7 @@ def make_chase_double(num_leds, cumulative_leds, total_ring_leds, color, trail_l
|
||||
|
||||
# Pregenerate one double buffer per strip
|
||||
chase_buffers = [
|
||||
make_chase_double(ws.num_leds, cumulative_leds[i], total_ring_leds, CHASE_COLOR, TRAIL_LEN)
|
||||
make_chase_double(ws.num_leds, cumulative_leds[i], total_ring_leds, COLOR1, COLOR2, N1, N2)
|
||||
for i, ws in enumerate(strips)
|
||||
]
|
||||
|
||||
@@ -74,5 +75,5 @@ while True:
|
||||
strip_len = strip.num_leds * 3
|
||||
head = (chase_pos + cumulative_leds[i]) * 3 % strip_len
|
||||
strip.show(chase_buffers[i], head)
|
||||
chase_pos = (chase_pos + 1) % total_ring_leds
|
||||
time.sleep_ms(20)
|
||||
chase_pos = (chase_pos + STEP) % total_ring_leds
|
||||
time.sleep_ms(40)
|
||||
|
||||
Reference in New Issue
Block a user