3 # Copyright 2009 Free Software Foundation, Inc.
5 # This file is part of GNU Radio
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)
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.
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.
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
32 from gnuradio.qtgui import qtgui
33 from PyQt4 import QtGui, QtCore
36 print "Please install gr-qtgui."
40 from usrp_display_qtgui import Ui_MainWindow
42 print "Error: could not find usrp_display_qtgui.py:"
43 print "\t\"pyuic4 usrp_display_qtgui.ui -o usrp_display_qtgui.py\""
47 # ////////////////////////////////////////////////////////////////////
48 # Define the QT Interface and Control Dialog
49 # ////////////////////////////////////////////////////////////////////
52 class main_window(QtGui.QMainWindow):
53 def __init__(self, snk, fg, parent=None):
55 QtGui.QWidget.__init__(self, parent)
56 self.gui = Ui_MainWindow()
57 self.gui.setupUi(self)
61 # Add the qtsnk widgets to the layout box
62 self.gui.sinkLayout.addWidget(snk)
64 # Connect up some signals
65 self.connect(self.gui.pauseButton, QtCore.SIGNAL("clicked()"),
67 self.connect(self.gui.frequencyEdit, QtCore.SIGNAL("editingFinished()"),
68 self.frequencyEditText)
69 self.connect(self.gui.gainEdit, QtCore.SIGNAL("editingFinished()"),
71 self.connect(self.gui.bandwidthEdit, QtCore.SIGNAL("editingFinished()"),
72 self.bandwidthEditText)
73 self.connect(self.gui.amplifierEdit, QtCore.SIGNAL("editingFinished()"),
74 self.amplifierEditText)
76 self.connect(self.gui.actionSaveData, QtCore.SIGNAL("activated()"),
78 self.gui.actionSaveData.setShortcut(QtGui.QKeySequence.Save)
81 if(self.gui.pauseButton.text() == "Pause"):
84 self.gui.pauseButton.setText("Unpause")
87 self.gui.pauseButton.setText("Pause")
90 # Functions to set the values in the GUI
91 def set_frequency(self, freq):
93 sfreq = eng_notation.num_to_str(self.freq)
94 self.gui.frequencyEdit.setText(QtCore.QString("%1").arg(sfreq))
96 def set_gain(self, gain):
98 self.gui.gainEdit.setText(QtCore.QString("%1").arg(self.gain))
100 def set_bandwidth(self, bw):
102 sbw = eng_notation.num_to_str(self.bw)
103 self.gui.bandwidthEdit.setText(QtCore.QString("%1").arg(sbw))
105 def set_amplifier(self, amp):
107 self.gui.amplifierEdit.setText(QtCore.QString("%1").arg(self.amp))
110 # Functions called when signals are triggered in the GUI
111 def frequencyEditText(self):
113 freq = eng_notation.str_to_num(self.gui.frequencyEdit.text().toAscii())
114 self.fg.set_frequency(freq)
119 def gainEditText(self):
121 gain = float(self.gui.gainEdit.text())
122 self.fg.set_gain(gain)
127 def bandwidthEditText(self):
129 bw = eng_notation.str_to_num(self.gui.bandwidthEdit.text().toAscii())
130 self.fg.set_bandwidth(bw)
135 def amplifierEditText(self):
137 amp = float(self.gui.amplifierEdit.text())
138 self.fg.set_amplifier_gain(amp)
144 fileName = QtGui.QFileDialog.getSaveFileName(self, "Save data to file", ".");
146 self.fg.save_to_file(str(fileName))
150 class my_top_block(gr.top_block):
152 gr.top_block.__init__(self)
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()
172 self.options = options
173 self.show_debug_info = True
175 self.qapp = QtGui.QApplication(sys.argv)
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)
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)
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)
193 self._fftsize = options.fft_size
195 self.snk = qtgui.sink_c(options.fft_size, gr.firdes.WIN_BLACKMAN_hARRIS,
196 self._freq, self._bandwidth,
198 True, True, False, True, False)
200 # Set up internal amplifier
201 self.amp = gr.multiply_const_cc(0.0)
202 self.set_amplifier_gain(100)
204 self.connect(self.u, self.amp, self.snk)
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()
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)
216 self.main_win = main_window(self.pysink, self)
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)
226 def save_to_file(self, name):
227 # Pause the flow graph
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)
238 def set_gain(self, gain):
240 self.u.set_gain(self._gain)
242 def set_frequency(self, freq):
244 r = self.u.set_center_freq(freq)
247 self.snk.set_frequency_range(self._freq, self._bandwidth)
251 def set_bandwidth(self, bw):
253 self._decim = int(self._adc_rate / self._bandwidth)
254 self.u.set_decim(self._decim)
257 self.snk.set_frequency_range(self._freq, self._bandwidth)
261 def set_amplifier_gain(self, amp):
262 self._amp_value = amp
263 self.amp.set_k(self._amp_value)
270 if __name__ == '__main__':
273 except KeyboardInterrupt: