-
Notifications
You must be signed in to change notification settings - Fork 0
/
LivePlot.py
61 lines (49 loc) · 1.64 KB
/
LivePlot.py
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
from time import sleep
import serial
ser = serial.Serial('/dev/ttyACM0',9600)
def toPlotStream(val):
plotStream = (float(val-plotStreamRange[0])/float(plotStreamRangeW))
plotStream = int(plotStream * plotStreamWidth)
plotStream = plotStreamChar * plotStream
plotStream = plotStream.ljust(plotStreamWidth)
return plotStream
def getNumCans(rowData):
numCans = 0;
if rowData < 30:
numCans = 0;
elif rowData < 40:
numCans = 6;
elif rowData < 50:
numCans = 5;
elif rowData < 60:
numCans = 4;
elif rowData < 100:
numCans = 3;
elif rowData < 180:
numCans = 2;
elif rowData < 360:
numCans = 1;
else:
numCans = 0;
return numCans
plotStreamRange = [950,1024]
plotStreamRangeW = plotStreamRange[1] - plotStreamRange[0]
plotStreamWidth = 30
plotStreamChar = "|"
t = 0
while True:
try:
#serial line reading limits the rate
line = ser.readline()
red = int(line[line.index("r: ")+3:line.index("g: ")])
green = int(line[line.index("g: ")+3:line.index("b: ")])
blue = int(line[line.index("b: ")+3:line.index("row: ")])
rowData = int(line[line.index("row: ")+5:line.index("\r\n")])
#convert the resistance value to number of cans
numCans = getNumCans(rowData);
print(red,green,blue)
print(str(numCans).ljust(5)+"* "+toPlotStream(red)+toPlotStream(green)+toPlotStream(blue))
except ValueError:
print("Serial went too fast, flushing buffer before continuing...")
t += 1
sleep(.05)