From 4da8319928ba5bc81de317043682df1c0f984d56 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Wed, 23 Mar 2022 20:49:16 +0100 Subject: [PATCH] rgbw_racers: new version by Julian MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - racers now have a tail whose length depends on the racer’s speed - speed is limited to ±2 LEDs/frame --- rgbw_racers.py | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) mode change 100755 => 100644 rgbw_racers.py diff --git a/rgbw_racers.py b/rgbw_racers.py old mode 100755 new mode 100644 index 3923b6e..97b8a4f --- a/rgbw_racers.py +++ b/rgbw_racers.py @@ -83,15 +83,16 @@ for loc in range(MAXLOC): s.commit() s.commit() + +print("{:>10} {:>8} {:>8}".format("frame", "AVG", "MAX")) # Hauptschleife f = 0 # frame counter +clear_leds = [] while True: - for r in racers: + for strip, led in clear_leds: # Clear leds - strip, led = loc2sl(int(r['loc'])) - s.set_color(strip, led, 0,0,0,0) - strip, led = loc2sl(math.ceil(r['loc'])) s.set_color(strip, led, 0,0,0,0) + clear_leds = [] for r in racers: # Update location @@ -107,17 +108,47 @@ while True: # Interpolate color linearly between two leds strip, led = loc2sl(int(r['loc'])) s.add_color(strip, led, *tuple([int(c*(math.ceil(r['loc'])-r['loc'])) for c in r['color']])) + clear_leds.append((strip, led)) strip, led = loc2sl(math.ceil(r['loc'])) s.add_color(strip, led, *tuple([int(c*(r['loc']-int(r['loc']))) for c in r['color']])) + clear_leds.append((strip, led)) - # Random speed variation - r['speed'] += (random.random()-0.5)*0.01 + # Add tail of speed-depending length and logarithmicly decaying brightness + tail = r['speed'] * 10 + if tail > 0: + tail_range = range(1, int(tail)) + else: + tail_range = range(-1, int(tail), -1) + for i in tail_range: + if not 0 <= int(r['loc'])-i < MAXLOC: + continue + strip, led = loc2sl(int(r['loc'])-i) + color = tuple([int(c / (abs(i)*(abs(tail)/2)) ) for c in r['color'][:3]]+[0]) # blackout white + s.add_color(strip, led, *color) + clear_leds.append((strip, led)) + # last tail segment is interpolated + if tail_range: + if not 0 <= int(r['loc'])-i < MAXLOC: + continue + last_loc = r['loc']-i + strip, led = loc2sl(math.ceil(last_loc)) + s.add_color(strip, led, *tuple([int(c*(last_loc-int(last_loc))) for c in color])) + clear_leds.append((strip, led)) + + # Random speed variation, limited to [-2, 2] interval + r['speed'] = max(-2, min(2, r['speed'] + (random.random()-0.5)*0.01)) # Use speed to define W-intensity ("whiteness") - r['color'][3] = min(int(abs(r['speed'])*200), 255) + r['color'][3] = min(int(abs(r['speed'])*128), 255) + + if f % 600 == 0: + print("{:>10} {:>8.2f} {:>8.2f}".format( + f, + sum([abs(r['speed']) for r in racers])/len(racers), + max([abs(r['speed']) for r in racers]))) - #print(max([abs(r['speed']) for r in racers]), min([abs(r['speed']) for r in racers])) s.commit() + # Warte bis zum nächsten Frame time.sleep(interval) f += 1