graph_plotter_rewrite.py
2.37 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
#!/usr/bin/env python
import pyglet
#import math
#import time
import serial
from colours import *
datafeed = serial.Serial(
port='/dev/ttyUSB0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
class Series:
def __init__(self, points=100, title="Series title", xname="x-axis name", yname="y-axis name"):
self.title = title
self.xname = xname
self.yname = yname
self.data = []
self.points = points
def addpoint(self, point):
self.data.append(point)
if len(self.data) > self.points:
del self.points[-1]
class Plot:
def __init__(self, series, size=(640, 480)):
"""Setup a the details of a plot, and create a corresponding window"""
self.series = series
self.title = self.series.title
self.size = size
self.font = 'Arkhip'
self.window = pyglet.window.Window(self.size[0], self.size[1], resizable=True)
self.window.set_caption(self.title)
self.window.on_resize = self.resize
self.window.on_draw = self.draw
def resize(self, width, height):
"""Handle a pyglet resize event, then give control back to the event loop"""
self.size = (width, height)
super(pyglet.window.Window, self.window).on_resize(width, height)
def draw(self):
"""Draw all the components of the graph"""
self.drawBackground()
self.drawHeading()
def drawBackground(self):
"""Draw the graph background, currently a plain colour"""
pyglet.image.SolidColorImagePattern(WHITE).create_image(self.size[0], self.size[1]).blit(0, 0)
def drawHeading(self):
"""Draw a title for the graph (duplicated in the window titlebar, if present"""
heading = pyglet.text.Label(self.title, color=BLACK,
font_name=self.font, font_size=self.size[0]/50, x=self.size[0]/2, y=self.size[1],
anchor_x='center', anchor_y='top')
heading.draw()
testseries = Series()
plots = []
plots.append(Plot(testseries))
def pollSerial(foo):
"""Check serial port for incoming data"""
# Note, foo seems to be a float
values = datafeed.readline().strip().split(", ")
testseries.addpoint(values)
pyglet.clock.schedule_interval(pollSerial, 0.1)
pyglet.app.run()