graph_plotter.py
3.12 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
#!/usr/bin/env python
import pyglet
import math
import random
import time
import serial
datafeed = serial.Serial(
port='/dev/ttyUSB0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
red = [1, 0, 0]
green = [0, 1, 0]
blue = [0.2, 0.5, 1]
window0 = pyglet.window.Window(800, 480, resizable=True)
window0.set_caption("First graph")
window1 = pyglet.window.Window(800, 480, resizable=True)
window1.set_caption("Second graph")
starttime = time.time()
def drawgrid(target, xlines, ylines, xlimits, ylimits):
pyglet.gl.glColor3f(0.5, 0.5, 0.5)
for xpos in range(0, target.width, target.width/xlines):
pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (xpos, 0, xpos, target.height)))
tagtext = str(round((xlimits[1]-xlimits[0])*(float(xpos)/target.width) + xlimits[0], 1))
tag = pyglet.text.Label(tagtext, font_name='Arkhip', font_size=10, x=xpos-2, y=0, anchor_x='right', anchor_y='bottom')
tag.draw()
for ypos in range(0, target.width, target.height/ylines):
pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (0, ypos, target.width, ypos)))
tagtext = str(round((ylimits[1]-ylimits[0])*(float(ypos)/target.height) + ylimits[0], 1))
tag = pyglet.text.Label(tagtext, font_name='Arkhip', font_size=10, x=0, y=ypos-2, anchor_x='left', anchor_y='top')
tag.draw()
def plotline(target, xdata, ydata, colour):
pyglet.gl.glColor3f(colour[0], colour[1], colour[2])
points = []
for n in range(max(len(xdata), len(ydata))):
try:
xpos = ((xdata[n]-min(xdata))*target.width)/(max(xdata)-min(xdata))
except:
xpos = 0
xpos = int(xpos)
try:
ypos = ((ydata[n]-min(ydata))*target.height)/(max(ydata)-min(ydata))
except:
ypos = 0
ypos = int(ypos)
points.append([xpos, ypos])
for n in range(len(points)-1):
pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (points[n][0], points[n][1], points[n+1][0], points[n+1][1])))
xdata = [0]
y0data = [0]
y1data = [0]
def poll_serial(foo):
max_points = 200
value0 = datafeed.readline().strip().split(", ")[0]
value1 = datafeed.readline().strip().split(", ")[1]
try:
value0 = float(value0)
y0data.append(value0)
if len(y0data) > max_points:
del y0data[0]
value1 = float(value1)
y1data.append(value1)
if len(y1data) > max_points:
del y1data[0]
xdata.append(round(time.time() - starttime, 3))
if len(xdata) > max_points:
del xdata[0]
except:
pass
pyglet.clock.schedule_interval(poll_serial, 0.01)
def drawgraph(target, xpoints, ypoints, colour):
plotline(target, xpoints, ypoints, colour)
drawgrid(target, 16, 10, [min(xpoints), max(xpoints)], [min(ypoints), max(ypoints)])
@window0.event
def on_draw():
window0.clear()
drawgraph(window0, xdata, y0data, red)
@window1.event
def on_draw():
window1.clear()
drawgraph(window1, xdata, y1data, green)
pyglet.app.run()