Blame view

telemetry/code/monitor/graph_plotter_rewrite.py 3.05 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
29
30
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]

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

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

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

948da001   Christopher Stone   More progress on ...
87
pyglet.clock.schedule_interval(pollSerial, 0.1)
001c428d   Christopher Stone   Beginnings of a c...
88
89

pyglet.app.run()