Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-examples / python / usrp / usrp_oscope.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2005,2006 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
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)
10 # any later version.
11
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.
16
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.
21
22
23 # print "Loading revised usrp_oscope with additional options for scopesink..."
24
25 from gnuradio import gr, gru
26 from gnuradio import usrp
27 from gnuradio import eng_notation
28 from gnuradio.eng_option import eng_option
29 from gnuradio.wxgui import stdgui, fftsink, waterfallsink, scopesink, form, slider
30 from optparse import OptionParser
31 import wx
32 import sys
33
34
35 def pick_subdevice(u):
36     """
37     The user didn't specify a subdevice on the command line.
38     If there's a daughterboard on A, select A.
39     If there's a daughterboard on B, select B.
40     Otherwise, select A.
41     """
42     if u.db[0][0].dbid() >= 0:       # dbid is < 0 if there's no d'board or a problem
43         return (0, 0)
44     if u.db[1][0].dbid() >= 0:
45         return (1, 0)
46     return (0, 0)
47
48
49 class app_flow_graph(stdgui.gui_flow_graph):
50     def __init__(self, frame, panel, vbox, argv):
51         stdgui.gui_flow_graph.__init__(self)
52
53         self.frame = frame
54         self.panel = panel
55         
56         parser = OptionParser(option_class=eng_option)
57         parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
58                           help="select USRP Rx side A or B (default=first one with a daughterboard)")
59         parser.add_option("-d", "--decim", type="int", default=16,
60                           help="set fgpa decimation rate to DECIM [default=%default]")
61         parser.add_option("-f", "--freq", type="eng_float", default=None,
62                           help="set frequency to FREQ", metavar="FREQ")
63         parser.add_option("-g", "--gain", type="eng_float", default=None,
64                           help="set gain in dB (default is midpoint)")
65         parser.add_option("-8", "--width-8", action="store_true", default=False,
66                           help="Enable 8-bit samples across USB")
67         parser.add_option("-n", "--frame-decim", type="int", default=1,
68                           help="set oscope frame decimation factor to n [default=1]")
69         parser.add_option("-v", "--v-scale", type="eng_float", default=1000,
70                           help="set oscope initial V/div to SCALE [default=%default]")
71         parser.add_option("-t", "--t-scale", type="eng_float", default=49e-6,
72                           help="set oscope initial s/div to SCALE [default=50us]")
73         (options, args) = parser.parse_args()
74         if len(args) != 0:
75             parser.print_help()
76             sys.exit(1)
77
78         self.show_debug_info = True
79         
80         # build the graph
81
82         self.u = usrp.source_c(decim_rate=options.decim)
83         if options.rx_subdev_spec is None:
84             options.rx_subdev_spec = pick_subdevice(self.u)
85         self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
86
87         if options.width_8:
88             width = 8
89             shift = 8
90             format = self.u.make_format(width, shift)
91             #print "format =", hex(format)
92             r = self.u.set_format(format)
93             #print "set_format =", r
94             
95         # determine the daughterboard subdevice we're using
96         self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
97
98         input_rate = self.u.adc_freq() / self.u.decim_rate()
99
100         self.scope = scopesink.scope_sink_c(self, panel, sample_rate=input_rate,
101                                             frame_decim=options.frame_decim,
102                                             v_scale=options.v_scale,
103                                             t_scale=options.t_scale)
104         self.connect(self.u, self.scope)
105
106         self._build_gui(vbox)
107
108         # set initial values
109
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
114
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
119
120         self.set_gain(options.gain)
121
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)
128                         
129         if not(self.set_freq(options.freq)):
130             self._set_status_msg("Failed to set initial frequency")
131
132
133     def _set_status_msg(self, msg):
134         self.frame.GetStatusBar().SetStatusText(msg, 0)
135
136     def _build_gui(self, vbox):
137
138         def _form_set_freq(kv):
139             return self.set_freq(kv['freq'])
140             
141         vbox.Add(self.scope.win, 10, wx.EXPAND)
142         
143         # add control area at the bottom
144         self.myform = myform = form.form()
145         hbox = wx.BoxSizer(wx.HORIZONTAL)
146         hbox.Add((5,0), 0, 0)
147         myform['freq'] = form.float_field(
148             parent=self.panel, sizer=hbox, label="Center freq", weight=1,
149             callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
150
151         hbox.Add((5,0), 0, 0)
152         g = self.subdev.gain_range()
153         myform['gain'] = form.slider_field(parent=self.panel, sizer=hbox, label="Gain",
154                                            weight=3,
155                                            min=int(g[0]), max=int(g[1]),
156                                            callback=self.set_gain)
157
158         hbox.Add((5,0), 0, 0)
159         vbox.Add(hbox, 0, wx.EXPAND)
160
161         self._build_subpanel(vbox)
162
163     def _build_subpanel(self, vbox_arg):
164         # build a secondary information panel (sometimes hidden)
165
166         # FIXME figure out how to have this be a subpanel that is always
167         # created, but has its visibility controlled by foo.Show(True/False)
168         
169         def _form_set_decim(kv):
170             return self.set_decim(kv['decim'])
171
172         if not(self.show_debug_info):
173             return
174
175         panel = self.panel
176         vbox = vbox_arg
177         myform = self.myform
178
179         #panel = wx.Panel(self.panel, -1)
180         #vbox = wx.BoxSizer(wx.VERTICAL)
181
182         hbox = wx.BoxSizer(wx.HORIZONTAL)
183         hbox.Add((5,0), 0)
184
185         myform['decim'] = form.int_field(
186             parent=panel, sizer=hbox, label="Decim",
187             callback=myform.check_input_and_call(_form_set_decim, self._set_status_msg))
188
189         hbox.Add((5,0), 1)
190         myform['fs@usb'] = form.static_float_field(
191             parent=panel, sizer=hbox, label="Fs@USB")
192
193         hbox.Add((5,0), 1)
194         myform['dbname'] = form.static_text_field(
195             parent=panel, sizer=hbox)
196
197         hbox.Add((5,0), 1)
198         myform['baseband'] = form.static_float_field(
199             parent=panel, sizer=hbox, label="Analog BB")
200
201         hbox.Add((5,0), 1)
202         myform['ddc'] = form.static_float_field(
203             parent=panel, sizer=hbox, label="DDC")
204
205         hbox.Add((5,0), 0)
206         vbox.Add(hbox, 0, wx.EXPAND)
207
208         
209     def set_freq(self, target_freq):
210         """
211         Set the center frequency we're interested in.
212
213         @param target_freq: frequency in Hz
214         @rypte: bool
215
216         Tuning is a two step process.  First we ask the front-end to
217         tune as close to the desired frequency as it can.  Then we use
218         the result of that operation and our target_frequency to
219         determine the value for the digital down converter.
220         """
221         r = usrp.tune(self.u, 0, self.subdev, target_freq)
222         
223         if r:
224             self.myform['freq'].set_value(target_freq)     # update displayed value
225             if self.show_debug_info:
226                 self.myform['baseband'].set_value(r.baseband_freq)
227                 self.myform['ddc'].set_value(r.dxc_freq)
228             return True
229
230         return False
231
232     def set_gain(self, gain):
233         self.myform['gain'].set_value(gain)     # update displayed value
234         self.subdev.set_gain(gain)
235
236     def set_decim(self, decim):
237         ok = self.u.set_decim_rate(decim)
238         if not ok:
239             print "set_decim failed"
240         input_rate = self.u.adc_freq() / self.u.decim_rate()
241         self.scope.set_sample_rate(input_rate)
242         if self.show_debug_info:  # update displayed values
243             self.myform['decim'].set_value(self.u.decim_rate())
244             self.myform['fs@usb'].set_value(self.u.adc_freq() / self.u.decim_rate())
245         return ok
246
247 def main ():
248     app = stdgui.stdapp(app_flow_graph, "USRP O'scope", nstatus=1)
249     app.MainLoop()
250
251 if __name__ == '__main__':
252     main ()