-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrtvtchannelwidget.cpp
605 lines (517 loc) · 18.2 KB
/
rtvtchannelwidget.cpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include "rtvtchannelwidget.h"
#include "rtvtmainwindow.h"
#include "External-Code/fftreal_wrapper.h"
#include <QKeyEvent>
RTVTChannelWidget::RTVTChannelWidget(QWidget *parent, int inAssignedChannel, int inWidgetNumber, int viewType)
{
this->channelData = (unsigned char *) malloc (sizeof(unsigned char) * CHANNEL_BUFFER_LENGTH);
this->filteredChannelData = (double *) malloc(sizeof(double) * CHANNEL_BUFFER_LENGTH);
this->numberOfChannelsInData = DEFAULT_NUM_CHANNELS_IN_DATA;
if(viewType)
this->usesContinuousView = true;
else
this->usesContinuousView = false;
this->assignedChannel = inAssignedChannel;
this->assignedWidgetNumber = inWidgetNumber;
this->nextWriteLocation = 0;
this->nextFilterWriteLocation = 0;
this->theMean = 0;
this->isSelected = false;
this->sampleFrameLength = SAMPLE_FRAME_LENGTH;
this->isFiltering = false;
this->channelTimer = new QTimer();
this->viewRedrawTimer = new QTimer();
connect(this->viewRedrawTimer, SIGNAL(timeout()), this, SLOT(viewRedrawTimerTimeout()));
this->setBackgroundRole(QPalette::NoRole);
this->setAttribute(Qt::WA_OpaquePaintEvent, true);
this->setAutoFillBackground(false);
this->setParent(parent);
/*** Views ***/
horizontalLayout = new QHBoxLayout();
if(this->usesContinuousView)
continuousViewer = new RTVTContinuousViewerWidget(50, assignedChannel, this, "cViewer");
waveViewer = new RTVTWaveViewerGLWidget(50, assignedChannel, this, "wViewer");
timerValueLabel = new QLabel("0");
timerValueLabel->setText("<font color='white'>0</font>");
connect(this->channelTimer, SIGNAL(timeout()), this, SLOT(channelTimeout()));
//connect(this->channelTimer, SIGNAL(value()), this->timerValueLabel, SLOT(text()));
horizontalLayout->setSpacing(0);
horizontalLayout->setContentsMargins(0,0,0,0);
horizontalLayout->addSpacing(11);
horizontalLayout->addWidget(waveViewer);
if(this->usesContinuousView)
{
horizontalLayout->addSpacing(2);
horizontalLayout->addWidget(continuousViewer);
}
//horizontalLayout->addWidget(timerValueLabel);
this->isPlaying = false;
connect(this->waveViewer, SIGNAL(iHaveWaveformsToWrite(waveformDatum)), this, SLOT(childHasWaveformsToWrite(waveformDatum)));
connect(this->waveViewer, SIGNAL(waveViewEditedTrigger(hoop)), this, SLOT(childHasEditedTrigger(hoop)));
connect(this->waveViewer, SIGNAL(passTriggerToOtherWaveViewers(hoop)), this, SLOT(passTriggerUp(hoop)));
this->setLayout(horizontalLayout);
}
RTVTChannelWidget::~RTVTChannelWidget()
{
if(channelData !=0)
//delete channelData;
if(filteredChannelData != 0)
//delete filteredChannelData;
delete waveViewer;
if(this->usesContinuousView)
delete continuousViewer;
delete channelTimer;
delete viewRedrawTimer;
delete horizontalLayout;
delete timerValueLabel;
}
void RTVTChannelWidget::keyPressEvent( QKeyEvent *e )
{
switch(e->key())
{
case Qt::Key_Escape:
close();
break;
case Qt::Key_0:
if(!waveViewer->lineSmoothingEnabled)
waveViewer->enableLineSmoothing(true);
else
waveViewer->enableLineSmoothing(false);
if(this->usesContinuousView)
{
if(!continuousViewer->lineSmoothingEnabled)
continuousViewer->enableLineSmoothing(true);
else
continuousViewer->enableLineSmoothing(false);
}
break;
case Qt::Key_T:
waveViewer->keyPressEvent(e);
break;
case Qt::Key_M:
waveViewer->keyPressEvent(e);
break;
case Qt::Key_Y:
if(!waveViewer->setYScaleMode)
waveViewer->setYScaleMode = true;
else
waveViewer->setYScaleMode = false;
if(this->usesContinuousView)
{
if(!continuousViewer->setYScaleMode)
continuousViewer->setYScaleMode = true;
else
continuousViewer->setYScaleMode = false;
}
break;
case Qt::Key_D:
waveViewer->timeWindowInS += waveViewer->timeWindowInS/10;
break;
case Qt::Key_S:
waveViewer->yPeakToPeak += waveViewer->yPeakToPeak/10;
break;
case Qt::Key_A:
waveViewer->timeWindowInS -= waveViewer->timeWindowInS/10;
break;
case Qt::Key_W:
waveViewer->yPeakToPeak -= waveViewer->yPeakToPeak/10;
break;
case Qt::Key_G:
waveViewer->keyPressEvent(e);
break;
case Qt::Key_Up:
if(this->usesContinuousView)
continuousViewer->baseline += 10;
break;
case Qt::Key_Down:
if(this->usesContinuousView)
continuousViewer->baseline -= 10;
break;
case Qt::Key_Left:
if(this->usesContinuousView)
{
if(continuousViewer->timeWindowInS > 1.0)
continuousViewer->timeWindowInS -= 0.5;
else
continuousViewer->timeWindowInS = 1.0;
}
break;
case Qt::Key_Right:
if(this->usesContinuousView)
{
if(continuousViewer->timeWindowInS < 20.5)
continuousViewer->timeWindowInS += 0.5;
else
continuousViewer->timeWindowInS = 20.0;
}
break;
case Qt::Key_Z:
if(this->usesContinuousView)
continuousViewer->yPeakToPeak += continuousViewer->yPeakToPeak/10;
break;
case Qt::Key_X:
if(this->usesContinuousView)
continuousViewer->yPeakToPeak -= continuousViewer->yPeakToPeak/10;
break;
case Qt::Key_I:
if(this->usesContinuousView)
{
if(continuousViewer->showInformation)
continuousViewer->showInformation = false;
else
continuousViewer->showInformation = true;
}
break;
default:
break;
}
}
void RTVTChannelWidget::play()
{
this->channelTimer->setInterval(100);
this->channelMSCount = 0;
this->channelTimer->start(100);
this->viewRedrawTimer->setInterval(3000);
this->timerValueLabel->setText("0");
this->isPlaying = true;
this->waveViewer->isPlaying = true;
this->waveViewer->drawingFromData = true;
this->waveViewer->play();
if(this->usesContinuousView)
{
this->continuousViewer->isPlaying = true;
this->continuousViewer->drawingFromData = true;
this->continuousViewer->play();
}
}
void RTVTChannelWidget::stop()
{
this->waveViewer->stop();
QString text = QString::number(this->channelMSCount);
QString fonttemplate = tr("<font color='white'>%1</font>");
this->channelTimer->stop();
this->timerValueLabel->setText(fonttemplate.arg(text));
this->isPlaying = false;
this->waveViewer->isPlaying = false;
if(this->usesContinuousView)
{
this->continuousViewer->stop();
this->continuousViewer->isPlaying = false;
}
}
void RTVTChannelWidget::resetReadPointer()
{
this->waveViewer->readFromChannelDataPtr = 0;
this->waveViewer->readFromChannelFilterDataPtr = 0;
this->waveViewer->findStartPosition();
if(this->usesContinuousView)
{
this->continuousViewer->readFromChannelFilterDataPtr = 0;
this->continuousViewer->readFromChannelDataPtr = 0;
this->continuousViewer->findStartPosition();
this->continuousViewer->baseline = 4600;
}
// Why do i have these baselines?
this->waveViewer->baseline = 4450;
}
void RTVTChannelWidget::setDrawFromData(bool isDrawing)
{
this->waveViewer->drawingFromData = isDrawing;
if(this->usesContinuousView)
this->continuousViewer->drawingFromData = isDrawing;
}
void RTVTChannelWidget::updateNextWriteLocationWithAddition(unsigned int bytesWritten)
{
if(this->nextWriteLocation > (CHANNEL_BUFFER_LENGTH - bytesWritten))
{
this->nextWriteLocation = 0;
//reset continuous here too? Should all the viewers reference the same ptr? probably...
this->waveViewer->readFromChannelDataPtr = 0;
if(this->usesContinuousView)
{
this->continuousViewer->readFromChannelDataPtr = 0;
this->continuousViewer->readFromChannelFilterDataPtr = 0;
}
}
else
{
this->nextWriteLocation += bytesWritten;
// Make sure we continue parsing data
this->waveViewer->waitUntilNextTransfer = false;
if(this->usesContinuousView)
this->continuousViewer->waitUntilNextTransfer = false;
}
}
void RTVTChannelWidget::updateNextFilterWriteLocationWithAddition(unsigned int lengthWritten)
{
if(this->nextFilterWriteLocation > (CHANNEL_BUFFER_LENGTH/2 - lengthWritten))
{
this->nextFilterWriteLocation = 0;
//reset continuous here too? Should all the viewers reference the same ptr? probably...
this->waveViewer->readFromChannelFilterDataPtr = 0;
if(this->usesContinuousView)
this->continuousViewer->readFromChannelFilterDataPtr = 0;
}
else
{
this->nextFilterWriteLocation += lengthWritten;
// Make sure we continue parsing data
this->waveViewer->waitUntilNextTransfer = false;
if(this->usesContinuousView)
this->continuousViewer->waitUntilNextTransfer = false;
}
}
void RTVTChannelWidget::updateMean(double inMean)
{
theMean = ((float)inMean + theMean)/2;
//this->waveViewer->localMean = theMean;
//this->continuousViewer->localMean = theMean;
}
unsigned int RTVTChannelWidget::getNextFilterWriteLocation()
{
if(this->nextFilterWriteLocation > (CHANNEL_BUFFER_LENGTH/2 - BACK_BUFFER_LENGTH/2/numberOfChannelsInData))
{
return 0;
}
else
return this->nextFilterWriteLocation;
}
unsigned int RTVTChannelWidget::getNextWriteLocation()
{
if(this->nextWriteLocation > (CHANNEL_BUFFER_LENGTH - BACK_BUFFER_LENGTH/numberOfChannelsInData))
{
return 0;
}
else
return this->nextWriteLocation;
}
int RTVTChannelWidget::calculateFFTWithLength(unsigned int numberOfAverages, QVector<qreal>& amplitude, QVector<qreal>& frequency)
{
#ifdef USE_QWT_PLOT
m_fft = new FFTRealWrapper();
int intSampleRate = SYSTEM_CLOCK_RATE/SAMPLE_FRAME_LENGTH/this->numberOfChannelsInData;
int bytesPerSample = 2; //sizeof(unsigned char)*
unsigned int numberOfSamples = SpectrumLengthSamples;
// Divide into X sections
unsigned int nSections = numberOfAverages;
unsigned int lengthRecorded = this->nextWriteLocation/bytesPerSample;
// Check if we have enough data
if(lengthRecorded/nSections < numberOfSamples)
{
amplitude.fill(0.0,numberOfSamples);
frequency.fill(0.0,numberOfSamples);
return 1;
}
// Else continue to calculate and return vectors
for(unsigned int s=0;s<nSections;s++)
{
QVector<float> inputToFFT(numberOfSamples, 0.0);
QVector<float> outputFromFFT(numberOfSamples, 0.0);
// init window
m_window = QVector<float>(numberOfSamples, 0.0);
this->calculateWindow();
//Q_ASSERT(lengthRecorded == m_numSamples * bytesPerSample);
// Initialize data array
const unsigned char *ptr = this->channelData;
const unsigned int dataOffset = s*numberOfSamples;
ptr += dataOffset;
for (unsigned int i=0; i<numberOfSamples; ++i) {
const qint16 pcmSample = *reinterpret_cast<const qint16*>(ptr);
// Scale down to range [-1.0, 1.0]
const float realSample = pcmToReal(pcmSample);
const float windowedSample = realSample * m_window[i];
inputToFFT[i] = windowedSample;
ptr += bytesPerSample;
}
// Calculate the FFT
m_fft->calculateFFT(outputFromFFT.data(), inputToFFT.data());
//QVector<qreal> frequencyV(SpectrumLengthSamples,0.0);
//QVector<qreal> amplitudeV(SpectrumLengthSamples,0.0);
for (unsigned int i=2; i<=numberOfSamples/2; ++i) {
const qreal real = outputFromFFT[i];
qreal imag = 0.0;
if (i>0 && i<numberOfSamples/2)
imag = outputFromFFT[numberOfSamples/2 + i];
const qreal magnitude = sqrt(real*real + imag*imag);
if(s==0)
{
amplitude << 10*log(magnitude); // should this be 20?
frequency << qreal(i * intSampleRate) / (numberOfSamples);
}
else
{
amplitude[i-2] = (amplitude[i-2] + 10*log(magnitude))/2;
//frequency[i-2] = (frequency[i-2] + qreal(i * intSampleRate) / (numberOfSamples))/2;
}
}
}
#endif
return 0;
}
void RTVTChannelWidget::calculateWindow()
{
int m_windowFunction = 1;
int m_numSamples = SpectrumLengthSamples;
for (int i=0; i<m_numSamples; ++i) {
DataType x = 0.0;
switch (m_windowFunction) {
case 0:
x = 1.0;
break;
case 1:
x = 0.5 * (1 - qCos((2 * M_PI * i) / (m_numSamples - 1)));
break;
default:
Q_ASSERT(false);
}
m_window[i] = x;
}
}
void RTVTChannelWidget::updateChannelViews()
{
this->waveViewer->viewNeedsRedraw = true;
//this->waveViewer->update();
if(this->usesContinuousView)
this->continuousViewer->viewNeedsRedraw = true;
//this->continuousViewer->update();
}
void RTVTChannelWidget::channelTimeout()
{
this->channelMSCount++;
this->channelTimer->start(100);
}
void RTVTChannelWidget::viewRedrawTimerTimeout()
{
if(this->usesContinuousView)
this->continuousViewer->viewNeedsRedraw = true;
this->viewRedrawTimer->start();
}
void RTVTChannelWidget::resizeEvent (QResizeEvent *)
{
// Set needs redraw
if(this->usesContinuousView)
this->continuousViewer->viewNeedsRedraw = true;
this->update();
}
void RTVTChannelWidget::childHasWaveformsToWrite(waveformDatum datum)
{
// Pass to parent to write with singular writer
//QGridLayout *test = (QGridLayout *)this->parent();
//RTVTMainWindow *test2 = (RTVTMainWindow *)test->parent();
// Maybe want to have a specific pointer to the mainwindow instead of parent tree...
//waveformDatum testDatum;
((RTVTMainWindow*)(this->parent()->parent()))->writeWaveformsToFile(datum);
}
void RTVTChannelWidget::paintEvent(QPaintEvent * event )
{
Q_UNUSED(event);
QPainter painter(this);
QFont f = QFont("Helvetica [Cronyx]",7,1,false);
QFontMetrics fm(f);
QPen pen;
// Clear the old drawing
QRectF clearRect = QRect(0, 0, 11, height());
painter.fillRect(clearRect,QColor(0,0,0));
if(this->isSelected)
{
// Draw selected channel region
QRectF frameRect = QRect(0, 0, 11, height()-5);
painter.fillRect(frameRect,QColor(0,255,0));
pen = QPen(QBrush(QColor(0,0,0),Qt::SolidPattern),0.2,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
}
else
{
// Draw selected channel region
QRectF frameRect = QRect(0, 0, 11, height()-5);
painter.fillRect(frameRect,QColor(100,100,100));
pen = QPen(QBrush(QColor(255,255,255),Qt::SolidPattern),0.2,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
}
painter.setPen(pen);
// Display assigned channel number and widget number
QString widgetNumberString = QString::number(this->assignedWidgetNumber);
painter.setFont(f);
painter.drawText(3,fm.height(), widgetNumberString.at(0));
if(widgetNumberString.length() >1)
painter.drawText(3,fm.height()*2, widgetNumberString.at(1));
if(widgetNumberString.length() >2)
painter.drawText(3,fm.height()*3, widgetNumberString.at(2));
QString channelNumberString = QString::number(this->assignedChannel);
painter.setFont(f);
if(channelNumberString.length() > 1)
{
if(channelNumberString.length() > 2)
{
painter.drawText(3,height()-fm.height()*3, channelNumberString.at(0));
painter.drawText(3,height()-fm.height()*2, channelNumberString.at(1));
painter.drawText(3,height()-fm.height(), channelNumberString.at(2));
}
else
{
painter.drawText(3,height()-fm.height()*2, channelNumberString.at(0));
painter.drawText(3,height()-fm.height(), channelNumberString.at(1));
}
}
else
painter.drawText(3,height()-fm.height(), channelNumberString.at(0));
}
void RTVTChannelWidget::childHasEditedTrigger(hoop theTrigger)
{
QList<hoop> currentListOfTriggers = ((RTVTMainWindow*)(this->parent()->parent()))->theWaveTableModel->getTriggerList();
for (int i=0; i<currentListOfTriggers.count(); i++)
{
if((currentListOfTriggers.value(i).channel == theTrigger.channel) && (currentListOfTriggers.value(i).triggerNumber == theTrigger.triggerNumber)) // Found match
{
currentListOfTriggers.replace(i, theTrigger);
break;
}
}
((RTVTMainWindow*)(this->parent()->parent()))->theWaveTableModel->setTriggerList(currentListOfTriggers);
//((RTVTMainWindow*)(this->parent()->parent()))->rtvtWaveHUD->triggeredWaveformDataTable->update()
}
void RTVTChannelWidget::setNumberOfChannelsInData(int numChannels)
{
this->numberOfChannelsInData = numChannels;
}
void RTVTChannelWidget::passTriggerUp(hoop theTrigger)
{
foreach (RTVTChannelWidget *cw, ((RTVTMainWindow*)(this->parent()->parent()))->channelWidgetList)
{
// Just copy over the needed parts of the trigger to avoid cross-display
cw->waveViewer->triggers[this->waveViewer->selectedTrigger].top = theTrigger.top;
cw->waveViewer->triggers[this->waveViewer->selectedTrigger].bottom = theTrigger.bottom;
cw->waveViewer->triggers[this->waveViewer->selectedTrigger].sampleNumberToTest = theTrigger.sampleNumberToTest;
//cw->waveViewer->triggers[this->waveViewer->selectedTrigger].tickOfSampleCrossing = theTrigger.tickOfSampleCrossing;
}
}
void RTVTChannelWidget::setChannelSelected(bool status)
{
this->isSelected = status;
update();
}
void RTVTChannelWidget::setAddressScheme( unsigned int inWireAddressing)
{
if(inWireAddressing == 2)
{
this->sampleFrameLength = TWO_WIRE_SAMPLE_FRAME_LENGTH;
this->waveViewer->sampleRate = SYSTEM_CLOCK_RATE/TWO_WIRE_SAMPLE_FRAME_LENGTH/this->numberOfChannelsInData;
if(this->usesContinuousView)
this->continuousViewer->sampleRate = SYSTEM_CLOCK_RATE/TWO_WIRE_SAMPLE_FRAME_LENGTH/this->numberOfChannelsInData;
}
else if(inWireAddressing == 4)
{
this->sampleFrameLength = FOUR_WIRE_SAMPLE_FRAME_LENGTH;
this->waveViewer->sampleRate = SYSTEM_CLOCK_RATE/FOUR_WIRE_SAMPLE_FRAME_LENGTH/this->numberOfChannelsInData;
if(this->usesContinuousView)
this->continuousViewer->sampleRate = SYSTEM_CLOCK_RATE/FOUR_WIRE_SAMPLE_FRAME_LENGTH/this->numberOfChannelsInData;
}
}
void RTVTChannelWidget::mousePressEvent(QMouseEvent *event)
{
if(this->isSelected)
event->ignore();
else
{
((RTVTMainWindow*)(this->parent()->parent()))->setSelectedWidget(this->assignedWidgetNumber);
event->ignore();
}
}