switch source package format to 3.0 quilt
[debian/gnuradio] / gnuradio-examples / python / usrp / usrp_wxapt_rcv.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006,2007 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 from gnuradio import gr, gru, eng_notation, optfir
24 from gnuradio import audio
25 from gnuradio import usrp
26 from gnuradio import blks2
27 from gnuradio.eng_option import eng_option
28 from gnuradio.wxgui import slider, powermate
29 from gnuradio.wxgui import stdgui2, fftsink2, form
30 from optparse import OptionParser
31 from usrpm import usrp_dbid
32 import sys
33 import math
34 import wx
35
36 def pick_subdevice(u):
37     """
38     The user didn't specify a subdevice on the command line.
39     Try for one of these, in order: TV_RX, BASIC_RX, whatever is on side A.
40
41     @return a subdev_spec
42     """
43     return usrp.pick_subdev(u, (usrp_dbid.TV_RX,
44                                 usrp_dbid.TV_RX_REV_2,
45                                 usrp_dbid.TV_RX_REV_3,
46                                 usrp_dbid.TV_RX_MIMO,
47                                 usrp_dbid.TV_RX_REV_2_MIMO,
48                                 usrp_dbid.TV_RX_REV_3_MIMO,
49                                 usrp_dbid.BASIC_RX))
50
51
52 class wxapt_rx_block (stdgui2.std_top_block):
53     def __init__(self,frame,panel,vbox,argv):
54         stdgui2.std_top_block.__init__ (self,frame,panel,vbox,argv)
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=A)")
59         parser.add_option("-f", "--freq", type="eng_float", default=137.5e6,
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("-V", "--volume", type="eng_float", default=None,
64                           help="set volume (default is midpoint)")
65         parser.add_option("-O", "--audio-output", type="string", default="",
66                           help="pcm device name.  E.g., hw:0,0 or surround51 or /dev/dsp")
67
68         (options, args) = parser.parse_args()
69         if len(args) != 0:
70             parser.print_help()
71             sys.exit(1)
72         
73         self.frame = frame
74         self.panel = panel
75         
76         self.vol = 0
77         self.state = "FREQ"
78         self.freq = 0
79
80         # build graph
81         
82         self.u = usrp.source_c()                    # usrp is data source
83
84         adc_rate = self.u.adc_rate()                # 64 MS/s
85         usrp_decim = 200
86         self.u.set_decim_rate(usrp_decim)
87         usrp_rate = adc_rate / usrp_decim           # 320 kS/s
88         chanfilt_decim = 4
89         demod_rate = usrp_rate / chanfilt_decim
90         audio_decimation = 10
91         audio_rate = demod_rate / audio_decimation  # 32 kHz
92
93         if options.rx_subdev_spec is None:
94             options.rx_subdev_spec = pick_subdevice(self.u)
95
96         self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
97         self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
98         print "Using RX d'board %s" % (self.subdev.side_and_name(),)
99
100
101         chan_filt_coeffs = optfir.low_pass (1,           # gain
102                                             usrp_rate,   # sampling rate
103                                             40e3,        # passband cutoff
104                                             60e3,       # stopband cutoff
105                                             0.1,         # passband ripple
106                                             60)          # stopband attenuation
107         #print len(chan_filt_coeffs)
108         chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
109
110         self.guts = blks2.wfm_rcv (demod_rate, audio_decimation)
111
112         self.volume_control = gr.multiply_const_ff(self.vol)
113
114         # sound card as final sink
115         audio_sink = audio.sink (int (audio_rate), options.audio_output)
116         
117         # now wire it all together
118         self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
119
120         self._build_gui(vbox, usrp_rate, demod_rate, audio_rate)
121
122         if options.gain is None:
123             # if no gain was specified, use the mid-point in dB
124             g = self.subdev.gain_range()
125             options.gain = float(g[0]+g[1])/2
126
127         if options.volume is None:
128             g = self.volume_range()
129             options.volume = float(g[0]+g[1])/2
130             
131         if abs(options.freq) < 1e6:
132             options.freq *= 1e6
133
134         # set initial values
135
136         self.set_gain(options.gain)
137         self.set_vol(options.volume)
138         if not(self.set_freq(options.freq)):
139             self._set_status_msg("Failed to set initial frequency")
140
141
142     def _set_status_msg(self, msg, which=0):
143         self.frame.GetStatusBar().SetStatusText(msg, which)
144
145
146     def _build_gui(self, vbox, usrp_rate, demod_rate, audio_rate):
147
148         def _form_set_freq(kv):
149             return self.set_freq(kv['freq'])
150
151
152         if 1:
153             self.src_fft = fftsink2.fft_sink_c (self.panel, title="Data from USRP",
154                                                fft_size=512, sample_rate=usrp_rate,
155                                                ref_scale=32768.0, ref_level=0, y_divs=12)
156             self.connect (self.u, self.src_fft)
157             vbox.Add (self.src_fft.win, 4, wx.EXPAND)
158
159         if 1:
160             post_deemph_fft = fftsink2.fft_sink_f (self.panel, title="Post Deemph",
161                                                   fft_size=512, sample_rate=demod_rate,
162                                                   y_per_div=10, ref_level=-20)
163             self.connect (self.guts.deemph, post_deemph_fft)
164             vbox.Add (post_deemph_fft.win, 4, wx.EXPAND)
165
166         if 1:
167             post_filt_fft = fftsink2.fft_sink_f (self.panel, title="Post Filter", 
168                                                 fft_size=512, sample_rate=audio_rate,
169                                                 y_per_div=10, ref_level=0)
170             self.connect (self.guts.audio_filter, post_filt_fft)
171             vbox.Add (post_filt_fft.win, 4, wx.EXPAND)
172
173         
174         # control area form at bottom
175         self.myform = myform = form.form()
176
177         hbox = wx.BoxSizer(wx.HORIZONTAL)
178         hbox.Add((5,0), 0)
179         myform['freq'] = form.float_field(
180             parent=self.panel, sizer=hbox, label="Freq", weight=1,
181             callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
182
183         hbox.Add((5,0), 0)
184         myform['freq_slider'] = \
185             form.quantized_slider_field(parent=self.panel, sizer=hbox, weight=3,
186                                         range=(137.0e6, 138.0e6, 0.0005e6),
187                                         callback=self.set_freq)
188         hbox.Add((5,0), 0)
189         vbox.Add(hbox, 0, wx.EXPAND)
190
191         hbox = wx.BoxSizer(wx.HORIZONTAL)
192         hbox.Add((5,0), 0)
193
194         myform['volume'] = \
195             form.quantized_slider_field(parent=self.panel, sizer=hbox, label="Volume",
196                                         weight=3, range=self.volume_range(),
197                                         callback=self.set_vol)
198         hbox.Add((5,0), 1)
199
200         myform['gain'] = \
201             form.quantized_slider_field(parent=self.panel, sizer=hbox, label="Gain",
202                                         weight=3, range=self.subdev.gain_range(),
203                                         callback=self.set_gain)
204         hbox.Add((5,0), 0)
205         vbox.Add(hbox, 0, wx.EXPAND)
206
207         try:
208             self.knob = powermate.powermate(self.frame)
209             self.rot = 0
210             powermate.EVT_POWERMATE_ROTATE (self.frame, self.on_rotate)
211             powermate.EVT_POWERMATE_BUTTON (self.frame, self.on_button)
212         except:
213             print "FYI: No Powermate or Contour Knob found"
214
215
216     def on_rotate (self, event):
217         self.rot += event.delta
218         if (self.state == "FREQ"):
219             if self.rot >= 3:
220                 self.set_freq(self.freq + .1e6)
221                 self.rot -= 3
222             elif self.rot <=-3:
223                 self.set_freq(self.freq - .1e6)
224                 self.rot += 3
225         else:
226             step = self.volume_range()[2]
227             if self.rot >= 3:
228                 self.set_vol(self.vol + step)
229                 self.rot -= 3
230             elif self.rot <=-3:
231                 self.set_vol(self.vol - step)
232                 self.rot += 3
233             
234     def on_button (self, event):
235         if event.value == 0:        # button up
236             return
237         self.rot = 0
238         if self.state == "FREQ":
239             self.state = "VOL"
240         else:
241             self.state = "FREQ"
242         self.update_status_bar ()
243         
244
245     def set_vol (self, vol):
246         g = self.volume_range()
247         self.vol = max(g[0], min(g[1], vol))
248         self.volume_control.set_k(10**(self.vol/10))
249         self.myform['volume'].set_value(self.vol)
250         self.update_status_bar ()
251                                         
252     def set_freq(self, target_freq):
253         """
254         Set the center frequency we're interested in.
255
256         @param target_freq: frequency in Hz
257         @rypte: bool
258
259         Tuning is a two step process.  First we ask the front-end to
260         tune as close to the desired frequency as it can.  Then we use
261         the result of that operation and our target_frequency to
262         determine the value for the digital down converter.
263         """
264         r = usrp.tune(self.u, 0, self.subdev, target_freq)
265         
266         if r:
267             self.freq = target_freq
268             self.myform['freq'].set_value(target_freq)         # update displayed value
269             self.myform['freq_slider'].set_value(target_freq)  # update displayed value
270             self.update_status_bar()
271             self._set_status_msg("OK", 0)
272             return True
273
274         self._set_status_msg("Failed", 0)
275         return False
276
277     def set_gain(self, gain):
278         self.myform['gain'].set_value(gain)     # update displayed value
279         self.subdev.set_gain(gain)
280
281     def update_status_bar (self):
282         msg = "Volume:%r  Setting:%s" % (self.vol, self.state)
283         self._set_status_msg(msg, 1)
284         self.src_fft.set_baseband_freq(self.freq)
285
286     def volume_range(self):
287         return (-20.0, 0.0, 0.5)
288         
289
290 if __name__ == '__main__':
291     app = stdgui2.stdapp (wxapt_rx_block, "USRP WXAPT RX")
292     app.MainLoop ()