-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrtvtwaveviewerglwidget.cpp
1382 lines (1162 loc) · 54.9 KB
/
rtvtwaveviewerglwidget.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "rtvtwaveviewerglwidget.h"
#include "rtvtchannelwidget.h"
#include "RTVTTimer.h"
#include <qtimer.h>
#include <QKeyEvent>
#include <QtOpenGL>
#include <QtGui>
#include <iostream>
extern RTVTTimer *theTime;
#define WAVE_BYTE_LENGTH 128
//#define MAX_WAVE_PER_TRIGGER 500
#define ADC_FACTOR (ADC_RANGE/4096)
#define DRAW_GL_TEXT 0
#define AMP_TO_UV 1000 // Multiplier for amp display values to be in uV
RTVTWaveViewerGLWidget::RTVTWaveViewerGLWidget(int timerInterval, int viewerChannel, QWidget *parent, QString name)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
Q_UNUSED(name);
// Draw widget boarder for reference
this->setStyleSheet("border-width: 1px; border-color: #000000;");
//this->setMinimumSize(150, 100);
this->lineSmoothingEnabled = false;
this->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding);
this->setParent(parent);
//// initialize some variables
// Viewport setup and calibration
fov = GLfloat(45.0);
zFromCamera = GLfloat(-10.0);
interSampleLength = GLfloat(0.0);
// Initialize the QVariant MetaType for waveformDatum
int id = qRegisterMetaType<waveformDatum>();
// Initialize the QVariant MetaType for waveformDatum
int id2 = qRegisterMetaType<hoop>();
// Input data structure
sampleRate = SYSTEM_CLOCK_RATE/SAMPLE_FRAME_LENGTH/DEFAULT_NUM_CHANNELS_IN_DATA;
timeWindowInS = 0.0016f;
numberOfSamples = (int)(sampleRate*timeWindowInS);
this->drawingFromData = false;
this->needsRedraw = false;
this->assignedViewerChannel = viewerChannel;
this->yPeakToPeak = 0.003f; // 300 uV
this->yDragOrigin = 0;
this->xDragOrigin = 0;
this->smooth = false;
this->setThresholdMode = false;
this->setYScaleMode = false;
this->thresholdPP = 0.0;
this->readFromDrawBufferFramePtr = 0;
this->readFromChannelDataPtr = 0;
this->readFromChannelFilterDataPtr = 0;
this->xScaleFactor = 1.0;
this->readStartPositionForChannel = 0;
this->isPlaying = false;
this->baseline = 4096; // 4096 = 2.4 v once channel number is removed.
this->readByteOffset = 0;
this->viewNeedsRedraw = true;
this->thresholdReadPtr = 0;
this->testMark = 0;
this->adcFactor = (float)2.4/4096;
this->ampGain = 200;
this->waitUntilNextTransfer = false;
this->selectedTrigger = -1;
this->numberOfActiveTriggers = 2;
this->selectedTriggerType = 0;
this->localMean = 0;
this->drawingMean = 0;
this->meanCounter = 0; // how many samples before next mean check
this->meanCheckPoint = 400; // number of samples to go before checking
this->numberOfWaveLengthsToCheck = 20;
this->mouseIsDown = false;
this->shouldUpdateTextDisplay = false; // Just the initial drawing - no need to draw right away
this->propogatesChangesInThreshold = false;
this->numberOfSamplesPerWave = (int)ceil(SYSTEM_CLOCK_RATE/DEFAULT_NUM_CHANNELS_IN_DATA/SAMPLE_FRAME_LENGTH*this->timeWindowInS);
this->numberOfSamplesBeforeTrigger = (int)ceil((float)(numberOfSamplesPerWave/3)); // copy 1/3 number samples before triggered point
this->numberOfSamplesAfterTrigger = (int)(numberOfSamplesPerWave-numberOfSamplesBeforeTrigger); // copy
this->waveByteLength = numberOfSamplesPerWave*2;
this->drawThresholds = true;
// Setup a trigger hoop for initial testing
this->BASE_TRIGGER_CONTROL_POINT_X1 = 0.5;
this->BASE_TRIGGER_CONTROL_POINT_Y1 = (GLfloat)0.1;
this->TRIGGER_CONTROL_POINT_X1 = (GLfloat)0.1;
this->TRIGGER_CONTROL_POINT_Y1 = 0.5;
this->triggers[0].bottom = -0.00005f;
this->triggers[0].top = 0.00005f;
this->triggers[0].nextWaveNumber = 0;
this->triggers[0].numberOfWaves = 0;
this->triggers[0].r = 0.6f;
this->triggers[0].g = 0.6f;
this->triggers[0].b = 0.6f;
this->triggers[0].channel = this->assignedViewerChannel;
this->triggers[0].triggerNumber = 0;
this->triggers[0].sampleNumberToTest = 1; // needed a placehold for this variable since not used in this trigger
this->triggers[1].bottom = -0.00020f;
this->triggers[1].top = 0.00020f;
this->triggers[1].nextWaveNumber = 0;
this->triggers[1].numberOfWaves = 0;
this->triggers[1].r = 1.0f;
this->triggers[1].g = 1.0f;
this->triggers[1].b = 0.1f;
this->triggers[1].sampleNumberToTest = this->numberOfSamples/3;
this->triggers[1].channel = this->assignedViewerChannel;
this->triggers[1].triggerNumber = 1;
/*
this->triggers[2].bottom = -0.00020;
this->triggers[2].top = 0.00020;
this->triggers[2].nextWaveNumber = 0;
this->triggers[2].numberOfWaves = 0;
this->triggers[2].r = 0.1;
this->triggers[2].g = 1.0;
this->triggers[2].b = 0.1;
this->triggers[2].sampleNumberToTest = 48;
this->triggers[2].channel = this->assignedViewerChannel;
this->triggers[2].triggerNumber = 2;
*/
// set a timer for text updates because they take a LONG time in GL world
this->textUpdateTimer = new QTimer(this);
connect(textUpdateTimer, SIGNAL(timeout()), this, SLOT(tellTextToUpdate()) );
this->triggerThreshold = 0.001f;
///// Triggers Thread
// create a QFuture and a QFutureWatcher
triggersFuture = new QFuture<void>;
triggersWatcher = new QFutureWatcher<void>;
// Set a timer for trigger checks
triggerTimer = new QTimer(this);
connect(triggerTimer, SIGNAL(timeout()), this, SLOT(runThreadedTiggers()) );
// display a message box when the calculation has finished
connect(triggersWatcher, SIGNAL(finished()), this, SLOT(triggersThreadFinishedRunning()));
//QString temp = QString("testNEV_" + this->assignedViewerChannel + ".nev");
//char fileName;
//int err = temp.toWCharArray(&fileName);
//this->nevWriter = bNEVwr("testNEV.nev",2,"Single E","Sample Comments");
//this->nevWriter.setupCh(1,2);
//this->nevWriter.setupCh(107,3);
if( timerInterval == 0 )
glRedrawTimer = 0;
else
{
glRedrawTimer = new QTimer( this );
connect( glRedrawTimer, SIGNAL(timeout()), this, SLOT(timeOutSlot()) );
}
//connect(this,SIGNAL(iWasDoubleClicked(int)),this->parentWidget(),SLOT(viewWasDoubleClicked(int)));
}
RTVTWaveViewerGLWidget::~RTVTWaveViewerGLWidget()
{
delete textUpdateTimer;
delete triggersFuture;
delete triggersWatcher;
delete triggerTimer;
delete glRedrawTimer;
}
QSize RTVTWaveViewerGLWidget::sizeHint() const
{
return QSize(75,50);
}
void RTVTWaveViewerGLWidget::keyPressEvent( QKeyEvent *e )
{
// std::cout << "Key Pressed..." << std::endl;
switch(e->key())
{
case Qt::Key_Escape:
close();
break;
/*case Qt::Key_0:
if(!this->lineSmoothingEnabled)
this->enableLineSmoothing(true);
else
this->enableLineSmoothing(false);
break;*/
case Qt::Key_T:
if(!this->setThresholdMode)
{
this->setThresholdMode = true;
//this->triggerTimer->setInterval(300);
this->triggerTimer->start(30);
}
else
{
this->triggerTimer->stop();
this->setThresholdMode = false;
}
break;
case Qt::Key_M:
if(this->textUpdateTimer->isActive())
this->textUpdateTimer->stop();
else
this->textUpdateTimer->start(30);
break;
case Qt::Key_Y:
if(!this->setYScaleMode)
this->setYScaleMode = true;
else
this->setYScaleMode = false;
break;
case Qt::Key_Period:
this->timeWindowInS += 0.0001f;
break;
case Qt::Key_Comma:
this->timeWindowInS -= 0.0001f;
break;
case Qt::Key_G:
this->drawThresholds = !this->drawThresholds;
break;
default:
break;
}
}
void RTVTWaveViewerGLWidget::mousePressEvent(QMouseEvent *event)
{
GLfloat yScaleFactorToVPSpace = this->vpHeight/this->yPeakToPeak;
// For testing the hit rects
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realY,realX;
GLdouble wx, wy, wz;
this->mouseIsDown = true;
// Set the original drag position
this->yDragOrigin = event->pos().y();
this->xDragOrigin = event->pos().x();
// /**** Find out where we clicked - i.e. what object we are on ***/
// Get the cursor and convert to model coordinates
cursorClickLocation = event->pos();
// Convert to model coordinates
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
realY = viewport[3] - (GLint) cursorClickLocation.y() - 1;
realX = viewport[2] - (GLint) cursorClickLocation.x() - 1;
gluUnProject((GLdouble) realX, (GLdouble) realY, 0.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
wx = -wx*100;
wy = wy*100;
// Make the cursor rect with margin
QRectF cursorRect((wx-.1),(wy+.1),0.2,-0.2);
// Iterate through the trigger control points
for(int t=0;t<this->numberOfActiveTriggers;t++)
{
if (t==0)
{
QPointF one(-vpWidth/2 - this->BASE_TRIGGER_CONTROL_POINT_X1, this->triggers[0].top*yScaleFactorToVPSpace + this->BASE_TRIGGER_CONTROL_POINT_Y1);
QPointF two(-vpWidth/2, this->triggers[0].top*yScaleFactorToVPSpace - this->BASE_TRIGGER_CONTROL_POINT_Y1);
/*QRectF baseTriggerControlRect((-vpWidth/2 - TRIGGER_CONTROL_POINT_X1*this->interSampleLength), this->triggers[0].top*yScaleFactorToVPSpace,
TRIGGER_CONTROL_POINT_X1*this->interSampleLength*2, TRIGGER_CONTROL_POINT_Y1*yScaleFactorToVPSpace*2);*/
QRectF baseTriggerControlRect(one,two);
if(cursorRect.intersects(baseTriggerControlRect)) { this->selectedTrigger = t; this->selectedTriggerType = BASE_TRIGGER_CONTROL_TYPE; break;}
}
else
{
// Make control rect
/*QRectF triggerXControlRect(this->triggers[t].sampleNumberToTest*this->interSampleLength - TRIGGER_CONTROL_POINT_X1*this->interSampleLength, -vpHeight/2,
TRIGGER_CONTROL_POINT_X1*this->interSampleLength*2 ,TRIGGER_CONTROL_POINT_Y1*yScaleFactorToVPSpace*2);*/
QPointF one((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - this->TRIGGER_CONTROL_POINT_X1, -vpHeight/2 - this->TRIGGER_CONTROL_POINT_Y1);
QPointF two((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + this->TRIGGER_CONTROL_POINT_X1, -vpHeight/2);
QPointF oneYT((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - this->TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].top*yScaleFactorToVPSpace + this->TRIGGER_CONTROL_POINT_Y1);
QPointF twoYT((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + this->TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].top*yScaleFactorToVPSpace);
QPointF oneYB((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - this->TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].bottom*yScaleFactorToVPSpace - this->TRIGGER_CONTROL_POINT_Y1);
QPointF twoYB((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + this->TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].bottom*yScaleFactorToVPSpace);
QRectF triggerXControlRect(one,two);
QRectF triggerYTopControlRect(oneYT,twoYT);
/*this->triggers[t].sampleNumberToTest*this->interSampleLength - TRIGGER_CONTROL_POINT_X1*this->interSampleLength/2,
this->triggers[t].top*yScaleFactorToVPSpace + TRIGGER_CONTROL_POINT_Y1*yScaleFactorToVPSpace/2,
TRIGGER_CONTROL_POINT_X1*this->interSampleLength ,TRIGGER_CONTROL_POINT_Y1*yScaleFactorToVPSpace);*/
QRectF triggerYBottomControlRect(oneYB,twoYB);
/*this->triggers[t].sampleNumberToTest*this->interSampleLength - TRIGGER_CONTROL_POINT_X1*this->interSampleLength/2,
this->triggers[t].bottom*yScaleFactorToVPSpace + TRIGGER_CONTROL_POINT_Y1*yScaleFactorToVPSpace/2,
TRIGGER_CONTROL_POINT_X1*this->interSampleLength ,TRIGGER_CONTROL_POINT_Y1*yScaleFactorToVPSpace);*/
if(cursorRect.intersects(triggerXControlRect)) { this->selectedTrigger = t; this->selectedTriggerType = X_TRIGGER_CONTROL_TYPE; break;}
else if(cursorRect.intersects(triggerYTopControlRect)) { this->selectedTrigger = t; this->selectedTriggerType = Y_TOP_TRIGGER_CONTROL_TYPE; break;}
else if(cursorRect.intersects(triggerYBottomControlRect)) { this->selectedTrigger = t; this->selectedTriggerType = Y_BOTTOM_TRIGGER_CONTROL_TYPE; break;}
}
// If we got to the end without hitting anything
this->selectedTrigger = -1;
//((RTVTChannelWidget*)this->parent())->setChannelSelected(true);
}
updateGL();
//this->lineSmoothingEnabled = !this->lineSmoothingEnabled;
//std::cout << "Enabled Changed" << std::endl;
}
void RTVTWaveViewerGLWidget::mouseDoubleClickEvent(QMouseEvent *)
{
emit iWasDoubleClicked(this->assignedViewerChannel);
}
void RTVTWaveViewerGLWidget::mouseMoveEvent(QMouseEvent *event)
{
if(this->setYScaleMode)
{
float yScaleDragPosition = (float)event->pos().y();
//this->yPeakToPeak = (float)(yScaleDragPosition/this->yDragOrigin);
//this->lineSmoothingEnabled = !this->lineSmoothingEnabled;
std::cout << "Dragging...::" << yScaleDragPosition << "::" << this->yDragOrigin << "::" << this->yPeakToPeak << std::endl;
}
else if(this->setThresholdMode && this->selectedTrigger != -1)
{
float thresholdDragPosition = (float)event->pos().y();
this->thresholdPP = (float)(thresholdDragPosition/this->yDragOrigin)*1024.0;
this->thresholdOrigin = this->xDragOrigin;
// Get the cursor and convert to model coordinates
cursorClickLocation = event->pos();
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realY, realX;
GLdouble wx, wy, wz;
// Convert to model coordinates
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
realY = viewport[3] - (GLint) cursorClickLocation.y() - 1;
realX = viewport[2] - (GLint) cursorClickLocation.x() - 1;
gluUnProject((GLdouble) realX, (GLdouble) realY, 0.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
wx = -wx*100;
wy = wy*100;
if(this->selectedTriggerType==BASE_TRIGGER_CONTROL_TYPE)
this->triggers[selectedTrigger].top = (wy)*(this->yPeakToPeak/this->vpHeight); // *100 for scale / 2 for moving wave to middle of window
if(this->selectedTriggerType==Y_TOP_TRIGGER_CONTROL_TYPE)
this->triggers[selectedTrigger].top = (wy)*(this->yPeakToPeak/this->vpHeight); // *100 for scale / 2 for moving wave to middle of window
else if(this->selectedTriggerType==Y_BOTTOM_TRIGGER_CONTROL_TYPE)
this->triggers[selectedTrigger].bottom = (wy)*(this->yPeakToPeak/this->vpHeight);
else if(this->selectedTriggerType==X_TRIGGER_CONTROL_TYPE)
{
//if(wx >= 0) {
this->triggers[selectedTrigger].sampleNumberToTest = (int)(wx/this->interSampleLength) + this->numberOfSamplesPerWave/2;//}
//else { this->triggers[selectedTrigger].sampleNumberToTest = ((int)(wx/this->interSampleLength) + 32) ;}
}
//emit waveViewEditedTrigger(this->triggers[selectedTrigger]);
//std::cout << "Dragging...::" << thresholdDragPosition << "::" << this->yDragOrigin << "::" << this->thresholdPP << std::endl;
}
//wait(200);
updateGL();
}
void RTVTWaveViewerGLWidget::mouseReleaseEvent(QMouseEvent *)
{
if(this->setThresholdMode && this->selectedTrigger != -1)
{
this->thresholdOrigin = this->xDragOrigin;
((RTVTChannelWidget*)this->parent())->childHasEditedTrigger(this->triggers[this->selectedTrigger]);
if(this->setThresholdMode && this->selectedTrigger != -1 && this->propogatesChangesInThreshold)
{
emit passTriggerToOtherWaveViewers(this->triggers[this->selectedTrigger]);
}
}
this->mouseIsDown = false;
}
void RTVTWaveViewerGLWidget::timeOut()
{
updateGL();
}
void RTVTWaveViewerGLWidget::timeOutSlot()
{
timeOut();
}
void RTVTWaveViewerGLWidget::initializeGL()
{
glShadeModel(GL_SMOOTH);
char* vendor = (char *) glGetString(GL_VENDOR);
qDebug(" OpenGL Vendor: %s\n",vendor);
char* renderer = (char *) glGetString(GL_RENDERER);
qDebug(" OpenGL Renderer: %s\n",renderer);
char* version = (char *) glGetString(GL_VERSION);
qDebug(" OpenGL Version: %s\n",version);
char* exts = (char *) glGetString(GL_EXTENSIONS);
qDebug(" OpenGL Extensions: \n%s\n",exts);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void RTVTWaveViewerGLWidget::resizeGL( int width, int height )
{
this->viewNeedsRedraw = true; // will need to redraw after adjustments
//* Shold maybe fix the aspect ratio
//std::cout << "Width: " << width << " Height: " << height << std::endl;
height = height?height:1;
glViewport( 0, 0, (GLint)width, (GLint)height );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(this->fov,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void RTVTWaveViewerGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Translate viewport to be in view of camera (-10f from center of camera)
glTranslatef(0.0f,0.0f,this->zFromCamera);
// Calculate and store the space
this->aspectRatio = (GLfloat)this->height()/(GLfloat)this->width();
this->vpWidth = -tan(fov/2)*(this->zFromCamera)/aspectRatio;
this->vpHeight = -tan(fov/2)*(this->zFromCamera);
this->numberOfSamples = (int)(this->sampleRate*this->timeWindowInS);
this->interSampleLength = this->vpWidth/this->numberOfSamples;
// Setup some variables for drawing properly
GLfloat yScaleFactorToVPSpace = this->vpHeight/this->yPeakToPeak;
// Draw Axis
GLint factor = 2; // Stippling factor
GLushort pattern = 0x5555; // Stipple pattern
//glDisable(GL_BLEND);
glEnable(GL_LINE_STIPPLE);
glLineStipple(factor,pattern);
glBegin(GL_LINES);
glVertex2f(-vpWidth/2, 0.0f);
glVertex2f(vpWidth/2, 0.0f);
glEnd();
glBegin(GL_LINES);
glVertex2f(-vpWidth/2, -vpHeight/2);
glVertex2f(-vpWidth/2, vpHeight/2);
glEnd();
glBegin(GL_LINES);
glVertex2f(vpWidth/2, -vpHeight/2);
glVertex2f(vpWidth/2, vpHeight/2);
glEnd();
// grid
glDisable(GL_LINE_STIPPLE);
glColor4f(0.0f,0.1f,1.1f,0.2f);
for(int g=0;g<=8;g++)
{
glBegin(GL_LINES);
glVertex2f(-vpWidth/2, (vpHeight/2)-(vpHeight/8)*g);
glVertex2f(vpWidth/2, (vpHeight/2)-(vpHeight/8)*g);
glEnd();
}
for(int g=0;g<=8;g++)
{
glBegin(GL_LINES);
glVertex2f(vpWidth/2 -(vpWidth/8)*g, -vpHeight/2);
glVertex2f(vpWidth/2 -(vpWidth/8)*g, vpHeight/2);
glEnd();
}
// Draw threshold
if(this->setYScaleMode)
glColor3f(0.1f,1.0f,0.1f);
// Render text
glColor3f(1.0f,1.0f,1.0f);
QFont font;
font.setPointSize(7);
QFontMetrics fm( font );
// Mixed precision here - need to fix
//DEBUG
if(this->shouldUpdateTextDisplay)
{
// multiplying by 1000 to show numbers in uV
if(((RTVTChannelWidget *)this->parent())->isFiltering)
{
renderText((float)-vpWidth/2,(float)vpHeight/2+((float)fm.height()/20*5/8),0.0,QString::number(this->readFromChannelFilterDataPtr),font);
renderText((float)0.0,(float)vpHeight/2+((float)fm.height()/20*5/8),0.0,QString::number(((RTVTChannelWidget *)this->parent())->nextFilterWriteLocation),font);
}
else
{
renderText((float)-vpWidth/2,(float)vpHeight/2+((float)fm.height()/20*5/8),0.0,QString::number(this->readFromChannelDataPtr/sizeof(unsigned char)),font);
renderText((float)0.0,(float)vpHeight/2+((float)fm.height()/20*5/8),0.0,QString::number(((RTVTChannelWidget *)this->parent())->nextWriteLocation/sizeof(unsigned char)),font);
}
renderText((float)-vpWidth/2 - (vpWidth/32)*5,(float)vpHeight/2 - ((float)fm.height()/20*5/8),0.0,QString::number(this->yPeakToPeak/2*this->ampGain*AMP_TO_UV*CONTROLLER_MULTIPLIER,'f',1),font);
renderText((float)-vpWidth/2 - (vpWidth/32)*5,(float)0.0,0,QString::number(this->localMean*this->ampGain*AMP_TO_UV,'f',3),font);
renderText((float)-vpWidth/2 - (vpWidth/32)*6,-(float)vpHeight/2,0.0,QString::number(-(this->yPeakToPeak/2*this->ampGain)*AMP_TO_UV*CONTROLLER_MULTIPLIER,'f',1),font);
renderText((float)vpWidth/2 + (vpWidth/32),0,0,QString::number(timeWindowInS*1000,'f',2),font);
renderText((float)vpWidth/2 + (vpWidth/32),(float)this->triggers[0].top*yScaleFactorToVPSpace,0.0,QString::number((this->triggers[0].top*this->ampGain)*AMP_TO_UV*CONTROLLER_MULTIPLIER,'f',1),font);
renderText((float)-vpWidth/2 - (vpWidth/32)*5,(float)-vpHeight/2 + fm.height()/2 - vpHeight/8,0,QString::number((this->yPeakToPeak/8)*this->ampGain*AMP_TO_UV*CONTROLLER_MULTIPLIER,'f',1),font);
this->shouldUpdateTextDisplay = false;
}
glDisable(GL_LINE_STIPPLE);
this->viewNeedsRedraw = false;
// Draw text from the drawBuffer
if(drawingFromData)
{
// Draw from this channel pointer on to end of window
GLfloat xPoint = -(float)vpWidth/2;
float waveMean = 0.0;
GLfloat vertices[DEFAULT_NUMBER_OF_SAMPLES_PER_WAVE*2];
glColor3f(1.0f,1.0f,1.0f);
// Loop through triggers and draw waveforms
for(int t=0;t<this->numberOfActiveTriggers;t++)
{
// Set color of waveform base on trigger
glColor3f((this->triggers[t].r),(this->triggers[t].g),(this->triggers[t].b));
// Loop through waves in trigger
for(int w=0;w<this->triggers[t].numberOfWaves;w++)
{
// Attempting to use optimized draw procedure...
xPoint = -(float)vpWidth/2; // Reset x location
// Get mean of wave
/*waveMean = 0.0;
for(int z=0;z<NUMBER_OF_SAMPLES_PER_WAVE*2;z+=2)
waveMean += this->triggers[t].waveSample[w][z/2];
waveMean = waveMean / NUMBER_OF_SAMPLES_PER_WAVE*2;*/
for(int s=0;s<this->numberOfSamplesPerWave*2;s+=2)
{
vertices[s] = xPoint;
vertices[s+1] = (this->triggers[t].waveSample[w][s/2] - this->triggers[t].waveSample[w][0])*yScaleFactorToVPSpace;//- this->triggers[t].waveSample[w][0])*yScaleFactorToVPSpace; //- this->drawingMean // this->triggers[t].waveSample[w][16]
xPoint = xPoint + this->interSampleLength;
}
// activate and specify pointer to vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
// draw a waveform
glDrawArrays(GL_LINE_STRIP, 0, this->numberOfSamplesPerWave);
// deactivate vertex arrays after drawing
glDisableClientState(GL_VERTEX_ARRAY);
}
}
}
// Draw the thresholds on top of the waves
if(this->drawThresholds)
{
// Draw threshold 0 which is always a line threshold
// Draw line for threshold
if(this->mouseIsDown && this->selectedTrigger == 0)
{
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset( 1.0f, 1.0f );
}
//glColor3f((this->triggers[0].r),(this->triggers[0].g),(this->triggers[0].b));
glColor3f(1.0f,0.0f,0.0f);
glBegin(GL_LINES);
glVertex2f(-vpWidth/2, this->triggers[0].top*yScaleFactorToVPSpace);
glVertex2f(vpWidth/2, this->triggers[0].top*yScaleFactorToVPSpace);
glEnd();
// Draw control point
glBegin(GL_POLYGON);
glVertex2f(-vpWidth/2 - BASE_TRIGGER_CONTROL_POINT_X1, this->triggers[0].top*yScaleFactorToVPSpace + BASE_TRIGGER_CONTROL_POINT_Y1);
glVertex2f(-vpWidth/2 - BASE_TRIGGER_CONTROL_POINT_X1*1/3, this->triggers[0].top*yScaleFactorToVPSpace + BASE_TRIGGER_CONTROL_POINT_Y1);
glVertex2f(-vpWidth/2, this->triggers[0].top*yScaleFactorToVPSpace);
glVertex2f(-vpWidth/2 - BASE_TRIGGER_CONTROL_POINT_X1*1/3, this->triggers[0].top*yScaleFactorToVPSpace - BASE_TRIGGER_CONTROL_POINT_Y1);
glVertex2f(-vpWidth/2 - BASE_TRIGGER_CONTROL_POINT_X1, this->triggers[0].top*yScaleFactorToVPSpace - BASE_TRIGGER_CONTROL_POINT_Y1);
glEnd();
if(this->mouseIsDown && this->selectedTrigger == 0)
{
glDisable(GL_POLYGON_OFFSET_FILL);
// Draw the borders
glColor3f(1.0f,1.0f,1.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_LINES);
glVertex2f(-vpWidth/2, this->triggers[0].top*yScaleFactorToVPSpace);
glVertex2f(vpWidth/2, this->triggers[0].top*yScaleFactorToVPSpace);
glEnd();
// Draw control point
glBegin(GL_POLYGON);
glVertex2f(-vpWidth/2 - BASE_TRIGGER_CONTROL_POINT_X1, this->triggers[0].top*yScaleFactorToVPSpace + BASE_TRIGGER_CONTROL_POINT_Y1);
glVertex2f(-vpWidth/2 - BASE_TRIGGER_CONTROL_POINT_X1*1/3, this->triggers[0].top*yScaleFactorToVPSpace + BASE_TRIGGER_CONTROL_POINT_Y1);
glVertex2f(-vpWidth/2, this->triggers[0].top*yScaleFactorToVPSpace);
glVertex2f(-vpWidth/2 - BASE_TRIGGER_CONTROL_POINT_X1*1/3, this->triggers[0].top*yScaleFactorToVPSpace - BASE_TRIGGER_CONTROL_POINT_Y1);
glVertex2f(-vpWidth/2 - BASE_TRIGGER_CONTROL_POINT_X1, this->triggers[0].top*yScaleFactorToVPSpace - BASE_TRIGGER_CONTROL_POINT_Y1);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// End Boarder
}
for (int t=1;t<this->numberOfActiveTriggers;t++)
{
glColor3f((this->triggers[t].r),(this->triggers[t].g),(this->triggers[t].b));
if(this->mouseIsDown && this->selectedTrigger == t)
{
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset( 1.0f, 1.0f );
}
// Draw sample number control point
glBegin(GL_POLYGON);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - TRIGGER_CONTROL_POINT_X1, -vpHeight/2 - TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - TRIGGER_CONTROL_POINT_X1, -vpHeight/2 - TRIGGER_CONTROL_POINT_Y1*1/3);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2), -vpHeight/2);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + TRIGGER_CONTROL_POINT_X1, -vpHeight/2 - TRIGGER_CONTROL_POINT_Y1*1/3);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + TRIGGER_CONTROL_POINT_X1, -vpHeight/2 - TRIGGER_CONTROL_POINT_Y1);
glEnd();
// Draw top control point
glBegin(GL_POLYGON);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].top*yScaleFactorToVPSpace + TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].top*yScaleFactorToVPSpace + TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2), this->triggers[t].top*yScaleFactorToVPSpace);
glEnd();
// Draw bottom control point
glBegin(GL_POLYGON);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].bottom*yScaleFactorToVPSpace - TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].bottom*yScaleFactorToVPSpace - TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2), this->triggers[t].bottom*yScaleFactorToVPSpace);
glEnd();
if(this->mouseIsDown && this->selectedTrigger == t)
{
glDisable(GL_POLYGON_OFFSET_FILL);
// Draw the borders
glColor3f(1.0f,1.0f,1.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// Draw sample number control point
glBegin(GL_POLYGON);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - TRIGGER_CONTROL_POINT_X1, -vpHeight/2 - TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - TRIGGER_CONTROL_POINT_X1, -vpHeight/2 - TRIGGER_CONTROL_POINT_Y1*1/3);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2), -vpHeight/2);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + TRIGGER_CONTROL_POINT_X1, -vpHeight/2 - TRIGGER_CONTROL_POINT_Y1*1/3);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + TRIGGER_CONTROL_POINT_X1, -vpHeight/2 - TRIGGER_CONTROL_POINT_Y1);
glEnd();
// Draw top control point
glBegin(GL_POLYGON);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].top*yScaleFactorToVPSpace + TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].top*yScaleFactorToVPSpace + TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2), this->triggers[t].top*yScaleFactorToVPSpace);
glEnd();
// Draw bottom control point
glBegin(GL_POLYGON);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) - TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].bottom*yScaleFactorToVPSpace - TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2) + TRIGGER_CONTROL_POINT_X1*2, this->triggers[t].bottom*yScaleFactorToVPSpace - TRIGGER_CONTROL_POINT_Y1);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2), this->triggers[t].bottom*yScaleFactorToVPSpace);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// End Boarder
}
// Draw line in-between y control points
glBegin(GL_LINES);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2),this->triggers[t].bottom*yScaleFactorToVPSpace);
glVertex2f((this->triggers[t].sampleNumberToTest*this->interSampleLength - vpWidth/2),this->triggers[t].top*yScaleFactorToVPSpace);
glEnd();
// For testing the hit rects
// Get the cursor and convert to model coordinates
//glColor3f(1.0f,1.0f,1.0f);
//GLint viewport[4];
//GLdouble mvmatrix[16], projmatrix[16];
//GLint realY,realX;
//GLdouble wx, wy, wz;
//// Convert to model coordinates
//glGetIntegerv(GL_VIEWPORT, viewport);
//glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
//glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
//realY = viewport[3] - (GLint) cursorClickLocation.y() - 1;
//realX = viewport[2] - (GLint) cursorClickLocation.x() - 1;
//gluUnProject((GLdouble) realX, (GLdouble) realY, 0.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
//wx = -wx*100;
//wy = wy*100;
//glBegin(GL_POLYGON);
// glVertex2f(wx-.1,wy-.1);
// glVertex2f(wx-.1,wy+.1);
// glVertex2f(wx+.1,wy+.1);
// glVertex2f(wx+.1,wy-.1);
//glEnd();
}
// Below here should not be called by paint... should call paint!
/*if(!(this->waitUntilNextTransfer))
{
if(this->assignedViewerChannel != SYNCH_CHANNEL)
{
if(((RTVTChannelWidget *)this->parent())->isFiltering)
this->applySimpleThresholdOnFilteredData();
else
this->applySimpleThreshold();
}
else //increment the synch channel just as much as the others
this->readFromChannelDataPtr += WAVE_BYTE_LENGTH*numberOfWaveLengthsToCheck;
}
*/
}
else
glColor3f(0.6f,0.6f,0.6f);
}
// Function to enable smoothing
void RTVTWaveViewerGLWidget::enableLineSmoothing( bool bEnable )
{
if( bEnable )
{
// Smooth out lines
glEnable( GL_LINE_SMOOTH );
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
// Enable Blending
glEnable(GL_BLEND);
// Specifies pixel arithmetic
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
this->smooth = true;
std::cout << "::Smoothing enabled" << std::endl;
}
else // if the options are disabled
{
glDisable(GL_LINE_SMOOTH); // Smooth out lines
glDisable(GL_BLEND);
this->smooth = false;
std::cout << "::Smoothing disabled" << std::endl;
}
this->lineSmoothingEnabled = bEnable;
}
// Utilities
void RTVTWaveViewerGLWidget::findStartPosition()
{
// Find frame synch
for(int f=0;f<15;f++)
{
// Convert to hex for comparrison
char sTmp1[30],sTmp2[30];
char out[128];
sprintf(sTmp1,"0x%x",(int)((RTVTChannelWidget *)this->parent())->channelData[(f*2)]);// Why is this ff for synch and not cd????
sprintf(sTmp2,"0x%x",(int)((RTVTChannelWidget *)this->parent())->channelData[(f*2)+1]);
out[f*5]=sTmp1[2];
out[f*5+1]=sTmp1[3];
out[f*5+2]=sTmp2[2];
out[f*5+3]=sTmp2[3];
out[f*5+4]=' ';
if(((unsigned char)((RTVTChannelWidget *)this->parent())->channelData[f*2] == 0x21)&& ((unsigned char)((RTVTChannelWidget *)this->parent())->channelData[f*2+1] == 0xCD))//if((sTmp1[2] == '2') && (sTmp1[3] == '1') && (sTmp2[2] == 'c') && (sTmp2[3] == 'd'))
{
//std::cout << "Found frame synch..." << std::endl;
this->readStartPositionForChannel = f;
f = 15;
}
}
}
void RTVTWaveViewerGLWidget::tiggerTimerTimedOut()
{
if(((RTVTChannelWidget *)this->parent())->isFiltering)
this->applySimpleThresholdOnFilteredData();
else
this->applySimpleThreshold();
}
void RTVTWaveViewerGLWidget::applySimpleThreshold()
{
//unsigned int test = (((RTVTChannelWidget *)this->parent())->nextWriteLocation - WAVE_BYTE_LENGTH*10);
if(this->readFromChannelDataPtr > (CHANNEL_BUFFER_LENGTH-waveByteLength*numberOfWaveLengthsToCheck)) {}
//this->readFromChannelDataPtr = 0;
if(this->readFromChannelDataPtr > (((RTVTChannelWidget *)this->parent())->nextWriteLocation - waveByteLength*numberOfWaveLengthsToCheck))
{
testMark++;
waitUntilNextTransfer = true;
return;
}
//int workingBufferLength = BACK_BUFFER_LENGTH/NUMBER_OF_CHANNELS_IN_DATA;
unsigned int i = 0;
//if(((RTVTChannelWidget *)this->parent())->nextWriteLocation == 0)
// return; // need to wait for some data in the buffer
//if(this->thresholdReadPtr < (((RTVTChannelWidget *)this->parent())->nextWriteLocation - workingBufferLength))
// this->thresholdReadPtr = ((RTVTChannelWidget *)this->parent())->nextWriteLocation - workingBufferLength;
//if(this->thresholdReadPtr > (((RTVTChannelWidget *)this->parent())->nextWriteLocation - WAVE_BYTE_LENGTH*10))
// this->thresholdReadPtr = ((RTVTChannelWidget *)this->parent())->nextWriteLocation;
//for(i=this->thresholdReadPtr;i<(this->thresholdReadPtr+WAVE_BYTE_LENGTH*10);)
// Shouldn't need this check here = wastes time
//float dMean = 0.0;
//for (unsigned int j = this->readFromChannelDataPtr ; j <(this->readFromChannelDataPtr+WAVE_BYTE_LENGTH*numberOfWaveLengthsToCheck); j+=2)
// dMean += (((RTVTChannelWidget *)this->parent())->channelData[j+1]*256 + ((RTVTChannelWidget *)this->parent())->channelData[j])*this->adcFactor/this->ampGain; //+= dRecastXfr; // Accumulate mean
//dMean = (dMean/(WAVE_BYTE_LENGTH*numberOfWaveLengthsToCheck))*2; // *3 works for the test vectors...
//this->localMean = dMean;
////this->localMean = this->localMean/2;
//this->drawingMean = this->localMean;
for(i=this->readFromChannelDataPtr;i<(this->readFromChannelDataPtr+waveByteLength*numberOfWaveLengthsToCheck);)
{
// if 0 shift over 32 - issue with -32 index...
if(i==0)
i = this->numberOfSamplesBeforeTrigger*2;
// Have a simple threshold to stop over checking for spikes - i.e. if we are getting more than 100 in
// should be value in V
float value = ((((float)((RTVTChannelWidget *)this->parent())->channelData[i+1]*256.0)+(float)((RTVTChannelWidget *)this->parent())->channelData[i]))*this->adcFactor/this->ampGain; // the *2 is for bit shift //*this->ampGain;//(((((RTVTChannelWidget *)this->parent())->channelData[i+1]*256)+((RTVTChannelWidget *)this->parent())->channelData[i]))/10;
// Now that we have the value, should we check / add to the mean?
if(this->meanCheckPoint == this->meanCounter) // YES
{
this->localMean += value;
this->localMean = this->localMean/2;
this->drawingMean = this->localMean;
this->meanCounter = 0;
}
else
this->meanCounter++;
// Have we passed the first threshold???
bool baseTriggerWasHit = false;
if(this->triggers[0].top > 0)
{
if((value - this->localMean) > this->triggers[0].top)
{
baseTriggerWasHit = true;
}
}
else // baseline trigger is below zero on view - need to check below
{
if((value - this->localMean) <= (this->triggers[0].top)) { baseTriggerWasHit = true;}
}
if(baseTriggerWasHit) // ***** NEEDS TO BE FIXED AS VALUE SHOULD BE -PEAK TO +PEAK
{
// YES, we have passed the baseline threshold (trigger[0])
// Let's copy the wave to the waveSample
float sampleValue = 0.0;
// Get and store the time
this->triggers[0].tickOfSampleCrossing[this->triggers[0].nextWaveNumber] = theTime->calculateElapsedTime();
// Fill the wave samples into the storage array - crossing threshold centered at 1/3 samples into wave
for(int z=-this->numberOfSamplesBeforeTrigger*2; z<(this->numberOfSamplesPerWave-this->numberOfSamplesBeforeTrigger)*2 ; z+=2)
{
sampleValue = ((((unsigned char)((RTVTChannelWidget *)this->parent())->channelData[(i+z)+1]*256)+(unsigned char)((RTVTChannelWidget *)this->parent())->channelData[(i+z)]))*this->adcFactor/this->ampGain;
// Remove the mean
//sampleValue -= this->localMean;
this->triggers[0].waveSample[this->triggers[0].nextWaveNumber][(z+this->numberOfSamplesBeforeTrigger*2)/2] = sampleValue - this->localMean;
}
if(this->triggers[0].nextWaveNumber >= NUMBER_OF_WAVES_PER_TRIGGER - 1)
{
// We need to send out waves for writing
// Copy and send up to heap
waveformDatum datum;
datum.channelNumber = (unsigned short)this->assignedViewerChannel;
datum.unitNumber = 0;
memcpy(&datum.waveSamples, &this->triggers[0].waveSample, sizeof(this->triggers[0].waveSample));
memcpy(&datum.tickStamps, &this->triggers[0].tickOfSampleCrossing, sizeof(this->triggers[0].tickOfSampleCrossing));
// Send it up - need to destroy later
((RTVTChannelWidget*)this->parent())->childHasWaveformsToWrite(datum);
//emit iHaveWaveformsToWrite(datum);
this->triggers[0].nextWaveNumber = 0;
this->triggers[0].numberOfWaves = 0;
}
else
{
if(this->triggers[0].numberOfWaves >= NUMBER_OF_WAVES_PER_TRIGGER)
this->triggers[0].nextWaveNumber++;
else
{
this->triggers[0].numberOfWaves++;
this->triggers[0].nextWaveNumber++;
}
}
// Update anything outside of this object
//emit waveViewEditedTrigger(this->triggers[0]);
//((RTVTChannelWidget*)this->parent())->childHasEditedTrigger(this->triggers[0]);
// Write the wave to NEV
//this->nevWriter.wr(QDateTime::currentDateTime().toUTC().toTime_t(),this->assignedViewerChannel,0,(short(*)[64])this->triggers[0].waveSample[this->triggers[0].nextWaveNumber-1]);
// Are there any other triggers to be used? let's iterate and find out
for (int t=1;t<this->numberOfActiveTriggers;t++)
{
// YES, there is another trigger to check
float tempTopValue = 0.0;
float tempBottomValue = 0.0;
// Set the top check
if(this->triggers[t].top > 0) { tempTopValue = this->triggers[t].top;} //+ this->localMean
else { tempTopValue = this->triggers[t].top;}
// Set the bottom check
if(this->triggers[t].bottom > 0) { tempBottomValue = this->triggers[t].bottom;} //+ this->localMean
else { tempBottomValue = this->triggers[t].bottom;}
// Check the trigger hoops (only one right now)
if((this->triggers[0].waveSample[this->triggers[0].nextWaveNumber-1][this->triggers[t].sampleNumberToTest] > tempBottomValue) &&
(this->triggers[0].waveSample[this->triggers[0].nextWaveNumber-1][this->triggers[t].sampleNumberToTest] < tempTopValue))
{
// YES, the waveSample at our request is in the hoop
// Save the time
this->triggers[t].tickOfSampleCrossing[this->triggers[t].nextWaveNumber] = theTime->calculateElapsedTime();
// Save the waveForm again in the hooped trigger for display later
for(int i=0;i<this->numberOfSamplesPerWave;i++)
this->triggers[t].waveSample[this->triggers[t].nextWaveNumber][i] = this->triggers[0].waveSample[this->triggers[0].nextWaveNumber-1][i];
// Now adjust the sample number
if(this->triggers[t].nextWaveNumber >= NUMBER_OF_WAVES_PER_TRIGGER - 1)
{
// We need to send out waves for writing
// Copy and send up to heap
waveformDatum datum;
datum.channelNumber = (unsigned short)this->assignedViewerChannel;
datum.unitNumber = t;
//unsigned int test1 = sizeof(datum.waveSamples);
//unsigned int test2 = sizeof(this->triggers[t].waveSample);