3 # Copyright 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 import usrp
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from gnuradio.wxgui import stdgui, fftsink, waterfallsink, scopesink, form, slider
28 from optparse import OptionParser
33 def pick_subdevice(u):
35 The user didn't specify a subdevice on the command line.
36 If there's a daughterboard on A, select A.
37 If there's a daughterboard on B, select B.
40 if u.db[0][0].dbid() >= 0: # dbid is < 0 if there's no d'board or a problem
42 if u.db[1][0].dbid() >= 0:
47 class app_flow_graph(stdgui.gui_flow_graph):
48 def __init__(self, frame, panel, vbox, argv):
49 stdgui.gui_flow_graph.__init__(self)
54 parser = OptionParser(option_class=eng_option)
55 parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
56 help="select USRP Rx side A or B (default=first one with a daughterboard)")
57 parser.add_option("-d", "--decim", type="int", default=16,
58 help="set fgpa decimation rate to DECIM [default=%default]")
59 parser.add_option("-f", "--freq", type="eng_float", default=None,
60 help="set frequency to FREQ", metavar="FREQ")
61 parser.add_option("-g", "--gain", type="eng_float", default=None,
62 help="set gain in dB (default is midpoint)")
63 parser.add_option("-W", "--waterfall", action="store_true", default=False,
64 help="Enable waterfall display")
65 parser.add_option("-8", "--width-8", action="store_true", default=False,
66 help="Enable 8-bit samples across USB")
67 parser.add_option("-S", "--oscilloscope", action="store_true", default=False,
68 help="Enable oscilloscope display")
69 (options, args) = parser.parse_args()
74 self.show_debug_info = True
78 self.u = usrp.source_c(decim_rate=options.decim)
79 if options.rx_subdev_spec is None:
80 options.rx_subdev_spec = pick_subdevice(self.u)
81 self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
86 format = self.u.make_format(width, shift)
87 print "format =", hex(format)
88 r = self.u.set_format(format)
89 print "set_format =", r
91 # determine the daughterboard subdevice we're using
92 self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
94 input_rate = self.u.adc_freq() / self.u.decim_rate()
98 waterfallsink.waterfall_sink_c (self, panel, fft_size=1024, sample_rate=input_rate)
99 elif options.oscilloscope:
100 self.scope = scopesink.scope_sink_c(self, panel, sample_rate=input_rate)
102 self.scope = fftsink.fft_sink_c (self, panel, fft_size=1024, sample_rate=input_rate)
104 self.connect(self.u, self.scope)
106 self._build_gui(vbox)
110 if options.gain is None:
111 # if no gain was specified, use the mid-point in dB
112 g = self.subdev.gain_range()
113 options.gain = float(g[0]+g[1])/2
115 if options.freq is None:
116 # if no freq was specified, use the mid-point
117 r = self.subdev.freq_range()
118 options.freq = float(r[0]+r[1])/2
120 self.set_gain(options.gain)
122 if self.show_debug_info:
123 self.myform['decim'].set_value(self.u.decim_rate())
124 self.myform['fs@usb'].set_value(self.u.adc_freq() / self.u.decim_rate())
125 self.myform['dbname'].set_value(self.subdev.name())
126 self.myform['baseband'].set_value(0)
127 self.myform['ddc'].set_value(0)
129 if not(self.set_freq(options.freq)):
130 self._set_status_msg("Failed to set initial frequency")
132 def _set_status_msg(self, msg):
133 self.frame.GetStatusBar().SetStatusText(msg, 0)
135 def _build_gui(self, vbox):
137 def _form_set_freq(kv):
138 return self.set_freq(kv['freq'])
140 vbox.Add(self.scope.win, 10, wx.EXPAND)
142 # add control area at the bottom
143 self.myform = myform = form.form()
144 hbox = wx.BoxSizer(wx.HORIZONTAL)
145 hbox.Add((5,0), 0, 0)
146 myform['freq'] = form.float_field(
147 parent=self.panel, sizer=hbox, label="Center freq", weight=1,
148 callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
150 hbox.Add((5,0), 0, 0)
151 g = self.subdev.gain_range()
152 myform['gain'] = form.slider_field(parent=self.panel, sizer=hbox, label="Gain",
154 min=int(g[0]), max=int(g[1]),
155 callback=self.set_gain)
157 hbox.Add((5,0), 0, 0)
158 vbox.Add(hbox, 0, wx.EXPAND)
160 self._build_subpanel(vbox)
162 def _build_subpanel(self, vbox_arg):
163 # build a secondary information panel (sometimes hidden)
165 # FIXME figure out how to have this be a subpanel that is always
166 # created, but has its visibility controlled by foo.Show(True/False)
168 def _form_set_decim(kv):
169 return self.set_decim(kv['decim'])
171 if not(self.show_debug_info):
178 #panel = wx.Panel(self.panel, -1)
179 #vbox = wx.BoxSizer(wx.VERTICAL)
181 hbox = wx.BoxSizer(wx.HORIZONTAL)
184 myform['decim'] = form.int_field(
185 parent=panel, sizer=hbox, label="Decim",
186 callback=myform.check_input_and_call(_form_set_decim, self._set_status_msg))
189 myform['fs@usb'] = form.static_float_field(
190 parent=panel, sizer=hbox, label="Fs@USB")
193 myform['dbname'] = form.static_text_field(
194 parent=panel, sizer=hbox)
197 myform['baseband'] = form.static_float_field(
198 parent=panel, sizer=hbox, label="Analog BB")
201 myform['ddc'] = form.static_float_field(
202 parent=panel, sizer=hbox, label="DDC")
205 vbox.Add(hbox, 0, wx.EXPAND)
208 def set_freq(self, target_freq):
210 Set the center frequency we're interested in.
212 @param target_freq: frequency in Hz
215 Tuning is a two step process. First we ask the front-end to
216 tune as close to the desired frequency as it can. Then we use
217 the result of that operation and our target_frequency to
218 determine the value for the digital down converter.
220 r = self.u.tune(0, self.subdev, target_freq)
223 self.myform['freq'].set_value(target_freq) # update displayed value
224 if self.show_debug_info:
225 self.myform['baseband'].set_value(r.baseband_freq)
226 self.myform['ddc'].set_value(r.dxc_freq)
231 def set_gain(self, gain):
232 self.myform['gain'].set_value(gain) # update displayed value
233 self.subdev.set_gain(gain)
235 def set_decim(self, decim):
236 ok = self.u.set_decim_rate(decim)
238 print "set_decim failed"
239 input_rate = self.u.adc_freq() / self.u.decim_rate()
240 self.scope.set_sample_rate(input_rate)
241 if self.show_debug_info: # update displayed values
242 self.myform['decim'].set_value(self.u.decim_rate())
243 self.myform['fs@usb'].set_value(self.u.adc_freq() / self.u.decim_rate())
247 app = stdgui.stdapp(app_flow_graph, "USRP FFT", nstatus=1)
250 if __name__ == '__main__':