summaryrefslogtreecommitdiff
path: root/gr-qtgui/src/lib/SpectrumGUIClass.cc
blob: f9988d8f54bc29864fe3e3fb638c4bd11ed98c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#ifndef SPECTRUM_GUI_CLASS_CPP
#define SPECTRUM_GUI_CLASS_CPP

#include <SpectrumGUIClass.h>
//Added by qt3to4:
#include <QEvent>
#include <QCustomEvent>

const long SpectrumGUIClass::MAX_FFT_SIZE;
const long SpectrumGUIClass::MIN_FFT_SIZE;

SpectrumGUIClass::SpectrumGUIClass(const uint64_t maxDataSize, const uint64_t fftSize, 
				   const double newStartFrequency, const double newStopFrequency){
  _dataPoints = maxDataSize;
  if(_dataPoints < 2){
    _dataPoints = 2;
  }
  _lastDataPointCount = _dataPoints;

  _fftSize = fftSize;

  _pendingGUIUpdateEventsCount = 0;
  _droppedEntriesCount = 0;

  _centerFrequency = 0;
  _startFrequency = newStartFrequency;
  _stopFrequency = newStopFrequency;

#warning SPECIFY THIS LATER...
  _windowType = 5;

  timespec_reset(&_lastGUIUpdateTime);

  _windowOpennedFlag = false;
  _fftBuffersCreatedFlag = false;

  // Create Mutex Lock
  //_windowStateLock = new MutexClass("_windowStateLock");

  _powerValue = 1;
}

SpectrumGUIClass::~SpectrumGUIClass(){
  if(GetWindowOpenFlag()){
    delete _spectrumDisplayForm;
  }

  if(_fftBuffersCreatedFlag){
    delete[] _fftPoints;
    delete[] _realTimeDomainPoints;
    delete[] _imagTimeDomainPoints;
  }

  //delete _windowStateLock;
}

void SpectrumGUIClass::OpenSpectrumWindow(QWidget* parent){
  //_windowStateLock->Lock();

  if(!_windowOpennedFlag){

    if(!_fftBuffersCreatedFlag){
      _fftPoints = new std::complex<float>[_dataPoints];
      _realTimeDomainPoints = new double[_dataPoints];
      _imagTimeDomainPoints = new double[_dataPoints];
      _fftBuffersCreatedFlag = true;
      
      
      memset(_fftPoints, 0x0, _dataPoints*sizeof(std::complex<float>));
      memset(_realTimeDomainPoints, 0x0, _dataPoints*sizeof(double));
      memset(_imagTimeDomainPoints, 0x0, _dataPoints*sizeof(double));
    }
    
    // Called from the Event Thread
    _spectrumDisplayForm = new SpectrumDisplayForm(parent);

    _windowOpennedFlag = true;

    _spectrumDisplayForm->setSystem(this, _dataPoints, _fftSize);

    qApp->processEvents();
  }

  //_windowStateLock->Unlock();

  SetDisplayTitle(_title);
  Reset();

  qApp->postEvent(_spectrumDisplayForm, new QEvent(QEvent::Type(QEvent::User+3)));

  _spectrumDisplayForm->show();

  qApp->processEvents();

  timespec_reset(&_lastGUIUpdateTime);

  // Draw Blank Display
  UpdateWindow(false, NULL, 0, NULL, 0, NULL, 0, 1.0, get_highres_clock(), true);

  // GUI Thread only
  qApp->processEvents();
}

void SpectrumGUIClass::Reset(){
  if(GetWindowOpenFlag()){
    qApp->postEvent(_spectrumDisplayForm, new SpectrumFrequencyRangeEvent(_centerFrequency, 
									  _startFrequency, 
									  _stopFrequency));
    qApp->postEvent(_spectrumDisplayForm, new SpectrumWindowResetEvent());
  }
  _droppedEntriesCount = 0;
  // Call the following function the the Spectrum Window Reset Event window
  // ResetPendingGUIUpdateEvents();
}

