Blame view

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

import pyglet
#import math
#import time
#import serial

from colours import *

6432a3a5   Christopher Stone   Added class to ke...
10
11
12
13
14
15
16
17
18
19
20
21
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...
22
class Plot:
6432a3a5   Christopher Stone   Added class to ke...
23
    def __init__(self, series, size=(640, 480)):
01bfd701   Christopher Stone   Added docstrings
24
        """Setup a the details of a plot, and create a corresponding window"""
6432a3a5   Christopher Stone   Added class to ke...
25
26
        self.series = series
        self.title = self.series.title
948da001   Christopher Stone   More progress on ...
27
28
29
        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...
30
        self.window.set_caption(self.title)
948da001   Christopher Stone   More progress on ...
31
32
33
        self.window.on_resize = self.resize
        self.window.on_draw = self.draw
    
4d80e306   Christopher Stone   inadvertently fix...
34
    def resize(self, width, height):
01bfd701   Christopher Stone   Added docstrings
35
        """Handle a pyglet resize event, then give control back to the event loop"""
948da001   Christopher Stone   More progress on ...
36
        self.size = (width, height)
44da1d6d   Christopher Stone   Fixed drawing bug...
37
        super(pyglet.window.Window, self.window).on_resize(width, height)
948da001   Christopher Stone   More progress on ...
38
        
001c428d   Christopher Stone   Beginnings of a c...
39
    def draw(self):
01bfd701   Christopher Stone   Added docstrings
40
        """Draw all the components of the graph"""
948da001   Christopher Stone   More progress on ...
41
42
43
        self.drawBackground()
        self.drawHeading()
        
001c428d   Christopher Stone   Beginnings of a c...
44
    def drawBackground(self):
01bfd701   Christopher Stone   Added docstrings
45
        """Draw the graph background, currently a plain colour"""
001c428d   Christopher Stone   Beginnings of a c...
46
47
48
        pyglet.image.SolidColorImagePattern(WHITE).create_image(self.size[0], self.size[1]).blit(0, 0)
        
    def drawHeading(self):
01bfd701   Christopher Stone   Added docstrings
49
        """Draw a title for the graph (duplicated in the window titlebar, if present"""
001c428d   Christopher Stone   Beginnings of a c...
50
51
52
53
54
        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...
55
56
testseries = Series()

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

4d80e306   Christopher Stone   inadvertently fix...
60
def pollSerial(foo):
01bfd701   Christopher Stone   Added docstrings
61
    """Dummy, will check serial port for incoming data"""
21ddadaa   Christopher Stone   Added a comment a...
62
    # Note, foo seems to be a float
948da001   Christopher Stone   More progress on ...
63
64
    pass

948da001   Christopher Stone   More progress on ...
65
pyglet.clock.schedule_interval(pollSerial, 0.1)
001c428d   Christopher Stone   Beginnings of a c...
66
67

pyglet.app.run()