-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaindrops.py
More file actions
193 lines (133 loc) · 4.15 KB
/
Raindrops.py
File metadata and controls
193 lines (133 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""
Rain simulation
Simulates rain drops on a surface by animating the scale and opacity
of 50 scatter points.
Author: Nicolas P. Rougier
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import sys, getopt
from LED import LED
import colorsys as cs
import serial
from struct import *
from threading import Thread, Lock
class Globals(object):
colors = []
def __init__(self):
return
NUM_LEDS = 181
COLOR_BYTES = 3
BOARDS = 12
leds = []
window = []
globals = Globals()
i = 0
while (i < NUM_LEDS):
globals.colors.append(cs.hsv_to_rgb(0 / 255, 1.0, 1.0))
i += 1
ser = serial.Serial(
port='/dev/ttyS1',
baudrate=9600,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.EIGHTBITS,
rtscts=True,
dsrdtr=True
)
def parse(infilePath, outfilePath, scale):
with open(infilePath) as infile:
lines = infile.readlines()
w, h = lines[1].strip().split("\t")
window.append(float(w) * ((BOARDS / 2) if (BOARDS > 1) else 1))
window.append(float(h) * (2 if (BOARDS > 1) else 1))
lls = lines[3:len(lines)]
for ll in lls:
x, y, rho, phi = ll.strip().split("\t")
leds.append(LED(x, y, rho, phi))
i = 1
while(i < BOARDS):
j = 0
while(j < NUM_LEDS):
led = leds[j]
if (i < BOARDS / 2):
x_off = float(w) * i
leds.append(LED(led.x + x_off, led.y, led.rho, led.phi))
else:
x_off = float(w) * (i - (BOARDS / 2))
y_off = (2 * float(h))
leds.append(LED(led.x + x_off, y_off - led.y, led.rho, led.phi))
j+=1
i+=1
return
def main(argv):
inputfile = ''
outputfile = ''
scale = 1000
try:
opts, args = getopt.getopt(argv,"hi:o:s:",["ifile=","ofile=","scale="])
except getopt.GetoptError:
print('test.py -i <inputfile> -o <outputfile> -s <scale>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('test.py -i <inputfile> -o <outputfile> -s <scale>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-s", "--scale"): # horizontal scale
scale = float(arg)
print('Input file is :', inputfile)
print('Output file is :', outputfile)
print('Scale is :', scale)
parse(inputfile, outputfile, scale)
if __name__ == "__main__":
main(sys.argv[1:])
# Create new Figure and an Axes which fills it.
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.set_xlim(0, window[0])
ax.set_ylim(0, window[1])
x_data = [led.x for led in leds]
y_data = [led.y for led in leds]
area = [led.area for led in leds]
# Construct the scatter which we will update during animation
# as the raindrops develop.
scat = ax.scatter(x_data, y_data,
s=area, alpha=1.0)
printStrs = ["r: ", "g: ", "b: ", "a: "]
def update(frame_number):
# Get an index which we can use to re-spawn the oldest raindrop.
current_index = frame_number % len(leds)
print("Frame: " + str(frame_number))
if ser.inWaiting() > 0:
print("We have stuff!!")
buffer = ser.read(NUM_LEDS * BOARDS * COLOR_BYTES)
ser.flush()
l = len(buffer)
arr = unpack(str(l) + 'c', buffer)
print("length: " + str(l))
i = 0
while (i < COLOR_BYTES):
byte = int.from_bytes(arr[i], byteorder='big')
print(printStrs[i%COLOR_BYTES] + str(byte))
i += 1
globals.colors.clear()
i = 0
while (i < NUM_LEDS * BOARDS * COLOR_BYTES):
j = 0
tuple = []
while (j < COLOR_BYTES):
tuple.append(int.from_bytes(arr[i+j], byteorder='big') / 255)
j += 1
globals.colors.append(tuple)
i += COLOR_BYTES
else:
print("We don't have stuff. :-(")
scat.set_facecolors(globals.colors)
# Construct the animation, using the update function as the animation
# director.
animation = FuncAnimation(fig, update, interval=10)
plt.show()