void SpectrumGUIClass::SetDisplayTitle(const std::string newString){
  _title.assign(newString);

  if(GetWindowOpenFlag()){
    qApp->postEvent(_spectrumDisplayForm, new SpectrumWindowCaptionEvent(_title.c_str()));
  }

}

bool SpectrumGUIClass::GetWindowOpenFlag(){
  bool returnFlag = false;
  //_windowStateLock->Lock();
  returnFlag =  _windowOpennedFlag;
  //_windowStateLock->Unlock();
  return returnFlag;
}


void SpectrumGUIClass::SetWindowOpenFlag(const bool newFlag){
  //_windowStateLock->Lock();
  _windowOpennedFlag = newFlag;
  //_windowStateLock->Unlock();
}

void SpectrumGUIClass::SetFrequencyRange(const double centerFreq, const double startFreq, const double stopFreq){
  //_windowStateLock->Lock();
  _centerFrequency = centerFreq;
  _startFrequency = startFreq;
  _stopFrequency = stopFreq;
  //_windowStateLock->Unlock();
}

double SpectrumGUIClass::GetStartFrequency()const{
  double returnValue = 0.0;
  //_windowStateLock->Lock();
  returnValue =  _startFrequency;
  //_windowStateLock->Unlock();
  return returnValue;
}

double SpectrumGUIClass::GetStopFrequency()const{
  double returnValue = 0.0;
  //_windowStateLock->Lock();
  returnValue =  _stopFrequency;
  //_windowStateLock->Unlock();
  return returnValue;
}

double SpectrumGUIClass::GetCenterFrequency()const{
  double returnValue = 0.0;
  //_windowStateLock->Lock();
  returnValue =  _centerFrequency;
  //_windowStateLock->Unlock();
  return returnValue;
}


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){

  int64_t bufferSize = inputBufferSize;
  bool repeatDataFlag = false;
  if(bufferSize > _dataPoints){
    bufferSize = _dataPoints;
  }
  int64_t timeDomainBufferSize = 0;

  if( updateDisplayFlag){
    if((fftBuffer != NULL) && (bufferSize > 0)){
      memcpy(_fftPoints, fftBuffer, bufferSize * sizeof(std::complex<float>));
    }

    // Can't do a memcpy since ths is going from float to double data type
    if((realTimeDomainData != NULL) && (realTimeDomainDataSize > 0)){
      const float* realTimeDomainDataPtr = realTimeDomainData;

      double* realTimeDomainPointsPtr = _realTimeDomainPoints;
      timeDomainBufferSize = realTimeDomainDataSize;

      memset( _imagTimeDomainPoints, 0x0, realTimeDomainDataSize*sizeof(double));
      for( uint64_t number = 0; number < realTimeDomainDataSize; number++){
	*realTimeDomainPointsPtr++ = *realTimeDomainDataPtr++;
      }
    }

    // Can't do a memcpy since ths is going from float to double data type
    if((complexTimeDomainData != NULL) && (complexTimeDomainDataSize > 0)){
      const float* complexTimeDomainDataPtr = complexTimeDomainData;

      double* realTimeDomainPointsPtr = _realTimeDomainPoints;
      double* imagTimeDomainPointsPtr = _imagTimeDomainPoints;

      timeDomainBufferSize = complexTimeDomainDataSize;
      for( uint64_t number = 0; number < complexTimeDomainDataSize; number++){
	*realTimeDomainPointsPtr++ = *complexTimeDomainDataPtr++;
	*imagTimeDomainPointsPtr++ = *complexTimeDomainDataPtr++;
      }
    }
  }

  // If bufferSize is zero, then just update the display by sending over the old data
  if(bufferSize < 1){
    bufferSize = _lastDataPointCount;
    repeatDataFlag = true;
  }
  else{
    // Since there is data this time, update the count
    _lastDataPointCount = bufferSize;
  }

  const timespec currentTime = get_highres_clock();
  const timespec lastUpdateGUITime = GetLastGUIUpdateTime();

  if((diff_timespec(currentTime, lastUpdateGUITime) > (4*timePerFFT)) && (GetPendingGUIUpdateEvents() > 0) && !timespec_empty(&lastUpdateGUITime)){
    // Do not update the display if too much data is pending to be displayed
    _droppedEntriesCount++;
  }
  else{
    // Draw the Data
    IncrementPendingGUIUpdateEvents();
    qApp->postEvent(_spectrumDisplayForm, new SpectrumUpdateEvent(_fftPoints, bufferSize, _realTimeDomainPoints, _imagTimeDomainPoints, timeDomainBufferSize, timePerFFT, timestamp, repeatDataFlag, lastOfMultipleFFTUpdateFlag, currentTime, _droppedEntriesCount));
    
    // Only reset the dropped entries counter if this is not repeat data since repeat data is dropped by the display systems
    if(!repeatDataFlag){
      _droppedEntriesCount = 0;
    }
  
    //qApp->wakeUpGuiThread();
  }
}

