new and improved simple/dual usrp blocks and wrapper classes
[debian/gnuradio] / grc / src / grc_gnuradio / usrp / common.py
1 # Copyright 2009 Free Software Foundation, Inc.
2 #
3 # This file is part of GNU Radio
4 #
5 # GNU Radio is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3, or (at your option)
8 # any later version.
9 #
10 # GNU Radio is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with GNU Radio; see the file COPYING.  If not, write to
17 # the Free Software Foundation, Inc., 51 Franklin Street,
18 # Boston, MA 02110-1301, USA.
19 #
20
21 import sys
22 from gnuradio import usrp, gr
23
24 ##################################################
25 # Set frequency function w/ verbose option
26 ##################################################
27 def set_frequency(u, which, subdev, frequency, verbose=False):
28         """
29         Set the carrier frequency for the given subdevice.
30         @param u the usrp source/sink
31         @param which specifies the DDC/DUC number
32         @param frequency the carrier frequency in Hz
33         @param verbose if true, print usrp tuning information
34         """
35         r = u.tune(which, subdev, frequency)
36         if not verbose: return
37         print subdev.side_and_name()
38         if r:
39                 print "\tr.baseband_frequency =", r.baseband_freq
40                 print "\tr.dxc_frequency =", r.dxc_freq
41                 print "\tr.residual_frequency =", r.residual_freq
42                 print "\tr.inverted =", r.inverted, "\n"
43         else: print >> sys.stderr, 'Error calling tune on subdevice.'
44
45 ##################################################
46 # Classes to associate usrp constructor w/ io size
47 ##################################################
48 class usrp_helper(object):
49         def _get_usrp_constructor(self): return self._usrp_args[0]
50         def _get_io_size(self): return self._usrp_args[1]
51 class usrp_source_c(usrp_helper): _usrp_args = (usrp.source_c, gr.sizeof_gr_complex)
52 class usrp_source_s(usrp_helper): _usrp_args = (usrp.source_s, gr.sizeof_short)
53 class usrp_sink_c(usrp_helper): _usrp_args = (usrp.sink_c, gr.sizeof_gr_complex)
54 class usrp_sink_s(usrp_helper): _usrp_args = (usrp.sink_s, gr.sizeof_short)
55
56 ##################################################
57 # RX antenna definitions and helpers
58 ##################################################
59 def is_flex(rx_ant): return rx_ant.upper() in ('TX/RX', 'RX2')
60 def to_spec(side, rx_ant='RXA'):
61         """
62         Convert the side to a spec number.
63         @param side A or B
64         @param rx_ant antenna type
65         @return the spec (0/1, 0/1)
66         """
67         try: return (
68                 {'A': 0, 'B': 1}[side.upper()], #side spec is 0 for A, 1 for B
69                 rx_ant.upper() == 'RXB' and 1 or 0, #subdev spec is 1 for RXB
70         )
71         except: raise ValueError, 'Side A or B expected.'