-
Notifications
You must be signed in to change notification settings - Fork 6
/
TekScopeGUI.py
283 lines (223 loc) · 8.88 KB
/
TekScopeGUI.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
Tektronix scope interface in python.
Andrew Dawes (dawes@pacificu.edu)
Based on demo by:
Eli Bendersky (eliben@gmail.com)
License: this code is in the public domain
"""
import os
import pprint
import random
import wx
# The recommended way to use wx with mpl is with the WXAgg
# backend.
#
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
FigureCanvasWxAgg as FigCanvas, \
NavigationToolbar2WxAgg as NavigationToolbar
import instrument
import numpy
class ScopeFrame(wx.Frame):
""" The main frame of the application
"""
title = 'Tektronix scope download'
def __init__(self):
wx.Frame.__init__(self, None, -1, self.title)
self.data = [0, 0, 0, 0, 0]
self.xdata = range(len(self.data))
self.create_menu()
self.create_status_bar()
self.create_main_panel()
self.draw_figure()
def create_menu(self):
self.menubar = wx.MenuBar()
menu_file = wx.Menu()
m_expt = menu_file.Append(-1, "&Save data to Numpy\tCtrl-S", "Save data to Numpy file")
self.Bind(wx.EVT_MENU, self.on_save_data, m_expt)
m_expt_csv = menu_file.Append(-1, "&Save data to CSV\tCtrl-Shift-S", "Save data to CSVfile")
self.Bind(wx.EVT_MENU, self.on_save_data_csv, m_expt_csv)
menu_file.AppendSeparator()
m_exit = menu_file.Append(-1, "E&xit\tCtrl-X", "Exit")
self.Bind(wx.EVT_MENU, self.on_exit, m_exit)
menu_device = wx.Menu()
m_device = menu_device.Append(-1, "&Select Device\tCtrl-D", "Select from multiple connected devices")
self.Bind(wx.EVT_MENU, self.on_device, m_device)
menu_help = wx.Menu()
m_about = menu_help.Append(-1, "&About\tF1", "About this software")
self.Bind(wx.EVT_MENU, self.on_about, m_about)
self.menubar.Append(menu_file, "&File")
self.menubar.Append(menu_device, "&Device")
self.menubar.Append(menu_help, "&Help")
self.SetMenuBar(self.menubar)
def create_main_panel(self):
""" Creates the main panel with all the controls on it:
* mpl canvas
* mpl navigation toolbar
* Control panel for interaction
"""
self.panel = wx.Panel(self)
# Create the mpl Figure and FigCanvas objects.
# 5x4 inches, 100 dots-per-inch
#
self.dpi = 100
self.fig = Figure((5.0, 4.0), dpi=self.dpi)
self.canvas = FigCanvas(self.panel, -1, self.fig)
# Since we have only one plot, we can use add_axes
# instead of add_subplot, but then the subplot
# configuration tool in the navigation toolbar wouldn't
# work.
#
self.axes = self.fig.add_subplot(111)
self.ch1button = wx.Button(self.panel, -1, "CH1")
self.Bind(wx.EVT_BUTTON, self.on_ch1_button, self.ch1button)
self.ch2button = wx.Button(self.panel, -1, "CH2")
self.Bind(wx.EVT_BUTTON, self.on_ch2_button, self.ch2button)
self.ch3button = wx.Button(self.panel, -1, "CH3")
self.Bind(wx.EVT_BUTTON, self.on_ch3_button, self.ch3button)
self.ch4button = wx.Button(self.panel, -1, "CH4")
self.Bind(wx.EVT_BUTTON, self.on_ch4_button, self.ch4button)
self.refabutton = wx.Button(self.panel, -1, "RefA")
self.Bind(wx.EVT_BUTTON, self.on_refa_button, self.refabutton)
self.refbbutton = wx.Button(self.panel, -1, "RefB")
self.Bind(wx.EVT_BUTTON, self.on_refb_button, self.refbbutton)
self.cb_grid = wx.CheckBox(self.panel, -1,
"Show Grid",
style=wx.ALIGN_RIGHT)
self.Bind(wx.EVT_CHECKBOX, self.on_cb_grid, self.cb_grid)
# Create the navigation toolbar, tied to the canvas
#
#self.toolbar = NavigationToolbar(self.canvas)
#self.toolbar.Realize()
#
# Layout with box sizers
#
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
#self.vbox.Add(self.toolbar, 0, wx.EXPAND)
self.vbox.AddSpacer(10)
self.hbox = wx.BoxSizer(wx.HORIZONTAL)
flags = wx.ALIGN_LEFT | wx.ALL | wx.ALIGN_CENTER_VERTICAL
self.hbox.Add(self.ch1button, 0, border=3, flag=flags)
self.hbox.Add(self.ch2button, 0, border=3, flag=flags)
self.hbox.Add(self.ch3button, 0, border=3, flag=flags)
self.hbox.Add(self.ch4button, 0, border=3, flag=flags)
self.hbox.Add(self.refabutton, 0, border=3, flag=flags)
self.hbox.Add(self.refbbutton, 0, border=3, flag=flags)
self.hbox.Add(self.cb_grid, 0, border=3, flag=flags)
self.hbox.AddSpacer(30)
self.vbox.Add(self.hbox, 0, flag = wx.ALIGN_LEFT | wx.TOP)
self.panel.SetSizer(self.vbox)
self.vbox.Fit(self)
def create_status_bar(self):
self.statusbar = self.CreateStatusBar()
def draw_figure(self):
""" Redraws the figure
"""
# clear the axes and redraw the plot anew
self.axes.clear()
self.axes.grid(self.cb_grid.IsChecked())
self.axes.plot([i*1000 for i in self.xdata], self.data)
self.canvas.draw()
def on_cb_grid(self, event):
self.draw_figure()
def on_ch1_button(self, event):
self.data = self.inst.get_data("CH1")
self.xdata = self.inst.get_xdata()
self.draw_figure()
def on_ch2_button(self, event):
self.data = self.inst.get_data("CH2")
self.xdata = self.inst.get_xdata()
self.draw_figure()
def on_ch3_button(self, event):
self.data = self.inst.get_data("CH3")
self.xdata = self.inst.get_xdata()
self.draw_figure()
def on_ch4_button(self, event):
self.data = self.inst.get_data("CH4")
self.xdata = self.inst.get_xdata()
self.draw_figure()
def on_refa_button(self, event):
self.data = self.inst.get_data("REFA")
self.xdata = self.inst.get_xdata()
self.draw_figure()
def on_refb_button(self, event):
self.data = self.inst.get_data("REFB")
self.xdata = self.inst.get_xdata()
self.draw_figure()
def on_save_data(self, event):
file_choices = "Numpy (*.npy)|*.npy"
dlg = wx.FileDialog(
self,
message="Save data as...",
defaultDir=os.getcwd(),
defaultFile="data",
wildcard=file_choices,
style=wx.FD_SAVE)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
#self.canvas.print_figure(path, dpi=self.dpi)
numpy.save(path, (self.xdata, self.data))
self.flash_status_message("Data saved to %s" % path)
def on_save_data_csv(self, event):
file_choices = "CSV (*.csv)|*.csv"
dlg = wx.FileDialog(
self,
message="Save data as...",
defaultDir=os.getcwd(),
defaultFile="data",
wildcard=file_choices,
style=wx.FD_SAVE)
if dlg.ShowModal() == wx.ID_OK:
path = dlg.GetPath()
#self.canvas.print_figure(path, dpi=self.dpi)
out = numpy.zeros((len(self.data), 2))
for (i,l) in enumerate(zip(self.xdata, self.data)):
out[i,:] = l
numpy.savetxt(path, out)
self.flash_status_message("Data saved to %s" % path)
def on_device(self, event):
msg = """ Select from multiple connected TDS1000 scopes
"""
caption = "Select Device"
#TODO find all connected serial numbers:
choices=["Upper","Lower"]
serial_numbers=["C010128","C010113"]
dlg = wx.SingleChoiceDialog(self, msg, caption, choices)
dlg.ShowModal()
serialno = serial_numbers[dlg.GetSelection()]
if(serialno):
#if(self.inst): self.inst.close()
self.inst = instrument.TekScope1000(serialno)
dlg.Destroy()
def on_exit(self, event):
self.Destroy()
def on_about(self, event):
msg = """ View and save data from Tektronix scope:
* Use the matplotlib navigation bar
* Select a channel to view or save
* Save the data to a file using the File menu
"""
dlg = wx.MessageDialog(self, msg, "About", wx.OK)
dlg.ShowModal()
dlg.Destroy()
def flash_status_message(self, msg, flash_len_ms=1500):
self.statusbar.SetStatusText(msg)
self.timeroff = wx.Timer(self)
self.Bind(
wx.EVT_TIMER,
self.on_flash_status_off,
self.timeroff)
self.timeroff.Start(flash_len_ms, oneShot=True)
def on_flash_status_off(self, event):
self.statusbar.SetStatusText('')
if __name__ == '__main__':
# upper scope is serial number C010128
# lower scope is serial number C010113
#inst = instrument.TekScope1000("C010113")
app = wx.App(False)
app.frame = ScopeFrame()
app.frame.Show()
app.MainLoop()