forked from vedderb/vesc_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·1595 lines (1385 loc) · 56.9 KB
/
main.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
/*
Copyright 2016 - 2023 Benjamin Vedder [email protected]
This file is part of VESC Tool.
VESC Tool 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.
VESC Tool 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 <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "boardsetupwindow.h"
#include "mobile/qmlui.h"
#include "mobile/fwhelper.h"
#include "mobile/vesc3ditem.h"
#include "mobile/logwriter.h"
#include "mobile/logreader.h"
#include "tcpserversimple.h"
#include "pages/pagemotorcomparison.h"
#include "codeloader.h"
#include "configparam.h"
#include "utility.h"
#include "heatshrink/heatshrinkif.h"
#include "minimp3/qminimp3.h"
#include <QApplication>
#include <QStyleFactory>
#include <QSettings>
#include <QDesktopWidget>
#include <QFontDatabase>
#include <QPixmapCache>
#include "tcphub.h"
#ifndef HAS_BLUETOOTH
#include "bleuartdummy.h"
#endif
#ifdef Q_OS_IOS
#include "ios/src/setIosParameters.h"
#endif
#ifdef Q_OS_LINUX
#include <signal.h>
#include <systemcommandexecutor.h>
#endif
#ifndef USE_MOBILE
#include <QProxyStyle>
#include <QtConcurrent/QtConcurrent>
// Disables focus drawing for all widgets
class Style_tweaks : public QProxyStyle
{
public:
using QProxyStyle::QProxyStyle;
void drawPrimitive(PrimitiveElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget) const
{
if (element == QStyle::PE_FrameFocusRect) return;
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
};
static void showHelp()
{
qDebug() << "Arguments";
qDebug() << "-h, --help : Show help text";
qDebug() << "--tcpServer [port] : Connect to VESC and start TCP server on [port]";
qDebug() << "--loadQml [file] : Load QML UI from file instead of the regular VESC Tool UI";
qDebug() << "--loadQmlVesc : Load QML UI from the connected VESC instead of the regular VESC Tool UI";
qDebug() << "--qmlAutoConn : Connect over USB before loading the QML UI";
qDebug() << "--qmlFullscreen : Run QML UI in fullscreen mode";
qDebug() << "--qmlOtherScreen : Run QML UI on other screen";
qDebug() << "--qmlRotation [deg] : Rotate screen by deg degrees";
qDebug() << "--qmlWindowSize [width:height] : Specify qml window size";
qDebug() << "--retryConn : Keep trying to reconnect to the VESC when the connection fails";
qDebug() << "--useMobileUi : Start the mobile UI instead of the full desktop UI";
qDebug() << "--tcpHub [port] : Start a TCP hub for remote access to connected VESCs";
qDebug() << "--buildPkg [pkgPath:lispPath:qmlPath:isFullscreen:optMd:optName] : Build VESC Package";
qDebug() << "--useBoardSetupWindow : Start board setup window instead of the main UI";
qDebug() << "--xmlConfToCode [xml-file] : Generate C code from XML configuration file (the files are saved in the same directory as the XML)";
qDebug() << "--vescPort [port] : VESC Port for commands that connect, e.g. /dev/ttyACM0. If this command is left out autoconnect will be used.";
qDebug() << "--canFwd [canId] : Can ID for CAN forwarding";
qDebug() << "--getMcConf [confPath] : Connect and read motor configuration and store the XML to confPath.";
qDebug() << "--setMcConf [confPath] : Connect and write motor configuration XML from confPath.";
qDebug() << "--getAppConf [confPath] : Connect and read app configuration and store the XML to confPath.";
qDebug() << "--setAppConf [confPath] : Connect and write app configuration XML from confPath.";
qDebug() << "--getCustomConf [confPath] : Connect and read custom configuration 1 and store the XML to confPath.";
qDebug() << "--setCustomConf [confPath] : Connect and write custom configuration 1 XML from confPath.";
qDebug() << "--debugOutFile [path] : Print debug output to file with path.";
qDebug() << "--uploadLisp [path] : Upload lisp-script.";
qDebug() << "--eraseLisp : Erase lisp-script.";
qDebug() << "--uploadFirmware [path] : Upload firmware-file from path.";
qDebug() << "--uploadBootloaderBuiltin : Upload bootloader from generic included bootloaders.";
qDebug() << "--writeFileToSdCard [fileLocal:pathSdcard] : Write file to SD-card.";
qDebug() << "--packFirmware [fileIn:fileOut] : Pack firmware-file for compatibility with the bootloader. ";
qDebug() << "--packLisp [fileIn:fileOut] : Pack lisp-file and the included imports.";
qDebug() << "--bridgeAppData : Send app data (such as data from send-data in lisp) to stdout.";
qDebug() << "--offscreen : Use offscreen QPA so that X is not required for the CLI-mode.";
qDebug() << "--downloadPackageArchive : Download package archive to application data directory.";
}
#ifdef Q_OS_LINUX
static void m_cleanup(int sig)
{
(void)sig;
qApp->quit();
}
#endif
#endif
QFile m_debug_msg_file;
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
(void)type;
(void)context;
if (m_debug_msg_file.isOpen()) {
m_debug_msg_file.write(msg.toUtf8());
m_debug_msg_file.write("\n");
m_debug_msg_file.flush();
}
}
static void addFonts() {
QFontDatabase::addApplicationFont("://res/fonts/DejaVuSans.ttf");
QFontDatabase::addApplicationFont("://res/fonts/DejaVuSans-Bold.ttf");
QFontDatabase::addApplicationFont("://res/fonts/DejaVuSans-BoldOblique.ttf");
QFontDatabase::addApplicationFont("://res/fonts/DejaVuSans-Oblique.ttf");
QFontDatabase::addApplicationFont("://res/fonts/DejaVuSansMono.ttf");
QFontDatabase::addApplicationFont("://res/fonts/DejaVuSansMono-Bold.ttf");
QFontDatabase::addApplicationFont("://res/fonts/DejaVuSansMono-BoldOblique.ttf");
QFontDatabase::addApplicationFont("://res/fonts/DejaVuSansMono-Oblique.ttf");
QFontDatabase::addApplicationFont("://res/fonts/Roboto/Roboto-Regular.ttf");
QFontDatabase::addApplicationFont("://res/fonts/Roboto/Roboto-Medium.ttf");
QFontDatabase::addApplicationFont("://res/fonts/Roboto/Roboto-Bolf.ttf");
QFontDatabase::addApplicationFont("://res/fonts/Roboto/Roboto-BoldItalic.ttf");
QFontDatabase::addApplicationFont("://res/fonts/Roboto/Roboto-Italic.ttf");
QFontDatabase::addApplicationFont(":/res/fonts/Roboto/RobotoMono-VariableFont_wght.ttf");
QFontDatabase::addApplicationFont("://res/fonts/Exan-Regular.ttf");
qApp->setFont(QFont("Roboto", 12));
}
int main(int argc, char *argv[])
{
// Settings
QCoreApplication::setOrganizationName("VESC");
QCoreApplication::setOrganizationDomain("vesc-project.com");
QCoreApplication::setApplicationName("VESC Tool");
QSettings set;
bool isDark = set.value("darkMode", true).toBool();
Utility::setDarkMode(isDark);
QPixmapCache::setCacheLimit(256000);
if (isDark) {
qputenv("QT_QUICK_CONTROLS_CONF", ":/qtquickcontrols2_dark.conf");
Utility::setAppQColor("lightestBackground", QColor(80,80,80));
Utility::setAppQColor("lightBackground", QColor(72,72,72));
Utility::setAppQColor("normalBackground", QColor(48,48,48));
Utility::setAppQColor("darkBackground", QColor(39,39,39));
Utility::setAppQColor("plotBackground", QColor(39,39,39));
Utility::setAppQColor("normalText", QColor(180,180,180));
Utility::setAppQColor("lightText", QColor(215,215,215));
Utility::setAppQColor("disabledText", QColor(127,127,127));
Utility::setAppQColor("lightAccent", QColor(0,161,221));
Utility::setAppQColor("tertiary1",QColor(229, 207, 51));
Utility::setAppQColor("tertiary2",QColor(51, 180, 229));
Utility::setAppQColor("tertiary3",QColor(136, 51, 229));
Utility::setAppQColor("midAccent", QColor(0,98,153));
Utility::setAppQColor("darkAccent", QColor(0,69,112));
Utility::setAppQColor("pink", QColor(219,98,139));
Utility::setAppQColor("red", QColor(200,52,52));
Utility::setAppQColor("orange", QColor(206,125,44));
Utility::setAppQColor("yellow", QColor(210,210,127));
Utility::setAppQColor("green", QColor(127,200,127));
Utility::setAppQColor("cyan",QColor(79,203,203));
Utility::setAppQColor("blue", QColor(77,127,196));
Utility::setAppQColor("magenta", QColor(157,127,210));
Utility::setAppQColor("white", QColor(255,255,255));
Utility::setAppQColor("black", QColor(0,0,0));
} else {
qputenv("QT_QUICK_CONTROLS_CONF", ":/qtquickcontrols2.conf");
Utility::setAppQColor("lightestBackground", QColor(200,200,200));
Utility::setAppQColor("lightBackground", QColor(225,225,225));
Utility::setAppQColor("normalBackground", QColor(240,240,240));
Utility::setAppQColor("darkBackground", QColor(255,255,255));
Utility::setAppQColor("plotBackground", QColor(250,250,250));
Utility::setAppQColor("normalText", QColor(60,20,60));
Utility::setAppQColor("lightText", QColor(33,33,33));
Utility::setAppQColor("disabledText", QColor(110,110,110));
Utility::setAppQColor("lightAccent", QColor(0,114,178));
Utility::setAppQColor("tertiary1",QColor(229, 207, 51));
Utility::setAppQColor("tertiary2",QColor(51, 180, 229));
Utility::setAppQColor("tertiary3",QColor(136, 51, 229));
Utility::setAppQColor("midAccent", QColor(0,114,178));
Utility::setAppQColor("darkAccent", QColor(0,161,221));
Utility::setAppQColor("pink", QColor(219,98,139));
Utility::setAppQColor("red", QColor(200,52,52));
Utility::setAppQColor("orange", QColor(206,125,44));
Utility::setAppQColor("yellow", QColor(210,210,127));
Utility::setAppQColor("green", QColor(127,200,127));
Utility::setAppQColor("cyan",QColor(79,203,203));
Utility::setAppQColor("blue", QColor(77,127,196));
Utility::setAppQColor("magenta", QColor(157,127,210));
Utility::setAppQColor("white", QColor(255,255,255));
Utility::setAppQColor("black", QColor(0,0,0));
}
// DPI settings
// TODO: http://www.qcustomplot.com/index.php/support/forum/1344
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#ifdef HAS_BLUETOOTH
qmlRegisterType<BleUart>("Vedder.vesc.bleuart", 1, 0, "BleUart");
#else
qmlRegisterType<BleUartDummy>("Vedder.vesc.bleuart", 1, 0, "BleUart");
#endif
qmlRegisterType<Commands>("Vedder.vesc.commands", 1, 0, "Commands");
qmlRegisterType<ConfigParams>("Vedder.vesc.configparams", 1, 0, "ConfigParams");
qmlRegisterType<FwHelper>("Vedder.vesc.fwhelper", 1, 0, "FwHelper");
qmlRegisterType<Esp32Flash>("Vedder.vesc.esp32flash", 1, 0, "Esp32Flash");
qmlRegisterType<TcpServerSimple>("Vedder.vesc.tcpserversimple", 1, 0, "TcpServerSimple");
qmlRegisterType<UdpServerSimple>("Vedder.vesc.udpserversimple", 1, 0, "UdpServerSimple");
qmlRegisterType<Vesc3dItem>("Vedder.vesc.vesc3ditem", 1, 0, "Vesc3dItem");
qmlRegisterType<LogWriter>("Vedder.vesc.logwriter", 1, 0, "LogWriter");
qmlRegisterType<LogReader>("Vedder.vesc.logreader", 1, 0, "LogReader");
qmlRegisterType<TcpHub>("Vedder.vesc.tcphub", 1, 0, "TcpHub");
qmlRegisterType<CodeLoader>("Vedder.vesc.codeloader", 1, 0, "CodeLoader");
qmlRegisterType<QMiniMp3>("Vedder.vesc.qminimp3", 1, 0, "QMiniMp3");
#ifdef Q_OS_LINUX
qmlRegisterType<SystemCommandExecutor>("Vedder.vesc.syscmd", 1, 0, "SysCmd");
#endif
qRegisterMetaType<VSerialInfo_t>();
qRegisterMetaType<MCCONF_TEMP>();
qRegisterMetaType<MC_VALUES>();
qRegisterMetaType<BMS_VALUES>();
qRegisterMetaType<FW_RX_PARAMS>();
qRegisterMetaType<PSW_STATUS>();
qRegisterMetaType<IO_BOARD_VALUES>();
qRegisterMetaType<MotorData>();
qRegisterMetaType<ENCODER_DETECT_RES>();
qRegisterMetaType<FILE_LIST_ENTRY>();
qRegisterMetaType<VescPackage>();
qRegisterMetaType<TCP_HUB_DEVICE>();
qRegisterMetaType<ConfigParam>();
qRegisterMetaType<GNSS_DATA>();
qRegisterMetaType<MiniMp3Dec>();
#ifdef USE_MOBILE
#ifndef DEBUG_BUILD
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
#else
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#ifdef Q_OS_LINUX
signal(SIGINT, m_cleanup);
signal(SIGTERM, m_cleanup);
#endif
// Parse command line arguments
QStringList args;
for (int i = 0;i < argc;i++) {
args.append(argv[i]);
}
bool useTcp = false;
bool retryConn = false;
int tcpPort = 65102;
QString loadQml = "";
bool qmlAutoConn = false;
bool qmlFullscreen = false;
bool loadQmlVesc = false;
bool qmlOtherScreen = false;
bool useMobileUi = false;
bool useBoardSetupWindow = false;
double qmlRot = 0.0;
bool isTcpHub = false;
QStringList pkgArgs;
QString xmlCodePath = "";
QString vescPort = "";
int canFwd = -1;
QString getMcConfPath = "";
QString setMcConfPath = "";
QString getAppConfPath = "";
QString setAppConfPath = "";
QString getCustomConfPath = "";
QString setCustomConfPath = "";
QSize qmlWindowSize = QSize(-1, -1);
QString lispPath = "";
bool eraseLisp = false;
QString firmwarePath = "";
bool uploadBootloaderBuiltin = false;
QString fwPackIn = "";
QString fwPackOut = "";
QString fileForSdIn = "";
QString fileForSdOut = "";
QString lispPackIn = "";
QString lispPackOut = "";
bool bridgeAppData = false;
bool offscreen = false;
bool downloadPackageArchive = false;
// Arguments can be hard-coded in a build like this:
// qmlWindowSize = QSize(400, 800);
// loadQmlVesc = true;
// retryConn = true;
for (int i = 0;i < args.size();i++) {
// Skip the program argument
if (i == 0) {
continue;
}
QString str = args.at(i);
// Skip path argument
if (i >= args.size() && args.size() >= 3) {
break;
}
bool dash = str.startsWith("-") && !str.startsWith("--");
bool found = false;
if ((dash && str.contains('h')) || str == "--help") {
showHelp();
return 0;
}
if (str == "--tcpServer") {
if ((i + 1) < args.size()) {
i++;
tcpPort = args.at(i).toInt();
useTcp = true;
found = true;
}
}
if (str == "--retryConn") {
retryConn = true;
found = true;
}
if (str == "--loadQml") {
if ((i + 1) < args.size()) {
i++;
loadQml = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path to qml UI file";
return 1;
}
}
if (str == "--loadQmlVesc") {
loadQmlVesc = true;
found = true;
}
if (str == "--qmlAutoConn") {
qmlAutoConn = true;
found = true;
}
if (str == "--qmlFullscreen") {
qmlFullscreen = true;
found = true;
}
if (str == "--qmlOtherScreen") {
qmlOtherScreen = true;
found = true;
}
if (str == "--useMobileUi") {
useMobileUi = true;
found = true;
}
if (str == "--useBoardSetupWindow") {
useBoardSetupWindow = true;
found = true;
}
if (str.startsWith("-qmljsdebugger")) {
found = true;
}
if (str == "--qmlRotation") {
if ((i + 1) < args.size()) {
i++;
qmlRot = args.at(i).toDouble();
found = true;
} else {
i++;
qCritical() << "No rotation specified";
return 1;
}
}
if (str == "--qmlWindowSize") {
if ((i + 1) < args.size()) {
i++;
auto p = args.at(i).split(":");
if (p.size() == 2) {
qmlWindowSize.setWidth(p.at(0).toInt());
qmlWindowSize.setHeight(p.at(1).toInt());
} else {
qCritical() << "Invalid size specified";
return 1;
}
found = true;
} else {
i++;
qCritical() << "No size specified";
return 1;
}
}
if (str == "--tcpHub") {
if ((i + 1) < args.size()) {
i++;
tcpPort = args.at(i).toInt();
isTcpHub = true;
found = true;
}
}
if (str == "--buildPkg") {
if ((i + 1) < args.size()) {
i++;
pkgArgs = args.at(i).split(":");
found = true;
}
}
if (str == "--xmlConfToCode") {
if ((i + 1) < args.size()) {
i++;
xmlCodePath = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path to xml file";
return 1;
}
}
if (str == "--vescPort") {
if ((i + 1) < args.size()) {
i++;
vescPort = args.at(i);
found = true;
} else {
i++;
qCritical() << "No port specified";
return 1;
}
}
if (str == "--canFwd") {
if ((i + 1) < args.size()) {
i++;
canFwd = args.at(i).toInt(),
found = true;
} else {
i++;
qCritical() << "No can id specified";
return 1;
}
}
if (str == "--getMcConf") {
if ((i + 1) < args.size()) {
i++;
getMcConfPath = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path specified";
return 1;
}
}
if (str == "--setMcConf") {
if ((i + 1) < args.size()) {
i++;
setMcConfPath = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path specified";
return 1;
}
}
if (str == "--getAppConf") {
if ((i + 1) < args.size()) {
i++;
getAppConfPath = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path specified";
return 1;
}
}
if (str == "--setAppConf") {
if ((i + 1) < args.size()) {
i++;
setAppConfPath = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path specified";
return 1;
}
}
if (str == "--getCustomConf") {
if ((i + 1) < args.size()) {
i++;
getCustomConfPath = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path specified";
return 1;
}
}
if (str == "--setCustomConf") {
if ((i + 1) < args.size()) {
i++;
setCustomConfPath = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path specified";
return 1;
}
}
if (str == "--uploadLisp") {
if ((i + 1) < args.size()) {
i++;
lispPath = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path specified";
return 1;
}
}
if (str == "--eraseLisp") {
eraseLisp = true;
found = true;
}
if (str == "--uploadFirmware") {
if ((i + 1) < args.size()) {
i++;
firmwarePath = args.at(i);
found = true;
} else {
i++;
qCritical() << "No path specified";
return 1;
}
}
if (str == "--uploadBootloaderBuiltin") {
uploadBootloaderBuiltin = true;
found = true;
}
if (str == "--debugOutFile") {
if ((i + 1) < args.size()) {
i++;
if (!m_debug_msg_file.isOpen()) {
m_debug_msg_file.setFileName(args.at(i));
if (m_debug_msg_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qInstallMessageHandler(myMessageOutput);
}
}
found = true;
} else {
i++;
qCritical() << "No path specified";
return 1;
}
}
if (str == "--writeFileToSdCard") {
if ((i + 1) < args.size()) {
i++;
auto p = args.at(i).split(":");
if (p.size() == 2) {
fileForSdIn = p.at(0);
fileForSdOut = p.at(1);
} else {
qCritical() << "Invalid paths specified";
return 1;
}
found = true;
} else {
i++;
qCritical() << "No paths specified";
return 1;
}
}
if (str == "--packFirmware") {
if ((i + 1) < args.size()) {
i++;
auto p = args.at(i).split(":");
if (p.size() == 2) {
fwPackIn = p.at(0);
fwPackOut = p.at(1);
} else {
qCritical() << "Invalid paths specified";
return 1;
}
found = true;
} else {
i++;
qCritical() << "No paths specified";
return 1;
}
}
if (str == "--packLisp") {
if ((i + 1) < args.size()) {
i++;
auto p = args.at(i).split(":");
if (p.size() == 2) {
lispPackIn = p.at(0);
lispPackOut = p.at(1);
} else {
qCritical() << "Invalid paths specified";
return 1;
}
found = true;
} else {
i++;
qCritical() << "No paths specified";
return 1;
}
}
if (str == "--bridgeAppData") {
bridgeAppData = true;
found = true;
}
if (str == "--offscreen") {
offscreen = true;
found = true;
}
if (str == "--downloadPackageArchive") {
downloadPackageArchive = true;
found = true;
}
if (!found) {
if (dash) {
qCritical() << "At least one of the flags is invalid:" << str;
} else {
qCritical() << "Invalid option:" << str;
}
showHelp();
return 1;
}
}
if (downloadPackageArchive) {
QCoreApplication appTmp(argc, argv);
CodeLoader loader;
qDebug() << "Downloading package archive...";
loader.downloadPackageArchive();
qDebug() << "Package archive downloaded!";
}
if (!xmlCodePath.isEmpty()) {
ConfigParams conf;
if (!conf.loadParamsXml(xmlCodePath)) {
qCritical() << "Could not parse XML-file" << xmlCodePath;
return 1;
}
QString nameConfig = "device_config";
if (conf.hasParam("config_name") && conf.getParam("config_name")->type == CFG_T_QSTRING) {
nameConfig = conf.getParamQString("config_name");
}
QFileInfo fi(xmlCodePath);
xmlCodePath.chop(fi.fileName().length());
QString pathDefines = xmlCodePath + "conf_default.h";
QString pathParser = xmlCodePath + "confparser.c";
QString pathCompressed = xmlCodePath + "confxml.c";
Utility::createCompressedConfigC(&conf, nameConfig, pathCompressed);
Utility::createParamParserC(&conf, nameConfig, pathParser);
conf.saveCDefines(pathDefines, true);
qDebug() << "Done!";
return 0;
}
if (!fwPackIn.isEmpty()) {
if (!fwPackIn.endsWith(".bin", Qt::CaseInsensitive)) {
qWarning() << "Warning: Unexpected file extension for a firmware-file.";
}
QFile fIn(fwPackIn);
if (!fIn.open(QIODevice::ReadOnly)) {
qWarning() << QString("Could not open %1 for reading.").arg(fwPackIn);
return 1;
}
QFile fOut(fwPackOut);
if (!fOut.open(QIODevice::WriteOnly)) {
qWarning() << QString("Could not open %1 for writing.").arg(fwPackOut);
return 1;
}
QByteArray newFirmware = fIn.readAll();
fIn.close();
int szTot = newFirmware.size();
bool useHeatshrink = false;
if (szTot > 393208 && szTot < 700000) { // If fw is much larger it is probably for the esp32
useHeatshrink = true;
qDebug() << "Firmware is big, using heatshrink compression library";
int szOld = szTot;
HeatshrinkIf hs;
newFirmware = hs.encode(newFirmware);
szTot = newFirmware.size();
qDebug() << "New size:" << szTot << "(" << 100.0 * (double)szTot / (double)szOld << "%)";
if (szTot > 393208) {
qWarning() << "Firmware too big" <<
"The firmware you are trying to upload is too large for the bootloader even after compression.";
return -1;
}
}
if (szTot > 5000000) {
qWarning() << "Firmware too big" <<
"The firmware you are trying to upload is unreasonably "
"large, most likely it is an invalid file";
return -2;
}
quint16 crc = Packet::crc16((const unsigned char*)newFirmware.constData(),
uint32_t(newFirmware.size()));
VByteArray sizeCrc;
if (useHeatshrink) {
uint32_t szShift = 0xCC;
szShift <<= 24;
szShift |= szTot;
sizeCrc.vbAppendUint32(szShift);
} else {
sizeCrc.vbAppendUint32(szTot);
}
sizeCrc.vbAppendUint16(crc);
newFirmware.prepend(sizeCrc);
fOut.write(newFirmware);
fOut.close();
qDebug() << "Done!";
return 0;
}
if (!lispPackIn.isEmpty()) {
if (!lispPackIn.endsWith(".lisp", Qt::CaseInsensitive)) {
qWarning() << "Warning: Unexpected file extension for a lisp-file.";
}
QFile fIn(lispPackIn);
if (!fIn.open(QIODevice::ReadOnly)) {
qWarning() << QString("Could not open %1 for reading.").arg(lispPackIn);
return 1;
}
QFile fOut(lispPackOut);
if (!fOut.open(QIODevice::WriteOnly)) {
qWarning() << QString("Could not open %1 for writing.").arg(lispPackOut);
return 1;
}
CodeLoader loader;
QFileInfo fi(fIn);
VByteArray vb = loader.lispPackImports(fIn.readAll(), fi.canonicalPath());
fIn.close();
quint16 crc = Packet::crc16((const unsigned char*)vb.constData(), uint32_t(vb.size()));
VByteArray data;
data.vbAppendUint32(vb.size() - 2);
data.vbAppendUint16(crc);
data.append(vb);
fOut.write(data);
fOut.close();
qDebug() << "Done!";
return 0;
}
if (!pkgArgs.isEmpty()) {
if (pkgArgs.size() < 4) {
qWarning() << "Invalid arguments";
return 1;
}
CodeLoader loader;
QString pkgPath = pkgArgs.at(0);
lispPath = pkgArgs.at(1);
QString qmlPath = pkgArgs.at(2);
bool isFullscreen = pkgArgs.at(3).toInt();
QString mdPath;
QString name;
VescPackage pkg;
if (pkgArgs.size() >= 6) {
mdPath = pkgArgs.at(4);
name = pkgArgs.at(5);
QFile f(mdPath);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Could not open markdown file for reading.";
return 1;
}
QString desc = QString::fromUtf8(f.readAll());
f.close();
pkg.name = name;
pkg.description_md = desc;
pkg.description = Utility::md2html(desc);
} else {
QFile f(pkgPath);
if (!f.open(QIODevice::ReadOnly)) {
qWarning() << QString("Could not open %1 for reading.").arg(pkgPath);
return 1;
}
pkg = loader.unpackVescPackage(f.readAll());
f.close();
qDebug() << "Opened package" << pkg.name;
}
if (!lispPath.isEmpty()) {
QFile f(lispPath);
if (!f.open(QIODevice::ReadOnly)) {
qWarning() << "Could not open lisp file for reading.";
return 1;
}
QFileInfo fi(f);
pkg.lispData = loader.lispPackImports(f.readAll(), fi.canonicalPath());
// Empty array means an error. Otherwise, CodeLoader.lispPackImports() always returns data.
if (pkg.lispData.isEmpty()) {
qWarning() << "Errors when processing lisp imports.";
return 1;
}
f.close();
qDebug() << "Read lisp script done";
}
if (!qmlPath.isEmpty()) {
QFile f(qmlPath);
if (!f.open(QIODevice::ReadOnly)) {
qWarning() << "Could not open qml file for reading.";
return 1;
}
pkg.qmlFile = f.readAll();
pkg.qmlIsFullscreen =isFullscreen;
f.close();
qDebug() << "Read qml script done";
}
QFile file(pkgPath);
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << QString("Could not open %1 for writing.").arg(pkgPath);
return 1;
}
file.write(loader.packVescPackage(pkg));
file.close();
qDebug() << "Package Saved!";
return 0;
}
double scale = set.value("app_scale_factor", 1.0).toDouble();
#ifdef Q_OS_ANDROID
scale = 1.0;
#endif
if (scale > 1.01) {
qputenv("QT_SCALE_FACTOR", QString::number(scale).toLocal8Bit());
}
#endif
QCoreApplication *app;
#ifdef USE_MOBILE
QApplication *a = new QApplication(argc, argv);
app = a;
addFonts();
QmlUi *qml = new QmlUi;
qml->startQmlUi();
// As background running is allowed, make sure to not update the GUI when
// running in the background.
QObject::connect(a, &QApplication::applicationStateChanged, [&qml](Qt::ApplicationState state) {
if(state == Qt::ApplicationHidden) {
qml->setVisible(false);
} else {
qml->setVisible(true);
}
});
#else
VescInterface *vesc = nullptr;
TcpHub *tcpHub = nullptr;
MainWindow *w = nullptr;
BoardSetupWindow *bw = nullptr;
QmlUi *qmlUi = nullptr;
QString qmlStr;
QTimer connTimer;
connTimer.setInterval(1000);
QObject::connect(&connTimer, &QTimer::timeout, [&]() {
if (!vesc->isPortConnected()) {
if (qmlUi != nullptr) {
qmlUi->clearQmlCache();
QTimer::singleShot(10, [&]() {
qmlUi->emitReloadCustomGui("qrc:/res/qml/DynamicLoader.qml");
});
}
bool ok = false;
if (vescPort.isEmpty()) {
ok = vesc->autoconnect();
} else {
ok = vesc->connectSerial(vescPort);
}
if (ok) {
qDebug() << "Connected";
} else {
qDebug() << "Could not connect";
if (!retryConn) {
qApp->quit();
}
}
}
});