-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathui.qml.in
More file actions
7333 lines (6080 loc) · 257 KB
/
ui.qml.in
File metadata and controls
7333 lines (6080 loc) · 257 KB
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 2022 Benjamin Vedder <benjamin@vedder.se>
// Copyright 2024 Lukas Hrazky
//
// This file is part of the Refloat VESC package.
//
// Refloat VESC package 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.
//
// Refloat VESC package 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/>.
import Qt.labs.settings 1.0
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Dialogs 1.3 as Dl
import QtQuick.Window 2.2
import QtGraphicalEffects 1.15
import Vedder.vesc.vescinterface 1.0
import Vedder.vesc.codeloader 1.0
import Vedder.vesc.commands 1.0
import Vedder.vesc.configparams 1.0
import Vedder.vesc.utility 1.0
import QtQuick.Controls.Material 2.2
Item {
id: mainItem
property string tabTitle: "Refloat"
property Commands vescCommands: VescIf.commands()
function round(num) {
if (num != num) {
return "--";
}
return Math.round(num);
}
// Used to fix the float negative zero problem where applicable
function toFixed1Zero(num) {
if (num != num) {
return "-.-";
}
return num.toFixed(1).replace("-0.0", "0.0");
}
function toFixed2Zero(num) {
if (num != num) {
return "-.--";
}
return num.toFixed(2).replace("-0.00", "0.00");
}
function clamp(val, min, max) {
if (val > max) {
return max;
}
if (val < min) {
return min;
}
return val;
}
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length !== b.length) return false;
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
function dist(a, b) {
return Math.abs(a - b);
}
function parseVersion(ver) {
var dotSplit = ver.split(".");
if (dotSplit.length < 3) {
return [0, 0, 0, ""];
}
var lastPart = dotSplit[2];
var dashPos = lastPart.search("-");
var patch = lastPart;
var suffix = "";
if (dashPos > 0) {
patch = lastPart.substring(0, dashPos);
suffix = lastPart.substring(dashPos + 1);
}
return [Number(dotSplit[0]), Number(dotSplit[1]), Number(patch), suffix];
}
function versionMajorMinor(ver) {
var arr = ver.split(".");
if (arr.length < 2) {
return [0, 0];
}
return [Number(arr[0]), Number(arr[1])];
}
// Expects arrays containing 3 numbers, returns -1 if a < b, 1 if a > b and 0 if a == b
function cmpVersion(a, b) {
for (var i = 0; i < 3; i++) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
}
}
return 0;
}
function getDarkThemeColor(name) {
switch (name) {
case "normalBackground":
return "#303030";
break;
case "darkBackground":
return "#272727";
break;
case "normalText":
return "#b4b4b4";
break;
case "lightText":
return "#dcdcdc";
break;
case "disabledText":
return "#7f7f7f";
break;
}
}
// 5s timeout for connection to package
Timer {
id: packageConnectionWatchdog
interval: 5000
running: true
onTriggered: {
state.pkgState = state.s_Disconnected;
}
}
// 10hz for realtime data
Timer {
running: true
repeat: true
interval: 100
onTriggered: {
vescCommands.getValuesSetup();
if (state.commsState === state.comms_Info) {
commands.sendGetInfo();
} else if (state.commsState === state.comms_RTDataIDs) {
commands.sendGetRtDataIds();
} else {
commands.sendGetRtData();
}
if (moveSlider.value != 0) {
commands.sendRcMove(moveSlider.value);
}
if (tiltEnabled.checked) {
vescCommands.lispSendReplCmd("(set-remote-state " + tiltSlider.value + " 0 0 0 0)");
}
}
}
// 4Hz timer to poll for lights status if enabled
Timer {
running: lights.enabled
repeat: true
interval: 250
triggeredOnStart: true
onTriggered: {
commands.sendLightsControl();
}
}
// 100hz for for UI updates
Timer {
running: true
repeat: true
interval: 10
onTriggered: {
if (!moveSlider.pressed) {
var stepSize = 0.05;
if (moveSlider.value > 0) {
if (moveSlider.value < stepSize) {
moveSlider.value = 0;
} else {
moveSlider.value -= stepSize;
}
} else if (moveSlider.value < 0) {
if (moveSlider.value > -stepSize) {
moveSlider.value = 0;
} else {
moveSlider.value += stepSize;
}
}
}
if (!tiltSlider.pressed) {
var stepSize = 0.05;
if (tiltSlider.value > 0) {
if (tiltSlider.value < stepSize) {
tiltSlider.value = 0;
} else {
tiltSlider.value -= stepSize;
}
} else if (tiltSlider.value < 0) {
if (tiltSlider.value > -stepSize) {
tiltSlider.value = 0;
} else {
tiltSlider.value += stepSize;
}
}
}
}
}
QtObject {
id: board
function getId() {
return VescIf.getLastBleAddr();
}
function getName() {
var name = VescIf.getBleName(VescIf.getLastBleAddr());
if (!name) {
return null;
}
return name;
}
function setName(name) {
VescIf.storeBleName(VescIf.getLastBleAddr(), name);
VescIf.storeSettings();
}
function getFwVersion() {
var params = VescIf.getLastFwRxParams();
return [params.major, params.minor];
}
}
Connections {
id: vescConfig
target: VescIf
property bool useImperial: VescIf.useImperialUnits()
property string distanceUnit: useImperial ? "mi" : "km"
property string speedUnit: useImperial ? "mph" : "km/h"
property real imperialFactor: useImperial ? 0.621371192 : 1.0;
function onUseImperialUnitsChanged(useImperialUnits) {
useImperial = useImperialUnits;
}
}
Connections {
id: motorConfig
property ConfigParams mcConfig: VescIf.mcConfig()
target: mcConfig
property int batteryCells: mcConfig.getParamInt("si_battery_cells")
property real tempMotorStart: mcConfig.getParamDouble("l_temp_motor_start")
property real tempFetStart: mcConfig.getParamDouble("l_temp_fet_start")
property real currentMin: mcConfig.getParamDouble("l_current_min")
property real currentMax: mcConfig.getParamDouble("l_current_max")
property real inCurrentMin: mcConfig.getParamDouble("l_in_current_min")
property real inCurrentMax: mcConfig.getParamDouble("l_in_current_max")
function onParamChangedDouble(src, name, newParam) {
if (name === "l_temp_motor_start") {
tempMotorStart = newParam;
} else if (name === "l_temp_fet_start") {
tempFetStart = newParam;
} else if (name === "l_current_min") {
currentMin = newParam;
} else if (name === "l_current_max") {
currentMax = newParam;
} else if (name === "l_in_current_min") {
inCurrentMin = newParam;
} else if (name === "l_in_current_max") {
inCurrentMax = newParam;
}
}
function onParamChangedInt(src, name, newParam) {
if (name === "si_battery_cells") {
batteryCells = newParam;
}
}
}
Connections {
id: packageConfig
target: VescIf
property ConfigParams pkgConfig: VescIf.customConfig(0);
property bool updateDue: false
signal update
property Connections pkgConns: Connections {
target: packageConfig.pkgConfig
function onParamChangedDouble(src, name, newParam) {
packageConfig.pendingUpdate();
}
function onParamChangedInt(src, name, newParam) {
packageConfig.pendingUpdate();
}
function onParamChangedEnum(src, name, newParam) {
packageConfig.pendingUpdate();
}
function onParamChangedQString(src, name, newParam) {
packageConfig.pendingUpdate();
}
function onParamChangedBool(src, name, newParam) {
packageConfig.pendingUpdate();
}
}
// There is no reliable signal for when the whole config changes,
// instead, we hook onto property changes and run a timer for a short
// while before we fire off the actual update() signal
property Timer updateTimer: Timer {
id: updateTimer
interval: 100
onTriggered: {
packageConfig.fireDueUpdate();
}
}
property Timer dueTimer: Timer {
id: dueTimer
interval: 2000
onTriggered: {
packageConfig.fireDueUpdate();
}
}
function pendingUpdate() {
updateTimer.restart();
}
function armDueUpdate() {
updateDue = true;
dueTimer.restart();
}
function disarmDueUpdate() {
updateDue = false;
dueTimer.stop();
}
function fireDueUpdate() {
if (updateDue) {
updateDue = false;
update();
}
}
function onCustomConfigLoadDone() {
pkgConfig = VescIf.customConfig(0);
// Config was just loaded and...
if (isDefault()) {
// ... it's default, fire a delayed update based on the config values changing
//
// Config being the default one here doesn't necessarily mean
// the actual package config has been reset, this sometimes
// triggers with the config being default at this point, but
// the config is updated later (and there is no signal sent on
// that event).
//
// We delay the actual check / update until a bit later when
// the actual data is loaded.
armDueUpdate();
} else {
// ... it's not default, trigger the update signal right away
update();
}
}
// Called explicitly after the Write button in Refloat Cfg qml page is pressed
function configWritten() {
disarmDueUpdate();
unsetDefault();
update();
}
function checkConfig() {
state.configLoadingError = !pkgConfig;
return !!pkgConfig;
}
function writeConfig() {
if (!checkConfig()) return;
unsetDefault();
vescCommands.customConfigSet(0, pkgConfig);
update();
}
function isDefault() {
if (!checkConfig()) return null;
return pkgConfig.getParamBool("meta.is_default");
}
// We unset the is_default flag on every config write. Once we write
// something, it's no longer default. This flag is being reset on the
// pacakge side as well, but the config is not re-read after write, so
// we need to reset it here too.
function unsetDefault() {
if (!checkConfig()) return;
pkgConfig.updateParamBool("meta.is_default", false);
}
function setDisabled(disabled) {
if (!checkConfig()) return;
pkgConfig.updateParamBool("disabled", disabled);
writeConfig();
}
function getParamValue(name) {
if (pkgConfig.isParamDouble(name)) {
return pkgConfig.getParamDouble(name);
} else if (pkgConfig.isParamInt(name)) {
return pkgConfig.getParamInt(name);
} else if (pkgConfig.isParamEnum(name)) {
return pkgConfig.getParamEnum(name);
} else if (pkgConfig.isParamBool(name)) {
return pkgConfig.getParamBool(name);
} else if (pkgConfig.isParamQString(name)) {
return pkgConfig.getParamQString(name);
} else if (pkgConfig.isParamBitfield(name)) {
return pkgConfig.getParamInt(name);
}
return null;
}
function getParamType(name) {
if (pkgConfig.isParamDouble(name)) {
return "Double";
} else if (pkgConfig.isParamInt(name)) {
return "Int";
} else if (pkgConfig.isParamEnum(name)) {
return "Enum";
} else if (pkgConfig.isParamBool(name)) {
return "Bool";
} else if (pkgConfig.isParamQString(name)) {
return "String";
} else if (pkgConfig.isParamBitfield(name)) {
return "Bitfield";
}
return null;
}
function getDisplayName(name) {
return pkgConfig.getLongName(name);
}
function getEnumNames(name) {
return pkgConfig.getParamEnumNames(name);
}
function paramIsTune(name) {
var tuneBlacklist = [
"tiltback_return_speed",
"tiltback_duty",
"tiltback_duty_angle",
"tiltback_duty_speed",
"is_dutybeep_enabled",
"tiltback_hv_angle",
"tiltback_hv_speed",
"tiltback_lv_angle",
"tiltback_lv_speed",
"disabled",
"fault_adc1",
"fault_adc2",
"hertz",
"inputtilt_remote_type",
"inputtilt_deadband",
"is_footbeep_enabled",
"is_beeper_enabled",
"tiltback_hv",
"tiltback_lv",
"tiltback_speed",
];
return !name.startsWith("haptic.") && !name.startsWith("hardware.") && !tuneBlacklist.includes(name);
}
function fetchConfig(tuneOnly) {
if (!checkConfig()) return null;
var settings = {};
for (let subGroup of pkgConfig.getParamSubgroups("General")) {
for (let param of pkgConfig.getParamsFromSubgroup("General", subGroup)) {
if (tuneOnly && !paramIsTune(param)) {
continue;
}
var value = getParamValue(param);
if (value !== null) {
settings[param] = getParamValue(param);
}
}
}
return settings;
}
function versionCompatible(config) {
var [major, minor] = versionMajorMinor(config.version);
if (major !== tuneManager.formatVersionMajor || minor > tuneManager.formatVersionMinor) {
VescIf.emitStatusMessage("Error: Incompatible version: %1".arg(config.version), false);
return false;
}
return true;
}
function applyConfig(config) {
if (!checkConfig()) return;
if (!versionCompatible(config)) {
return;
}
for (let [key, value] of Object.entries(config.settings)) {
if (pkgConfig.isParamDouble(key)) {
pkgConfig.updateParamDouble(key, value);
} else if (pkgConfig.isParamInt(key)) {
pkgConfig.updateParamInt(key, value);
} else if (pkgConfig.isParamBool(key)) {
pkgConfig.updateParamBool(key, value);
} else if (pkgConfig.isParamEnum(key)) {
pkgConfig.updateParamEnum(key, value);
} else if (pkgConfig.isParamQString(key)) {
pkgConfig.updateParamString(key, value);
} else if (pkgConfig.isParamBitfield(key)) {
pkgConfig.updateParamInt(key, value);
}
}
writeConfig();
}
function diffConfig(other) {
var differs = [];
var otherMissing = [];
var otherSettings = {};
if (other && other.settings) {
otherSettings = Object.assign({}, other.settings);
if (VescIf.customConfigsLoaded()) {
for (let subGroup of pkgConfig.getParamSubgroups("General")) {
for (let paramName of pkgConfig.getParamsFromSubgroup("General", subGroup)) {
var value = getParamValue(paramName);
if (value === null) {
continue;
}
if (otherSettings.hasOwnProperty(paramName)) {
var otherValue = otherSettings[paramName];
if (value !== otherValue) {
differs.push({
"name": paramName,
"value": value,
"otherValue": otherValue
});
}
delete otherSettings[paramName];
} else {
otherMissing.push(paramName);
}
}
}
}
}
return {
"differs": differs,
"missing": Object.values(otherSettings),
"otherMissing": otherMissing,
}
}
// TODO don't do this...
function setInputTiltRemoteType(type) {
if (!checkConfig()) return;
if (pkgConfig.getParamEnum("inputtilt_remote_type", 0) != 1) {
pkgConfig.updateParamEnum("inputtilt_remote_type", 1);
writeConfig();
}
}
}
Connections {
id: commands
target: vescCommands
readonly property int c_INFO: 0
readonly property int c_RC_MOVE: 7
readonly property int c_HANDTEST: 13
readonly property int c_LIGHTS_CONTROL: 20
readonly property int c_FLYWHEEL: 22
readonly property int c_REALTIME_DATA: 31
readonly property int c_REALTIME_DATA_IDS: 32
readonly property int c_ALERTS_LIST: 35
readonly property int c_ALERTS_CONTROL: 36
readonly property int c_DATA_RECORD_REQUEST: 41
readonly property int c_DATA_RECORD_HEADER: 42
readonly property int c_DATA_RECORD_DATA: 43
// IEEE-754 16-bit floating-point format (without infinity and NaNs)
// Dynamic range: +-131008.0
// Normal numbers closest to zero: +-6.1035156E-5
// Subnormal numbers closes to zero: +-5.9604645E-8
// Precision: 3.311 digits
//
// https://stackoverflow.com/a/60047308
function getFloat16(dataView, ind) {
var x = dataView.getUint16(ind);
var e = (x&0x7C00)>>10; // exponent
var m16 = x&0x03FF; // original float16 mantissa
var m = m16<<13; // new float 32 mantissa
var v = m16>0 ? 140+Math.floor(Math.log2(m16)) : 0;
// sign | normalized | denormalized
var r = (x&0x8000)<<16 | (e!=0)*((e+112)<<23|m) | ((e==0)&(m!=0))*((v-37)<<23|((m<<(150-v))&0x007FE000));
return new Float32Array(new Uint32Array([r]).buffer)[0];
}
function getString(dataView, ind) {
var str = "";
var size = dataView.getUint8(ind++);
for (var j = 0; j < size; j++) {
str += String.fromCharCode(dataView.getUint8(ind++));
}
return [str, ind];
}
function getStrings(dataView, ind) {
var num = dataView.getUint8(ind++);
var strs = [];
for (var i = 0; i < num; i++) {
var str;
[str, ind] = getString(dataView, ind);
strs.push(str);
}
return [strs, ind];
}
function createData(size, command) {
var data = new DataView(new ArrayBuffer(size));
var ind = 0;
data.setUint8(ind++, 101);
data.setUint8(ind++, command);
return data;
}
function sendGetInfo() {
var data = createData(4, c_INFO);
data.setUint8(2, 2); // version 2 of the INFO command
vescCommands.sendCustomAppData(data.buffer);
}
function sendGetRtData() {
vescCommands.sendCustomAppData(createData(2, c_REALTIME_DATA).buffer);
}
function sendGetRtDataIds() {
vescCommands.sendCustomAppData(createData(2, c_REALTIME_DATA_IDS).buffer);
}
function sendLightsControl(on, headlightsOn) {
var data = createData(7, c_LIGHTS_CONTROL);
var mask = 0;
var value = 0;
if (on !== undefined) {
mask |= 0b01;
if (on) {
value |= 0b01;
}
}
if (headlightsOn !== undefined) {
mask |= 0b10;
if (headlightsOn) {
value |= 0b10;
}
}
data.setUint32(2, mask);
data.setUint8(6, value);
vescCommands.sendCustomAppData(data.buffer);
}
function sendAlertsList(startTime) {
var data = createData(6, c_ALERTS_LIST);
if (startTime !== undefined && startTime !== null) {
data.setUint32(2, startTime);
}
vescCommands.sendCustomAppData(data.buffer);
}
function sendAlertsClearFatal() {
var data = createData(3, c_ALERTS_CONTROL);
data.setUint8(2, 0x1); // clear the fatal error flag
vescCommands.sendCustomAppData(data.buffer);
}
function sendRcMove(value) {
var data = createData(6, c_RC_MOVE);
var current = Math.abs(Math.round(value * 70)) + 10;
data.setUint8(2, value > 0 ? 1 : 0); // direction
data.setUint8(3, current); // current
data.setUint8(4, 1); // time
data.setUint8(5, current + 1); // sum = time + current
vescCommands.sendCustomAppData(data.buffer);
}
function sendHandtest(on) {
var data = createData(3, c_HANDTEST);
data.setUint8(2, on); // on (1) / off (0)
vescCommands.sendCustomAppData(data.buffer);
}
function sendFlywheel(on) {
var data = createData(9, c_FLYWHEEL);
data.setUint8(2, on ? 130 : 128); // on, force calibration (130) / off (128)
if (on) {
data.setUint8(3, 0); // use default kp
data.setUint8(4, 0); // use default kp2
data.setUint8(5, 0); // use default duty angle
data.setUint8(6, 0); // use default duty start
data.setUint8(7, 1); // allow abort with footpads
data.setUint8(8, 0); // use default duty speed
}
vescCommands.sendCustomAppData(data.buffer);
}
readonly property int dr_RECORD: 1
readonly property int dr_AUTOSTART: 2
readonly property int dr_AUTOSTOP: 3
// @param key One of the above dr_ properties
// @param value Value to set, true or false
function sendDataRecordControl(key, value) {
var data = createData(5, c_DATA_RECORD_REQUEST);
data.setUint8(2, 1); // control
data.setUint8(3, key);
data.setUint8(4, value);
vescCommands.sendCustomAppData(data.buffer);
}
function sendDataRecordRequest(header, offset) {
if (header) {
var data = createData(4, c_DATA_RECORD_REQUEST);
data.setUint8(2, 2); // send
data.setUint8(3, 1); // header
} else {
var data = createData(8, c_DATA_RECORD_REQUEST);
data.setUint8(2, 2); // send
data.setUint8(3, 2); // data
data.setUint32(4, offset);
}
vescCommands.sendCustomAppData(data.buffer);
}
function onValuesSetupReceived(values, mask) {
batteryBar.value = values.battery_level * 100;
odometer.value = values.odometer * 0.001 * vescConfig.imperialFactor;
tachometer.value = values.tachometer_abs * 0.001 * vescConfig.imperialFactor;
var speed = Math.abs(values.speed * 3.6 * vescConfig.imperialFactor);
consumption.add(Math.max(Math.min(values.current_in * values.v_in / Math.max(speed, 1e-6), 60) , -60));
}
function onCustomAppDataReceived(data) {
var dv = new DataView(data, 0);
var ind = 0;
var packageId = dv.getUint8(ind++);
if (packageId !== 101) {
return;
}
var msgtype = dv.getUint8(ind++);
if (msgtype == c_INFO) {
ind += 1; // skip version, should always be the current one
var flags = dv.getUint8(ind++);
ind += 47; // 20 bytes package name, 3 version numbers, 20 bytes suffix, 4 bytes git hash
state.ticksPerSecond = dv.getUint32(ind); ind += 4;
var capabilities = dv.getUint32(ind); ind += 4;
state.setCapabilities(capabilities);
var extraFlags = dv.getUint8(ind++);
state.commsState = state.comms_RTDataIDs;
} else if (msgtype == c_LIGHTS_CONTROL) {
var values = dv.getUint8(ind++);
lights.on = values & 0b01;
lights.headlightsOn = values & 0b10;
} else if (msgtype === c_ALERTS_LIST) {
alerts.receiveAlertsList(dv, ind);
} else if (msgtype === c_REALTIME_DATA) {
if (state.commsState !== state.comms_Initialized) {
// rt data came because another client app requested them,
// ignore until info is received
return;
}
var mask = dv.getUint8(ind++);
var hasRuntime = !!(mask & 0x1);
var hasCharging = !!(mask & 0x2);
var hasAlerts = !!(mask & 0x4);
var extraFlags = dv.getUint8(ind++);
state.extraRecording = extraFlags & 0x1;
state.extraRecAutostart = extraFlags & 0x2;
state.extraRecAutostop = extraFlags & 0x4;
state.fatalError = extraFlags & 0x8;
var time = dv.getUint32(ind); ind += 4;
state.setTimeReference(time);
var modeAndState = dv.getUint8(ind++);
state.pkgMode = modeAndState >> 4;
var pkgState = modeAndState & 0xF;
state.pkgState = pkgState;
var sensorAndFlags = dv.getUint8(ind++);
var fpState = sensorAndFlags >> 6;
state.fpState = fpState;
var wheelslip = sensorAndFlags & 0b00000001;
state.wheelslip = wheelslip;
state.darkride = sensorAndFlags & 0b00000010;
state.charging = sensorAndFlags & 0b00100000;
var setpointAndStop = dv.getUint8(ind++);
var sat = setpointAndStop >> 4;
state.setpointAdjustmentType = sat;
state.stopCondition = setpointAndStop & 0xF;
state.beepReason = dv.getUint8(ind++);
var rtDataIdleIts = state.rtDataIdleItems;
var rtDataAllIts = state.rtDataAllItems;
var newRt = Array(rtDataAllIts.length).fill(NaN);
// if runtime values are not in the message, limit the loop to
// read only values that are always present
var i = 0;
for (; i < rtDataIdleIts.length; i++) {
newRt[i] = getFloat16(dv, ind);
ind += 2;
}
if (hasRuntime) {
for (; i < rtDataAllIts.length; i++) {
newRt[i] = getFloat16(dv, ind);
ind += 2;
}
}
state.rtData = state.newRtData(newRt);
if (hasCharging) {
chargingInfo.current = getFloat16(dv, ind); ind += 2;
chargingInfo.voltage = getFloat16(dv, ind); ind += 2;
}
if (hasAlerts) {
ind += 9; // unused here
}
var running = +(state.pkgState === state.s_Running);
rtPlotData.addSample([time, running, wheelslip, sat, fpState].concat(newRt));
rtPlotData.makeReady();
} else if (msgtype === c_REALTIME_DATA_IDS) {
if (state.commsState !== state.comms_RTDataIDs) {
return;
}
[state.rtDataIdleItems, ind] = getStrings(dv, ind);
[state.rtDataRuntimeItems, ind] = getStrings(dv, ind);
state.rtDataAllItems = state.rtDataIdleItems.concat(state.rtDataRuntimeItems);
state.commsState = state.comms_Initialized;
} else if (msgtype === c_DATA_RECORD_HEADER) {
recordedPlotData.receiveHeader(dv, ind);
} else if (msgtype === c_DATA_RECORD_DATA) {
recordedPlotData.receiveData(dv, ind);
}
packageConnectionWatchdog.restart();
}
}
Settings {
id: preferences
category: "preferences"
property int speedDialMax: vescConfig.useImperial ? 30 : 40
property alias showBattVoltage: prefShowBattVoltage.checked
property alias showBattVoltagePerCell: prefShowBattVoltagePerCell.checked
property alias battCurrentLog: prefBattCurrentLog.checked
property int tempWarningOffset: 10
property alias tuneSlotCount: prefTuneSlotCount.value
property alias showTuneDiffCount: prefShowTuneDiffCount.checked
property int plotMaxWindowMinutes: 30
property alias showWelcomeDialog: prefShowWelcomeDialog.checked
property alias checkForNewVersions: prefCheckForNewVersions.checked
}
Settings {
id: persistentData
category: "persistentData"
property var lastPackageUpdateCheck
property var packageUpdateSnooze
}
QtObject {
id: updateChecker
property CodeLoader packageLoader: CodeLoader {
id: packageLoader
function getPackageVersion(pkgName) {
var pkgs = reloadPackageArchive();
for (var pkg of pkgs) {
if (!pkg.isLibrary && shouldShowPackage(pkg) && pkg.name === pkgName) {
// HTML text
var li = pkg.description.match(/<li>Version: [a-zA-Z0-9._-]+<\/li>/g);
if (li) {
var version = li[0];
return version.substring(13, version.length - 5);
}
// markdown text
var md_bullet = pkg.description.match(/\n- Version: [a-zA-Z0-9._-]+\n/g);
if (md_bullet) {
var version = md_bullet[0];
return version.substring(11, version.length);
}
}
}
return null;
}
Component.onCompleted: {
setVesc(VescIf)