-
Notifications
You must be signed in to change notification settings - Fork 13
/
oscilloscope.py
280 lines (228 loc) · 11.2 KB
/
oscilloscope.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
# FPGA configuration and API
from redpitaya.overlay.mercury import mercury as overlay
# system and mathematics libraries
import time
import math
import numpy as np
# visualization
from bokeh.io import push_notebook, show, output_notebook
from bokeh.models import HoverTool, Range1d, LinearAxis
from bokeh.plotting import figure
from bokeh.layouts import widgetbox
from bokeh.resources import INLINE
# widgets
from IPython.display import display
import ipywidgets as ipw
# threads
from IPython.lib import backgroundjobs as bg
class oscilloscope (object):
size = 1250
range_max = 1.2
# time per division (10 divisions on screen)
x_scales = [ '5s' , '2s' , '1s' ,
'500ms', '200ms', '100ms',
'50ms', '20ms', '10ms',
'5ms', '2ms', '1ms',
'500us', '200us', '100us',
'50us', '20us', '10us',
'5us', '2us', '1us']
x_scales_dict = { '5s' : 5000000, '2s' : 2000000, '1s' : 1000000,
'500ms': 500000, '200ms': 200000, '100ms': 100000,
'50ms': 50000, '20ms': 20000, '10ms': 10000,
'5ms': 5000, '2ms': 2000, '1ms': 1000,
'500us': 500, '200us': 200, '100us': 100,
'50us': 50, '20us': 20, '10us': 10,
'5us': 5, '2us': 2, '1us': 1}
def __init__ (self, channels = [0, 1], input_range = [1.0, 1.0]):
"""Oscilloscope application"""
# instantiate both oscilloscopes
self.channels = channels
self.input_range = input_range
# this will load the FPGA
try:
self.ovl = overlay()
except ResourceWarning:
print ("FPGA bitstream is already loaded")
# wait a bit for the overlay to be properly applied
# TODO it should be automated in the library
time.sleep(0.5)
# ocsilloscope channels
self.osc = [self.channel(top = self, ch = ch, input_range = self.input_range[ch]) for ch in self.channels]
# set parameters common to all channels
for ch in channels:
# trigger timing is in the middle of the screen
self.osc[ch].trigger_pre = self.size//2
self.osc[ch].trigger_post = self.size//2
# default trigger source
self.t_source = 0
# display widgets
self.display()
# threads
self.jobs = bg.BackgroundJobManager()
self.jobs.new('self.run()')
# def __del__ (self):
# # close widgets
# for ch in self.channels:
# self.osc[ch].close()
# # delete generator and overlay objects
# del (self.osc)
# # TODO: overlay should not be removed if a different app added it
# del (self.ovl)
def display (self):
ch = 0
self.x = (np.arange(self.size) - self.osc[ch].trigger_pre) / self.osc[ch].sample_rate
buff = [np.zeros(self.size) for ch in self.channels]
rmax = 1.0
#output_notebook(resources=INLINE)
output_notebook()
colors = ('red', 'blue')
tools = "pan,wheel_zoom,box_zoom,reset,crosshair"
self.p = figure(plot_height=500, plot_width=900, title="oscilloscope", toolbar_location="above", tools=(tools))
self.p.xaxis.axis_label = 'time [s]'
#self.p.yaxis.axis_label = 'voltage [V]'
self.p.x_range = Range1d(self.x[0], self.x[-1])
self.p.y_range = Range1d(-rmax, +rmax)
self.p.extra_y_ranges = {str(ch): Range1d(-rmax, +rmax) for ch in self.channels}
for ch in self.channels:
self.p.add_layout(LinearAxis(y_range_name=str(ch).format(ch), axis_label = 'CH {} voltage [V]'.format(ch)), 'left')
self.r = [self.p.line(self.x, buff[ch], line_width=1, line_alpha=0.7, color=colors[ch], y_range_name=str(ch)) for ch in self.channels]
# trigger time/amplitude
ch = 0
if self.osc[ch].edge is 'pos': level = self.osc[ch].level[1]
else : level = self.osc[ch].level[0]
self.h_trigger_t = [self.p.line ([0,0], [-rmax, +rmax], color="black", line_width=1, line_alpha=0.75)]
self.h_trigger_a = [self.p.line ([self.x[0], self.x[-1]], [level]*2, color="black", line_width=1, line_alpha=0.75),
self.p.quad(bottom=[self.osc[ch].level[0]], top=[self.osc[ch].level[1]],
left=[self.x[0]], right=[self.x[-1]], color="grey", alpha=0.25)]
# configure hover tool
hover = HoverTool(mode = 'vline', tooltips=[("T", "@x"), ("V", "@y")], renderers=self.r)
self.p.add_tools(hover)
# style
#self.p.yaxis[0].major_tick_line_color = None
#self.p.yaxis[0].minor_tick_line_color = None
self.p.yaxis[0].visible = False
# get an explicit handle to update the next show cell with
self.target = show(self.p, notebook_handle=True)
# create widgets
self.w_enable = ipw.ToggleButton (value=False, description='input enable')
self.w_x_scale = ipw.SelectionSlider (value=self.x_scales[-1], options=self.x_scales, description='X scale')
self.w_x_position = ipw.FloatSlider (value=0, min=-rmax, max=+rmax, step=0.02, description='X position')
self.w_t_source = ipw.ToggleButtons (value=self.t_source, options=[0, 1], description='T source')
# style widgets
self.w_enable.layout = ipw.Layout(width='100%')
self.w_x_scale.layout = ipw.Layout(width='100%')
self.w_x_position.layout = ipw.Layout(width='100%')
self.w_enable.observe (self.clb_enable , names='value')
self.w_x_scale.observe (self.clb_x_scale , names='value')
self.w_x_position.observe (self.clb_x_position, names='value')
self.w_t_source.observe (self.clb_t_source , names='value')
display(self.w_x_scale, self.w_t_source)
for ch in self.channels:
self.osc[ch].display()
def clb_enable (self, change):
i=0
def clb_x_scale (self, change):
for ch in self.channels:
self.osc[ch].decimation = self.x_scales_dict[change['new']]
self.osc[self.channels[0]].reset()
self.clb_x_update()
def clb_x_position (self, change):
i=0
def clb_x_update (self):
ch = 0
self.x = (np.arange(self.size) - self.osc[ch].trigger_pre) / self.osc[ch].sample_rate
for ch in self.channels:
self.r[ch].data_source.data['x'] = self.x
self.p.x_range.start = self.x[ 0]
self.p.x_range.end = self.x[-1]
# trigger level and edge
self.h_trigger_a[1].data_source.data['left'] = [self.x[ 0]]
self.h_trigger_a[1].data_source.data['right'] = [self.x[-1]]
self.h_trigger_a[0].data_source.data['x'] = [self.x[0], self.x[-1]]
push_notebook(handle=self.target)
def clb_t_source (self, change):
self.t_source = change['new']
for ch in self.channels:
self.osc[ch].sync_src = self.ovl.sync_src['osc0']
self.osc[ch].trig_src = self.ovl.trig_src['osc'+str(self.t_source)]
self.clb_t_update()
self.clb_y_update()
def clb_t_update (self):
osc = self.osc[self.t_source]
# trigger level and edge
self.h_trigger_a[1].data_source.data['bottom'] = [osc.level[0]]
self.h_trigger_a[1].data_source.data['top'] = [osc.level[1]]
if (osc.edge == 'pos'):
self.h_trigger_a[0].data_source.data['y'] = [osc.level[1]]*2
elif (osc.edge == 'neg'):
self.h_trigger_a[0].data_source.data['y'] = [osc.level[0]]*2
push_notebook(handle=self.target)
def clb_y_update (self):
osc = self.osc[self.t_source]
self.p.y_range.start = osc.y_position - osc.y_scale
self.p.y_range.end = osc.y_position + osc.y_scale
def run (self):
while True:
ch = 0
self.osc[ch].reset()
self.osc[ch].start()
while self.osc[ch].status_run(): pass
#buff = np.absolute(np.fft.fft(buff))
for ch in self.channels:
self.r[ch].data_source.data['y'] = self.osc[ch].data(self.size)
# push updates to the plot continuously using the handle (intererrupt the notebook kernel to stop)
push_notebook(handle=self.target)
#time.sleep(0.05)
class channel (overlay.osc):
# wigget related constants
y_scales = [0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2]
y_scale = 1
y_position = 0
def __init__ (self, top, ch, input_range = 1.0):
super().__init__(ch, input_range)
self.top = top
self.ch = ch
self.reset()
# decimation rate
self.decimation = 1
# trigger level [V] and edge
self.level = [-0.1, +0.1]
self.edge = 'pos'
# control event mask
self.sync_src = overlay.sync_src['osc0']
self.trig_src = overlay.trig_src['osc0']
# TODO: this default should be set in the oscilloscope application class
# create widgets
self.w_t_edge = ipw.ToggleButtons (value=self.edge, options=['pos', 'neg'], description='T edge')
self.w_t_position = ipw.FloatRangeSlider (value=self.level, min=-1.0, max=+1.0, step=0.02, description='T position')
self.w_y_position = ipw.FloatSlider (value=self.y_position, min=-1.0, max=+1.0, step=0.01, description='Y position')
self.w_y_scale = ipw.SelectionSlider (value=self.y_scale, options=self.y_scales, description='Y scale')
# style widgets
self.w_t_position.layout = ipw.Layout(width='100%')
self.w_y_scale.layout = ipw.Layout(width='100%')
self.w_y_position.layout = ipw.Layout(width='100%')
self.w_t_edge.observe (self.clb_t_edge , names='value')
self.w_t_position.observe (self.clb_t_position, names='value')
self.w_y_scale.observe (self.clb_y_scale , names='value')
self.w_y_position.observe (self.clb_y_position, names='value')
def clb_t_edge (self, change):
self.edge = change['new']
self.top.clb_t_update()
def clb_t_position (self, change):
self.level = change['new']
self.top.clb_t_update()
def clb_y_position (self, change):
self.y_position = change['new']
self.clb_y_update()
def clb_y_scale (self, change):
self.y_scale = change['new']
self.clb_y_update()
def clb_y_update (self):
# this does not work, probably a bug in bokeh
# https://github.com/bokeh/bokeh/issues/4014#issuecomment-199147242
#app.p.y_range = Range1d(-0.2, +0.2)
self.top.p.extra_y_ranges[str(self.ch)].start = self.y_position - self.y_scale
self.top.p.extra_y_ranges[str(self.ch)].end = self.y_position + self.y_scale
self.top.clb_y_update()
def display (self):
display(self.w_t_edge, self.w_t_position, self.w_y_position, self.w_y_scale)