Blame view

telemetry/code/monitor/graph_plotter_rewrite.py 2.78 KB
001c428d   Christopher Stone   Beginnings of a c...
1
2
3
4
5
#!/usr/bin/env python

import pyglet
#import math
#import time
e4e75d3f   Christopher Stone   Accept data from ...
6
import serial
001c428d   Christopher Stone   Beginnings of a c...
7
8
9

from colours import *

e4e75d3f   Christopher Stone   Accept data from ...
10
11
12
13
14
15
16
17
18
datafeed = serial.Serial(
port='/dev/ttyUSB0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)

6432a3a5   Christopher Stone   Added class to ke...
19
20
21
22
23
24
25
26
27
28
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:
4863d03c   Christopher Stone   Fixed obvious bug...
29
            del self.data[0]
6432a3a5   Christopher Stone   Added class to ke...
30

d9082b1e   Christopher Stone   Made plot a subcl...
31
32
class Plot(pyglet.window.Window):
    def __init__(self, series):
01bfd701   Christopher Stone   Added docstrings
33
        """Setup a the details of a plot, and create a corresponding window"""
d9082b1e   Christopher Stone   Made plot a subcl...
34
        pyglet.window.Window.__init__(self, resizable=True)
6432a3a5   Christopher Stone   Added class to ke...
35
36
        self.series = series
        self.title = self.series.title
948da001   Christopher Stone   More progress on ...
37
        self.font = 'Arkhip'
6dbc3a2c   Christopher Stone   fixed line coordi...
38
        self.margins = (0.05, 0.05) # Fractions of window size
cad30d6c   Christopher Stone   Progress towards ...
39
        self.lines = (12, 8)
d9082b1e   Christopher Stone   Made plot a subcl...
40
41
        #self.resizable = True
        self.set_caption(self.title)
6dbc3a2c   Christopher Stone   fixed line coordi...
42
43
        
    def on_resize(self, width, height):
d9082b1e   Christopher Stone   Made plot a subcl...
44
        self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))),
6dbc3a2c   Christopher Stone   fixed line coordi...
45
46
                (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1]))))
        pyglet.window.Window.on_resize(self, width, height)
948da001   Christopher Stone   More progress on ...
47
        
d9082b1e   Christopher Stone   Made plot a subcl...
48
    def on_draw(self):
01bfd701   Christopher Stone   Added docstrings
49
        """Draw all the components of the graph"""
948da001   Christopher Stone   More progress on ...
50
51
        self.drawBackground()
        self.drawHeading()
cad30d6c   Christopher Stone   Progress towards ...
52
        self.drawXAxis()
948da001   Christopher Stone   More progress on ...
53
        
001c428d   Christopher Stone   Beginnings of a c...
54
    def drawBackground(self):
01bfd701   Christopher Stone   Added docstrings
55
        """Draw the graph background, currently a plain colour"""
d9082b1e   Christopher Stone   Made plot a subcl...
56
        pyglet.image.SolidColorImagePattern(WHITE).create_image(self.width, self.height).blit(0, 0)
001c428d   Christopher Stone   Beginnings of a c...
57
58
        
    def drawHeading(self):
01bfd701   Christopher Stone   Added docstrings
59
        """Draw a title for the graph (duplicated in the window titlebar, if present"""
001c428d   Christopher Stone   Beginnings of a c...
60
        heading = pyglet.text.Label(self.title, color=BLACK,
6dbc3a2c   Christopher Stone   fixed line coordi...
61
                            font_name=self.font, font_size=self.height*self.margins[0]*0.8, x=self.width/2, y=self.height,
001c428d   Christopher Stone   Beginnings of a c...
62
63
                            anchor_x='center', anchor_y='top')
        heading.draw()
cad30d6c   Christopher Stone   Progress towards ...
64
65
66
     
    def drawXAxis(self):
        pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (self.bounds[0][0], self.bounds[1][0],
6dbc3a2c   Christopher Stone   fixed line coordi...
67
                                                             self.bounds[0][0], self.bounds[1][1])),
cad30d6c   Christopher Stone   Progress towards ...
68
69
70
                             ('c3B', (0, 0, 0, 0, 0, 0)))
        
        
001c428d   Christopher Stone   Beginnings of a c...
71
        
6432a3a5   Christopher Stone   Added class to ke...
72
73
testseries = Series()

001c428d   Christopher Stone   Beginnings of a c...
74
plots = []         
6432a3a5   Christopher Stone   Added class to ke...
75
plots.append(Plot(testseries))
001c428d   Christopher Stone   Beginnings of a c...
76

9159742e   Christopher Stone   Found out what th...
77
def pollSerial(elapsed):
e4e75d3f   Christopher Stone   Accept data from ...
78
    """Check serial port for incoming data"""
9159742e   Christopher Stone   Found out what th...
79
    # Note, elapsed is time since last call of this function
e4e75d3f   Christopher Stone   Accept data from ...
80
81
    values = datafeed.readline().strip().split(", ")
    testseries.addpoint(values)
948da001   Christopher Stone   More progress on ...
82

948da001   Christopher Stone   More progress on ...
83
pyglet.clock.schedule_interval(pollSerial, 0.1)
001c428d   Christopher Stone   Beginnings of a c...
84
85

pyglet.app.run()