Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-examples / python / digital / usrp_receive_path.py
1 #
2 # Copyright 2009 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 from gnuradio import gr
23 import usrp_options
24 import receive_path
25 from pick_bitrate import pick_rx_bitrate
26 from gnuradio import eng_notation
27
28 def add_freq_option(parser):
29     """
30     Hackery that has the -f / --freq option set both tx_freq and rx_freq
31     """
32     def freq_callback(option, opt_str, value, parser):
33         parser.values.rx_freq = value
34         parser.values.tx_freq = value
35
36     if not parser.has_option('--freq'):
37         parser.add_option('-f', '--freq', type="eng_float",
38                           action="callback", callback=freq_callback,
39                           help="set Tx and/or Rx frequency to FREQ [default=%default]",
40                           metavar="FREQ")
41
42 def add_options(parser, expert):
43     add_freq_option(parser)
44     usrp_options.add_rx_options(parser)
45     receive_path.receive_path.add_options(parser, expert)
46     expert.add_option("", "--rx-freq", type="eng_float", default=None,
47                           help="set Rx frequency to FREQ [default=%default]", metavar="FREQ")
48     parser.add_option("-v", "--verbose", action="store_true", default=False)
49
50 class usrp_receive_path(gr.hier_block2):
51
52     def __init__(self, demod_class, rx_callback, options):
53         '''
54         See below for what options should hold
55         '''
56         gr.hier_block2.__init__(self, "usrp_receive_path",
57                 gr.io_signature(0, 0, 0),                    # Input signature
58                 gr.io_signature(0, 0, 0)) # Output signature
59         if options.rx_freq is None:
60             sys.stderr.write("-f FREQ or --freq FREQ or --rx-freq FREQ must be specified\n")
61             raise SystemExit
62         rx_path = receive_path.receive_path(demod_class, rx_callback, options)
63         for attr in dir(rx_path): #forward the methods
64             if not attr.startswith('_') and not hasattr(self, attr):
65                 setattr(self, attr, getattr(rx_path, attr))
66         #setup usrp
67         self._demod_class = demod_class
68         self._setup_usrp_source(options)
69         #connect
70         self.connect(self.u, rx_path)
71
72     def _setup_usrp_source(self, options):
73         self.u = usrp_options.create_usrp_source(options)
74         adc_rate = self.u.adc_rate()
75         if options.verbose:
76             print 'USRP Source:', self.u
77         (self._bitrate, self._samples_per_symbol, self._decim) = \
78                         pick_rx_bitrate(options.bitrate, self._demod_class.bits_per_symbol(), \
79                                         options.samples_per_symbol, options.decim, adc_rate,  \
80                                         self.u.get_decim_rates())
81
82         self.u.set_decim(self._decim)
83
84         if not self.u.set_center_freq(options.rx_freq):
85             print "Failed to set Rx frequency to %s" % (eng_notation.num_to_str(options.rx_freq))
86             raise ValueError, eng_notation.num_to_str(options.rx_freq)