3 Copyright 2009 Free Software Foundation, Inc.
4 This file is part of GNU Radio
6 GNU Radio Companion is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 GNU Radio Companion 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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 from gnuradio import usrp2
30 from gnuradio.grc.gui.Dialogs import TextDisplay
32 from gnuradio.grc.python.Platform import Platform
35 flow_graph = platform.get_new_flow_graph()
36 block = flow_graph.get_new_block('usrp2_probe')
39 usrp_interface_param = block.get_param('interface')
40 usrp_type_param = block.get_param('type')
44 input = param.get_input()
47 class USRP2ProbeWindow(gtk.Window):
49 The main window for USRP Dignostics.
52 def delete_event(self, widget, event, data=None): return False
54 def destroy(self, widget, data=None): gtk.main_quit()
58 USRP2ProbeWindow contructor.
59 Create a new gtk Dialog with a close button, USRP2 input paramaters, and output labels.
61 self.usrp2_macs = list()
62 gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
64 self.connect("delete_event", self.delete_event)
65 self.connect("destroy", self.destroy)
67 self.set_title('USRP2 Probe')
68 #create decorative frame
71 #create vbox for storage
74 vbox.pack_start(get_input(usrp_interface_param), False)
75 vbox.pack_start(get_input(usrp_type_param), False)
76 #make the tree model for holding mac addrs
77 self.treestore = gtk.TreeStore(gobject.TYPE_STRING)
78 self.treeview = gtk.TreeView(self.treestore)
79 self.treeview.set_enable_search(False) #disable pop up search box
80 self.treeview.add_events(gtk.gdk.BUTTON_PRESS_MASK)
81 self.treeview.connect('button_press_event', self._handle_selection)
82 selection = self.treeview.get_selection()
83 selection.set_mode('single')
84 selection.connect('changed', self._handle_selection)
85 renderer = gtk.CellRendererText()
86 column = gtk.TreeViewColumn('Select a USRP2 MAC Address', renderer, text=0)
87 self.treeview.append_column(column)
88 vbox.pack_start(self.treeview, False)
90 self.probe_button = gtk.Button('Probe')
91 self.probe_button.connect('clicked', self._probe_usrp2)
92 vbox.pack_start(self.probe_button, False)
93 #Create a text box for USRP queries
94 self.query_buffer = TextDisplay()
95 self.query_buffer.set_text(block.get_doc())
96 vbox.pack_start(self.query_buffer)
100 def _probe_usrp2(self, widget=None):
101 """Probe the USRP2 device and copy the results into the query text box."""
103 args = ['find_usrps']
104 interface = usrp_interface_param.evaluate()
105 if interface: args.extend(['-e', interface])
106 p = subprocess.Popen(args=args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, universal_newlines=True)
107 msg = p.stdout.read()
109 self.usrp2_macs = sorted(map(lambda l: l.split()[0], filter(lambda l: l.count(':') >= 5, msg.strip().splitlines())))
110 #set the tree store with the mac addrs
111 self.treestore.clear()
112 for usrp2_mac in self.usrp2_macs: self.treestore.append(None, (usrp2_mac,))
113 #set the text with the error message for 0 found, hide the list
114 #when only 1 usrp2, auto handle selection, hide the list
115 #for multiple usrp2, show the list
116 if not self.usrp2_macs:
118 self.query_buffer.set_text(msg)
119 elif len(self.usrp2_macs) == 1:
121 self.query_buffer.set_text('')
122 self._handle_selection()
125 self.query_buffer.set_text('')
127 def _handle_selection(self, *args, **kwargs):
128 """A selection change or click occured."""
130 selection = self.treeview.get_selection()
131 treestore, iter = selection.get_selected()
132 mac_addr = iter and treestore.get_value(iter, 0) or ''
133 if not mac_addr and len(self.usrp2_macs) > 1:
134 return #no empty mac addrs for when multiple found
135 #make the usrp2 object
137 'rx': (usrp2.source_32fc, 'adc_rate'),
138 'tx': (usrp2.sink_32fc, 'dac_rate'),
139 }[usrp_type_param.evaluate()]
140 interface = usrp_interface_param.evaluate()
142 if not interface and not mac_addr: u = make()
143 elif not mac_addr: u = make(interface)
144 else: u = make(interface, mac_addr)
145 msg = ">>> USRP2 Probe\n"
146 msg = "%s\nMAC Addr:\n\t%s\n"%(msg, u.mac_addr())
147 msg = "%s\nName (ID):\n\t%s\n"%(msg, u.daughterboard_id())
148 msg = "%s\nConverter Rate:\n\t%s Hz\n"%(msg, getattr(u, rate_attr)())
149 gain_min, gain_max, gain_step = u.gain_range()
150 msg = "%s\nGain Range (min, max, step size):\n\t%s\n\t%s\n\t%s\n"%(msg, gain_min, gain_max, gain_step)
151 freq_min, freq_max = u.freq_range()
152 msg = "%s\nFreq Range (min, max):\n\t%s Hz\n\t%s Hz\n"%(msg, freq_min, freq_max)
153 self.query_buffer.set_text(msg)
154 except Exception, e: #display the error message
155 self.query_buffer.set_text('>>> Error\n%s'%str(e))
157 if __name__ == '__main__':
158 #setup icon using icon theme
159 try: gtk.window_set_default_icon(gtk.IconTheme().load_icon('gnuradio-grc', 256, 0))