Blame view

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

001c428d   Christopher Stone   Beginnings of a c...
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
38
        self.size = size
        self.font = 'Arkhip'
        self.window = pyglet.window.Window(self.size[0], self.size[1], resizable=True)
6432a3a5   Christopher Stone   Added class to ke...
39
        self.window.set_caption(self.title)
948da001   Christopher Stone   More progress on ...
40
41
42
        self.window.on_resize = self.resize
        self.window.on_draw = self.draw
    
4d80e306   Christopher Stone   inadvertently fix...
43
    def resize(self, width, height):
01bfd701   Christopher Stone   Added docstrings
44
        """Handle a pyglet resize event, then give control back to the event loop"""
948da001   Christopher Stone   More progress on ...
45
        self.size = (width, height)
44da1d6d   Christopher Stone   Fixed drawing bug...
46
        super(pyglet.window.Window, self.window).on_resize(width, height)
948da001   Christopher Stone   More progress on ...
47
        
001c428d   Christopher Stone   Beginnings of a c...
48
    def draw(self):
01bfd701   Christopher Stone   Added docstrings
49
        """Draw all the components of the graph"""
948da001   Christopher Stone   More progress on ...
50
51
52
        self.drawBackground()
        self.drawHeading()
        
001c428d   Christopher Stone   Beginnings of a c...
53
    def drawBackground(self):
01bfd701   Christopher Stone   Added docstrings
54
        """Draw the graph background, currently a plain colour"""
001c428d   Christopher Stone   Beginnings of a c...
55
56
57
        pyglet.image.SolidColorImagePattern(WHITE).create_image(self.size[0], self.size[1]).blit(0, 0)
        
    def drawHeading(self):
01bfd701   Christopher Stone   Added docstrings
58
        """Draw a title for the graph (duplicated in the window titlebar, if present"""
001c428d   Christopher Stone   Beginnings of a c...
59
60
61
62
63
        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()
        
6432a3a5   Christopher Stone   Added class to ke...
64
65
testseries = Series()

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

4d80e306   Christopher Stone   inadvertently fix...
69
def pollSerial(foo):
e4e75d3f   Christopher Stone   Accept data from ...
70
    """Check serial port for incoming data"""
21ddadaa   Christopher Stone   Added a comment a...
71
    # Note, foo seems to be a float
e4e75d3f   Christopher Stone   Accept data from ...
72
73
    values = datafeed.readline().strip().split(", ")
    testseries.addpoint(values)
948da001   Christopher Stone   More progress on ...
74

948da001   Christopher Stone   More progress on ...
75
pyglet.clock.schedule_interval(pollSerial, 0.1)
001c428d   Christopher Stone   Beginnings of a c...
76
77

pyglet.app.run()