rgbw_racers: new version by Julian
- racers now have a tail whose length depends on the racer’s speed - speed is limited to ±2 LEDs/frame
This commit is contained in:
parent
f6cfbb12f3
commit
4da8319928
47
rgbw_racers.py
Executable file → Normal file
47
rgbw_racers.py
Executable file → Normal file
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue