3 # Copyright 2003,2004,2005 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING. If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
23 from gnuradio import gr, gru
24 from gnuradio.wxgui import stdgui
26 import gnuradio.wxgui.plot as plot
33 default_stripchartsink_size = (640,140)
36 def default_cfunc(datum):
39 class stripchart_sink_base(object):
40 def __init__(self, input_is_real=False, y_per_div=10, ref_level=50,
41 sample_rate=1, stripsize=4,
42 title='',xlabel="X", ylabel="Y", divbase=0.025,
43 cfunc=default_cfunc, parallel=False, scaling=1.0, autoscale=False):
45 # initialize common attributes
47 self.y_per_div=y_per_div
48 self.ref_level = ref_level
49 self.autoscale = autoscale
50 self.sample_rate = sample_rate
51 self.parallel = parallel
55 self.divbase = divbase
56 self.scaling = scaling
58 self.input_is_real = input_is_real
59 self.msgq = gr.msg_queue(2) # queue that holds a maximum of 2 messages
60 self.vector=Numeric.zeros(stripsize,Numeric.Float64)
63 self.stripsize=stripsize
65 def set_y_per_div(self, y_per_div):
66 self.y_per_div = y_per_div
68 def set_ref_level(self, ref_level):
69 self.ref_level = ref_level
71 def set_autoscale(self, auto):
74 class stripchart_sink_f(gr.hier_block, stripchart_sink_base):
75 def __init__(self, fg, parent,
76 y_per_div=10, ref_level=50, sample_rate=1,
77 title='', stripsize=4,
78 size=default_stripchartsink_size,xlabel="X",
79 ylabel="Y", divbase=0.025, cfunc=default_cfunc,
80 parallel=False, scaling=1.0, autoscale=False):
82 stripchart_sink_base.__init__(self, input_is_real=True,
83 y_per_div=y_per_div, ref_level=ref_level,
84 sample_rate=sample_rate,
86 xlabel=xlabel, ylabel=ylabel,
87 divbase=divbase, title=title,
88 cfunc=cfunc, parallel=parallel,
89 scaling=scaling, autoscale=autoscale)
91 if (parallel == True):
92 one = gr.keep_one_in_n (gr.sizeof_float*stripsize, 1)
93 sink = gr.message_sink(gr.sizeof_float*stripsize, self.msgq, True)
95 one = gr.keep_one_in_n (gr.sizeof_float, 1)
96 sink = gr.message_sink(gr.sizeof_float, self.msgq, True)
97 fg.connect (one, sink)
99 gr.hier_block.__init__(self, fg, one, sink)
101 self.win = stripchart_window(self, parent, size=size)
105 # ------------------------------------------------------------------------
107 myDATA_EVENT = wx.NewEventType()
108 EVT_DATA_EVENT = wx.PyEventBinder (myDATA_EVENT, 0)
111 class DataEvent(wx.PyEvent):
112 def __init__(self, data):
113 wx.PyEvent.__init__(self)
114 self.SetEventType (myDATA_EVENT)
118 self.__class__ (self.GetId())
121 class input_watcher (threading.Thread):
122 def __init__ (self, msgq, evsize, event_receiver, **kwds):
123 threading.Thread.__init__ (self, **kwds)
127 self.event_receiver = event_receiver
128 self.keep_running = True
132 while (self.keep_running):
133 msg = self.msgq.delete_head() # blocking read of message queue
134 itemsize = int(msg.arg1())
135 nitems = int(msg.arg2())
137 s = msg.to_string() # get the body of the msg as a string
139 # There may be more than one frame in the message.
140 # If so, we take only the last one
142 start = itemsize * (nitems - 1)
143 s = s[start:start+itemsize]
145 complex_data = Numeric.fromstring (s, Numeric.Float32)
146 de = DataEvent (complex_data)
147 wx.PostEvent (self.event_receiver, de)
150 class stripchart_window(plot.PlotCanvas):
151 def __init__ (self, stripchartsink, parent, id = -1,
152 pos = wx.DefaultPosition, size = wx.DefaultSize,
153 style = wx.DEFAULT_FRAME_STYLE, name = ""):
154 plot.PlotCanvas.__init__ (self, parent, id, pos, size, style, name)
157 self.stripchartsink = stripchartsink
159 self.SetEnableGrid (True)
160 # self.SetEnableZoom (True)
161 # self.SetBackgroundColour ('black')
163 self.build_popup_menu()
165 EVT_DATA_EVENT (self, self.set_data)
167 wx.EVT_CLOSE (self, self.on_close_window)
168 self.Bind(wx.EVT_RIGHT_UP, self.on_right_click)
170 self.input_watcher = input_watcher(stripchartsink.msgq, 1, self)
173 def on_close_window (self, event):
174 print "stripchart_window:on_close_window"
175 self.keep_running = False
178 def set_data (self, evt):
182 calc_min = min(indata)
183 calc_max = max(indata)
184 d = calc_max - calc_min
186 if self.stripchartsink.autoscale == True and self.stripchartsink.parallel == True:
187 self.y_range = self._axisInterval ('min', calc_min-d, calc_max+d)
189 if (self.stripchartsink.parallel != True):
190 indata = self.stripchartsink.cfunc(indata)
192 N = self.stripchartsink.stripsize
193 if self.stripchartsink.parallel != True:
196 self.stripchartsink.vector[pooey] = self.stripchartsink.vector[pooey-1]
198 self.stripchartsink.vector[0] = indata
201 self.stripchartsink.vector = indata
203 if self.stripchartsink.parallel == True:
205 for i in range(0,self.stripchartsink.stripsize):
206 if self.stripchartsink.vector[i] > 0:
207 avg += self.stripchartsink.vector[i]
208 if self.stripchartsink.vector[i] < calc_min:
209 calc_min = self.stripchartsink.vector[i]
210 if self.stripchartsink.vector[i] > calc_max:
211 calc_max = self.stripchartsink.vector[i]
213 avg /= self.stripchartsink.stripsize
216 for i in range(0,self.stripchartsink.stripsize):
217 if (self.stripchartsink.vector[i] > 0 and
218 self.stripchartsink.vector[i] > (avg*5)):
219 markers.append((i*self.stripchartsink.scaling,
220 self.stripchartsink.vector[i]))
223 points = Numeric.zeros((N,2), Numeric.Float64)
225 if self.stripchartsink.scaling == 1.0:
228 points[i,0] = i * self.stripchartsink.scaling
229 points[i,1] = self.stripchartsink.vector[i]
231 if self.stripchartsink.parallel == True and placedmarkers > 1:
233 self.stripchartsink.vector[i] = 0
235 marks = plot.PolyMarker(markers, colour='BLACK', marker='triangle_down')
237 lines = plot.PolyLine (points, colour='RED')
239 # Temporary--I'm find the markers distracting
241 xlab = self.stripchartsink.xlabel
242 ylab = self.stripchartsink.ylabel
243 if (self.stripchartsink.parallel == False) or (placedmarkers <= 1):
244 graphics = plot.PlotGraphics ([lines],
245 title=self.stripchartsink.title,
246 xLabel = xlab, yLabel = ylab)
249 graphics = plot.PlotGraphics ([lines,marks],
250 title=self.stripchartsink.title,
251 xLabel = xlab, yLabel = ylab)
253 self.Draw (graphics, xAxis=None, yAxis=self.y_range)
255 if self.stripchartsink.autoscale == False or self.stripchartsink.parallel == False:
256 self.update_y_range ()
259 def update_y_range (self):
260 ymax = self.stripchartsink.ref_level
261 ymin = self.stripchartsink.ref_level - self.stripchartsink.y_per_div * self.stripchartsink.y_divs
262 self.y_range = self._axisInterval ('min', ymin, ymax)
264 def on_incr_ref_level(self, evt):
265 # print "on_incr_ref_level"
266 self.stripchartsink.set_ref_level(self.stripchartsink.ref_level
267 + self.stripchartsink.y_per_div)
269 def on_decr_ref_level(self, evt):
270 # print "on_decr_ref_level"
271 self.stripchartsink.set_ref_level(self.stripchartsink.ref_level
272 - self.stripchartsink.y_per_div)
274 def on_autoscale(self, evt):
275 self.stripchartsink.set_autoscale(evt.IsChecked())
277 def on_incr_y_per_div(self, evt):
278 divbase = self.stripchartsink.divbase
284 # print "on_incr_y_per_div"
285 self.stripchartsink.set_y_per_div(next_up(self.stripchartsink.y_per_div, (x1,x2,x4,x10,x20)))
287 def on_decr_y_per_div(self, evt):
288 # print "on_decr_y_per_div"
289 divbase = self.stripchartsink.divbase
295 self.stripchartsink.set_y_per_div(next_down(self.stripchartsink.y_per_div, (x1,x2,x4,x10,x20)))
297 def on_y_per_div(self, evt):
298 # print "on_y_per_div"
299 divbase=self.stripchartsink.divbase
301 if Id == self.id_y_per_div_1:
302 self.stripchartsink.set_y_per_div(1*divbase)
303 elif Id == self.id_y_per_div_2:
304 self.stripchartsink.set_y_per_div(2*divbase)
305 elif Id == self.id_y_per_div_5:
306 self.stripchartsink.set_y_per_div(4*divbase)
307 elif Id == self.id_y_per_div_10:
308 self.stripchartsink.set_y_per_div(10*divbase)
309 elif Id == self.id_y_per_div_20:
310 self.stripchartsink.set_y_per_div(20*divbase)
313 def on_right_click(self, event):
314 menu = self.popup_menu
315 for id, pred in self.checkmarks.items():
316 item = menu.FindItemById(id)
318 self.PopupMenu(menu, event.GetPosition())
321 def build_popup_menu(self):
322 divbase=self.stripchartsink.divbase
323 self.id_incr_ref_level = wx.NewId()
324 self.id_decr_ref_level = wx.NewId()
325 self.id_autoscale = wx.NewId()
326 self.id_incr_y_per_div = wx.NewId()
327 self.id_decr_y_per_div = wx.NewId()
328 self.id_y_per_div_1 = wx.NewId()
329 self.id_y_per_div_2 = wx.NewId()
330 self.id_y_per_div_5 = wx.NewId()
331 self.id_y_per_div_10 = wx.NewId()
332 self.id_y_per_div_20 = wx.NewId()
334 self.Bind(wx.EVT_MENU, self.on_incr_ref_level, id=self.id_incr_ref_level)
335 self.Bind(wx.EVT_MENU, self.on_decr_ref_level, id=self.id_decr_ref_level)
336 self.Bind(wx.EVT_MENU, self.on_autoscale, id=self.id_autoscale)
337 self.Bind(wx.EVT_MENU, self.on_incr_y_per_div, id=self.id_incr_y_per_div)
338 self.Bind(wx.EVT_MENU, self.on_decr_y_per_div, id=self.id_decr_y_per_div)
339 self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_1)
340 self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_2)
341 self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_5)
342 self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_10)
343 self.Bind(wx.EVT_MENU, self.on_y_per_div, id=self.id_y_per_div_20)
348 self.popup_menu = menu
349 menu.Append(self.id_incr_ref_level, "Incr Ref Level")
350 menu.Append(self.id_decr_ref_level, "Decr Ref Level")
351 menu.AppendSeparator()
352 menu.AppendCheckItem(self.id_autoscale, "Auto Scale")
353 # we'd use RadioItems for these, but they're not supported on Mac
356 menu.AppendCheckItem(self.id_y_per_div_1, s)
359 menu.AppendCheckItem(self.id_y_per_div_2, s)
362 menu.AppendCheckItem(self.id_y_per_div_5, s)
365 menu.AppendCheckItem(self.id_y_per_div_10, s)
368 menu.AppendCheckItem(self.id_y_per_div_20, s)
371 self.id_autoscale : lambda : self.stripchartsink.autoscale,
372 self.id_y_per_div_1 : lambda : self.stripchartsink.y_per_div == 1*divbase,
373 self.id_y_per_div_2 : lambda : self.stripchartsink.y_per_div == 2*divbase,
374 self.id_y_per_div_5 : lambda : self.stripchartsink.y_per_div == 4*divbase,
375 self.id_y_per_div_10 : lambda : self.stripchartsink.y_per_div == 10*divbase,
376 self.id_y_per_div_20 : lambda : self.stripchartsink.y_per_div == 20*divbase,
382 Return the first item in seq that is > v.
389 def next_down(v, seq):
391 Return the last item in seq that is < v.