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