3 # Copyright 2004,2005,2007,2008 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 import usrp2
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from gnuradio.wxgui import stdgui2, fftsink2, waterfallsink2, scopesink2, form, slider
28 from optparse import OptionParser
33 class app_top_block(stdgui2.std_top_block):
34 def __init__(self, frame, panel, vbox, argv):
35 stdgui2.std_top_block.__init__(self, frame, panel, vbox, argv)
40 parser = OptionParser(option_class=eng_option)
41 parser.add_option("-e", "--interface", type="string", default="eth0",
42 help="select Ethernet interface, default is eth0")
43 parser.add_option("-m", "--mac-addr", type="string", default="",
44 help="select USRP by MAC address, default is auto-select")
45 #parser.add_option("-A", "--antenna", default=None,
46 # help="select Rx Antenna (only on RFX-series boards)")
47 parser.add_option("-d", "--decim", type="int", default=16,
48 help="set fgpa decimation rate to DECIM [default=%default]")
49 parser.add_option("-f", "--freq", type="eng_float", default=None,
50 help="set frequency to FREQ", metavar="FREQ")
51 parser.add_option("-g", "--gain", type="eng_float", default=None,
52 help="set gain in dB (default is midpoint)")
53 parser.add_option("-W", "--waterfall", action="store_true", default=False,
54 help="Enable waterfall display")
55 parser.add_option("-S", "--oscilloscope", action="store_true", default=False,
56 help="Enable oscilloscope display")
57 parser.add_option("", "--avg-alpha", type="eng_float", default=1e-1,
58 help="Set fftsink averaging factor, default=[%default]")
59 parser.add_option("", "--ref-scale", type="eng_float", default=1.0,
60 help="Set dBFS=0dB input value, default=[%default]")
61 (options, args) = parser.parse_args()
65 self.options = options
66 self.show_debug_info = True
68 self.u = usrp2.source_32fc(options.interface, options.mac_addr)
69 self.u.set_decim(options.decim)
71 input_rate = self.u.adc_rate() / self.u.decim()
75 waterfallsink2.waterfall_sink_c (panel, fft_size=1024, sample_rate=input_rate)
76 elif options.oscilloscope:
77 self.scope = scopesink2.scope_sink_c(panel, sample_rate=input_rate)
79 self.scope = fftsink2.fft_sink_c (panel, fft_size=1024, sample_rate=input_rate,
80 ref_scale=options.ref_scale, ref_level=0.0, y_divs = 10,
81 avg_alpha=options.avg_alpha)
83 self.connect(self.u, self.scope)
90 if options.gain is None:
91 # if no gain was specified, use the mid-point in dB
92 g = self.u.gain_range()
93 options.gain = float(g[0]+g[1])/2
95 if options.freq is None:
96 # if no freq was specified, use the mid-point
97 r = self.u.freq_range()
98 options.freq = float(r[0]+r[1])/2
100 self.set_gain(options.gain)
102 #if options.antenna is not None:
103 # print "Selecting antenna %s" % (options.antenna,)
104 # self.subdev.select_rx_antenna(options.antenna)
106 if self.show_debug_info:
107 self.myform['decim'].set_value(self.u.decim())
108 self.myform['fs@gbe'].set_value(input_rate)
109 self.myform['dbname'].set_value("0x%04X" % (self.u.daughterboard_id(),)) # FIXME: add text name
110 self.myform['baseband'].set_value(0)
111 self.myform['ddc'].set_value(0)
113 if not(self.set_freq(options.freq)):
114 self._set_status_msg("Failed to set initial frequency")
116 def _set_status_msg(self, msg):
117 self.frame.GetStatusBar().SetStatusText(msg, 0)
119 def _build_gui(self, vbox):
121 def _form_set_freq(kv):
122 return self.set_freq(kv['freq'])
124 vbox.Add(self.scope.win, 10, wx.EXPAND)
126 # add control area at the bottom
127 self.myform = myform = form.form()
128 hbox = wx.BoxSizer(wx.HORIZONTAL)
129 hbox.Add((5,0), 0, 0)
130 myform['freq'] = form.float_field(
131 parent=self.panel, sizer=hbox, label="Center freq", weight=1,
132 callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
134 hbox.Add((5,0), 0, 0)
135 g = self.u.gain_range()
136 if self.u.daughterboard_id() == 0x0003: # FIXME: get range right in firmware for TVRX
139 myform['gain'] = form.slider_field(parent=self.panel, sizer=hbox, label="Gain",
141 min=int(g[0]), max=int(g[1]),
142 callback=self.set_gain)
144 hbox.Add((5,0), 0, 0)
145 vbox.Add(hbox, 0, wx.EXPAND)
147 self._build_subpanel(vbox)
149 def _build_subpanel(self, vbox_arg):
150 # build a secondary information panel (sometimes hidden)
152 # FIXME figure out how to have this be a subpanel that is always
153 # created, but has its visibility controlled by foo.Show(True/False)
155 def _form_set_decim(kv):
156 return self.set_decim(kv['decim'])
158 if not(self.show_debug_info):
165 #panel = wx.Panel(self.panel, -1)
166 #vbox = wx.BoxSizer(wx.VERTICAL)
168 hbox = wx.BoxSizer(wx.HORIZONTAL)
171 myform['decim'] = form.int_field(
172 parent=panel, sizer=hbox, label="Decim",
173 callback=myform.check_input_and_call(_form_set_decim, self._set_status_msg))
176 myform['fs@gbe'] = form.static_float_field(
177 parent=panel, sizer=hbox, label="Fs@GbE")
180 myform['dbname'] = form.static_text_field(
181 parent=panel, sizer=hbox)
184 myform['baseband'] = form.static_float_field(
185 parent=panel, sizer=hbox, label="Analog BB")
188 myform['ddc'] = form.static_float_field(
189 parent=panel, sizer=hbox, label="DDC")
192 vbox.Add(hbox, 0, wx.EXPAND)
195 def set_freq(self, target_freq):
197 Set the center frequency we're interested in.
199 @param target_freq: frequency in Hz
202 Tuning is a two step process. First we ask the front-end to
203 tune as close to the desired frequency as it can. Then we use
204 the result of that operation and our target_frequency to
205 determine the value for the digital down converter.
207 r = self.u.set_center_freq(target_freq)
210 self.myform['freq'].set_value(target_freq) # update displayed value
211 if self.show_debug_info:
212 self.myform['baseband'].set_value(r.baseband_freq)
213 self.myform['ddc'].set_value(r.dxc_freq)
214 if not self.options.oscilloscope:
215 self.scope.win.set_baseband_freq(target_freq)
220 def set_gain(self, gain):
221 self.myform['gain'].set_value(gain) # update displayed value
222 self.u.set_gain(gain)
224 def set_decim(self, decim):
225 ok = self.u.set_decim(decim)
227 print "set_decim failed"
228 input_rate = self.u.adc_rate() / self.u.decim()
229 self.scope.set_sample_rate(input_rate)
230 if self.show_debug_info: # update displayed values
231 self.myform['decim'].set_value(self.u.decim())
232 self.myform['fs@gbe'].set_value(input_rate)
235 def _setup_events(self):
236 if not self.options.waterfall and not self.options.oscilloscope:
237 self.scope.win.Bind(wx.EVT_LEFT_DCLICK, self.evt_left_dclick)
239 def evt_left_dclick(self, event):
240 (ux, uy) = self.scope.win.GetXY(event)
242 # Re-center on maximum power
243 points = self.scope.win._points
244 if self.scope.win.peak_hold:
245 if self.scope.win.peak_vals is not None:
246 ind = numpy.argmax(self.scope.win.peak_vals)
248 ind = int(points.shape()[0]/2)
250 ind = numpy.argmax(points[:,1])
251 (freq, pwr) = points[ind]
252 target_freq = freq/self.scope.win._scale_factor
254 self.set_freq(target_freq)
256 # Re-center on clicked frequency
257 target_freq = ux/self.scope.win._scale_factor
258 self.set_freq(target_freq)
262 app = stdgui2.stdapp(app_top_block, "USRP2 FFT", nstatus=1)
265 if __name__ == '__main__':