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