Removing Waterfall3DPlot. The qwt_plot3d is too much of a hassle to deal with and...
[debian/gnuradio] / gr-qtgui / src / lib / qtgui_sink_c.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009,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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <qtgui_sink_c.h>
28 #include <gr_io_signature.h>
29 #include <string.h>
30
31 #include <QTimer>
32
33 qtgui_sink_c_sptr
34 qtgui_make_sink_c (int fftsize, int wintype,
35                    double fc, double bw,
36                    const std::string &name,
37                    bool plotfreq, bool plotwaterfall,
38                    bool plotwaterfall3d, bool plottime,
39                    bool plotconst,
40                    bool use_openGL,
41                    QWidget *parent)
42 {
43   return gnuradio::get_initial_sptr(new qtgui_sink_c (fftsize, wintype,
44                                               fc, bw, name,
45                                               plotfreq, plotwaterfall,
46                                               plotwaterfall3d, plottime,
47                                               plotconst,
48                                               use_openGL,
49                                               parent));
50 }
51
52 qtgui_sink_c::qtgui_sink_c (int fftsize, int wintype,
53                             double fc, double bw,
54                             const std::string &name,
55                             bool plotfreq, bool plotwaterfall,
56                             bool plotwaterfall3d, bool plottime,
57                             bool plotconst,
58                             bool use_openGL,
59                             QWidget *parent)
60   : gr_block ("sink_c",
61               gr_make_io_signature (1, -1, sizeof(gr_complex)),
62               gr_make_io_signature (0, 0, 0)),
63     d_fftsize(fftsize),
64     d_wintype((gr_firdes::win_type)(wintype)), 
65     d_center_freq(fc), d_bandwidth(bw), d_name(name),
66     d_plotfreq(plotfreq), d_plotwaterfall(plotwaterfall),
67     d_plottime(plottime), d_plotconst(plotconst),
68     d_parent(parent)
69 {
70   if(plotwaterfall3d == true) {
71     fprintf(stderr, "Warning: plotting Waterfall3D has been removed; enabling plotwaterfall3d has no effect.\n");
72   }
73
74   d_main_gui = NULL;
75   pthread_mutex_init(&d_pmutex, NULL);
76   lock();
77
78   // Perform fftshift operation;
79   // this is usually desired when plotting
80   d_shift = true;  
81
82   d_fft = new gri_fft_complex (d_fftsize, true);
83
84   d_index = 0;
85   d_residbuf = new gr_complex[d_fftsize];
86
87   buildwindow();
88
89   initialize(use_openGL);
90 }
91
92 qtgui_sink_c::~qtgui_sink_c()
93 {
94   delete d_main_gui;
95   delete [] d_residbuf;
96   delete d_fft;
97 }
98
99 void
100 qtgui_sink_c::forecast(int noutput_items, gr_vector_int &ninput_items_required)
101 {
102   unsigned int ninputs = ninput_items_required.size();
103   for (unsigned int i = 0; i < ninputs; i++) {
104     ninput_items_required[i] = std::min(d_fftsize, 8191);
105   }
106 }
107
108 void qtgui_sink_c::lock()
109 {
110   pthread_mutex_lock(&d_pmutex);
111 }
112
113 void qtgui_sink_c::unlock()
114 {
115   pthread_mutex_unlock(&d_pmutex);
116 }
117
118
119 void
120 qtgui_sink_c::initialize(const bool opengl)
121 {
122   if(qApp != NULL) {
123     d_qApplication = qApp;
124   }
125   else {
126     int argc;
127     char **argv = NULL;
128     d_qApplication = new QApplication(argc, argv);
129   }
130
131   if(d_center_freq < 0) {
132     throw std::runtime_error("qtgui_sink_c: Received bad center frequency.\n");
133   }
134
135   uint64_t maxBufferSize = 32768;
136   d_main_gui = new SpectrumGUIClass(maxBufferSize, d_fftsize, 
137                                     d_center_freq, 
138                                     -d_bandwidth/2.0, 
139                                     d_bandwidth/2.0);
140
141   d_main_gui->SetDisplayTitle(d_name);
142   d_main_gui->SetFFTSize(d_fftsize);
143   d_main_gui->SetWindowType((int)d_wintype);
144
145   d_main_gui->OpenSpectrumWindow(d_parent, 
146                                  d_plotfreq, d_plotwaterfall,
147                                  d_plottime, d_plotconst,
148                                  opengl);
149
150   // initialize update time to 10 times a second
151   set_update_time(0.1);
152
153   d_object = new qtgui_obj(d_qApplication);
154   qApp->postEvent(d_object, new qtgui_event(&d_pmutex));
155 }
156
157
158 void
159 qtgui_sink_c::exec_()
160 {
161   d_qApplication->exec();
162 }
163
164 QWidget*
165 qtgui_sink_c::qwidget()
166 {
167   return d_main_gui->qwidget();
168 }
169
170 PyObject*
171 qtgui_sink_c::pyqwidget()
172 {
173   PyObject *w = PyLong_FromVoidPtr((void*)d_main_gui->qwidget());
174   PyObject *retarg = Py_BuildValue("N", w);
175   return retarg;
176 }
177
178 void
179 qtgui_sink_c::set_frequency_range(const double centerfreq, 
180                                   const double bandwidth)
181 {
182   d_center_freq = centerfreq;
183   d_bandwidth = bandwidth;
184   d_main_gui->SetFrequencyRange(d_center_freq, 
185                                 -d_bandwidth/2.0,
186                                 d_bandwidth/2.0);
187 }
188
189 void
190 qtgui_sink_c::set_time_domain_axis(double min, double max)
191 {
192   d_main_gui->SetTimeDomainAxis(min, max);
193 }
194
195 void
196 qtgui_sink_c::set_constellation_axis(double xmin, double xmax,
197                                      double ymin, double ymax)
198 {
199   d_main_gui->SetConstellationAxis(xmin, xmax, ymin, ymax);
200 }
201
202 void 
203 qtgui_sink_c::set_constellation_pen_size(int size)
204 {
205   d_main_gui->SetConstellationPenSize(size);
206 }
207
208
209 void
210 qtgui_sink_c::set_frequency_axis(double min, double max)
211 {
212   d_main_gui->SetFrequencyAxis(min, max);
213 }
214
215 void
216 qtgui_sink_c::set_update_time(double t)
217 {
218   d_update_time = t;
219   d_main_gui->SetUpdateTime(d_update_time);
220 }
221
222 void
223 qtgui_sink_c::fft(const gr_complex *data_in, int size)
224 {
225   if (d_window.size()) {
226     gr_complex *dst = d_fft->get_inbuf();
227     int i;
228     for (i = 0; i < size; i++)          // apply window
229       dst[i] = data_in[i] * d_window[i];
230   }
231   else {
232     memcpy (d_fft->get_inbuf(), data_in, sizeof(gr_complex)*size);
233   }
234
235   d_fft->execute ();     // compute the fft
236 }
237
238 void 
239 qtgui_sink_c::windowreset()
240 {
241   gr_firdes::win_type newwintype = (gr_firdes::win_type)d_main_gui->GetWindowType();  
242   if(d_wintype != newwintype) {
243     d_wintype = newwintype;
244     buildwindow();
245   }
246 }
247
248 void
249 qtgui_sink_c::buildwindow()
250 {
251   d_window.clear();
252   if(d_wintype != 0) {
253     d_window = gr_firdes::window(d_wintype, d_fftsize, 6.76);
254   }
255 }
256
257 void
258 qtgui_sink_c::fftresize()
259 {
260   int newfftsize = d_main_gui->GetFFTSize();
261
262   if(newfftsize != d_fftsize) {
263
264     // Resize residbuf and replace data
265     delete [] d_residbuf;
266     d_residbuf = new gr_complex[newfftsize];
267
268     // Set new fft size and reset buffer index 
269     // (throws away any currently held data, but who cares?) 
270     d_fftsize = newfftsize;
271     d_index = 0;
272     
273     // Reset window to reflect new size
274     buildwindow();
275
276     // Reset FFTW plan for new size
277     delete d_fft;
278     d_fft = new gri_fft_complex (d_fftsize, true);
279   }
280 }
281
282
283 int
284 qtgui_sink_c::general_work (int noutput_items,
285                             gr_vector_int &ninput_items,
286                             gr_vector_const_void_star &input_items,
287                             gr_vector_void_star &output_items)
288 {
289   int j=0;
290   const gr_complex *in = (const gr_complex*)input_items[0];
291
292   pthread_mutex_lock(&d_pmutex);
293
294   // Update the FFT size from the application
295   fftresize();
296   windowreset();
297
298   for(int i=0; i < noutput_items; i+=d_fftsize) {
299     unsigned int datasize = noutput_items - i;
300     unsigned int resid = d_fftsize-d_index;
301
302     // If we have enough input for one full FFT, do it
303     if(datasize >= resid) {
304       const timespec currentTime = get_highres_clock();
305       
306       // Fill up residbuf with d_fftsize number of items
307       memcpy(d_residbuf+d_index, &in[j], sizeof(gr_complex)*resid);
308       d_index = 0;
309
310       j += resid;
311       fft(d_residbuf, d_fftsize);
312       
313       d_main_gui->UpdateWindow(true, d_fft->get_outbuf(), d_fftsize,
314                                NULL, 0, (float*)d_residbuf, d_fftsize,
315                                currentTime, true);
316     }
317     // Otherwise, copy what we received into the residbuf for next time
318     else {
319       memcpy(d_residbuf+d_index, &in[j], sizeof(gr_complex)*datasize);
320       d_index += datasize;
321       j += datasize;
322     }   
323   }
324
325   pthread_mutex_unlock(&d_pmutex);
326
327   consume_each(j);
328   return j;
329 }