fixed issue 387: removed uses of win.set for wx sinks
[debian/gnuradio] / usrp2 / host / apps / stdin_int32_fft.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2005,2007,2008,2010 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, gru
24 from gnuradio import eng_notation
25 from gnuradio.eng_option import eng_option
26 from gnuradio.wxgui import stdgui2, fftsink2, waterfallsink2, scopesink2, form, slider
27 from optparse import OptionParser
28 import wx
29 import sys
30 import numpy
31
32
33 class app_top_block(stdgui2.std_top_block):
34     def __init__(self, frame, panel, vbox, argv):
35         stdgui2.std_top_block.__init__(self, frame, panel, vbox, argv)
36
37         self.frame = frame
38         self.panel = panel
39         
40         parser = OptionParser(option_class=eng_option)
41         parser.add_option("-d", "--decim", type="int", default=16,
42                           help="set fgpa decimation rate to DECIM [default=%default]")
43         parser.add_option("-f", "--freq", type="eng_float", default=None,
44                           help="set frequency to FREQ", metavar="FREQ")
45         parser.add_option("-W", "--waterfall", action="store_true", default=False,
46                           help="Enable waterfall display")
47         parser.add_option("-S", "--oscilloscope", action="store_true", default=False,
48                           help="Enable oscilloscope display")
49         (options, args) = parser.parse_args()
50         if len(args) != 0:
51             parser.print_help()
52             sys.exit(1)
53         self.options = options
54         self.options.gain = 1.0
55         self.show_debug_info = True
56         
57
58         input_rate = 100e6 / options.decim
59
60         self.src = gr.file_descriptor_source(gr.sizeof_short, 0, False);
61         self.s2c = gr.interleaved_short_to_complex()
62
63         if options.waterfall:
64             self.scope = \
65               waterfallsink2.waterfall_sink_c (panel, fft_size=1024, sample_rate=input_rate)
66         elif options.oscilloscope:
67             self.scope = scopesink2.scope_sink_c(panel, sample_rate=input_rate)
68         else:
69             self.scope = fftsink2.fft_sink_c (panel, fft_size=1024, y_divs=12, sample_rate=input_rate,ref_level=110,fft_rate=20)
70
71         self.connect(self.src, self.s2c, self.scope)
72
73         self._build_gui(vbox)
74         self._setup_events()
75         
76         # set initial values
77
78         if options.freq is None:
79             # if no freq was specified, use the mid-point
80             options.freq = 0.0
81
82         if self.show_debug_info:
83             self.myform['decim'].set_value(self.options.decim)
84
85
86     def _set_status_msg(self, msg):
87         self.frame.GetStatusBar().SetStatusText(msg, 0)
88
89     def _build_gui(self, vbox):
90
91         def _form_set_freq(kv):
92             return self.set_freq(kv['freq'])
93             
94         vbox.Add(self.scope.win, 10, wx.EXPAND)
95         
96         # add control area at the bottom
97         self.myform = myform = form.form()
98         hbox = wx.BoxSizer(wx.HORIZONTAL)
99         hbox.Add((5,0), 0, 0)
100         myform['freq'] = form.float_field(
101             parent=self.panel, sizer=hbox, label="Center freq", weight=1,
102             callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
103
104         hbox.Add((5,0), 0, 0)
105         vbox.Add(hbox, 0, wx.EXPAND)
106
107         self._build_subpanel(vbox)
108
109     def _build_subpanel(self, vbox_arg):
110         # build a secondary information panel (sometimes hidden)
111
112         # FIXME figure out how to have this be a subpanel that is always
113         # created, but has its visibility controlled by foo.Show(True/False)
114         
115         def _form_set_decim(kv):
116             return self.set_decim(kv['decim'])
117
118         if not(self.show_debug_info):
119             return
120
121         panel = self.panel
122         vbox = vbox_arg
123         myform = self.myform
124
125         #panel = wx.Panel(self.panel, -1)
126         #vbox = wx.BoxSizer(wx.VERTICAL)
127
128         hbox = wx.BoxSizer(wx.HORIZONTAL)
129         hbox.Add((5,0), 0)
130
131         myform['decim'] = form.int_field(
132             parent=panel, sizer=hbox, label="Decim",
133             callback=myform.check_input_and_call(_form_set_decim, self._set_status_msg))
134
135         hbox.Add((5,0), 0)
136         vbox.Add(hbox, 0, wx.EXPAND)
137
138         
139     def set_freq(self, target_freq):
140         """
141         Set the center frequency we're interested in.
142
143         @param target_freq: frequency in Hz
144         @rypte: bool
145
146         Tuning is a two step process.  First we ask the front-end to
147         tune as close to the desired frequency as it can.  Then we use
148         the result of that operation and our target_frequency to
149         determine the value for the digital down converter.
150         """
151         
152         if True:
153             self.myform['freq'].set_value(target_freq)     # update displayed value
154             if not self.options.waterfall and not self.options.oscilloscope:
155                 self.scope.set_baseband_freq(target_freq)
156             return True
157
158         return False
159
160     def set_gain(self, gain):
161         self.myform['gain'].set_value(gain)     # update displayed value
162
163     def set_decim(self, decim):
164         input_rate = 100e6 / self.options.decim
165         self.scope.set_sample_rate(input_rate)
166         if self.show_debug_info:  # update displayed values
167             self.myform['decim'].set_value(self.u.decim_rate())
168         return ok
169
170     def _setup_events(self):
171         if not self.options.waterfall and not self.options.oscilloscope:
172             self.scope.win.Bind(wx.EVT_LEFT_DCLICK, self.evt_left_dclick)
173             
174     def evt_left_dclick(self, event):
175         (ux, uy) = self.scope.win.GetXY(event)
176         if event.CmdDown():
177             # Re-center on maximum power
178             points = self.scope.win._points
179             if self.scope.win.peak_hold:
180                 if self.scope.win.peak_vals is not None:
181                     ind = numpy.argmax(self.scope.win.peak_vals)
182                 else:
183                     ind = int(points.shape()[0]/2)
184             else:
185                 ind = numpy.argmax(points[:,1])
186             (freq, pwr) = points[ind]
187             target_freq = freq/self.scope.win._scale_factor
188             print ind, freq, pwr
189             self.set_freq(target_freq)            
190         else:
191             # Re-center on clicked frequency
192             target_freq = ux/self.scope.win._scale_factor
193             self.set_freq(target_freq)
194             
195         
196 def main ():
197     app = stdgui2.stdapp(app_top_block, "USRP FFT", nstatus=1)
198     app.MainLoop()
199
200 if __name__ == '__main__':
201     main ()