2 # Copyright 2008 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
22 ##################################################
24 ##################################################
29 from constants import *
30 from gnuradio import gr #for gr.prefs
33 ##################################################
35 ##################################################
36 NEG_INF = float('-inf')
38 AVG_ALPHA_MIN_EXP, AVG_ALPHA_MAX_EXP = -3, 0
39 DEFAULT_NUMBER_RATE = gr.prefs().get_long('wxgui', 'number_rate', 5)
40 DEFAULT_WIN_SIZE = (300, 300)
41 DEFAULT_GAUGE_RANGE = 1000
42 VALUE_REPR_KEY = 'value_repr'
43 VALUE_REAL_KEY = 'value_real'
44 VALUE_IMAG_KEY = 'value_imag'
46 ##################################################
47 # Number window control panel
48 ##################################################
49 class control_panel(wx.Panel):
51 A control panel with wx widgits to control the averaging.
54 def __init__(self, parent):
56 Create a new control panel.
57 @param parent the wx parent window
60 wx.Panel.__init__(self, parent)
61 parent[SHOW_CONTROL_PANEL_KEY] = True
62 parent.subscribe(SHOW_CONTROL_PANEL_KEY, self.Show)
63 control_box = wx.BoxSizer(wx.VERTICAL)
64 #checkboxes for average and peak hold
65 control_box.AddStretchSpacer()
66 options_box = forms.static_box_sizer(
67 parent=self, sizer=control_box, label='Options',
68 bold=True, orient=wx.VERTICAL,
71 sizer=options_box, parent=self, label='Peak Hold',
72 ps=parent, key=PEAK_HOLD_KEY,
75 sizer=options_box, parent=self, label='Average',
76 ps=parent, key=AVERAGE_KEY,
78 #static text and slider for averaging
79 avg_alpha_text = forms.static_text(
80 sizer=options_box, parent=self, label='Avg Alpha',
81 converter=forms.float_converter(lambda x: '%.4f'%x),
82 ps=parent, key=AVG_ALPHA_KEY, width=50,
84 avg_alpha_slider = forms.log_slider(
85 sizer=options_box, parent=self,
86 min_exp=AVG_ALPHA_MIN_EXP,
87 max_exp=AVG_ALPHA_MAX_EXP,
88 num_steps=SLIDER_STEPS,
89 ps=parent, key=AVG_ALPHA_KEY,
91 for widget in (avg_alpha_text, avg_alpha_slider):
92 parent.subscribe(AVERAGE_KEY, widget.Enable)
93 widget.Enable(parent[AVERAGE_KEY])
95 control_box.AddStretchSpacer()
97 sizer=control_box, parent=self,
98 true_label='Stop', false_label='Run',
99 ps=parent, key=RUNNING_KEY,
102 self.SetSizerAndFit(control_box)
104 ##################################################
105 # Numbersink window with label and gauges
106 ##################################################
107 class number_window(wx.Panel, pubsub.pubsub):
126 pubsub.pubsub.__init__(self)
127 wx.Panel.__init__(self, parent, style=wx.SUNKEN_BORDER)
129 self.peak_val_real = NEG_INF
130 self.peak_val_imag = NEG_INF
133 self.decimal_places = decimal_places
135 self.proxy(MSG_KEY, controller, msg_key)
136 self.proxy(AVERAGE_KEY, controller, average_key)
137 self.proxy(AVG_ALPHA_KEY, controller, avg_alpha_key)
138 self.proxy(SAMPLE_RATE_KEY, controller, sample_rate_key)
140 self[PEAK_HOLD_KEY] = peak_hold
141 self[RUNNING_KEY] = True
142 self[VALUE_REAL_KEY] = minval
143 self[VALUE_IMAG_KEY] = minval
144 #setup the box with display and controls
145 self.control_panel = control_panel(self)
146 main_box = wx.BoxSizer(wx.HORIZONTAL)
147 sizer = forms.static_box_sizer(
148 parent=self, sizer=main_box, label=title,
149 bold=True, orient=wx.VERTICAL, proportion=1,
151 main_box.Add(self.control_panel, 0, wx.EXPAND)
152 sizer.AddStretchSpacer()
154 parent=self, sizer=sizer,
155 ps=self, key=VALUE_REPR_KEY, width=size[0],
156 converter=forms.str_converter(),
158 sizer.AddStretchSpacer()
159 self.gauge_real = forms.gauge(
160 parent=self, sizer=sizer, style=wx.GA_HORIZONTAL,
161 ps=self, key=VALUE_REAL_KEY, length=size[0],
162 minimum=minval, maximum=maxval, num_steps=DEFAULT_GAUGE_RANGE,
164 self.gauge_imag = forms.gauge(
165 parent=self, sizer=sizer, style=wx.GA_HORIZONTAL,
166 ps=self, key=VALUE_IMAG_KEY, length=size[0],
167 minimum=minval, maximum=maxval, num_steps=DEFAULT_GAUGE_RANGE,
170 self.show_gauges(show_gauge)
171 self.SetSizerAndFit(main_box)
173 self.subscribe(MSG_KEY, self.handle_msg)
175 def show_gauges(self, show_gauge):
177 Show or hide the gauges.
178 If this is real, never show the imaginary gauge.
179 @param show_gauge true to show
181 self.gauge_real.ShowItems(show_gauge)
182 self.gauge_imag.ShowItems(show_gauge and not self.real)
184 def handle_msg(self, msg):
186 Handle a message from the message queue.
187 Convert the string based message into a float or complex.
188 If more than one number was read, only take the last number.
189 Perform peak hold operations, set the gauges and display.
190 @param event event.data is the number sample as a character array
192 if not self[RUNNING_KEY]: return
193 format_string = "%%.%df"%self.decimal_places
195 sample = numpy.fromstring(msg, numpy.float32)[-1]
196 if self[PEAK_HOLD_KEY]: sample = self.peak_val_real = max(self.peak_val_real, sample)
197 label_text = "%s %s"%(format_string%sample, self.units)
198 self[VALUE_REAL_KEY] = sample
200 sample = numpy.fromstring(msg, numpy.complex64)[-1]
201 if self[PEAK_HOLD_KEY]:
202 self.peak_val_real = max(self.peak_val_real, sample.real)
203 self.peak_val_imag = max(self.peak_val_imag, sample.imag)
204 sample = self.peak_val_real + self.peak_val_imag*1j
205 label_text = "%s + %sj %s"%(format_string%sample.real, format_string%sample.imag, self.units)
206 self[VALUE_REAL_KEY] = sample.real
207 self[VALUE_IMAG_KEY] = sample.imag
209 self[VALUE_REPR_KEY] = label_text
211 if not self[PEAK_HOLD_KEY]:
212 self.peak_val_real = NEG_INF
213 self.peak_val_imag = NEG_INF