probe for the usrp2
[debian/gnuradio] / grc / scripts / usrp2_probe
1 #!/usr/bin/env python
2 """
3 Copyright 2009 Free Software Foundation, Inc.
4 This file is part of GNU Radio
5
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.
10
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.
15
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
19 """
20
21 from gnuradio import usrp2
22 import subprocess
23 import os
24
25 import pygtk
26 pygtk.require('2.0')
27 import gtk
28 import gobject
29
30 from gnuradio.grc.gui.Dialogs import TextDisplay
31
32 from gnuradio.grc.platforms.python.Platform import Platform
33 platform = Platform(block_paths_internal_only=['usrp2_probe.xml'])
34
35 from gnuradio.grc.platforms.gui.Platform import Platform
36 platform = Platform(platform)
37
38 flow_graph = platform.get_new_flow_graph()
39 block = flow_graph.get_new_block('usrp2_probe')
40
41 ##all params
42 usrp_interface_param = block.get_param('interface')
43 usrp_type_param = block.get_param('type')
44
45 class USRP2ProbeWindow(gtk.Window):
46         """
47         The main window for USRP Dignostics.
48         """
49
50         def delete_event(self, widget, event, data=None): return False
51
52         def destroy(self, widget, data=None): gtk.main_quit()
53
54         def __init__(self):
55                 """
56                 USRP2ProbeWindow contructor.
57                 Create a new gtk Dialog with a close button, USRP2 input paramaters, and output labels.
58                 """
59                 self.usrp2_macs = list()
60                 gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
61                 #quit signals
62                 self.connect("delete_event", self.delete_event)
63                 self.connect("destroy", self.destroy)
64                 #set the title
65                 self.set_title('USRP2 Probe')
66                 #create decorative frame
67                 frame = gtk.Frame()
68                 self.add(frame)
69                 #create vbox for storage
70                 vbox = gtk.VBox()
71                 frame.add(vbox)
72                 vbox.pack_start(usrp_interface_param.get_input_object(), False)
73                 vbox.pack_start(usrp_type_param.get_input_object(), False)
74                 #make the tree model for holding mac addrs
75                 self.treestore = gtk.TreeStore(gobject.TYPE_STRING)
76                 self.treeview = gtk.TreeView(self.treestore)
77                 self.treeview.set_enable_search(False) #disable pop up search box
78                 self.treeview.add_events(gtk.gdk.BUTTON_PRESS_MASK)
79                 self.treeview.connect('button_press_event', self._handle_selection)
80                 selection = self.treeview.get_selection()
81                 selection.set_mode('single')
82                 selection.connect('changed', self._handle_selection)
83                 renderer = gtk.CellRendererText()
84                 column = gtk.TreeViewColumn('Select a USRP2 MAC Address', renderer, text=0)
85                 self.treeview.append_column(column)
86                 vbox.pack_start(self.treeview, False)
87                 #create probe button
88                 self.probe_button = gtk.Button('Probe')
89                 self.probe_button.connect('clicked', self._probe_usrp2)
90                 vbox.pack_start(self.probe_button, False)
91                 #Create a text box for USRP queries
92                 self.query_buffer = TextDisplay()
93                 self.query_buffer.set_text(block.get_doc())
94                 vbox.pack_start(self.query_buffer)
95                 self.show_all()
96                 self.treeview.hide()
97
98         def _probe_usrp2(self, widget=None):
99                 """Probe the USRP2 device and copy the results into the query text box."""
100                 #call find usrps
101                 args = ['find_usrps']
102                 interface = usrp_interface_param.evaluate()
103                 if interface: args.extend(['-e', interface])
104                 p = subprocess.Popen(args=args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, universal_newlines=True)
105                 msg = p.stdout.read()
106                 #extract mac addrs
107                 self.usrp2_macs = map(lambda l: l.split()[0], filter(lambda l: l.count(':') >= 5, msg.strip().splitlines()))
108                 #set the tree store with the mac addrs
109                 self.treestore.clear()
110                 for usrp2_mac in self.usrp2_macs:
111                         self.treestore.append(None, (usrp2_mac,))
112                 #set the text with the error message for 0 found, hide the list
113                 #when only 1 usrp2, auto handle selection, hide the list
114                 #for multiple usrp2, show the list
115                 if not self.usrp2_macs:
116                         self.treeview.hide()
117                         self.query_buffer.set_text(msg)
118                 elif len(self.usrp2_macs) == 1:
119                         self.treeview.hide()
120                         self.query_buffer.set_text('')
121                         self._handle_selection()
122                 else:
123                         self.treeview.show()
124                         self.query_buffer.set_text('')
125
126         def _handle_selection(self, *args, **kwargs):
127                 """A selection change or click occured."""
128                 #get the mac addr
129                 selection = self.treeview.get_selection()
130                 treestore, iter = selection.get_selected()
131                 mac_addr = iter and treestore.get_value(iter, 0) or ''
132                 if not mac_addr and len(self.usrp2_macs) > 1:
133                         return #no empty mac addrs for when multiple found
134                 #make the usrp2 object
135                 make, rate_attr = {
136                         'rx': (usrp2.source_32fc, 'adc_rate'),
137                         'tx': (usrp2.sink_32fc, 'dac_rate'),
138                 }[usrp_type_param.evaluate()]
139                 try:
140                         u = make(usrp_interface_param.evaluate(), mac_addr)
141                         msg = ">>> USRP2 Probe\n"
142                         msg = "%s\nMAC Addr:\n\t%s\n"%(msg, u.mac_addr())
143                         msg = "%s\nName (ID):\n\t%s\n"%(msg, u.daughterboard_id())
144                         msg = "%s\nConverter Rate:\n\t%s Hz\n"%(msg, getattr(u, rate_attr)())
145                         gain_min, gain_max, gain_step = u.gain_range()
146                         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)
147                         freq_min, freq_max = u.freq_range()
148                         msg = "%s\nFreq Range (min, max):\n\t%s Hz\n\t%s Hz\n"%(msg, freq_min, freq_max)
149                         self.query_buffer.set_text(msg)
150                 except Exception, e: #display the error message
151                         self.query_buffer.set_text('>>> Error\n%s'%str(e))
152
153 if __name__ == '__main__':
154         #setup icon using icon theme
155         try: gtk.window_set_default_icon(gtk.IconTheme().load_icon('gnuradio-grc', 256, 0))
156         except: pass
157         #enter the mainloop
158         USRP2ProbeWindow()
159         gtk.main()