1 #ifndef SPECTRUM_GUI_CLASS_CPP
2 #define SPECTRUM_GUI_CLASS_CPP
4 #include <SpectrumGUIClass.h>
7 #include <QCustomEvent>
9 const long SpectrumGUIClass::MAX_FFT_SIZE;
10 const long SpectrumGUIClass::MIN_FFT_SIZE;
12 SpectrumGUIClass::SpectrumGUIClass(const uint64_t maxDataSize, const uint64_t fftSize, const double newStartFrequency, const double newStopFrequency){
13 _dataPoints = maxDataSize;
17 _lastDataPointCount = _dataPoints;
21 _pendingGUIUpdateEventsCount = 0;
22 _droppedEntriesCount = 0;
25 _startFrequency = newStartFrequency;
26 _stopFrequency = newStopFrequency;
28 #warning SPECIFY THIS LATER...
31 timespec_reset(&_lastGUIUpdateTime);
33 _windowOpennedFlag = false;
34 _fftBuffersCreatedFlag = false;
37 //_windowStateLock = new MutexClass("_windowStateLock");
42 SpectrumGUIClass::~SpectrumGUIClass(){
43 if(GetWindowOpenFlag()){
44 delete _spectrumDisplayForm;
47 if(_fftBuffersCreatedFlag){
49 delete[] _realTimeDomainPoints;
50 delete[] _imagTimeDomainPoints;
53 //delete _windowStateLock;
56 void SpectrumGUIClass::OpenSpectrumWindow(QWidget* parent){
57 //_windowStateLock->Lock();
59 if(!_windowOpennedFlag){
61 if(!_fftBuffersCreatedFlag){
62 _fftPoints = new std::complex<float>[_dataPoints];
63 _realTimeDomainPoints = new double[_dataPoints];
64 _imagTimeDomainPoints = new double[_dataPoints];
65 _fftBuffersCreatedFlag = true;
68 memset(_fftPoints, 0x0, _dataPoints*sizeof(std::complex<float>));
69 memset(_realTimeDomainPoints, 0x0, _dataPoints*sizeof(double));
70 memset(_imagTimeDomainPoints, 0x0, _dataPoints*sizeof(double));
73 // Called from the Event Thread
74 _spectrumDisplayForm = new SpectrumDisplayForm(parent);
76 _windowOpennedFlag = true;
78 _spectrumDisplayForm->setSystem(this, _dataPoints, _fftSize);
80 qApp->processEvents();
83 //_windowStateLock->Unlock();
85 SetDisplayTitle(_title);
88 qApp->postEvent(_spectrumDisplayForm, new QEvent(QEvent::Type(QEvent::User+3)));
90 _spectrumDisplayForm->show();
92 qApp->processEvents();
94 timespec_reset(&_lastGUIUpdateTime);
97 UpdateWindow(false, NULL, 0, NULL, 0, NULL, 0, 1.0, get_highres_clock(), true);
100 qApp->processEvents();
103 void SpectrumGUIClass::Reset(){
104 if(GetWindowOpenFlag()){
105 qApp->postEvent(_spectrumDisplayForm, new SpectrumFrequencyRangeEvent(_centerFrequency, _startFrequency, _stopFrequency));
106 qApp->postEvent(_spectrumDisplayForm, new SpectrumWindowResetEvent());
108 _droppedEntriesCount = 0;
109 // Call the following function the the Spectrum Window Reset Event window
110 // ResetPendingGUIUpdateEvents();
113 void SpectrumGUIClass::SetDisplayTitle(const std::string newString){
114 _title.assign(newString);
116 if(GetWindowOpenFlag()){
117 qApp->postEvent(_spectrumDisplayForm, new SpectrumWindowCaptionEvent(_title.c_str()));
122 bool SpectrumGUIClass::GetWindowOpenFlag(){
123 bool returnFlag = false;
124 //_windowStateLock->Lock();
125 returnFlag = _windowOpennedFlag;
126 //_windowStateLock->Unlock();
131 void SpectrumGUIClass::SetWindowOpenFlag(const bool newFlag){
132 //_windowStateLock->Lock();
133 _windowOpennedFlag = newFlag;
134 //_windowStateLock->Unlock();
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();
145 double SpectrumGUIClass::GetStartFrequency()const{
146 double returnValue = 0.0;
147 //_windowStateLock->Lock();
148 returnValue = _startFrequency;
149 //_windowStateLock->Unlock();
153 double SpectrumGUIClass::GetStopFrequency()const{
154 double returnValue = 0.0;
155 //_windowStateLock->Lock();
156 returnValue = _stopFrequency;
157 //_windowStateLock->Unlock();
161 double SpectrumGUIClass::GetCenterFrequency()const{
162 double returnValue = 0.0;
163 //_windowStateLock->Lock();
164 returnValue = _centerFrequency;
165 //_windowStateLock->Unlock();
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){
172 int64_t bufferSize = inputBufferSize;
173 bool repeatDataFlag = false;
174 if(bufferSize > _dataPoints){
175 bufferSize = _dataPoints;
177 int64_t timeDomainBufferSize = 0;
179 if( updateDisplayFlag){
180 if((fftBuffer != NULL) && (bufferSize > 0)){
181 memcpy(_fftPoints, fftBuffer, bufferSize * sizeof(std::complex<float>));
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;
188 double* realTimeDomainPointsPtr = _realTimeDomainPoints;
189 timeDomainBufferSize = realTimeDomainDataSize;
191 memset( _imagTimeDomainPoints, 0x0, realTimeDomainDataSize*sizeof(double));
192 for( uint64_t number = 0; number < realTimeDomainDataSize; number++){
193 *realTimeDomainPointsPtr++ = *realTimeDomainDataPtr++;
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;
201 double* realTimeDomainPointsPtr = _realTimeDomainPoints;
202 double* imagTimeDomainPointsPtr = _imagTimeDomainPoints;
204 timeDomainBufferSize = complexTimeDomainDataSize;
205 for( uint64_t number = 0; number < complexTimeDomainDataSize; number++){
206 *realTimeDomainPointsPtr++ = *complexTimeDomainDataPtr++;
207 *imagTimeDomainPointsPtr++ = *complexTimeDomainDataPtr++;
212 // If bufferSize is zero, then just update the display by sending over the old data
214 bufferSize = _lastDataPointCount;
215 repeatDataFlag = true;
218 // Since there is data this time, update the count
219 _lastDataPointCount = bufferSize;
222 const timespec currentTime = get_highres_clock();
223 const timespec lastUpdateGUITime = GetLastGUIUpdateTime();
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++;
231 IncrementPendingGUIUpdateEvents();
232 qApp->postEvent(_spectrumDisplayForm, new SpectrumUpdateEvent(_fftPoints, bufferSize, _realTimeDomainPoints, _imagTimeDomainPoints, timeDomainBufferSize, timePerFFT, timestamp, repeatDataFlag, lastOfMultipleFFTUpdateFlag, currentTime, _droppedEntriesCount));
234 // Only reset the dropped entries counter if this is not repeat data since repeat data is dropped by the display systems
236 _droppedEntriesCount = 0;
239 //qApp->wakeUpGuiThread();
243 float SpectrumGUIClass::GetPowerValue()const{
244 float returnValue = 0;
245 //_windowStateLock->Lock();
246 returnValue = _powerValue;
247 //_windowStateLock->Unlock();
251 void SpectrumGUIClass::SetPowerValue(const float value){
252 //_windowStateLock->Lock();
254 //_windowStateLock->Unlock();
257 int SpectrumGUIClass::GetWindowType()const{
259 //_windowStateLock->Lock();
260 returnValue = _windowType;
261 //_windowStateLock->Unlock();
265 void SpectrumGUIClass::SetWindowType(const int newType){
266 //_windowStateLock->Lock();
267 _windowType = newType;
268 //_windowStateLock->Unlock();
271 int SpectrumGUIClass::GetFFTSize()const{
273 //_windowStateLock->Lock();
274 returnValue = _fftSize;
275 //_windowStateLock->Unlock();
279 int SpectrumGUIClass::GetFFTSizeIndex()const{
280 int fftsize = GetFFTSize();
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;
292 void SpectrumGUIClass::SetFFTSize(const int newSize){
293 //_windowStateLock->Lock();
295 //_windowStateLock->Unlock();
298 timespec SpectrumGUIClass::GetLastGUIUpdateTime()const{
299 timespec returnValue;
300 //_windowStateLock->Lock();
301 returnValue = _lastGUIUpdateTime;
302 //_windowStateLock->Unlock();
306 void SpectrumGUIClass::SetLastGUIUpdateTime(const timespec newTime){
307 //_windowStateLock->Lock();
308 _lastGUIUpdateTime = newTime;
309 //_windowStateLock->Unlock();
312 unsigned int SpectrumGUIClass::GetPendingGUIUpdateEvents()const{
313 unsigned int returnValue = 0;
314 //_windowStateLock->Lock();
315 returnValue = _pendingGUIUpdateEventsCount;
316 //_windowStateLock->Unlock();
320 void SpectrumGUIClass::IncrementPendingGUIUpdateEvents(){
321 //_windowStateLock->Lock();
322 _pendingGUIUpdateEventsCount++;
323 //_windowStateLock->Unlock();
326 void SpectrumGUIClass::DecrementPendingGUIUpdateEvents(){
327 //_windowStateLock->Lock();
328 if(_pendingGUIUpdateEventsCount > 0){
329 _pendingGUIUpdateEventsCount--;
331 //_windowStateLock->Unlock();
334 void SpectrumGUIClass::ResetPendingGUIUpdateEvents(){
335 //_windowStateLock->Lock();
336 _pendingGUIUpdateEventsCount = 0;
337 //_windowStateLock->Unlock();
341 #endif /* SPECTRUM_GUI_CLASS_CPP */