|
| 1 | +#Written by Milan Dasgupta |
| 2 | + |
| 3 | +from Queue import Queue |
| 4 | +import time |
| 5 | +from animations import FadeAnimation |
| 6 | +import sys |
| 7 | +from oscapi import ColorsOut |
| 8 | + |
| 9 | + |
| 10 | +def check_speed(curr, speed, end): #Helper function that keeps the ball in bounds |
| 11 | + delta = 1 |
| 12 | + if curr + speed >= end: |
| 13 | + delta = -1 |
| 14 | + |
| 15 | + if curr + speed <= -1: |
| 16 | + delta = -1 |
| 17 | + |
| 18 | + return delta * speed |
| 19 | + |
| 20 | +if __name__ == "__main__": |
| 21 | + MAX_X = 6 #Width and length of Chroma |
| 22 | + MAX_Y = 8 |
| 23 | + tailsize = 4 #Size of ping-pong ball's tail |
| 24 | + speed = 15. #Speed of the Ball |
| 25 | + |
| 26 | + green_x = 0 #The Ball starts at (0,0) going bottom-right |
| 27 | + green_y = 0 |
| 28 | + green_dir_x = 1 |
| 29 | + green_dir_y = 1 |
| 30 | + |
| 31 | + off = (0.0,0.0,0.0) #We define three states the lights can be in |
| 32 | + half = (0.0, 512.0, 0.0) |
| 33 | + green = (0.0, 1023.0, 0.0) |
| 34 | + pix = [] #Set up data structures that hold light information |
| 35 | + x_vals = Queue(maxsize=tailsize) |
| 36 | + y_vals = Queue(maxsize=tailsize) |
| 37 | + out = FadeAnimation() #Set up the fade animation |
| 38 | + out.FADERATE = 1.0 |
| 39 | + out.start() |
| 40 | + |
| 41 | + for i in range(MAX_X * MAX_Y): |
| 42 | + pix.append(off) #Turns Off all Lights |
| 43 | + x_vals.put(green_x) #Put the first light on the array and turns it on |
| 44 | + y_vals.put(green_y) |
| 45 | + pix[(green_y * MAX_X) + green_x] = green |
| 46 | + out.write(pix) |
| 47 | + |
| 48 | + while True: |
| 49 | + time.sleep(1/speed) #Sleeps to keep the animation running at speed |
| 50 | + |
| 51 | + green_dir_x = check_speed(green_x, green_dir_x, MAX_X) #Find the next direction we're going |
| 52 | + green_dir_y = check_speed(green_y, green_dir_y, MAX_Y) |
| 53 | + pix[(green_y * MAX_X) + green_x] = half #Set the last light to half |
| 54 | + if x_vals.full(): #If we're at tailsize, we turn off the last light in the tail |
| 55 | + pix[(y_vals.get() * MAX_X) + x_vals.get()] = off |
| 56 | + green_x += green_dir_x #Set coordinates to the next light |
| 57 | + green_y += green_dir_y |
| 58 | + if (green_x == 0 and green_y == 0) or (green_x == MAX_X-1 and green_y == MAX_Y-1): |
| 59 | + while not x_vals.empty(): #If we're in a corner, we turn off the tail |
| 60 | + pix[(y_vals.get() * MAX_X) + x_vals.get()] = off |
| 61 | + x_vals.put(green_x) #Put the new coordinates on the queue |
| 62 | + y_vals.put(green_y) |
| 63 | + pix[(green_y * MAX_X) + green_x] = green #Turn on the light |
| 64 | + |
| 65 | + out.write(pix) |
0 commit comments