forked from TeamOpenFIRE/OpenFIRE-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guiwindow.cpp
2477 lines (2231 loc) · 111 KB
/
guiwindow.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
/* OpenFIRE App: a configuration utility for the OpenFIRE light gun system.
Copyright (C) 2024 Team OpenFIRE
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "guiwindow.h"
#include "constants.h"
#include "ui_guiwindow.h"
#include "ui_about.h"
#include <QGraphicsScene>
#include <QMessageBox>
#include <QRadioButton>
#include <QSvgRenderer>
#include <QSvgWidget>
#include <QSerialPortInfo>
#include <QtDebug>
#include <QProgressBar>
#include <QProcess>
#include <QStorageInfo>
#include <QThread>
#include <QColorDialog>
#include <QInputDialog>
#include <QTimer>
#include <QDesktopServices>
#include <QUrl>
// Currently loaded board object
boardInfo_s board;
// Currently loaded board's TinyUSB identifier info
tinyUSBtable_s tinyUSBtable;
// TinyUSB ident, as loaded from the board
tinyUSBtable_s tinyUSBtable_orig;
#define PROFILES_COUNT 4
// Current calibration profiles
QVector<profilesTable_s> profilesTable(PROFILES_COUNT);
// Calibration profiles, as loaded from the board
QVector<profilesTable_s> profilesTable_orig(PROFILES_COUNT);
// Map of what inputs are put where,
// Key = button/output, Value = pin number occupying, if any.
// Value of -1 means unmapped.
// Key order based on boardInputs_e, minus 1
// Map functions used in deduplication
QMap<uint8_t, int8_t> inputsMap;
// Inputs map, as loaded from the board
QMap<uint8_t, int8_t> inputsMap_orig;
// ^^^-----Typedefs up there:----^^^
//
// vvv---UI Objects down here:---vvv
// Guess I'll have to do the dynamic layout spawning/destroying stuff to make things work.
// How else do I hide things lol?
QVBoxLayout *PinsCenter;
QGridLayout *PinsCenterSub;
QGridLayout *PinsLeft;
QGridLayout *PinsRight;
QComboBox *pinBoxes[30];
QLabel *pinLabel[30];
QWidget *padding[30];
// buttons in the test screen
QLabel *testLabel[16];
QRadioButton *selectedProfile[PROFILES_COUNT];
QLabel *topOffset[PROFILES_COUNT];
QLabel *bottomOffset[PROFILES_COUNT];
QLabel *leftOffset[PROFILES_COUNT];
QLabel *rightOffset[PROFILES_COUNT];
QLabel *TLled[PROFILES_COUNT];
QLabel *TRled[PROFILES_COUNT];
QComboBox *irSens[PROFILES_COUNT];
QComboBox *runMode[PROFILES_COUNT];
QComboBox *layoutMode[PROFILES_COUNT];
QPushButton *color[PROFILES_COUNT];
QPushButton *renameBtn[PROFILES_COUNT];
QSvgWidget *centerPic;
QGraphicsScene *testScene;
#define ALIVE_TIMER 5000
//
// ^^^-------GLOBAL VARS UP THERE----------^^^
//
// vvv-------GUI METHODS DOWN HERE---------vvv
//
void guiWindow::PortsSearch()
{
serialFoundList = QSerialPortInfo::availablePorts();
if(serialFoundList.isEmpty()) {
//statusBar()->showMessage("FATAL: No COM devices detected!");
PopupWindow("No devices detected!", "Is the microcontroller board currently running OpenFIRE and is currently plugged in? Make sure it's connected and recognized by the PC.\n\nThis app will now close.", "ERROR", 4);
exit(1);
} else {
// Yeah, sue me, we reading this backwards to make stack management easier.
for(int i = serialFoundList.length() - 1; i >= 0; --i) {
if(serialFoundList[i].vendorIdentifier() == 0xF143) {
usbName.prepend(serialFoundList[i].systemLocation());
qDebug() << "Found device @" << serialFoundList[i].systemLocation();
} else {
qDebug() << "Deleting dummy device" << serialFoundList[i].systemLocation();
serialFoundList.removeAt(i);
}
}
if(!usbName.length()) {
PopupWindow("No OpenFIRE devices detected!", "Is the microcontroller board currently running OpenFIRE and is currently plugged in? Make sure it's connected and recognized by the PC.\n\nThis app will now close.", "ERROR", 4);
exit(1);
}
}
}
guiWindow::guiWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::guiWindow)
{
ui->setupUi(this);
#if !defined(Q_OS_MAC) && !defined(Q_OS_WIN)
if(qEnvironmentVariable("USER") != "root") {
QProcess *externalProg = new QProcess;
QStringList args;
externalProg->start("/usr/bin/groups", args);
externalProg->waitForFinished();
if(!externalProg->readAllStandardOutput().contains("dialout")) {
PopupWindow("User doesn't have serial permissions!", QString("Currently, your user is not allowed to have access to serial devices.\n\nTo add yourself to the right group, run this command in a terminal and then re-login to your session: \n\nsudo usermod -aG dialout %1").arg(qEnvironmentVariable("USER")), "Permission error", 2);
exit(0);
}
} else {
PopupWindow("Running as root is not allowed!", "Please run the OpenFIRE app as a normal user.", "ERROR", 4);
exit(2);
}
#endif
connect(&serialPort, &QSerialPort::readyRead, this, &guiWindow::serialPort_readyRead);
// just to be sure, init the inputsMap hashes
for(uint8_t i = 0; i < boardInputsCount-1; i++) {
inputsMap[i] = -1;
inputsMap_orig[i] = -1;
}
// sending all these children to die upon comPortSelector->on_currentIndexChanged
// (which gets fired immediately after ui->comPortSelector->addItems).
PinsCenter = new QVBoxLayout();
PinsCenterSub = new QGridLayout();
PinsLeft = new QGridLayout();
PinsRight = new QGridLayout();
ui->PinsTopHalf->addLayout(PinsLeft);
ui->PinsTopHalf->addLayout(PinsCenter);
ui->PinsTopHalf->addLayout(PinsRight);
for(uint8_t i = 0; i < 30; i++) {
pinBoxes[i] = new QComboBox();
connect(pinBoxes[i], SIGNAL(activated(int)), this, SLOT(pinBoxes_activated(int)));
pinLabel[i] = new QLabel();
pinLabel[i]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
padding[i] = new QWidget();
padding[i]->setMinimumHeight(25);
}
// These can actually stay, tho.
for(uint8_t i = 0; i < PROFILES_COUNT; i++) {
renameBtn[i] = new QPushButton();
renameBtn[i]->setFlat(true);
renameBtn[i]->setFixedWidth(20);
renameBtn[i]->setIcon(QIcon(":/icon/edit.png"));
connect(renameBtn[i], SIGNAL(clicked()), this, SLOT(renameBoxes_clicked()));
selectedProfile[i] = new QRadioButton(QString("%1.").arg(i+1));
connect(selectedProfile[i], SIGNAL(toggled(bool)), this, SLOT(selectedProfile_isChecked(bool)));
topOffset[i] = new QLabel("0");
bottomOffset[i] = new QLabel("0");
leftOffset[i] = new QLabel("0");
rightOffset[i] = new QLabel("0");
TLled[i] = new QLabel("0");
TRled[i] = new QLabel("0");
irSens[i] = new QComboBox();
runMode[i] = new QComboBox();
layoutMode[i] = new QComboBox();
color[i] = new QPushButton();
topOffset[i]->setAlignment(Qt::AlignCenter);
bottomOffset[i]->setAlignment(Qt::AlignCenter);
leftOffset[i]->setAlignment(Qt::AlignCenter);
rightOffset[i]->setAlignment(Qt::AlignCenter);
TLled[i]->setAlignment(Qt::AlignCenter);
TRled[i]->setAlignment(Qt::AlignCenter);
irSens[i]->addItem("Default");
irSens[i]->addItem("Higher");
irSens[i]->addItem("Highest");
connect(irSens[i], SIGNAL(activated(int)), this, SLOT(irBoxes_activated(int)));
runMode[i]->addItem("Normal");
runMode[i]->addItem("1-Frame Avg");
runMode[i]->addItem("2-Frame Avg");
layoutMode[i]->addItems({"Square", "Diamond"});
connect(layoutMode[i], SIGNAL(activated(int)), this, SLOT(layoutBoxes_activated(int)));
connect(runMode[i], SIGNAL(activated(int)), this, SLOT(runModeBoxes_activated(int)));
color[i]->setFixedWidth(32);
connect(color[i], SIGNAL(clicked()), this, SLOT(colorBoxes_clicked()));
ui->profilesArea->addWidget(renameBtn[i], i+1, 0, 1, 1);
ui->profilesArea->addWidget(selectedProfile[i], i+1, 1, 1, 1);
ui->profilesArea->addWidget(topOffset[i], i+1, 2, 1, 1);
ui->profilesArea->addWidget(bottomOffset[i], i+1, 4, 1, 1);
ui->profilesArea->addWidget(leftOffset[i], i+1, 6, 1, 1);
ui->profilesArea->addWidget(rightOffset[i], i+1, 8, 1, 1);
ui->profilesArea->addWidget(TLled[i], i+1, 10, 1, 1);
ui->profilesArea->addWidget(TRled[i], i+1, 12, 1, 1);
ui->profilesArea->addWidget(irSens[i], i+1, 14, 1, 1);
ui->profilesArea->addWidget(runMode[i], i+1, 16, 1, 1);
ui->profilesArea->addWidget(layoutMode[i], i+1, 18, 1, 1);
ui->profilesArea->addWidget(color[i], i+1, 20, 1, 1);
}
// Setup test screen buttons
for(uint8_t i = 0; i < 16; i++) {
testLabel[i] = new QLabel;
if(i == 14) {
testLabel[i]->setText(valuesNameList[tempPin]);
} else if(i == 15) {
testLabel[i]->setText("Analog Stick");
} else {
testLabel[i]->setText(valuesNameList[i+1]);
}
testLabel[i]->setEnabled(false);
testLabel[i]->setAlignment(Qt::AlignCenter);
testLabel[i]->setFrameStyle(QFrame::Box | QFrame::Raised);
if(i == 15) {
ui->buttonsTestLayout->addWidget(testLabel[i], 3, 3, 1, 1);
} else if(i == 14) {
ui->buttonsTestLayout->addWidget(testLabel[i], 3, 1, 1, 1);
} else if(i > 9) {
ui->buttonsTestLayout->addWidget(testLabel[i], 2, i-10, 1, 1);
} else if(i > 4) {
ui->buttonsTestLayout->addWidget(testLabel[i], 1, i-5, 1, 1);
} else {
ui->buttonsTestLayout->addWidget(testLabel[i], 0, i, 1, 1);
}
}
ui->buttonsTestLayout->setRowMinimumHeight(0, 32);
ui->buttonsTestLayout->setRowMinimumHeight(1, 32);
ui->buttonsTestLayout->setRowMinimumHeight(2, 32);
ui->buttonsTestLayout->setRowMinimumHeight(3, 32);
// Setup Test Mode screen colors
testPointTLPen.setColor(Qt::green);
testPointTRPen.setColor(Qt::green);
testPointBLPen.setColor(Qt::blue);
testPointBRPen.setColor(Qt::blue);
testPointMedPen.setColor(Qt::gray);
testPointDPen.setColor(Qt::red);
testPointTLPen.setWidth(3);
testPointTRPen.setWidth(3);
testPointBLPen.setWidth(3);
testPointBRPen.setWidth(3);
testPointMedPen.setWidth(3);
testPointDPen.setWidth(3);
testPointTL.setPen(testPointTLPen);
testPointTR.setPen(testPointTRPen);
testPointBL.setPen(testPointBLPen);
testPointBR.setPen(testPointBRPen);
testPointMed.setPen(testPointMedPen);
testPointD.setPen(testPointDPen);
// Actually setup the Test Mode scene
testScene = new QGraphicsScene();
testScene->setSceneRect(0, 0, 1024, 768);
testScene->setBackgroundBrush(Qt::darkGray);
ui->testView->setScene(testScene);
testScene->addItem(&testBox);
testScene->addItem(&testPointTL);
testScene->addItem(&testPointTR);
testScene->addItem(&testPointBL);
testScene->addItem(&testPointBR);
testScene->addItem(&testPointMed);
testScene->addItem(&testPointD);
// TODO: is there a way of dynamically scaling QGraphicsViews?
ui->testView->scale(0.5, 0.5);
// hiding tUSB elements by default since this can't be done from default
ui->tUSBLayoutAdvanced->setVisible(false);
// set hidden by default until a board with presets is loaded
ui->presetsBox->setHidden(true);
// Finally get to the thing!
aliveTimer = new QTimer();
connect(aliveTimer, &QTimer::timeout, this, &guiWindow::aliveTimer_timeout);
statusBar()->showMessage("Welcome to the OpenFIRE app!", 3000);
PortsSearch();
usbName.prepend("[No device]");
ui->productIdConverted->setEnabled(false);
ui->productIdInput->setValidator(new QIntValidator());
// TODO: what's a good validator to only accept character values within the range of an unsigned char?
//ui->productNameInput->setValidator(new QRegExpValidator(QRegExp("[A-Za-z0-9_]+"), this));
ui->comPortSelector->addItems(usbName);
}
guiWindow::~guiWindow()
{
if(serialPort.isOpen()) {
statusBar()->showMessage("Sending undock request to board...");
serialPort.write("XE");
serialPort.waitForBytesWritten(2000);
serialPort.waitForReadyRead(2000);
serialPort.close();
}
delete ui;
}
void guiWindow::PopupWindow(QString errorTitle, QString errorMessage, QString windowTitle, int errorType)
{
QMessageBox messageBox;
messageBox.setText(errorTitle);
messageBox.setInformativeText(errorMessage);
messageBox.setWindowTitle(windowTitle);
switch(errorType) {
case 0:
// lol nothing here
break;
case 1:
messageBox.setIcon(QMessageBox::Question);
break;
case 2:
messageBox.setIcon(QMessageBox::Information);
break;
case 3:
messageBox.setIcon(QMessageBox::Warning);
break;
case 4:
messageBox.setIcon(QMessageBox::Critical);
break;
}
messageBox.exec();
// TODO: maybe we should be using Serial Port errors instead of assuming,
// but for now just clear it here for cleanliness.
serialPort.clearError();
}
void guiWindow::SerialLoad()
{
serialActive = true;
serialPort.write("Xlb");
if(serialPort.waitForBytesWritten(2000)) {
if(serialPort.waitForReadyRead(2000)) {
// booleans
QString bufStr = serialPort.readLine().trimmed();
QStringList buffer = bufStr.split(',');
for(uint8_t i = 0; i < boolTypesCount; i++) {
boolSettings[i] = buffer[i].toInt();
boolSettings_orig[i] = boolSettings[i];
}
// pins
if(boolSettings[customPins]) {
serialPort.write("Xlp");
serialPort.waitForBytesWritten(2000);
serialPort.waitForReadyRead(2000);
bufStr = serialPort.readLine().trimmed();
buffer = bufStr.split(',');
for(uint8_t i = 0; i < boardInputsCount-1; i++) {
inputsMap_orig[i] = buffer[i].toInt();
}
inputsMap = inputsMap_orig;
}
// settings
serialPort.write("Xls");
serialPort.waitForBytesWritten(2000);
serialPort.waitForReadyRead(2000);
bufStr = serialPort.readLine().trimmed();
buffer = bufStr.split(',');
for(uint8_t i = 0; i < settingsTypesCount; i++) {
settingsTable[i] = buffer[i].toInt();
settingsTable_orig[i] = settingsTable[i];
}
// profiles
for(uint8_t i = 0; i < PROFILES_COUNT; i++) {
QString genString = QString("XlP%1").arg(i);
serialPort.write(genString.toLocal8Bit());
serialPort.waitForBytesWritten(2000);
serialPort.waitForReadyRead(2000);
bufStr = serialPort.readLine().trimmed();
buffer = bufStr.split(',');
topOffset[i]->setText(buffer[0]), profilesTable[i].topOffset = buffer[0].toInt(), profilesTable_orig[i].topOffset = profilesTable[i].topOffset;
bottomOffset[i]->setText(buffer[1]), profilesTable[i].bottomOffset = buffer[1].toInt(), profilesTable_orig[i].bottomOffset = profilesTable[i].bottomOffset;
leftOffset[i]->setText(buffer[2]), profilesTable[i].leftOffset = buffer[2].toInt(), profilesTable_orig[i].leftOffset = profilesTable[i].leftOffset;
rightOffset[i]->setText(buffer[3]), profilesTable[i].rightOffset = buffer[3].toInt(), profilesTable_orig[i].rightOffset = profilesTable[i].rightOffset;
TLled[i]->setText(buffer[4]), profilesTable[i].TLled = buffer[4].toFloat(), profilesTable_orig[i].TLled = profilesTable[i].TLled;
TRled[i]->setText(buffer[5]), profilesTable[i].TRled = buffer[5].toFloat(), profilesTable_orig[i].TRled = profilesTable[i].TRled;
profilesTable[i].irSensitivity = buffer[6].toInt(), profilesTable_orig[i].irSensitivity = profilesTable[i].irSensitivity, irSens[i]->setCurrentIndex(profilesTable[i].irSensitivity);
profilesTable[i].runMode = buffer[7].toInt(), profilesTable_orig[i].runMode = profilesTable[i].runMode, runMode[i]->setCurrentIndex(profilesTable[i].runMode);
layoutMode[i]->setCurrentIndex(buffer[8].toInt()), profilesTable[i].layoutType = buffer[8].toInt(), profilesTable_orig[i].layoutType = profilesTable[i].layoutType;
color[i]->setStyleSheet(QString("background-color: #%1").arg(buffer[9].toLong(), 6, 16, QLatin1Char('0'))), profilesTable[i].color = buffer[9].toLong(), profilesTable_orig[i].color = profilesTable[i].color;
selectedProfile[i]->setText(buffer[10]), profilesTable[i].profName = buffer[10], profilesTable_orig[i].profName = profilesTable[i].profName;
}
serialActive = false;
} else {
PopupWindow("Data hasn't arrived!", "Device was detected, but settings request wasn't received in time!\nThis can happen if the app was closed in the middle of an operation.\n\nTry selecting the device again.", "Sync Error!", 4);
//qDebug() << "Didn't receive any data in time! Dammit Seong, you jiggled the cable too much again!";
}
} else {
qDebug() << "Couldn't send any data in time! Does the port even exist??? Fucking dammit Seong!?!?!?";
}
}
// Bool returns success (false if failed)
bool guiWindow::SerialInit(int portNum)
{
serialPort.setPort(serialFoundList[portNum]);
serialPort.setBaudRate(QSerialPort::Baud9600);
if(serialPort.open(QIODevice::ReadWrite)) {
qDebug() << "Opened port successfully!";
serialActive = true;
// windows needs DTR enabled to actually read responses.
serialPort.setDataTerminalReady(true);
serialPort.write("XP");
if(serialPort.waitForBytesWritten(2000)) {
if(serialPort.waitForReadyRead(2000)) {
QString bufStr = serialPort.readLine().trimmed();
QStringList buffer = bufStr.split(',');
if(buffer[0].contains("OpenFIRE")) {
qDebug() << "OpenFIRE gun detected!";
board.versionNumber = buffer[1];
qDebug() << "Version number:" << board.versionNumber;
board.versionCodename = buffer[2];
qDebug() << "Version codename:" << board.versionCodename;
if(buffer[3] == "rpipico") {
board.type = rpipico;
} else if(buffer[3] == "rpipicow") {
board.type = rpipicow;
} else if(buffer[3] == "adafruitItsyRP2040") {
board.type = adafruitItsyRP2040;
} else if(buffer[3] == "adafruitKB2040") {
board.type = adafruitKB2040;
} else if(buffer[3] == "arduinoNanoRP2040") {
board.type = arduinoNanoRP2040;
} else if(buffer[3] == "waveshareZero") {
board.type = waveshareZero;
} else if(buffer[3] == "vccgndYD") {
board.type = vccgndYD;
} else {
board.type = generic;
}
board.selectedProfile = buffer[4].toInt();
board.previousProfile = board.selectedProfile;
selectedProfile[board.selectedProfile]->setChecked(true);
serialPort.write("Xli");
serialPort.waitForReadyRead(1000);
bufStr = serialPort.readLine().trimmed();
buffer = bufStr.split(',');
tinyUSBtable.tinyUSBid = buffer[0];
tinyUSBtable_orig.tinyUSBid = tinyUSBtable.tinyUSBid;
if(buffer[1] == "SERIALREADERR01") {
tinyUSBtable.tinyUSBname = "";
} else {
tinyUSBtable.tinyUSBname = buffer[1];
}
tinyUSBtable_orig.tinyUSBname = tinyUSBtable.tinyUSBname;
SerialLoad();
return true;
} else if(buffer[0].contains("Device not available")) {
PopupWindow("Camera not available!", "Device was detected, but data received indicates that the camera is in a bad state.\nThis can happen if the camera wires are crossed (data wire to clock pin, clock wire to data pin).\n\nThe camera must be removed or resoldered to resolve this.", "Device Error!", 3);
return false;
} else {
qDebug() << "Port did not respond with expected response! Seong fucked this up again.";
return false;
}
} else {
PopupWindow("Data hasn't arrived! (Stale state?)", "Device was detected, but initial settings request wasn't received in time!\nThis can happen if the app was unexpectedly closed and the gun is in a stale docked state.\n\nTry selecting the device again.", "Sync Error!", 3);
qDebug() << "Didn't receive any data in time! Dammit Seong, you jiggled the cable too much again!";
return false;
}
} else {
qDebug() << "Couldn't send any data in time! Does the port even exist??? Fucking dammit Seong!?!?!?";
return false;
}
} else {
PopupWindow("Serial port is blocked!", "This usually indicates that the port is being used by something else, e.g. Arduino IDE's serial monitor, or another command line app (stty, screen).\n\nPlease close the offending application and try selecting this port again.", "Port In Use!", 3);
return false;
}
}
void guiWindow::BoxesUpdate()
{
if(boolSettings[customPins]) {
// if the custom pins setting *grabbed from the gun* has been set
if(boolSettings_orig[customPins]) {
// clear map
currentPins.clear();
// set or clear the local pins mapping
for(uint8_t i = 0; i < 30; i++) {
currentPins[i] = btnUnmapped;
}
// (re)-copy pins settings grabbed from the gun to the app catalog
inputsMap = inputsMap_orig;
// else, if the board *was using default maps* before switching to custom
} else {
for(uint8_t i = 0; i < 30; i++) {
if(currentPins[i] > btnUnmapped) {
inputsMap[currentPins[i]-1] = i;
}
}
}
// enable pinboxes
for(uint8_t i = 0; i < 30; i++) {
pinBoxes[i]->setEnabled(true);
}
// copy OF's native inputs map layout to app's current pins layout, copy to pinboxes.
for(uint8_t i = 0; i < boardInputsCount-1; i++) {
if(inputsMap.value(i) >= 0) {
currentPins[inputsMap.value(i)] = i+1;
pinBoxes[inputsMap.value(i)]->setCurrentIndex(currentPins[inputsMap.value(i)]);
}
}
return;
} else {
switch(board.type) {
// Copy preloaded values to current pins map based on board.
// pico and w are the same physical board, so why need a new layout for it?
case rpipico:
case rpipicow:
for(uint8_t i = 0; i < 30; i++) { currentPins[i] = rpipicoLayout[i].pinAssignment; }
break;
case adafruitItsyRP2040:
for(uint8_t i = 0; i < 30; i++) { currentPins[i] = adafruitItsyRP2040Layout[i].pinAssignment; }
break;
case adafruitKB2040:
for(uint8_t i = 0; i < 30; i++) { currentPins[i] = adafruitKB2040Layout[i].pinAssignment; }
break;
case arduinoNanoRP2040:
for(uint8_t i = 0; i < 30; i++) { currentPins[i] = arduinoNanoRP2040Layout[i].pinAssignment; }
break;
case waveshareZero:
for(uint8_t i = 0; i < 30; i++) { currentPins[i] = waveshareZeroLayout[i].pinAssignment; }
break;
}
// assign pinboxes from custom pins map
for(uint8_t i = 0; i < 30; i++) {
pinBoxes[i]->setCurrentIndex(currentPins[i]);
pinBoxes[i]->setEnabled(false);
}
// convert app's current pins map (each pin = 1 function) to OF's native input map layout (each function = 1 pin)
for(uint8_t i = 0; i < 30; i++) {
if(currentPins[i] > btnUnmapped) {
inputsMap[currentPins[i]-1] = i;
}
}
}
}
void guiWindow::DiffUpdate()
{
settingsDiff = 0;
if(boolSettings_orig[customPins] != boolSettings[customPins]) {
settingsDiff++;
}
if(boolSettings[customPins]) {
if(inputsMap_orig != inputsMap) {
settingsDiff++;
}
}
for(uint8_t i = 1; i < boolTypesCount; i++) {
if(boolSettings_orig[i] != boolSettings[i]) {
settingsDiff++;
}
}
for(uint8_t i = 0; i < settingsTypesCount; i++) {
if(settingsTable_orig[i] != settingsTable[i]) {
settingsDiff++;
}
}
if(tinyUSBtable_orig.tinyUSBid != tinyUSBtable.tinyUSBid) {
settingsDiff++;
}
if(tinyUSBtable_orig.tinyUSBname != tinyUSBtable.tinyUSBname) {
settingsDiff++;
}
if(board.selectedProfile != board.previousProfile) {
settingsDiff++;
}
for(uint8_t i = 0; i < PROFILES_COUNT; i++) {
if(profilesTable_orig[i].profName != profilesTable[i].profName) {
settingsDiff++;
}
if(profilesTable_orig[i].topOffset != profilesTable[i].topOffset) {
settingsDiff++;
}
if(profilesTable_orig[i].bottomOffset != profilesTable[i].bottomOffset) {
settingsDiff++;
}
if(profilesTable_orig[i].leftOffset != profilesTable[i].leftOffset) {
settingsDiff++;
}
if(profilesTable_orig[i].rightOffset != profilesTable[i].rightOffset) {
settingsDiff++;
}
if(profilesTable_orig[i].TLled != profilesTable[i].TLled) {
settingsDiff++;
}
if(profilesTable_orig[i].TRled != profilesTable[i].TRled) {
settingsDiff++;
}
if(profilesTable_orig[i].irSensitivity != profilesTable[i].irSensitivity) {
settingsDiff++;
}
if(profilesTable_orig[i].runMode != profilesTable[i].runMode) {
settingsDiff++;
}
if(profilesTable_orig[i].layoutType != profilesTable[i].layoutType) {
settingsDiff++;
}
if(profilesTable_orig[i].color != profilesTable[i].color) {
settingsDiff++;
}
}
if(settingsDiff) {
ui->confirmButton->setText("Save and Send Settings");
ui->confirmButton->setEnabled(true);
} else {
ui->confirmButton->setText("[Nothing To Save]");
ui->confirmButton->setEnabled(false);
}
}
void guiWindow::SyncSettings()
{
for(uint8_t i = 0; i < boolTypesCount; i++) {
boolSettings_orig[i] = boolSettings[i];
}
if(boolSettings_orig[customPins]) {
inputsMap_orig = inputsMap;
} else {
for(uint8_t i = 0; i < boardInputsCount-1; i++)
inputsMap_orig[i] = -1;
}
for(uint8_t i = 0; i < settingsTypesCount; i++) {
settingsTable_orig[i] = settingsTable[i];
}
tinyUSBtable_orig.tinyUSBid = tinyUSBtable.tinyUSBid;
tinyUSBtable_orig.tinyUSBname = tinyUSBtable.tinyUSBname;
board.previousProfile = board.selectedProfile;
for(uint8_t i = 0; i < PROFILES_COUNT; i++) {
profilesTable_orig[i].irSensitivity = profilesTable[i].irSensitivity;
profilesTable_orig[i].runMode = profilesTable[i].runMode;
profilesTable_orig[i].layoutType = profilesTable[i].layoutType;
profilesTable_orig[i].color = profilesTable[i].color;
profilesTable_orig[i].profName = profilesTable[i].profName;
}
LabelsUpdate();
}
QString PrettifyName()
{
QString name;
if(!tinyUSBtable.tinyUSBname.isEmpty()) {
name = tinyUSBtable.tinyUSBname;
} else {
name = "Unnamed Device";
}
// append name of board to gun name string.
switch(board.type) {
case nothing:
name = "";
break;
case rpipico:
name = name + " | Raspberry Pi Pico";
break;
case rpipicow:
name = name + " | Raspberry Pi Pico W";
break;
case adafruitItsyRP2040:
name = name + " | Adafruit ItsyBitsy RP2040";
break;
case adafruitKB2040:
name = name + " | Adafruit KB2040";
break;
case arduinoNanoRP2040:
name = name + " | Arduino Nano RP2040 Connect";
break;
case waveshareZero:
name = name + " | Waveshare RP2040 Zero";
break;
case generic:
name = name + " | Generic RP2040 Board";
break;
}
return name;
}
void guiWindow::PixelsDiff()
{
if(settingsTable[customLEDcount] == settingsTable_orig[customLEDcount] &&
settingsTable[customLEDstatic] == settingsTable_orig[customLEDstatic] &&
settingsTable[customLEDcolor1] == settingsTable_orig[customLEDcolor1] &&
settingsTable[customLEDcolor2] == settingsTable_orig[customLEDcolor2] &&
settingsTable[customLEDcolor3] == settingsTable_orig[customLEDcolor3]) {
ui->pixelChangeNotice->setVisible(false);
} else {
ui->pixelChangeNotice->setVisible(true);
}
}
void guiWindow::on_confirmButton_clicked()
{
QMessageBox messageBox(QMessageBox::Information, "Commit Confirmation", "Are these settings okay?", QMessageBox::Yes | QMessageBox::No);
messageBox.setInformativeText("These settings will be committed to your lightgun. Is that okay?");
if(messageBox.exec() == QMessageBox::Yes) {
if(serialPort.isOpen()) {
serialActive = true;
aliveTimer->stop();
// send a signal so the gun pauses its test outputs for the save op.
serialPort.write("Xm");
serialPort.waitForBytesWritten(1000);
QProgressBar *statusProgressBar = new QProgressBar();
ui->statusBar->addPermanentWidget(statusProgressBar);
ui->tabWidget->setEnabled(false);
ui->comPortSelector->setEnabled(false);
ui->confirmButton->setEnabled(false);
QStringList serialQueue;
for(uint8_t i = 0; i < boolTypesCount; i++) {
serialQueue.append(QString("Xm.0.%1.%2").arg(i).arg(boolSettings[i]));
}
if(boolSettings[customPins]) {
for(uint8_t i = 0; i < boardInputsCount-1; i++) {
serialQueue.append(QString("Xm.1.%1.%2").arg(i).arg(inputsMap.value(i)));
}
}
for(uint8_t i = 0; i < settingsTypesCount; i++) {
serialQueue.append(QString("Xm.2.%1.%2").arg(i).arg(settingsTable[i]));
}
serialQueue.append(QString("Xm.3.0.%1").arg(tinyUSBtable.tinyUSBid));
if(!tinyUSBtable.tinyUSBname.isEmpty()) {
serialQueue.append(QString("Xm.3.1.%1").arg(tinyUSBtable.tinyUSBname));
}
for(uint8_t i = 0; i < 4; i++) {
serialQueue.append(QString("Xm.P.i.%1.%2").arg(i).arg(profilesTable[i].irSensitivity));
serialQueue.append(QString("Xm.P.r.%1.%2").arg(i).arg(profilesTable[i].runMode));
serialQueue.append(QString("Xm.P.l.%1.%2").arg(i).arg(profilesTable[i].layoutType));
serialQueue.append(QString("Xm.P.c.%1.%2").arg(i).arg(profilesTable[i].color));
serialQueue.append(QString("Xm.P.n.%1.%2").arg(i).arg(profilesTable[i].profName));
}
serialQueue.append("XS");
statusProgressBar->setRange(0, serialQueue.length()-1);
bool success = true;
// throw out whatever's in the buffer if there's anything there.
while(!serialPort.atEnd()) {
serialPort.readLine();
}
for(uint8_t i = 0; i < serialQueue.length(); i++) {
serialPort.write(serialQueue[i].toLocal8Bit());
serialPort.waitForBytesWritten(2000);
if(serialPort.waitForReadyRead(2000)) {
QString buffer = serialPort.readLine();
if(buffer.contains("OK:") || buffer.contains("NOENT:")) {
statusProgressBar->setValue(statusProgressBar->value() + 1);
} else if(i == serialQueue.length() - 1 && buffer.contains("Saving preferences...")) {
for(uint8_t t = 0; t < 3; t++) {
if(serialPort.atEnd()) { serialPort.waitForReadyRead(2000); }
buffer = serialPort.readLine();
if(buffer.contains("Settings saved to")) {
success = true;
t = 3;
}
}
if(success) {
while(!serialPort.atEnd()) {
serialPort.readLine();
}
}
}
}
}
ui->statusBar->removeWidget(statusProgressBar);
delete statusProgressBar;
ui->tabWidget->setEnabled(true);
ui->comPortSelector->setEnabled(true);
if(!success) { qDebug() << "Ah shit, it failed! What did you do, Seong?"; }
else {
statusBar()->showMessage("Sent settings successfully!", 5000);
SyncSettings();
PixelsDiff();
DiffUpdate();
ui->boardLabel->setText(PrettifyName());
}
serialActive = false;
aliveTimer->start(ALIVE_TIMER);
serialQueue.clear();
if(!serialPort.atEnd()) {
serialPort.readAll();
}
} else { qDebug() << "Wait, this port wasn't open to begin with!!! WTF SEONG!?!?"; }
} else { statusBar()->showMessage("Save operation canceled.", 3000); }
}
void guiWindow::aliveTimer_timeout()
{
if(serialPort.isOpen()) {
serialPort.write(".");
if(!serialPort.waitForBytesWritten(1)) {
statusBar()->showMessage("Board hasn't responded to pulse; assuming it's been disconnected.");
serialPort.close();
ui->comPortSelector->setCurrentIndex(0);
}
}
}
void guiWindow::on_comPortSelector_currentIndexChanged(int index)
{
// Indiscriminately clears the board layout views.
// yes, every time. goddammit QT.
// fuck it, it works until QT provides a better mechanism to remove widgets without deleting them.
if(pinBoxes[0]->count() > 0) {
for(uint8_t i = 0; i < 30; i++) {
pinBoxes[i]->clear();
delete pinBoxes[i];
delete padding[i];
delete pinLabel[i];
}
delete centerPic;
}
delete PinsCenter;
delete PinsLeft;
delete PinsRight;
PinsCenter = new QVBoxLayout();
PinsCenterSub = new QGridLayout();
PinsLeft = new QGridLayout();
PinsRight = new QGridLayout();
ui->PinsTopHalf->addLayout(PinsLeft);
ui->PinsTopHalf->addLayout(PinsCenter);
ui->PinsTopHalf->addLayout(PinsRight);
for(uint8_t i = 0; i < 30; i++) {
pinBoxes[i] = new QComboBox();
pinBoxes[i]->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
connect(pinBoxes[i], SIGNAL(activated(int)), this, SLOT(pinBoxes_activated(int)));
padding[i] = new QWidget();
padding[i]->setMinimumHeight(25);
// I2C channel coloring
if(i & 0b0000010) { pinLabel[i] = new QLabel(QString("<font color=#FF8800>«GPIO%1»</font>").arg(i)); }
else { pinLabel[i] = new QLabel(QString("<font color=#0099FF>«GPIO%1»</font>").arg(i)); }
pinLabel[i]->setEnabled(false);
pinLabel[i]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
pinLabel[i]->setToolTip(QString("GPIO Pin number %1\n\nBlue pin numbers are members of I2C0\nOrange are members of I2C1").arg(i));
}
if(index > 0) {
qDebug() << "COM port set to" << ui->comPortSelector->currentIndex();
// Clear stale states if any, and unmount old board if mounted.
if(testMode) {
testMode = false;
ui->testView->setEnabled(false);
ui->buttonsTestArea->setEnabled(true);
ui->testBtn->setText("Enable IR Test Mode");
ui->pinsTab->setEnabled(true);
ui->settingsTab->setEnabled(true);
ui->profilesTab->setEnabled(true);
ui->feedbackTestsBox->setEnabled(true);
ui->dangerZoneBox->setEnabled(true);
serialActive = false;
}
if(serialPort.isOpen()) {
serialActive = true;
serialPort.write("XE");
serialPort.waitForBytesWritten(2000);
serialPort.waitForReadyRead(2000);
serialPort.readAll();
serialPort.close();
serialActive = false;
}
// try to init serial port
// if returns false, it failed, so just turn the index back to initial.
if(!SerialInit(index - 1)) {
ui->comPortSelector->setCurrentIndex(0);
aliveTimer->stop();
// else, serial port is online! What do we got?
} else {
aliveTimer->start(ALIVE_TIMER);
ui->versionLabel->setText(QString("v%1 - \"%2\"").arg(board.versionNumber, board.versionCodename));
BoxesFill();
LabelsUpdate();
switch(board.type) {
case rpipico:
{
centerPic = new QSvgWidget(":/boardPics/pico.svg");
QSvgRenderer *picRenderer = centerPic->renderer();
picRenderer->setAspectRatioMode(Qt::KeepAspectRatio);
ui->boardLabel->setText(PrettifyName());
// left side
PinsLeft->addWidget(padding[0], 0, 0); // padding
PinsLeft->addWidget(pinBoxes[0], 1, 0), PinsLeft->addWidget(pinLabel[0], 1, 1);
PinsLeft->addWidget(pinBoxes[1], 2, 0), PinsLeft->addWidget(pinLabel[1], 2, 1);
PinsLeft->addWidget(padding[1], 3, 0); // gnd
PinsLeft->addWidget(pinBoxes[2], 4, 0), PinsLeft->addWidget(pinLabel[2], 4, 1);
PinsLeft->addWidget(pinBoxes[3], 5, 0), PinsLeft->addWidget(pinLabel[3], 5, 1);
PinsLeft->addWidget(pinBoxes[4], 6, 0), PinsLeft->addWidget(pinLabel[4], 6, 1);
PinsLeft->addWidget(pinBoxes[5], 7, 0), PinsLeft->addWidget(pinLabel[5], 7, 1);
PinsLeft->addWidget(padding[2], 8, 0); // gnd
PinsLeft->addWidget(pinBoxes[6], 9, 0), PinsLeft->addWidget(pinLabel[6], 9, 1);
PinsLeft->addWidget(pinBoxes[7], 10, 0), PinsLeft->addWidget(pinLabel[7], 10, 1);
PinsLeft->addWidget(pinBoxes[8], 11, 0), PinsLeft->addWidget(pinLabel[8], 11, 1);
PinsLeft->addWidget(pinBoxes[9], 12, 0), PinsLeft->addWidget(pinLabel[9], 12, 1);
PinsLeft->addWidget(padding[3], 13, 0); // gnd
PinsLeft->addWidget(pinBoxes[10], 14, 0), PinsLeft->addWidget(pinLabel[10], 14, 1);
PinsLeft->addWidget(pinBoxes[11], 15, 0), PinsLeft->addWidget(pinLabel[11], 15, 1);
PinsLeft->addWidget(pinBoxes[12], 16, 0), PinsLeft->addWidget(pinLabel[12], 16, 1);
PinsLeft->addWidget(pinBoxes[13], 17, 0), PinsLeft->addWidget(pinLabel[13], 17, 1);
PinsLeft->addWidget(padding[4], 18, 0); // gnd
PinsLeft->addWidget(pinBoxes[14], 19, 0), PinsLeft->addWidget(pinLabel[14], 19, 1);
PinsLeft->addWidget(pinBoxes[15], 20, 0), PinsLeft->addWidget(pinLabel[15], 20, 1);
// right side
PinsRight->addWidget(padding[5], 0, 1); // padding
PinsRight->addWidget(padding[6], 1, 1); // VBUS
PinsRight->addWidget(padding[7], 2, 1); // VSYS
PinsRight->addWidget(padding[8], 3, 1); // gnd
PinsRight->addWidget(padding[9], 4, 1); // 3V3 EN
PinsRight->addWidget(padding[10], 5, 1); // 3V3 OUT
PinsRight->addWidget(padding[11], 6, 1); // ADC VREF
PinsRight->addWidget(pinBoxes[28], 7, 1), PinsRight->addWidget(pinLabel[28], 7, 0);
PinsRight->addWidget(padding[12], 8, 1); // gnd
PinsRight->addWidget(pinBoxes[27], 9, 1), PinsRight->addWidget(pinLabel[27], 9, 0);
PinsRight->addWidget(pinBoxes[26], 10, 1), PinsRight->addWidget(pinLabel[26], 10, 0);
PinsRight->addWidget(padding[13], 11, 1); // RUN
PinsRight->addWidget(pinBoxes[22], 12, 1), PinsRight->addWidget(pinLabel[22], 12, 0);
PinsRight->addWidget(padding[14], 13, 1); // gnd
PinsRight->addWidget(pinBoxes[21], 14, 1), PinsRight->addWidget(pinLabel[21], 14, 0);
PinsRight->addWidget(pinBoxes[20], 15, 1), PinsRight->addWidget(pinLabel[20], 15, 0);
PinsRight->addWidget(pinBoxes[19], 16, 1), PinsRight->addWidget(pinLabel[19], 16, 0);
PinsRight->addWidget(pinBoxes[18], 17, 1), PinsRight->addWidget(pinLabel[18], 17, 0);
PinsRight->addWidget(padding[17], 18, 1); // gnd
PinsRight->addWidget(pinBoxes[17], 19, 1), PinsRight->addWidget(pinLabel[17], 19, 0);
PinsRight->addWidget(pinBoxes[16], 20, 1), PinsRight->addWidget(pinLabel[16], 20, 0);
// center
PinsCenter->addWidget(centerPic);
centerPic->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
break;
}
case rpipicow:
{
centerPic = new QSvgWidget(":/boardPics/picow.svg");
QSvgRenderer *picRenderer = centerPic->renderer();
picRenderer->setAspectRatioMode(Qt::KeepAspectRatio);
ui->boardLabel->setText(PrettifyName());
// left side
PinsLeft->addWidget(padding[0], 0, 0); // padding
PinsLeft->addWidget(pinBoxes[0], 1, 0), PinsLeft->addWidget(pinLabel[0], 1, 1);
PinsLeft->addWidget(pinBoxes[1], 2, 0), PinsLeft->addWidget(pinLabel[1], 2, 1);
PinsLeft->addWidget(padding[1], 3, 0); // gnd
PinsLeft->addWidget(pinBoxes[2], 4, 0), PinsLeft->addWidget(pinLabel[2], 4, 1);
PinsLeft->addWidget(pinBoxes[3], 5, 0), PinsLeft->addWidget(pinLabel[3], 5, 1);