Imported Upstream version 3.2.2
[debian/gnuradio] / grc / 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 # USRP base class with common methods
26 ##################################################
27 class usrp_helper(object):
28         def _make_usrp(self, *args, **kwargs): self._u = self._usrp_args[0](*args, **kwargs)
29         def _get_u(self): return self._u
30         def _get_io_size(self): return self._usrp_args[1]
31         def _set_frequency(self, chan, subdev, frequency, verbose=False):
32                 """
33                 Set the carrier frequency for the given subdevice.
34                 @param chan specifies the DDC/DUC number
35                 @param frequency the carrier frequency in Hz
36                 @param verbose if true, print usrp tuning information
37                 """
38                 r = self._get_u().tune(chan, subdev, frequency)
39                 if not verbose: return
40                 print subdev.side_and_name()
41                 if r:
42                         print "\tr.baseband_frequency =", r.baseband_freq
43                         print "\tr.dxc_frequency =", r.dxc_freq
44                         print "\tr.residual_frequency =", r.residual_freq
45                         print "\tr.inverted =", r.inverted, "\n"
46                 else: print >> sys.stderr, 'Error calling tune on subdevice.'
47         def set_format(self, width, shift): self._get_u().set_format(self._get_u().make_format(width, shift))
48
49 ##################################################
50 # Classes to associate usrp constructor w/ io size
51 ##################################################
52 class usrp_source_c(usrp_helper): _usrp_args = (usrp.source_c, gr.sizeof_gr_complex)
53 class usrp_source_s(usrp_helper): _usrp_args = (usrp.source_s, gr.sizeof_short)
54 class usrp_sink_c(usrp_helper): _usrp_args = (usrp.sink_c, gr.sizeof_gr_complex)
55 class usrp_sink_s(usrp_helper): _usrp_args = (usrp.sink_s, gr.sizeof_short)
56
57 ##################################################
58 # Side spec and antenna spec functions
59 ##################################################
60 def is_flex(rx_ant): return rx_ant.upper() in ('TX/RX', 'RX2')
61 def to_spec(side, rx_ant='RXA'):
62         """
63         Convert the side to a spec number.
64         @param side A or B
65         @param rx_ant antenna type
66         @return the spec (0/1, 0/1/2)
67         """
68         #determine the side spec
69         try: side_spec = {'A': 0, 'B': 1}[side.upper()]
70         except: raise ValueError, 'Side A or B expected.'
71         #determine the subdevice spec
72         if rx_ant.upper() == 'RXB': subdev_spec = 1
73         elif rx_ant.upper() == 'RXAB': subdev_spec = 2
74         else: subdev_spec = 0
75         return (side_spec, subdev_spec)