float SpectrumGUIClass::GetPowerValue()const{
  float returnValue = 0;
  //_windowStateLock->Lock();
  returnValue = _powerValue;
  //_windowStateLock->Unlock();
  return returnValue;
}

void SpectrumGUIClass::SetPowerValue(const float value){
  //_windowStateLock->Lock();
  _powerValue = value;
  //_windowStateLock->Unlock();
}

int SpectrumGUIClass::GetWindowType()const{
  int returnValue = 0;
  //_windowStateLock->Lock();
  returnValue = _windowType;
  //_windowStateLock->Unlock();
  return returnValue;
}

void SpectrumGUIClass::SetWindowType(const int newType){
  //_windowStateLock->Lock();
  _windowType = newType;
  //_windowStateLock->Unlock();
}

int SpectrumGUIClass::GetFFTSize()const{
  int returnValue = 0;
  //_windowStateLock->Lock();
  returnValue = _fftSize;
  //_windowStateLock->Unlock();
  return returnValue;
}

int SpectrumGUIClass::GetFFTSizeIndex()const{
  int fftsize = GetFFTSize();
  switch(fftsize) {
  case(1024): return 0; break;
  case(2048): return 1; break;
  case(4096): return 2; break;
  case(8192): return 3; break;
  case(16384): return 3; break;
  case(32768): return 3; break;
  default: return 0;
  }
}

void SpectrumGUIClass::SetFFTSize(const int newSize){
  //_windowStateLock->Lock();
  _fftSize = newSize;
  //_windowStateLock->Unlock();
}

timespec SpectrumGUIClass::GetLastGUIUpdateTime()const{
  timespec returnValue;
  //_windowStateLock->Lock();
  returnValue = _lastGUIUpdateTime;
  //_windowStateLock->Unlock();
  return returnValue;
}

void SpectrumGUIClass::SetLastGUIUpdateTime(const timespec newTime){
  //_windowStateLock->Lock();
  _lastGUIUpdateTime = newTime;
  //_windowStateLock->Unlock();
}

unsigned int SpectrumGUIClass::GetPendingGUIUpdateEvents()const{
  unsigned int returnValue = 0;
  //_windowStateLock->Lock();
  returnValue = _pendingGUIUpdateEventsCount;
  //_windowStateLock->Unlock();
  return returnValue;
}

void SpectrumGUIClass::IncrementPendingGUIUpdateEvents(){
  //_windowStateLock->Lock();
  _pendingGUIUpdateEventsCount++;
  //_windowStateLock->Unlock();
}

void SpectrumGUIClass::DecrementPendingGUIUpdateEvents(){
  //_windowStateLock->Lock();
  if(_pendingGUIUpdateEventsCount > 0){
    _pendingGUIUpdateEventsCount--;
  }
  //_windowStateLock->Unlock();
}

void SpectrumGUIClass::ResetPendingGUIUpdateEvents(){
  //_windowStateLock->Lock();
  _pendingGUIUpdateEventsCount = 0;
  //_windowStateLock->Unlock();
}


#endif /* SPECTRUM_GUI_CLASS_CPP */