a57395a6719654ed3595a929f45b9ff4f7255362
[debian/gnuradio] / gr-qtgui / src / lib / SpectrumGUIClass.cc
1 #ifndef SPECTRUM_GUI_CLASS_CPP
2 #define SPECTRUM_GUI_CLASS_CPP
3
4 #include <SpectrumGUIClass.h>
5 //Added by qt3to4:
6 #include <QEvent>
7 #include <QCustomEvent>
8
9 const long SpectrumGUIClass::MAX_FFT_SIZE;
10 const long SpectrumGUIClass::MIN_FFT_SIZE;
11
12 SpectrumGUIClass::SpectrumGUIClass(const uint64_t maxDataSize, const uint64_t fftSize, const double newStartFrequency, const double newStopFrequency){
13   _dataPoints = maxDataSize;
14   if(_dataPoints < 2){
15     _dataPoints = 2;
16   }
17   _lastDataPointCount = _dataPoints;
18
19   _fftSize = fftSize;
20
21   _pendingGUIUpdateEventsCount = 0;
22   _droppedEntriesCount = 0;
23
24   _centerFrequency = 0;
25   _startFrequency = newStartFrequency;
26   _stopFrequency = newStopFrequency;
27
28 #warning SPECIFY THIS LATER...
29   _windowType = 5;
30
31   timespec_reset(&_lastGUIUpdateTime);
32
33   _windowOpennedFlag = false;
34   _fftBuffersCreatedFlag = false;
35
36   // Create Mutex Lock
37   //_windowStateLock = new MutexClass("_windowStateLock");
38
39   _powerValue = 1;
40 }
41
42 SpectrumGUIClass::~SpectrumGUIClass(){
43   if(GetWindowOpenFlag()){
44     delete _spectrumDisplayForm;
45   }
46
47   if(_fftBuffersCreatedFlag){
48     delete[] _fftPoints;
49     delete[] _realTimeDomainPoints;
50     delete[] _imagTimeDomainPoints;
51   }
52
53   //delete _windowStateLock;
54 }
55
56 void SpectrumGUIClass::OpenSpectrumWindow(QWidget* parent){
57   //_windowStateLock->Lock();
58
59   if(!_windowOpennedFlag){
60
61     if(!_fftBuffersCreatedFlag){
62       _fftPoints = new std::complex<float>[_dataPoints];
63       _realTimeDomainPoints = new double[_dataPoints];
64       _imagTimeDomainPoints = new double[_dataPoints];
65       _fftBuffersCreatedFlag = true;
66       
67       
68       memset(_fftPoints, 0x0, _dataPoints*sizeof(std::complex<float>));
69       memset(_realTimeDomainPoints, 0x0, _dataPoints*sizeof(double));
70       memset(_imagTimeDomainPoints, 0x0, _dataPoints*sizeof(double));
71     }
72     
73     // Called from the Event Thread
74     _spectrumDisplayForm = new SpectrumDisplayForm(parent);
75
76     _windowOpennedFlag = true;
77
78     _spectrumDisplayForm->setSystem(this, _dataPoints, _fftSize);
79
80     qApp->processEvents();
81   }
82
83   //_windowStateLock->Unlock();
84
85   SetDisplayTitle(_title);
86   Reset();
87
88   qApp->postEvent(_spectrumDisplayForm, new QEvent(QEvent::Type(QEvent::User+3)));
89
90   _spectrumDisplayForm->show();
91
92   qApp->processEvents();
93
94   timespec_reset(&_lastGUIUpdateTime);
95
96   // Draw Blank Display
97   UpdateWindow(false, NULL, 0, NULL, 0, NULL, 0, 1.0, get_highres_clock(), true);
98
99   // GUI Thread only
100   qApp->processEvents();
101 }
102
103 void SpectrumGUIClass::Reset(){
104   if(GetWindowOpenFlag()){
105     qApp->postEvent(_spectrumDisplayForm, new SpectrumFrequencyRangeEvent(_centerFrequency, _startFrequency, _stopFrequency));
106     qApp->postEvent(_spectrumDisplayForm, new SpectrumWindowResetEvent());
107   }
108   _droppedEntriesCount = 0;
109   // Call the following function the the Spectrum Window Reset Event window
110   // ResetPendingGUIUpdateEvents();
111 }
112
113 void SpectrumGUIClass::SetDisplayTitle(const std::string newString){
114   _title.assign(newString);
115
116   if(GetWindowOpenFlag()){
117     qApp->postEvent(_spectrumDisplayForm, new SpectrumWindowCaptionEvent(_title.c_str()));
118   }
119
120 }
121
122 bool SpectrumGUIClass::GetWindowOpenFlag(){
123   bool returnFlag = false;
124   //_windowStateLock->Lock();
125   returnFlag =  _windowOpennedFlag;
126   //_windowStateLock->Unlock();
127   return returnFlag;
128 }
129
130
131 void SpectrumGUIClass::SetWindowOpenFlag(const bool newFlag){
132   //_windowStateLock->Lock();
133   _windowOpennedFlag = newFlag;
134   //_windowStateLock->Unlock();
135 }
136
137 void SpectrumGUIClass::SetFrequencyRange(const double centerFreq, const double startFreq, const double stopFreq){
138   //_windowStateLock->Lock();
139   _centerFrequency = centerFreq;
140   _startFrequency = startFreq;
141   _stopFrequency = stopFreq;
142   //_windowStateLock->Unlock();
143 }
144
145 double SpectrumGUIClass::GetStartFrequency()const{
146   double returnValue = 0.0;
147   //_windowStateLock->Lock();
148   returnValue =  _startFrequency;
149   //_windowStateLock->Unlock();
150   return returnValue;
151 }
152
153 double SpectrumGUIClass::GetStopFrequency()const{
154   double returnValue = 0.0;
155   //_windowStateLock->Lock();
156   returnValue =  _stopFrequency;
157   //_windowStateLock->Unlock();
158   return returnValue;
159 }
160
161 double SpectrumGUIClass::GetCenterFrequency()const{
162   double returnValue = 0.0;
163   //_windowStateLock->Lock();
164   returnValue =  _centerFrequency;
165   //_windowStateLock->Unlock();
166   return returnValue;
167 }
168
169
170 void SpectrumGUIClass::UpdateWindow(const bool updateDisplayFlag, const std::complex<float>* fftBuffer, const uint64_t inputBufferSize, const float* realTimeDomainData, const uint64_t realTimeDomainDataSize, const float* complexTimeDomainData, const uint64_t complexTimeDomainDataSize, const double timePerFFT, const timespec timestamp, const bool lastOfMultipleFFTUpdateFlag){
171
172   int64_t bufferSize = inputBufferSize;
173   bool repeatDataFlag = false;
174   if(bufferSize > _dataPoints){
175     bufferSize = _dataPoints;
176   }
177   int64_t timeDomainBufferSize = 0;
178
179   if( updateDisplayFlag){
180     if((fftBuffer != NULL) && (bufferSize > 0)){
181       memcpy(_fftPoints, fftBuffer, bufferSize * sizeof(std::complex<float>));
182     }
183
184     // Can't do a memcpy since ths is going from float to double data type
185     if((realTimeDomainData != NULL) && (realTimeDomainDataSize > 0)){
186       const float* realTimeDomainDataPtr = realTimeDomainData;
187
188       double* realTimeDomainPointsPtr = _realTimeDomainPoints;
189       timeDomainBufferSize = realTimeDomainDataSize;
190
191       memset( _imagTimeDomainPoints, 0x0, realTimeDomainDataSize*sizeof(double));
192       for( uint64_t number = 0; number < realTimeDomainDataSize; number++){
193         *realTimeDomainPointsPtr++ = *realTimeDomainDataPtr++;
194       }
195     }
196
197     // Can't do a memcpy since ths is going from float to double data type
198     if((complexTimeDomainData != NULL) && (complexTimeDomainDataSize > 0)){
199       const float* complexTimeDomainDataPtr = complexTimeDomainData;
200
201       double* realTimeDomainPointsPtr = _realTimeDomainPoints;
202       double* imagTimeDomainPointsPtr = _imagTimeDomainPoints;
203
204       timeDomainBufferSize = complexTimeDomainDataSize;
205       for( uint64_t number = 0; number < complexTimeDomainDataSize; number++){
206         *realTimeDomainPointsPtr++ = *complexTimeDomainDataPtr++;
207         *imagTimeDomainPointsPtr++ = *complexTimeDomainDataPtr++;
208       }
209     }
210   }
211
212   // If bufferSize is zero, then just update the display by sending over the old data
213   if(bufferSize < 1){
214     bufferSize = _lastDataPointCount;
215     repeatDataFlag = true;
216   }
217   else{
218     // Since there is data this time, update the count
219     _lastDataPointCount = bufferSize;
220   }
221
222   const timespec currentTime = get_highres_clock();
223   const timespec lastUpdateGUITime = GetLastGUIUpdateTime();
224
225   if((diff_timespec(currentTime, lastUpdateGUITime) > (4*timePerFFT)) && (GetPendingGUIUpdateEvents() > 0) && !timespec_empty(&lastUpdateGUITime)){
226     // Do not update the display if too much data is pending to be displayed
227     _droppedEntriesCount++;
228   }
229   else{
230     // Draw the Data
231     IncrementPendingGUIUpdateEvents();
232     qApp->postEvent(_spectrumDisplayForm, new SpectrumUpdateEvent(_fftPoints, bufferSize, _realTimeDomainPoints, _imagTimeDomainPoints, timeDomainBufferSize, timePerFFT, timestamp, repeatDataFlag, lastOfMultipleFFTUpdateFlag, currentTime, _droppedEntriesCount));
233     
234     // Only reset the dropped entries counter if this is not repeat data since repeat data is dropped by the display systems
235     if(!repeatDataFlag){
236       _droppedEntriesCount = 0;
237     }
238   
239     //qApp->wakeUpGuiThread();
240   }
241 }
242
243 float SpectrumGUIClass::GetPowerValue()const{
244   float returnValue = 0;
245   //_windowStateLock->Lock();
246   returnValue = _powerValue;
247   //_windowStateLock->Unlock();
248   return returnValue;
249 }
250
251 void SpectrumGUIClass::SetPowerValue(const float value){
252   //_windowStateLock->Lock();
253   _powerValue = value;
254   //_windowStateLock->Unlock();
255 }
256
257 int SpectrumGUIClass::GetWindowType()const{
258   int returnValue = 0;
259   //_windowStateLock->Lock();
260   returnValue = _windowType;
261   //_windowStateLock->Unlock();
262   return returnValue;
263 }
264
265 void SpectrumGUIClass::SetWindowType(const int newType){
266   //_windowStateLock->Lock();
267   _windowType = newType;
268   //_windowStateLock->Unlock();
269 }
270
271 int SpectrumGUIClass::GetFFTSize()const{
272   int returnValue = 0;
273   //_windowStateLock->Lock();
274   returnValue = _fftSize;
275   //_windowStateLock->Unlock();
276   return returnValue;
277 }
278
279 int SpectrumGUIClass::GetFFTSizeIndex()const{
280   int fftsize = GetFFTSize();
281   switch(fftsize) {
282   case(1024): return 0; break;
283   case(2048): return 1; break;
284   case(4096): return 2; break;
285   case(8192): return 3; break;
286   case(16384): return 3; break;
287   case(32768): return 3; break;
288   default: return 0;
289   }
290 }
291
292 void SpectrumGUIClass::SetFFTSize(const int newSize){
293   //_windowStateLock->Lock();
294   _fftSize = newSize;
295   //_windowStateLock->Unlock();
296 }
297
298 timespec SpectrumGUIClass::GetLastGUIUpdateTime()const{
299   timespec returnValue;
300   //_windowStateLock->Lock();
301   returnValue = _lastGUIUpdateTime;
302   //_windowStateLock->Unlock();
303   return returnValue;
304 }
305
306 void SpectrumGUIClass::SetLastGUIUpdateTime(const timespec newTime){
307   //_windowStateLock->Lock();
308   _lastGUIUpdateTime = newTime;
309   //_windowStateLock->Unlock();
310 }
311
312 unsigned int SpectrumGUIClass::GetPendingGUIUpdateEvents()const{
313   unsigned int returnValue = 0;
314   //_windowStateLock->Lock();
315   returnValue = _pendingGUIUpdateEventsCount;
316   //_windowStateLock->Unlock();
317   return returnValue;
318 }
319
320 void SpectrumGUIClass::IncrementPendingGUIUpdateEvents(){
321   //_windowStateLock->Lock();
322   _pendingGUIUpdateEventsCount++;
323   //_windowStateLock->Unlock();
324 }
325
326 void SpectrumGUIClass::DecrementPendingGUIUpdateEvents(){
327   //_windowStateLock->Lock();
328   if(_pendingGUIUpdateEventsCount > 0){
329     _pendingGUIUpdateEventsCount--;
330   }
331   //_windowStateLock->Unlock();
332 }
333
334 void SpectrumGUIClass::ResetPendingGUIUpdateEvents(){
335   //_windowStateLock->Lock();
336   _pendingGUIUpdateEventsCount = 0;
337   //_windowStateLock->Unlock();
338 }
339
340
341 #endif /* SPECTRUM_GUI_CLASS_CPP */