Blame view

telemetry/code/monitor/graph_plotter_rewrite.py 4.08 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
3ce449c7   Christopher Stone   Added axis title ...
7
import numpy
001c428d   Christopher Stone   Beginnings of a c...
8
9
10

from colours import *

e4e75d3f   Christopher Stone   Accept data from ...
11
12
13
14
15
16
17
18
19
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...
20
21
22
23
24
25
26
27
28
29
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...
30
            del self.data[0]
6432a3a5   Christopher Stone   Added class to ke...
31

d9082b1e   Christopher Stone   Made plot a subcl...
32
33
class Plot(pyglet.window.Window):
    def __init__(self, series):
01bfd701   Christopher Stone   Added docstrings
34
        """Setup a the details of a plot, and create a corresponding window"""
d9082b1e   Christopher Stone   Made plot a subcl...
35
        pyglet.window.Window.__init__(self, resizable=True)
6432a3a5   Christopher Stone   Added class to ke...
36
        self.series = series
948da001   Christopher Stone   More progress on ...
37
        self.font = 'Arkhip'
3ce449c7   Christopher Stone   Added axis title ...
38
        self.margins = (0.08, 0.08) # Fractions of window size
cad30d6c   Christopher Stone   Progress towards ...
39
        self.lines = (12, 8)
d9082b1e   Christopher Stone   Made plot a subcl...
40
        #self.resizable = True
3ce449c7   Christopher Stone   Added axis title ...
41
        self.set_caption(self.series.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()
3ce449c7   Christopher Stone   Added axis title ...
53
        self.drawYAxis()
948da001   Christopher Stone   More progress on ...
54
        
001c428d   Christopher Stone   Beginnings of a c...
55
    def drawBackground(self):
01bfd701   Christopher Stone   Added docstrings
56
        """Draw the graph background, currently a plain colour"""
d9082b1e   Christopher Stone   Made plot a subcl...
57
        pyglet.image.SolidColorImagePattern(WHITE).create_image(self.width, self.height).blit(0, 0)
001c428d   Christopher Stone   Beginnings of a c...
58
59
        
    def drawHeading(self):
01bfd701   Christopher Stone   Added docstrings
60
        """Draw a title for the graph (duplicated in the window titlebar, if present"""
3ce449c7   Christopher Stone   Added axis title ...
61
62
        heading = pyglet.text.Label(self.series.title, color=BLACK,
                            font_name=self.font, font_size=self.height*self.margins[0]*0.6, x=self.width/2, y=self.height,
001c428d   Christopher Stone   Beginnings of a c...
63
64
                            anchor_x='center', anchor_y='top')
        heading.draw()
cad30d6c   Christopher Stone   Progress towards ...
65
66
     
    def drawXAxis(self):
3ce449c7   Christopher Stone   Added axis title ...
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
        top = self.bounds[1][1]
        bot = self.bounds[1][0]
        start = self.bounds[0][0]
        stop = self.bounds[0][1]
        increment = float(stop-start)/self.lines[0]
        for x in numpy.arange(start, stop+1, increment):
            # Using fp arithmetic to avoid intermittent fencepost errors
            x = int(x)
            pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (x, top, x, bot)),
                                 ('c3B', (0, 0, 0, 0, 0, 0)))
            tag = pyglet.text.Label("123", color=BLACK,
                            font_name=self.font, font_size=self.height*self.margins[0]*0.3,
                            x=x, y=self.height*self.margins[0],
                            anchor_x='center', anchor_y='top')
            tag.draw()
        axistitle = pyglet.text.Label(self.series.xname, color=BLACK,
                            font_name=self.font, font_size=self.height*self.margins[0]*0.3, x=self.width/2, y=0,
                            anchor_x='center', anchor_y='bottom')
        axistitle.draw()
            
    def drawYAxis(self):
        lef = self.bounds[0][0]
        rig = self.bounds[0][1]
        start = self.bounds[1][0]
        stop = self.bounds[1][1]
        increment = float(stop-start)/self.lines[1]
        for y in numpy.arange(start, stop+1, increment):
            # Using fp arithmetic to avoid intermittent fencepost errors
            y = int(y)
            pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (lef, y, rig, y)),
                                 ('c3B', (0, 0, 0, 0, 0, 0)))
cad30d6c   Christopher Stone   Progress towards ...
98
99
        
        
001c428d   Christopher Stone   Beginnings of a c...
100
        
6432a3a5   Christopher Stone   Added class to ke...
101
102
testseries = Series()

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

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

948da001   Christopher Stone   More progress on ...
112
pyglet.clock.schedule_interval(pollSerial, 0.1)
001c428d   Christopher Stone   Beginnings of a c...
113
114

pyglet.app.run()