-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtvtmainwindow.cpp
1679 lines (1380 loc) · 58.3 KB
/
rtvtmainwindow.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 "rtvtmainwindow.h"
#include <QKeyEvent>
#include <iostream>
#include <QtConcurrentRun>
#include <QThread>
#include <QApplication>
#include <QDebug>
#include <QtOpenGL>
#include <math.h>
//#define RTVT_DEBUG
//#define USE_AUDIO
#define DATA_PATH_TYPE HSUSB_DATA_PATH_TYPE
#ifdef EXTERNAL_MEMCPY
#include "External-Code/memcpy.h"
#endif
unsigned char *drawingByteBuffer;
//unsigned char *workingByteBuffer;
//unsigned char *workingChannelBuffer;
unsigned char *storageByteBuffer;
unsigned char *audioByteBuffer;
// Timer
RTVTTimer *theTime;
#ifdef USE_HS_USB
extern unsigned char *backByteBuffer; // from CPSHSUSB
extern CPSHSUSBHardwareAbstractionLayer *hsHAL;
#else
unsigned char *backByteBuffer;
#endif
static long length = BACK_BUFFER_LENGTH; // this is the Xfer length, not bb length - needs to be bblength/2
RTVTMainWindow::RTVTMainWindow()
{
// Init procedure for non-view variables
this->initializeVariables();
theTime = new RTVTTimer();
// Set the frame in the center of the screen and fix it
this->setGeometry(450,50,1100,900);
this->layout()->setContentsMargins(0,0,0,0);
this->setStatusBar(0);
//this->setMinimumSize(1200,700);
//this->setMaximumSize(1200,700);
// Make the BG black
//this->setStyleSheet("background-color: black;");
// Make unified bar
//this->setUnifiedTitleAndToolBarOnMac(true);
this->setFocusPolicy(Qt::StrongFocus);
QAudioFormat format;
format.setFrequency(SYSTEM_CLOCK_RATE/SAMPLE_FRAME_LENGTH/DEFAULT_NUM_CHANNELS_IN_DATA);
format.setChannels(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
this->audioOutput = new QAudioOutput(format,this);
this->startAudio = false;
this->useAudio = false;
//this->redrawChannelViews();
// Start with waves as default view
this->changeChannelDisplayToWaves();
#ifdef USE_AUDIO
// Channels are created - we can create and set the audio system
audioPlayer = new AudioTest();
//audioPlayer->setChannelPtr(this->channelWidgetList[0]);
connect(audioPlayer, SIGNAL(currentIndexChanged(int)), this, SLOT(updateAudioChannel(int)));
QStringList items;
for(int ac = 0;ac<this->numberOfDataChannels;ac++)
{
items << QString::number(ac+1);
}
this->audioPlayer->channelComboBox->clear();
this->audioPlayer->channelComboBox->addItems(items);
this->audioPlayer->setGeometry(100,100,200,150);
audioPlayer->show();
#endif
// Filtering
this->firFilters = new FirKernels*[MAX_NUMBER_OF_FILTERS];
for(int filterCount=0;filterCount<MAX_NUMBER_OF_FILTERS;filterCount++)
{
this->firFilters[filterCount] = new FirKernels();
}
this->numberOfFiltersCreated = 0;
//// Placeholder widgets
// Top
#ifdef RTVT_DEBUG
/*QDockWidget *phDockTop = new QDockWidget("Top widget area placeholder",this);
phDockTop->setGeometry(0,0,300,800);
phDockTop->setStyleSheet("border-width: 1px; border-color: #535353; background-color: white;");
this->rtvtConsole = new QTextBrowser(this);
this->rtvtConsole->append("Browser created and setup for appending data.");
this->rtvtConsole->setGeometry(0,0,300,myCentralWidget->height());
QScrollBar *vbar = new QScrollBar(Qt::Vertical, this);
this->rtvtConsole->setVerticalScrollBar(vbar);
this->rtvtConsole->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding);
this->rtvtConsole->setReadOnly(false);
phDockTop->layout()->addWidget(this->rtvtConsole);
this->addDockWidget(Qt::LeftDockWidgetArea,phDockTop);*/
#endif
// Bottom
//QDockWidget *phDockBottom = new QDockWidget("Bottom widget area placeholder",this);
//phDockBottom->setGeometry(0,0,1200,200);
//phDockBottom->setStyleSheet("border-width: 1px; border-color: #535353; background-color: white;");
//dataListWidget = new QListWidget(phDockBottom);
//dataListWidget->setGeometry(0,0,1200,200);
//dataListWidget->setFocusPolicy(Qt::NoFocus);
//phDockBottom->layout()->addWidget(dataListWidget);
//this->addDockWidget(Qt::BottomDockWidgetArea, phDockBottom);
// Center the window
this->centerMainWindow();
//// BB Thread
// create a QFuture and a QFutureWatcher
bbFuture = new QFuture<void>;
bbWatcher = new QFutureWatcher<void>;
// Set a timer for buffer transfers
bbTransferTimer = new QTimer(this);
connect(bbTransferTimer, SIGNAL(timeout()), this, SLOT(runThreadedDataBBTransfer()));
// display a message box when the calculation has finished
connect(bbWatcher, SIGNAL(finished()), this, SLOT(bbThreadFinishedRunning()));
///// SB Thread
// create a QFuture and a QFutureWatcher
sbFuture = new QFuture<void>;
sbWatcher = new QFutureWatcher<void>;
// Set a timer for buffer transfers
sbTransferTimer = new QTimer(this);
connect(sbTransferTimer, SIGNAL(timeout()), this, SLOT(runThreadedDataSBTransfer()));
// display a message box when the calculation has finished
connect(sbWatcher, SIGNAL(finished()), this, SLOT(sbThreadFinishedRunning()));
// Set a timer for buffer transfers
playTimer = new QTimer(this);
connect(playTimer, SIGNAL(timeout()), this, SLOT(updatePlay()));
// /**** Create the HUD ****/
// Create the RTVT Controller page - load from form
rtvtControllerView = new RTVTHUDControllerView();
rtvtWaveHUD = new RTVTHUDWaveView();
// Setup the table in the Wave view and make datamodel
QList<hoop> currentTriggerList;
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
{
for(int t=0;t<cw->waveViewer->numberOfActiveTriggers;t++)
currentTriggerList << cw->waveViewer->triggers[t];
}
theWaveTableModel = new RTVTTriggerModel(currentTriggerList, this); // Make a model
rtvtWaveHUD->setTableModel(theWaveTableModel); // Set the model in the view
connect(this->rtvtWaveHUD, SIGNAL(clearWaveformButtonClicked()), this, SLOT(clearWaveformsOnChannels()));
connect(this->rtvtWaveHUD, SIGNAL(resetWaveformTriggersButtonClicked()), this, SLOT(resetTriggersOnChannels()));
connect(this->rtvtWaveHUD, SIGNAL(saveToNEVFileCheckboxClicked(bool, char*)), this, SLOT(propogateSaveNEV(bool, char*)));
connect(this->rtvtWaveHUD, SIGNAL(saveNEVToggleButtonState(bool)), this, SLOT(setSaveNEVState(bool)));
connect(this->rtvtWaveHUD, SIGNAL(updateWaveCheckValue(int)), this, SLOT(updateLocalWaveCheckValue(int)));
connect(this->rtvtWaveHUD, SIGNAL(controllerSaysMoveThresholdsTogether(bool)), this, SLOT(tellWaveViewsToPropogateChangesWhenDone(bool)));
connect(this->rtvtControllerView, SIGNAL(signalToAddFilterToController()), this, SLOT(addFilterFromFile()));
connect(this->rtvtControllerView, SIGNAL(selectedFilterChanged(int)), this, SLOT(updateSelectedFilter(int)));
connect(this->rtvtControllerView, SIGNAL(signalNoFiltering()), this, SLOT(stopFilteringData()));
connect(this->rtvtControllerView, SIGNAL(changeNumberOfChannelViews(int)), this, SLOT(updateNumberOfChannelViews(int)));
connect(this->rtvtControllerView, SIGNAL(changeNumberOfDataChannels(int)), this, SLOT(updateNumberOfDataChannels(int)));
connect(this->rtvtControllerView, SIGNAL(changeChannelDisplayToWavesAndContinuous()), this, SLOT(changeChannelDisplayToWavesAndContinuous()));
connect(this->rtvtControllerView, SIGNAL(changeChannelDisplayToWaves()), this, SLOT(changeChannelDisplayToWaves()));
connect(this->rtvtControllerView, SIGNAL(changeMainWindowAmplifierAddressing(int)), this, SLOT(setAddressingTo(int)));
/*** Add first filter - always the null filter and average (11 samples) so we have them around
***/
this->selectedFilterIndex = -1;
// Null Filter
QString fileName = "C:/Users/OptoBOSS/Desktop/Programming/RTVT_2011/Filtering/null_filter.txt";
QByteArray ba = fileName.toLatin1();
this->firFilters[this->numberOfFiltersCreated]->SetFilterData(ba.data());
this->numberOfFiltersCreated++;
QFileInfo nullPathInfo(fileName);
QString theNullName(nullPathInfo.fileName());
this->rtvtControllerView->addToFilterList(this->numberOfFiltersCreated, theNullName);
// Average filter
fileName = "Filtering/average.txt";
ba = fileName.toLatin1();
this->firFilters[this->numberOfFiltersCreated]->SetFilterData(ba.data());
this->numberOfFiltersCreated++;
QFileInfo averagePathInfo(fileName);
QString theAverageName(averagePathInfo.fileName());
this->rtvtControllerView->addToFilterList(this->numberOfFiltersCreated, theAverageName);
/***
***/
//QWidget *HUDWindow = new QWidget(this);
//HUDWindow->setGeometry(100,100,400,800);
hud = new MultiPageWidget(0);
hud->setGeometry(50,550,375,450);
hud->setContentsMargins(0,0,0,0);
hud->insertPage(0,rtvtControllerView);
hud->insertPage(1,rtvtWaveHUD);
//hud->setStyle(new QWindowsStyle);
hud->setCurrentIndex(0);
// Load qss file for styling
QFile file("qss/rtvthuddockstyle.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
//hudDock = new QDockWidget("HUD Dock");
//hudDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
// to hide the title bar completely must replace the default widget with a generic one
//QWidget* titleWidget = new QWidget(this); /* where this a QMainWindow object */
//hudDock->setTitleBarWidget( titleWidget );
//hudDock->setStyleSheet(styleSheet);//"border-width: 1px; border-color: #535353; background-color: white;");
//dataListWidget = new QListWidget(phDockBottom);
//dataListWidget->setGeometry(0,0,1200,200);
//hudDock->setFocusPolicy(Qt::NoFocus);
//hudDock->setWidget(hud);
//this->addDockWidget(Qt::LeftDockWidgetArea, hudDock);
hud->hide();
//this->connect(this->centralWidget(), SIGNAL(resize()), this, SLOT(resizeSelfAndViews()));
}
RTVTMainWindow::~RTVTMainWindow()
{
#ifndef USE_HS_USB
free(backByteBuffer);
#endif
free(drawingByteBuffer);
free(storageByteBuffer);
//free(workingByteBuffer);
//free(workingChannelBuffer);
//free(transferBuf);
delete byteArray;
delete dataObject;
delete theTime;
delete audioOutput;
#ifdef USE_AUDIO
delete audioPlayer;
#endif
delete firFilters;
delete bbFuture;
delete bbWatcher;
delete bbTransferTimer;
delete sbFuture;
delete sbWatcher;
delete sbTransferTimer;
delete playTimer;
delete rtvtControllerView;
delete rtvtWaveHUD;
delete theWaveTableModel;
delete hud;
}
void RTVTMainWindow::initializeVariables()
{
this->numberOfChannelViews = DEFAULT_NUM_CHANNEL_VIEWS;
cout<<this->numberOfChannelViews <<endl;
this->numberOfDataChannels = DEFAULT_NUM_CHANNELS_IN_DATA;
this->playStep = 0;
this->readDataStep = 0;
this->dataPath = -1; //FILE_DATA_PATH_TYPE;
this->dataSource = TEST_VECTOR_DATA_TYPE;
this->readAllData = true;
this->audioChannel = 1;
this->continuousViewerNeedsDataNumber = 0;
this->sbThreadIsRunning = false;
this->missedSynch = 0;
this->selectedWidgetNumber = 0;
this->singleFrameLoss = 0;
this->squareChannelGrid = false;
this->byteArray = new QByteArray();
this->rtvtSaveDataPath = "C:/Users/Public/test.dat";
this->rtvtSavingData = false;
this->numberOfFileSaves = 0;
this->wireAddressing = DEFAULT_WIRE_ADDRESSING;
this->dataObject = new RTVTDataObject();
this->saveNEVState = false;
this->shouldFilter = false;
this->sampleFrameLength = SAMPLE_FRAME_LENGTH;
sbWriteEndPtr = 0;
sbDrawReadEndPtr = 0;
sbAudioReadEndPtr = 0;
backByteBuffer = (unsigned char *) malloc (sizeof(unsigned char) * length);
drawingByteBuffer = (unsigned char *) malloc (sizeof(unsigned char) * DRAW_BUFFER_LENGTH);
storageByteBuffer = (unsigned char *) malloc (sizeof(unsigned char) * STORAGE_BUFFER_LENGTH);
//workingByteBuffer = (unsigned char *) malloc (sizeof(unsigned char) * BACK_BUFFER_LENGTH);
//workingChannelBuffer = (unsigned char *) malloc (sizeof(unsigned char) * BACK_BUFFER_LENGTH / numberOfDataChannels);
theDataArray.clear();
transferBuf=(unsigned char *) malloc (sizeof(unsigned char) * BACK_BUFFER_LENGTH);
//this->aBuffer = new QBuffer(&audioByteBuffer,this);
//this->aBuffer->open(QIODevice::ReadOnly);
}
void RTVTMainWindow::setupChannelGrid()
{
// Create grid for wave viewers
}
void RTVTMainWindow::centerMainWindow()
{
QRect myAvailableGeometry = QDesktopWidget().availableGeometry();
QRect myCurrentGeometry = this->frameGeometry();
this->setGeometry(myAvailableGeometry.width() / 2 - myCurrentGeometry.width() / 2,
myAvailableGeometry.height() / 2 - myCurrentGeometry.height() / 2,
myCurrentGeometry.width(),
myCurrentGeometry.height());
}
void RTVTMainWindow::keyPressEvent(QKeyEvent *e)
{
switch(e->key())
{
case Qt::Key_L:
if(this->sbThreadIsRunning)//this->sbTransferTimer->isActive())
{
this->sbThreadIsRunning = false; //this->sbTransferTimer->stop();
this->saveDataFile.close();
emit appendToConsole("Data loading stopped.");
this->rtvtControllerView->dataLoadingStopped();
}
else
{
//this->sbTransferTimer->setInterval(300);
//const char *filepath = (const char*)this->rtvtSaveDataPath.data();
//this->saveDataFile.setFileName(this->rtvtSaveDataPath);
//if (!this->saveDataFile.open(QIODevice::WriteOnly))
//{int t = 1;}
// update the save path to new file name +1
#ifdef USE_AUDIO
QString savePath = this->rtvtSaveDataPath;
savePath = QString(savePath.replace(".snifr", QString("_" + QString::number(this->numberOfFileSaves) + ".snifr")));
this->saveDataFile.setFileName(savePath);
this->numberOfFileSaves++;
if(!this->saveDataFile.open(QIODevice::WriteOnly))
qDebug()<<"Error opening the file";
this->binStreamToWrite.setDevice(&this->saveDataFile);
//this->streamToWrite.setDevice(&this->saveDataFile);
//saveDataFile.open( QFile::WriteOnly );
#endif
this->sbTransferTimer->setSingleShot(true);
this->sbThreadIsRunning = true;
this->sbTransferTimer->start();
emit appendToConsole("Data loading started.");
this->rtvtControllerView->dataLoadingStarted();
}
break;
case Qt::Key_R:
this->resetAllBuffers();
break;
case Qt::Key_N:
//this->channelWidgetList[0]->setupNoisePlot();
break;
case Qt::Key_P:
if(this->channelWidgetList.at(0)->isPlaying)
{
for(int i=0;i<this->channelWidgetList.count();i++)
this->channelWidgetList.at(i)->stop();
/*if(this->saveNEVState)
this->nevWriter->end();*/
}
else
{
//memcpy (drawingByteBuffer, &storageByteBuffer[0], DRAW_BUFFER_LENGTH);
//this->sbDrawReadEndPtr += DRAW_BUFFER_LENGTH;
for(int i=0;i<this->channelWidgetList.count();i++)
this->channelWidgetList.at(i)->play();
this->updatePlay();
//if(this->saveNEVState)
// this->nevWriter->start();
}
break;
case Qt::Key_3:
this->setupChannelGrid();
break;
case Qt::Key_U:
if(!this->useAudio)
{
this->startAudio = true;
this->useAudio = true;
}
else
{
this->audioOutput->stop();
this->useAudio = false;
}
break;
case Qt::Key_0:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_T:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_M:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_Y:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_D:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_S:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_W:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_A:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
//this->audioPlayer->startPlayingAudio();
break;
case Qt::Key_O:
this->audioPlayer->show();
//this->audioPlayer->startPlayingAudio();
break;
case Qt::Key_Up:
if(this->channelWidgetList[this->selectedWidgetNumber]->assignedChannel == this->numberOfDataChannels)
this->channelWidgetList[this->selectedWidgetNumber]->assignedChannel = 1;
else
this->channelWidgetList[this->selectedWidgetNumber]->assignedChannel += 1;
this->channelWidgetList[this->selectedWidgetNumber]->update();
#ifdef USE_AUDIO
this->audioPlayer->setCurrentIndex(this->channelWidgetList[this->selectedWidgetNumber]->assignedChannel-1);
#endif
//foreach (RTVTChannelWidget *cw, this->channelWidgetList)
//cw->keyPressEvent(e);
break;
case Qt::Key_Down:
if(this->channelWidgetList[this->selectedWidgetNumber]->assignedChannel == 1)
this->channelWidgetList[this->selectedWidgetNumber]->assignedChannel = this->numberOfDataChannels;
else
this->channelWidgetList[this->selectedWidgetNumber]->assignedChannel -= 1;
this->channelWidgetList[this->selectedWidgetNumber]->update();
#ifdef USE_AUDIO
this->audioPlayer->setCurrentIndex(this->channelWidgetList[this->selectedWidgetNumber]->assignedChannel-1);
#endif
//foreach (RTVTChannelWidget *cw, this->channelWidgetList)
//cw->keyPressEvent(e);
break;
case Qt::Key_Left:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_Right:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_Z:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_X:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_I:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
case Qt::Key_C:
this->clearWaveformsOnChannels();
break;
case Qt::Key_G:
foreach (RTVTChannelWidget *cw, this->channelWidgetList)
cw->keyPressEvent(e);
break;
default:
e->ignore();
}
e->ignore();
}
QString RTVTMainWindow::openExistingData()
{
// Open dialog
QFileDialog::Options options;
QString selectedFilter;
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Existing Data File:"),
"",
tr("SNIFR Files (*.snifr);;All Files (*);;Data Files (*.dat);;CSV Files (*.csv)"),
&selectedFilter,
options);
if (!fileName.isEmpty())
{
this->openDataFileHandle.setFileName(fileName);
if (!this->openDataFileHandle.open(QIODevice::ReadOnly)) {
std::cerr << "Cannot open file for reading: "<< qPrintable(this->openDataFileHandle.errorString()) << std::endl;
return QString("Error opening file");
}
this->playStep = 0;
this->readDataStep = 0;
std::cout << "Done opening file: " << qPrintable(fileName) << std::endl;
return fileName;
}
else
{}
return QString("No error, no file...");
}
void RTVTMainWindow::chooseFileForSavingData()
{
// Open dialog
QFileDialog::Options options;
QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this,
tr("Choose a file to save data:"),
"",
tr("All Files (*)"),
&selectedFilter,
options);
if (!fileName.isEmpty())
{
this->saveDataFileHandle.setFileName(fileName);
if (!this->saveDataFileHandle.open(QIODevice::WriteOnly)) {
std::cerr << "Cannot open file for writing: "<< qPrintable(this->saveDataFileHandle.errorString()) << std::endl;
return;
}
std::cout << "Done opening file for data out stream: " << qPrintable(fileName) << std::endl;
}
else
{}
}
void RTVTMainWindow::setAddressingTo(int numWire)
{
this->wireAddressing = numWire;
if(numWire==2)
{
this->sampleFrameLength = 12;
this->systemClockRate = 24000000;
}
else
{
this->sampleFrameLength = 24;
this->systemClockRate = 13560000;
}
for(int i=0;i<this->channelWidgetList.count();i++)
{
this->channelWidgetList.at(i)->setAddressScheme(numWire);
//this->channelWidgetList.at(i)->setSystemClockRate(this->systemClockRate);
}
this->redrawChannelViews();
}
void RTVTMainWindow::playFromController(bool withRecording)
{
Q_UNUSED(withRecording);
// Start the loading
if(this->sbThreadIsRunning)//this->sbTransferTimer->isActive())
{
this->sbThreadIsRunning = false; //this->sbTransferTimer->stop();
if(this->rtvtSavingData)
this->saveDataFile.close();
#ifdef USE_AUDIO
this->audioPlayer->toggleSuspendResume();
#endif
emit appendToConsole("Data loading stopped at: " + (QTime::currentTime()).toString());
this->rtvtControllerView->dataLoadingStopped();
}
else
{
if(this->rtvtSavingData)
{
QString savePath = this->rtvtSaveDataPath;
savePath = QString(savePath.replace(".snifr", QString("_" + QString::number(this->numberOfFileSaves) + ".snifr")));
this->saveDataFile.setFileName(savePath);
this->numberOfFileSaves++;
if(!this->saveDataFile.open(QIODevice::WriteOnly))
qDebug()<<"Error opening the file";
this->binStreamToWrite.setDevice(&this->saveDataFile);
}
this->sbTransferTimer->setSingleShot(true);
this->sbThreadIsRunning = true;
this->sbTransferTimer->start();
emit appendToConsole("-------------------------------------------------------------");
emit appendToConsole("Data loading started at: " + (QTime::currentTime()).toString());
this->rtvtControllerView->dataLoadingStarted();
}
// Start the playing
if(this->channelWidgetList.at(0)->isPlaying)
for(int i=0;i<this->channelWidgetList.count();i++)
this->channelWidgetList.at(i)->stop();
else
for(int i=0;i<this->channelWidgetList.count();i++)
this->channelWidgetList.at(i)->play();
// Start the thresholding
//QKeyEvent e = QKeyEvent(QEvent::KeyPress,Qt::Key,Qt::NoModifier);
QKeyEvent e = QKeyEvent(QEvent::KeyPress,Qt::Key_T,Qt::NoModifier);
this->keyPressEvent(&e);
}
void RTVTMainWindow::updatePlay()
{
// Check if we need to update the drawing buffer from storage
// if((playStep*DRAW_FRAME_LENGTH) > (DRAW_BUFFER_LENGTH-DRAW_FRAME_LENGTH))
//{
// Check if we still have data to pull from storage buffer - if not, repeat
/*
if(this->sbDrawReadEndPtr > (this->sbWriteEndPtr - DRAW_BUFFER_LENGTH))
this->sbDrawReadEndPtr = 0;
memcpy (drawingByteBuffer, &storageByteBuffer[this->sbDrawReadEndPtr], DRAW_BUFFER_LENGTH);
if(this->sbDrawReadEndPtr >= (STORAGE_BUFFER_LENGTH - DRAW_BUFFER_LENGTH))
this->sbDrawReadEndPtr = 0;
else
this->sbDrawReadEndPtr += DRAW_BUFFER_LENGTH;
// Reset the read ptr for views
int c = this->channelWidgetList.count();
for(int i=0;i<this->channelWidgetList.count();i++)
{
this->channelWidgetList.at(i)->resetReadPointer();
this->channelWidgetList.at(i)->setDrawFromData(true);
}
this->playStep = 0;
#ifdef RTVT_DEBUG
this->rtvtControllerView->ui->debugConsole->append(":: Made storageBuffer-drawBuffer transfer ::");
#endif
//}
//this->playStep++;
*/
}
void RTVTMainWindow::makeDataStoreToBackBufferTransfer()
{
if(this->dataPath == FILE_DATA_PATH_TYPE)
{
if(!this->openDataFileHandle.isReadable())
return;
int tempPos = this->openDataFileHandle.pos();
this->openDataFileHandle.seek(tempPos + BACK_BUFFER_LENGTH);
if(!this->openDataFileHandle.atEnd())
{
this->openDataFileHandle.seek(tempPos);
}
else {
qDebug("@ end of file");
this->openDataFileHandle.seek(0);
}
// Lock to transfer
bbMutex.lock();
theDataArray = this->openDataFileHandle.read(BACK_BUFFER_LENGTH);
transferBuf = (unsigned char*)theDataArray.data();
memcpy(&backByteBuffer[0], transferBuf, BACK_BUFFER_LENGTH);
bbMutex.unlock();
// Unlocked
}
#ifdef USE_HS_USB
else if(this->dataPath == HSUSB_DATA_PATH_TYPE)
{
// Check for space in storage buffer
if(this->sbWriteEndPtr > (STORAGE_BUFFER_LENGTH-BACK_BUFFER_LENGTH))
this->sbWriteEndPtr = 0;
hsHAL->makeUSBTransfer();
}
#endif
}
void RTVTMainWindow::makeBackToStorageBufferTransfer()
{
// Should make the working buffer here so it can be used by each thread as it wants...
unsigned char *workingByteBuffer;
workingByteBuffer = (unsigned char *) malloc (sizeof(unsigned char) * BACK_BUFFER_LENGTH);
this->makeDataStoreToBackBufferTransfer(); // Fill the back buffer
// No matter what, update the pointer
if(this->sbWriteEndPtr > (STORAGE_BUFFER_LENGTH - (BACK_BUFFER_LENGTH)))
this->sbWriteEndPtr = 0; // Write over at the beginning of the buffer
#ifdef USE_HS_USB
if(this->dataPath == HSUSB_DATA_PATH_TYPE)
{
// Always add the new data to the end of the storage buffer and increment ptr
memcpy(&storageByteBuffer[this->sbWriteEndPtr],&backByteBuffer[hsHAL->bbNextReadPtr],(BACK_BUFFER_LENGTH));
/*** Break into channels and place in channel data buffer ***/
// Copy into workign buffer
memcpy(workingByteBuffer,&backByteBuffer[hsHAL->bbNextReadPtr],BACK_BUFFER_LENGTH);
}
else if(this->dataPath == FILE_DATA_PATH_TYPE)
{
memcpy(&storageByteBuffer[this->sbWriteEndPtr],&backByteBuffer[0],(BACK_BUFFER_LENGTH));
memcpy(workingByteBuffer,&backByteBuffer[0],BACK_BUFFER_LENGTH);
}
#else
memcpy(&storageByteBuffer[this->sbWriteEndPtr],&backByteBuffer[0],(BACK_BUFFER_LENGTH));
memcpy(workingByteBuffer,&backByteBuffer[0],BACK_BUFFER_LENGTH);
#endif
// No matter what, update the pointer
this->sbWriteEndPtr += BACK_BUFFER_LENGTH;
// Should need to spin this to a thread... running slowly
if(this->rtvtSavingData)
{
//extern void threadedSaveRawData();
QFuture<void> future = QtConcurrent::run(this,&RTVTMainWindow::threadedSaveRawData);
// Try not threaded for now
//this->threadedSaveRawData();
}
// Spin thread to burn through the data / separating to channels, etc.
QFuture<void> future = QtConcurrent::run(this,&RTVTMainWindow::threadedChannelSeparation, workingByteBuffer);
//future.waitForFinished();
//this->threadedChannelSeparation();
#ifdef RTVT_DEBUG
//this->rtvtControllerView->appendToDebugConsole(":: Made Working to Channel transfer ::");
#endif
/*QTimer *silkThreadTimer = new QTimer(this);
connect(silkThreadTimer, SIGNAL(timeout()), this, SLOT(threadedChannelSeparation()));
silkThreadTimer->start();*/
}
void RTVTMainWindow::threadedSaveRawData()
{
// Binary save
this->binStreamToWrite.writeRawData((char*)backByteBuffer, BACK_BUFFER_LENGTH);
//Text Save
/*for(int w=0;w<BACK_BUFFER_LENGTH/2;w++)
this->streamToWrite<<(backByteBuffer[w*2+1]*256+backByteBuffer[w*2]);*/
}
void RTVTMainWindow::saveSNIFFRToMat()
{
QFile saveFileHandle;
QFileDialog::Options options;
QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this,
tr("Choose save file name:"),
"",
tr("Data Files (*.dat)"),
&selectedFilter,
options);
if (!fileName.isEmpty())
{
saveFileHandle.setFileName(fileName);
if (!saveFileHandle.open(QIODevice::WriteOnly)) {
std::cerr << "Cannot open file for writing: "<< qPrintable(this->openDataFileHandle.errorString()) << std::endl;
}
std::cout << "Opened file to write: " << qPrintable(fileName) << std::endl;
}
else
{}
this->openDataFileHandle.seek(0);
QTextStream out(&saveFileHandle);
unsigned int i = 0;
QByteArray theDataArray;
unsigned char *buf;
buf=(unsigned char *) malloc (sizeof(unsigned char) * BACK_BUFFER_LENGTH);
int frameSynchIndex = -1;
unsigned int counter = 0;
int values[102]; // need to fix this - does not need to be a number in MAC OS X
theDataArray = this->openDataFileHandle.readAll();
unsigned int lengthOfFile = theDataArray.length();
buf = (unsigned char*)theDataArray.data();
// Find start point in the data after frame synch
frameSynchIndex = this->findFrameSynchInData(buf);
i = frameSynchIndex*2+2;
// Start a progress bar dialog since this task takes all of thread
QProgressDialog progress("Converting SNIFFR to DAT...", "Abort save", 0, (lengthOfFile-frameSynchIndex*2-102*2), this); // fix here for both 16 and 101 channels ystems
progress.setWindowModality(Qt::WindowModal);
// Always starting at channel 0, so find synch, go up two bytes, and begin reading
// Burn through all the data and write to file - start at framesynchindex + 1 for channel 0
for(unsigned int k = 0;k<(lengthOfFile-frameSynchIndex*2-this->numberOfDataChannels*2);k=k+204)
{
progress.setValue(k);
if (progress.wasCanceled())
break;
//fill the values array
unsigned int chCounter=0;
for(chCounter=0;chCounter<this->numberOfDataChannels;chCounter++)
values[chCounter] = ((buf[i+k+chCounter*2+1] & 0x0F)*256 + buf[i+k+chCounter*2]);
for(chCounter=0;chCounter<this->numberOfDataChannels;chCounter++)
out << (qint16)values[chCounter] << QString(" ");
out << endl;
/*out << (qint16)values[0] << QString(" ") << (qint16)values[1] << QString(" ") << (qint16)values[2] << QString(" ")
<< (qint16)values[3] << QString(" ") << (qint16)values[4] << QString(" ") << (qint16)values[5] << QString(" ")
<< (qint16)values[6] << QString(" ") << (qint16)values[7] << QString(" ") << (qint16)values[8] << QString(" ")
<< (qint16)values[9] << QString(" ") << (qint16)values[10] << QString(" ") << (qint16)values[11] << QString(" ")
<< (qint16)values[12] << QString(" ") << (qint16)values[13] << QString(" ") << (qint16)values[14] << QString(" ")
<< (qint16)values[15] << endl;
*/
}
progress.setValue((lengthOfFile-frameSynchIndex*2-this->numberOfDataChannels*2));
counter++;
saveFileHandle.close();
}
void RTVTMainWindow::threadedChannelSeparation(unsigned char *workingInBuffer)
{
// Find start point in the data after frame synch
int startingLocation = 0;
int workingChannel = 0;
unsigned int startChannel = 0;
unsigned int evenOddShift = 0;
unsigned int frameSynchIndex = this->findFrameSynchInData(workingInBuffer);
// Should make the working buffer here so it can be used by each thread as it wants...
unsigned char *workingChannelBufferTemp;
workingChannelBufferTemp = (unsigned char *) malloc (sizeof(unsigned char) * BACK_BUFFER_LENGTH);
/*if(frameSynchIndex < (this->numberOfDataChannels)) // Meaning that frame synch is not the first data in the buffer
{
startChannel = (this->numberOfDataChannels) - frameSynchIndex; // find channel to start working on first
startingLocation = 0;//(15 - frameSynchIndex)*2; // align the data to fit the channel
}
else // start from this point forward
{
startChannel = this->numberOfDataChannels;
startingLocation = frameSynchIndex*2;
}
*/
// Find the channel that the 0 and 1 index represent
// check if FSI is > number of channels - meaning we missed a synch and want to start at new frame
if(frameSynchIndex > numberOfDataChannels)
startChannel = numberOfDataChannels;
else
{
// if odd, we need to adjust since only half the startLocation is part of the startChannel sample
int test1 = frameSynchIndex/2;
double test2 = ceil((((double)frameSynchIndex+1)/2));
if(test1 == test2)
{
// add shift to startingLocation and set start channel to 1 higher
startChannel = numberOfDataChannels - (frameSynchIndex+1)/2 +1;
evenOddShift = 1;
}
else //if even
startChannel = numberOfDataChannels - (frameSynchIndex)/2;
}
// Substract from synch the channels before to get to starting channel of data
// Always starting at channel 0, so find synch, go up two bytes, and begin reading
// Burn through all the data and separate into channels
workingChannel = startChannel;
for(int z = 0; z<numberOfChannelViews;z++) // changing to numberOfDataChannels
{
// Need to find the channel to copy to - check assigned channel of view #1 and so on
unsigned int activeChannel = this->channelWidgetList.at(z)->assignedChannel;
/*if(activeChannel <= startChannel)
startingLocation = (frameSynchIndex + activeChannel)*2;
else
startingLocation = ((activeChannel) - (startChannel))*2;
*/
// Check if we are going to read from before or after the frameSynch
if (activeChannel == numberOfDataChannels)
startingLocation = frameSynchIndex;
else if(activeChannel < startChannel)
startingLocation = frameSynchIndex + activeChannel*2 + evenOddShift;
else if(activeChannel > (startChannel))
startingLocation = (activeChannel - startChannel)*2 + evenOddShift; // was this: frameSynchIndex - (activeChannel - startChannel)*2 + evenOddShift; // were getting data displayed across channels
// Check to see where we should begin writin // should optimize this to not have if all the time
unsigned int bytesWritten = 0;
for(int i=startingLocation;i<BACK_BUFFER_LENGTH;)
{
if(activeChannel == DEFAULT_SYNCH_CHANNEL)
if(!(((unsigned char)workingInBuffer[i] == 0x21)&&((unsigned char)workingInBuffer[i+1]== 0xCD)))
{
if((((unsigned char)workingInBuffer[i+numberOfDataChannels*2] == 0x21)&&((unsigned char)workingInBuffer[i+1+numberOfDataChannels*2]== 0xCD)))
this->singleFrameLoss++;
//else
//this->missedSynch++;
/*char sTmp1[4],sTmp2[4];
sprintf(sTmp1,"0x%x",workingByteBuffer[i]);// Why is this ff for synch and not cd????
sprintf(sTmp2,"0x%x",workingByteBuffer[i+1]);
*/
}
// Add data from single channel to working buffer
// Bring down to 12 bit value
workingChannelBufferTemp[bytesWritten + 1] = workingInBuffer[i+1] & 0x0F;
workingChannelBufferTemp[bytesWritten] = workingInBuffer[i];
//memcpy(&workingChannelBuffer[bytesWritten], &workingByteBuffer[i],2*sizeof(unsigned char));
i += numberOfDataChannels * 2;
bytesWritten += 2;