Imported Upstream version 3.2.2
[debian/gnuradio] / gr-qtgui / src / python / usrp2_display.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2009 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 from gnuradio import gr
24 from gnuradio import usrp2
25 from gnuradio import eng_notation
26 from gnuradio.eng_option import eng_option
27 from gnuradio.qtgui import qtgui
28 from optparse import OptionParser
29 import sys
30
31 try:
32     from gnuradio.qtgui import qtgui
33     from PyQt4 import QtGui, QtCore
34     import sip
35 except ImportError:
36     print "Please install gr-qtgui."
37     sys.exit(1)
38
39 try:
40     from usrp_display_qtgui import Ui_MainWindow
41 except ImportError:
42     print "Error: could not find usrp_display_qtgui.py:"
43     print "\t\"pyuic4 usrp_display_qtgui.ui -o usrp_display_qtgui.py\""
44     sys.exit(1)
45
46
47 # ////////////////////////////////////////////////////////////////////
48 #        Define the QT Interface and Control Dialog
49 # ////////////////////////////////////////////////////////////////////
50
51
52 class main_window(QtGui.QMainWindow):
53     def __init__(self, snk, fg, parent=None):
54
55         QtGui.QWidget.__init__(self, parent)
56         self.gui = Ui_MainWindow()
57         self.gui.setupUi(self)
58
59         self.fg = fg
60
61         # Add the qtsnk widgets to the layout box
62         self.gui.sinkLayout.addWidget(snk)
63
64         # Connect up some signals
65         self.connect(self.gui.pauseButton, QtCore.SIGNAL("clicked()"),
66                      self.pauseFg)
67         self.connect(self.gui.frequencyEdit, QtCore.SIGNAL("editingFinished()"),
68                      self.frequencyEditText)
69         self.connect(self.gui.gainEdit, QtCore.SIGNAL("editingFinished()"),
70                      self.gainEditText)
71         self.connect(self.gui.bandwidthEdit, QtCore.SIGNAL("editingFinished()"),
72                      self.bandwidthEditText)
73         self.connect(self.gui.amplifierEdit, QtCore.SIGNAL("editingFinished()"),
74                      self.amplifierEditText)
75
76         self.connect(self.gui.actionSaveData, QtCore.SIGNAL("activated()"),
77                      self.saveData)
78         self.gui.actionSaveData.setShortcut(QtGui.QKeySequence.Save)
79
80     def pauseFg(self):
81         if(self.gui.pauseButton.text() == "Pause"):
82             self.fg.stop()
83             self.fg.wait()
84             self.gui.pauseButton.setText("Unpause")
85         else:
86             self.fg.start()
87             self.gui.pauseButton.setText("Pause")
88       
89
90     # Functions to set the values in the GUI
91     def set_frequency(self, freq):
92         self.freq = freq
93         sfreq = eng_notation.num_to_str(self.freq)
94         self.gui.frequencyEdit.setText(QtCore.QString("%1").arg(sfreq))
95         
96     def set_gain(self, gain):
97         self.gain = gain
98         self.gui.gainEdit.setText(QtCore.QString("%1").arg(self.gain))
99
100     def set_bandwidth(self, bw):
101         self.bw = bw
102         sbw = eng_notation.num_to_str(self.bw)
103         self.gui.bandwidthEdit.setText(QtCore.QString("%1").arg(sbw))
104
105     def set_amplifier(self, amp):
106         self.amp = amp
107         self.gui.amplifierEdit.setText(QtCore.QString("%1").arg(self.amp))
108
109
110     # Functions called when signals are triggered in the GUI
111     def frequencyEditText(self):
112         try:
113             freq = eng_notation.str_to_num(self.gui.frequencyEdit.text().toAscii()) 
114             self.fg.set_frequency(freq)
115             self.freq = freq
116         except RuntimeError:
117             pass
118
119     def gainEditText(self):
120         try:
121             gain = float(self.gui.gainEdit.text())
122             self.fg.set_gain(gain)
123             self.gain = gain
124         except ValueError:
125             pass
126                 
127     def bandwidthEditText(self):
128         try:
129             bw = eng_notation.str_to_num(self.gui.bandwidthEdit.text().toAscii())
130             self.fg.set_bandwidth(bw)
131             self.bw = bw
132         except ValueError:
133             pass
134         
135     def amplifierEditText(self):
136         try:
137             amp = float(self.gui.amplifierEdit.text())
138             self.fg.set_amplifier_gain(amp)
139             self.amp = amp
140         except ValueError:
141             pass
142
143     def saveData(self):
144         fileName = QtGui.QFileDialog.getSaveFileName(self, "Save data to file", ".");
145         if(len(fileName)):
146             self.fg.save_to_file(str(fileName))
147
148
149         
150 class my_top_block(gr.top_block):
151     def __init__(self):
152         gr.top_block.__init__(self)
153
154         parser = OptionParser(option_class=eng_option)
155         parser.add_option("-e", "--interface", type="string", default="eth0",
156                           help="select Ethernet interface, default is eth0")
157         parser.add_option("-m", "--mac-addr", type="string", default="",
158                           help="select USRP by MAC address, default is auto-select")
159         parser.add_option("-W", "--bw", type="float", default=1e6,
160                           help="set bandwidth of receiver [default=%default]")
161         parser.add_option("-f", "--freq", type="eng_float", default=None,
162                           help="set frequency to FREQ", metavar="FREQ")
163         parser.add_option("-g", "--gain", type="eng_float", default=None,
164                           help="set gain in dB (default is midpoint)")
165         parser.add_option("--fft-size", type="int", default=2048,
166                           help="Set number of FFT bins [default=%default]")
167         (options, args) = parser.parse_args()
168
169         if len(args) != 0:
170             parser.print_help()
171             sys.exit(1)
172         self.options = options
173         self.show_debug_info = True
174         
175         self.qapp = QtGui.QApplication(sys.argv)
176
177         self.u = usrp2.source_32fc(options.interface, options.mac_addr)
178         self._adc_rate = self.u.adc_rate()
179         self.set_bandwidth(options.bw)
180
181         if options.gain is None:
182             # if no gain was specified, use the mid-point in dB
183             g = self.u.gain_range()
184             options.gain = float(g[0]+g[1])/2
185         self.set_gain(options.gain)
186
187         if options.freq is None:
188             # if no frequency was specified, use the mid-point of the subdev
189             f = self.u.freq_range()
190             options.freq = float(f[0]+f[1])/2
191         self.set_frequency(options.freq)
192
193         self._fftsize = options.fft_size
194
195         self.snk = qtgui.sink_c(options.fft_size, gr.firdes.WIN_BLACKMAN_hARRIS,
196                                 self._freq, self._bandwidth,
197                                 "USRP2 Display",
198                                 True, True, False, True, False)
199
200         # Set up internal amplifier
201         self.amp = gr.multiply_const_cc(0.0)
202         self.set_amplifier_gain(100)
203
204         self.connect(self.u, self.amp, self.snk)
205
206         if self.show_debug_info:
207             print "Decimation rate: ", self._decim
208             print "Bandwidth: ", self._bandwidth
209             print "D'board: ", self.u.daughterboard_id()
210
211         # Get the reference pointer to the SpectrumDisplayForm QWidget
212         # Wrap the pointer as a PyQt SIP object
213         #     This can now be manipulated as a PyQt4.QtGui.QWidget
214         self.pysink = sip.wrapinstance(self.snk.pyqwidget(), QtGui.QWidget)
215
216         self.main_win = main_window(self.pysink, self)
217
218         self.main_win.set_frequency(self._freq)
219         self.main_win.set_gain(self._gain)
220         self.main_win.set_bandwidth(self._bandwidth)
221         self.main_win.set_amplifier(self._amp_value)
222
223         self.main_win.show()
224
225
226     def save_to_file(self, name):
227         # Pause the flow graph
228         self.stop()
229         self.wait()
230
231         # Add file sink to save data
232         self.file_sink = gr.file_sink(gr.sizeof_gr_complex, name)
233         self.connect(self.amp, self.file_sink)
234
235         # Restart flow graph
236         self.start()
237
238     def set_gain(self, gain):
239         self._gain = gain
240         self.u.set_gain(self._gain)
241
242     def set_frequency(self, freq):
243         self._freq = freq
244         r = self.u.set_center_freq(freq)
245
246         try:
247             self.snk.set_frequency_range(self._freq, self._bandwidth)
248         except:
249             pass
250
251     def set_bandwidth(self, bw):
252         self._bandwidth = bw
253         self._decim = int(self._adc_rate / self._bandwidth)
254         self.u.set_decim(self._decim)
255
256         try:
257             self.snk.set_frequency_range(self._freq, self._bandwidth)
258         except:
259             pass
260
261     def set_amplifier_gain(self, amp):
262             self._amp_value = amp
263             self.amp.set_k(self._amp_value)
264
265 def main ():
266     tb = my_top_block()
267     tb.start()
268     tb.snk.exec_();
269
270 if __name__ == '__main__':
271     try:
272         main ()
273     except KeyboardInterrupt:
274         pass
275