forked from gregpinero/ArduinoPlot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SerialGraph.py
157 lines (132 loc) · 4.64 KB
/
SerialGraph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
import sys
import time
import optparse
import serial
import SerialData
import wx
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
FigureCanvasWxAgg as FigCanvas, \
NavigationToolbar2WxAgg as NavigationToolbar
import numpy as np
import pylab
REFRESH_INTERVAL_MS = 100
DEBUG = False
def processArguments():
"""Handle cli args"""
parser = optparse.OptionParser(version="%prog 0.1")
parser.set_usage("%prog [options]\nGraph arbitrary data received from serial port")
parser.add_option("-p", "--port", dest="port", default="/dev/tty.usbserial", help="Serial port. Default: %default")
parser.add_option("-b", "--baudrate", dest="baudrate", default=9600, help="Baud rate. Default: %default")
parser.add_option("-x", "--xlabel", dest="xlabel", default="seconds", help="Label for X axis. Default: %default")
parser.add_option("-y", "--ylabel", dest="ylabel", default="mW", help="Label for Y axis. Default: %default")
parser.add_option("-d", "--debug", action="store_true", dest="debug", help="Enable debugging messages")
(options, args) = parser.parse_args()
return options, args
class GraphFrame(wx.Frame):
title = 'SerialGraph'
def __init__(self, data, xlabel, ylabel):
self.datasource = data
self.xlabel = xlabel
self.ylabel = ylabel
self.paused = False
self.max = 0.0
self.data = [self.datasource.next()]
wx.Frame.__init__(self, None, -1, self.title)
self.create_main_panel()
self.redraw_timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer)
self.redraw_timer.Start(REFRESH_INTERVAL_MS)
def create_main_panel(self):
self.panel = wx.Panel(self)
self.init_plot()
self.canvas = FigCanvas(self.panel, -1, self.fig)
self.pause_button = wx.Button(self.panel, -1, "Pause")
self.Bind(wx.EVT_BUTTON, self.on_pause_button, self.pause_button)
self.Bind(wx.EVT_UPDATE_UI, self.on_update_pause_button, self.pause_button)
self.reset_button = wx.Button(self.panel, -1, "Reset")
self.Bind(wx.EVT_BUTTON, self.on_reset_button, self.reset_button)
self.Bind(wx.EVT_UPDATE_UI, self.on_update_reset_button, self.reset_button)
self.hbox1 = wx.BoxSizer(wx.HORIZONTAL)
self.hbox1.Add(self.pause_button, border=5, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL)
self.hbox1.AddSpacer(20)
self.hbox1.Add(self.reset_button, border=5, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, flag=wx.LEFT | wx.TOP | wx.GROW)
self.vbox.Add(self.hbox1, 0, flag=wx.ALIGN_LEFT | wx.TOP)
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
def init_plot(self):
self.dpi = 100
self.fig = Figure((10.0, 6.0), dpi=self.dpi)
self.axes = self.fig.add_subplot(111, xlabel=self.xlabel, ylabel="mW")
self.axes.set_axis_bgcolor('black')
self.axes.set_title('test', size=12)
pylab.setp(self.axes.get_xticklabels(), fontsize=8)
pylab.setp(self.axes.get_yticklabels(), fontsize=8)
self.plot_data = self.axes.plot (
self.data,
linewidth = 1,
color = (1, 0, 0)
)[0]
def on_pause_button(self, event):
self.paused = not self.paused
def on_reset_button(self, event):
self.data = []
self.max = 0
def on_update_pause_button(self, event):
if self.paused:
label = "Resume"
else:
label = "Pause"
self.pause_button.SetLabel(label)
def on_update_reset_button(self, event):
pass
def on_redraw_timer(self, event):
if not self.paused:
self.data.append(self.datasource.next())
self.draw_plot()
def draw_plot(self):
"""Redraws the plot"""
width = 50
data_len = len(self.data)
xmax = data_len if data_len > width else width
xmin = xmax - width
ymin = 0
if data_len > width:
visible_max = max(self.data[-width:])
ymax = max(visible_max*1.2, 5)
else:
ymax = max(max(self.data), 4) + 1
self.axes.set_xbound(lower=xmin, upper=xmax)
self.axes.set_ybound(lower=ymin, upper=ymax)
self.axes.grid(True, color='green')
self.plot_data.set_xdata(np.arange(data_len))
self.plot_data.set_ydata(np.array(self.data))
if self.data[-1] > self.max:
self.max = self.data[-1]
self.axes.set_title('Current Value: %s. Max Value: %s.' % (self.data[-1], self.max), size=12)
self.canvas.draw()
def main():
global DEBUG
options = processArguments()
DEBUG = options[0].debug
s = SerialData.SerialData(
port=options[0].port,
baudrate=options[0].baudrate,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
xonxoff=0,
rtscts=0,
debug=DEBUG
)
app = wx.PySimpleApp()
app.frame = GraphFrame(s, options[0].xlabel, options[0].ylabel)
app.frame.Show()
app.MainLoop()
if __name__ == '__main__':
main()