forked from vedderb/vesc_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectScreen.qml
913 lines (797 loc) · 30.8 KB
/
ConnectScreen.qml
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
/*
Copyright 2021 Benjamin Vedder benjamin@vedder.se
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/>.
*/
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.3
import Vedder.vesc.vescinterface 1.0
import Vedder.vesc.bleuart 1.0
import Vedder.vesc.commands 1.0
import Vedder.vesc.utility 1.0
import Vedder.vesc.udpserversimple 1.0
Item {
id: rootItem
property int animationDuration: 500
property BleUart mBle: VescIf.bleDevice()
property Commands mCommands: VescIf.commands()
property bool opened: true
property bool pingTcpHub: false
property bool scanning: false
onOpenedChanged: {
if(opened){
animationDuration = 500
y = 0
} else {
y = Qt.binding(function() {return parent.height})
}
}
Behavior on y {
NumberAnimation {
duration: animationDuration
easing.type: Easing.InOutSine
}
}
Rectangle {
color: Utility.getAppHexColor("darkBackground")
anchors.fill: parent
}
// Prevents events from passing to components behind
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.AllButtons
onWheel: {wheel.accepted=true}
hoverEnabled: true
}
function startBleScan() {
if (Utility.hasLocationPermission()) {
scanning = true
scanDotTimer.running = true
bleModel.clear()
vescsUdp = []
mBle.startScan()
} else {
bleScanStart.open()
}
}
onYChanged: {
if (y > 1) {
enableDialog()
}
if(!opened & y == height){
animationDuration = 0
}
}
ColumnLayout {
id: column
anchors.fill: parent
anchors.margins: 10
anchors.leftMargin: notchLeft
anchors.rightMargin: notchRight
Rectangle {
Layout.preferredHeight: notchTop
Layout.fillWidth: true
opacity: 0
}
Image {
id: image
Layout.preferredWidth: Math.min(column.width, column.height*0.8) * 0.8
Layout.preferredHeight: (sourceSize.height * Layout.preferredWidth) / sourceSize.width
Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom
Layout.topMargin: Math.min(rootItem.width, rootItem.height) * 0.025
Layout.bottomMargin: 0
source: "qrc" + Utility.getThemePath() + "/logo.png"
DragHandler {
id: handler
target: rootItem
margin: 0
xAxis.enabled: false
yAxis.maximum: rootItem.height
yAxis.minimum: 0
onActiveChanged: {
if (handler.active) {
animationDuration = 3
} else {
animationDuration = 500
if(opened) {
if (rootItem.y > (rootItem.height / 4)) {
rootItem.opened = false
} else {
rootItem.y = 0
}
}
}
}
}
}
RowLayout {
Layout.fillWidth: true
Button {
text: qsTr("Hide")
Layout.preferredWidth: 120
flat: true
onClicked: {
rootItem.opened = false
}
}
Text {
id: text
Layout.fillWidth: true
color: Utility.getAppHexColor("lightText")
text: qsTr("Devices Found")
font.bold: true
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.WordWrap
}
Button {
id: scanButton
text: qsTr("Scan...")
enabled: true
flat: true
Layout.preferredWidth: 120
onClicked: {
scanMenu.open()
}
Menu {
id: scanMenu
MenuItem {
text: "Scan BLE"
enabled: !scanning
onTriggered: {
startBleScan()
}
}
MenuItem {
text: "Ping TCP hub"
onTriggered: {
bleModel.clear()
pingTcpHub = true
mBle.emitScanDone()
}
}
MenuItem {
text: "Connect TCP Hub..."
onTriggered: {
tcpHubConnDialog.open()
}
Dialog {
id: tcpHubConnDialog
standardButtons: Dialog.Ok | Dialog.Cancel
modal: true
focus: true
rightMargin: 10
leftMargin: 10
closePolicy: Popup.CloseOnEscape
y: 10 + parent.height / 2 - height / 2
x: parent.width/2 - width/2
width: parent.width - 20 - notchLeft - notchRight
parent: rootItem.parent
Overlay.modal: Rectangle {
color: "#AA000000"
}
onAccepted: {
hubBox.connectToHub()
}
title: "Connect TCP Hub"
TcpHubBox {
id: hubBox
anchors.fill: parent
showControls: false
}
}
}
MenuItem {
text: "Clear TCP hub devs"
onTriggered: {
tcpHubClearDialog.open()
}
Dialog {
id: tcpHubClearDialog
standardButtons: Dialog.Ok | Dialog.Cancel
modal: true
focus: true
rightMargin: 10
leftMargin: 10
closePolicy: Popup.CloseOnEscape
y: 10 + parent.height / 2 - height / 2
x: parent.width/2 - width/2
width: parent.width - 20 - notchLeft - notchRight
parent: rootItem.parent
Overlay.modal: Rectangle {
color: "#AA000000"
}
onAccepted: {
VescIf.clearTcpHubDevs()
}
title: "Clear TCP Hub Devices"
Text {
color: Utility.getAppHexColor("lightText")
verticalAlignment: Text.AlignVCenter
anchors.fill: parent
wrapMode: Text.WordWrap
text:
"This is going to clear all previous TCP hub connections. Continue?"
}
}
}
}
}
Timer {
id: scanDotTimer
interval: 500
running: false
repeat: true
property int dots: 0
onTriggered: {
var text = "Scanning"
for (var i = 0;i < dots;i++) {
text = "-" + text + "-"
}
dots++;
if (dots > 3) {
dots = 0;
}
scanButton.text = text
}
}
}
ListModel {
id: bleModel
}
ListView {
id: bleList
Layout.fillWidth: true
Layout.fillHeight: true
focus: true
clip: true
spacing: 5
z:2
Component {
id: bleDelegate
Rectangle {
width: bleList.width
height: 120
color: Utility.getAppHexColor("normalBackground")
radius: 10
RowLayout {
anchors.fill: parent
spacing: 10
Rectangle {
Layout.leftMargin: 10
Layout.fillWidth: true
opacity: 1.0
color: preferred ? ("#55" + Utility.getAppHexColor("lightAccent").slice(1)) : (Utility.getAppHexColor("lightestBackground") )
height: column.height + 10
radius: height / 2
ColumnLayout {
id: column
anchors.centerIn: parent
Image {
id: image
fillMode: Image.PreserveAspectFit
Layout.preferredWidth: 40
Layout.preferredHeight: 40
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
source: "qrc" + Utility.getThemePath() + ((isSerial === 0) ? "icons/bluetooth.png" : "icons/Connected-96.png")
}
Text {
Layout.fillWidth: true
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
text: name
horizontalAlignment: Text.AlignHCenter
color: Utility.getAppHexColor("lightText")
wrapMode: Text.WordWrap
}
}
}
ColumnLayout {
visible: isSerial === 0
Text {
Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom
Layout.rightMargin: 5
color: Utility.getAppHexColor("lightText")
text: "Preferred"
horizontalAlignment: Text.AlignHCenter
}
Switch {
Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
enabled: true
checked: preferred
onToggled: {
VescIf.storeBlePreferred(bleAddr, checked)
bleModel.clear()
mBle.emitScanDone()
}
}
}
ColumnLayout {
Layout.rightMargin: 10
spacing: -5
Timer {
id: workaroundTimerConnect
property string bleAddr: ""
interval: 0
repeat: false
running: false
onTriggered: {
VescIf.connectBle(workaroundTimerConnect.bleAddr)
}
}
Button {
Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom
Layout.preferredHeight: 55
Layout.preferredWidth: {
if (isSerial === 0) {
nameButton.width
} else if (isSerial === 3) {
passButton.width
} else {
nameButton.width
}
}
text: "Connect"
onClicked: {
if (isSerial === 1) {
if (bleAddr === "") {
VescIf.autoconnect()
} else {
VescIf.connectSerial(bleAddr, 115200)
}
} else if (isSerial === 2) {
VescIf.connectTcp(bleAddr, 65102)
} else if (isSerial === 3) {
VescIf.connectTcpHubUuid(hubUuid)
} else {
disableDialog()
workaroundTimerConnect.bleAddr = bleAddr
workaroundTimerConnect.start()
}
}
}
Button {
id: passButton
Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom
Layout.preferredHeight: 55
text: "Update Password"
visible: isSerial === 3
onClicked: {
hubPassDialog.uuid = hubUuid
hubPassDialog.open()
}
}
Button {
id: nameButton
Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom
Layout.preferredHeight: 55
text: "Set Name"
visible: isSerial === 0
onClicked: {
bleNameDialog.addr = bleAddr
stringInput.text = setName
bleNameDialog.open()
}
}
}
}
}
}
model: bleModel
delegate: bleDelegate
footer: BusyIndicator {
Layout.alignment: Qt.AlignCenter
Layout.fillHeight: true
running: scanning
width: parent.width
height: BusyIndicator.implicitHeight*1.5
Behavior on y {
NumberAnimation {
duration: 300;
easing.type: Easing.InOutSine
}
}
}
}
}
property var vescsUdp: []
UdpServerSimple {
Component.onCompleted: {
startServerBroadcast(65109)
}
onDataRx: {
var tokens = Utility.arr2str(data).split("::")
if (tokens.length === 3) {
var found = false
for (var i = 0; i < vescsUdp.length;i++) {
if (vescsUdp[i].ip === tokens[1]) {
vescsUdp[i].updateTime = new Date().getTime()
found = true
break
}
}
if (!found) {
vescsUdp[vescsUdp.length] = {
"name" : tokens[0],
"ip" : tokens[1],
"port" : tokens[2],
"updateTime" : new Date().getTime()
}
mBle.emitScanDone()
}
}
}
}
Timer {
running: true
repeat: true
interval: 500
onTriggered: {
var removed = false
for (var i = 0; i < vescsUdp.length;i++) {
var age = new Date().getTime() - vescsUdp[i].updateTime
if (age > 4000) {
vescsUdp.splice(i, 1)
removed = true
i--;
if (i < 0) {
break
}
}
}
if (removed) {
bleModel.clear()
mBle.emitScanDone()
}
// Re-scan serial regularly
if (Utility.hasSerialport()) {
mBle.emitScanDone()
}
}
}
Connections {
target: mBle
function onScanDone(devs, done) {
if (done) {
scanDotTimer.running = false
scanning = false
scanButton.text = qsTr("Scan...")
}
for (var addr in devs) {
var name = devs[addr]
if(Qt.platform.os == "ios" || Qt.platform.os == "mac") {
var ids = addr.split('-')
var shortAddr = ids[0].slice(1,3) + '-' + ids[1].slice(0,2) + '-' +
ids[2].slice(0,2) + '-' + ids[3].slice(0,2) + '-' + ids[4].slice(0,2)
} else {
shortAddr = addr;
}
var setName = VescIf.getBleName(addr)
var setNameShort = setName
var preferred = VescIf.getBlePreferred(addr)
if (setName.length > 0) {
setName += "\n[" + shortAddr + "]"
} else {
setName = name + "\n[" + shortAddr + "]"
}
var addToList = true
var j = 0
var k = 0
for(j=0; j < bleModel.count; j++) {
if(bleModel.get(j).bleAddr === addr){
addToList = false
}
}
if (addToList) {
if (preferred) {
bleModel.insert(0, {"name": setName,
"setName": setNameShort,
"preferred": preferred,
"bleAddr": addr,
"hubUuid": "",
"isSerial": 0})
} else {
bleModel.append({"name": setName,
"setName": setNameShort,
"preferred": preferred,
"bleAddr": addr,
"hubUuid": "",
"isSerial": 0})
}
}
}
for (k = 0; k < vescsUdp.length;k++) {
addToList = true
for (j = 0; j < bleModel.count; j++) {
if (bleModel.get(j).bleAddr === (vescsUdp[k].ip)) {
addToList = false
break
}
}
if (addToList) {
bleModel.insert(0, {"name": vescsUdp[k].name + " (TCP)\n" + vescsUdp[k].ip,
"setName": "",
"preferred": true,
"bleAddr": vescsUdp[k].ip,
"hubUuid": "",
"isSerial": 2})
}
}
if (Utility.hasSerialport()) {
var ports = VescIf.listSerialPorts()
for (j = 0; j < ports.length; j++) {
if (ports[j].isEsp) {
var nameNow = "Serial ESP32\n" + ports[j].systemPath
addToList = true
for (k = 0; k < bleModel.count; k++) {
if(bleModel.get(k).name === nameNow){
addToList = false
}
}
if (addToList) {
bleModel.insert(0, {"name": nameNow,
"setName": "",
"preferred": true,
"bleAddr": ports[j].systemPath,
"hubUuid": "",
"isSerial": 1})
}
}
if (ports[j].isVesc) {
nameNow = "Serial STM32\n" + ports[j].systemPath
addToList = true
for (k = 0; k < bleModel.count; k++) {
if (bleModel.get(k).name === nameNow){
addToList = false
}
}
if (addToList) {
bleModel.insert(0, {"name": nameNow,
"setName": "",
"preferred": true,
"bleAddr": ports[j].systemPath,
"hubUuid": "",
"isSerial": 1})
}
}
}
// Remove ports that no longer are there
var removed = true
while (removed) {
removed = false
for (k = 0; k < bleModel.count; k++) {
var found = false
for (j = 0; j < ports.length; j++) {
if (ports[j].systemPath === bleModel.get(k).bleAddr) {
found = true
break
}
}
if (bleModel.get(k).isSerial === 1 && !found) {
bleModel.remove(k)
removed = true
break
}
}
}
}
if (pingTcpHub) {
pingTcpHub = false
disableDialog()
var hubDevs = VescIf.getTcpHubDevs()
for (j = 0; j < hubDevs.length; j++) {
if (hubDevs[j].ping()) {
bleModel.insert(0, {"name": hubDevs[j].id + " (TCP Hub)\n" + hubDevs[j].server,
"setName": "",
"preferred": true,
"bleAddr": "",
"hubUuid": hubDevs[j].uuid(),
"isSerial": 3})
}
}
enableDialog()
}
}
function onBleError(info) {
VescIf.emitMessageDialog("BLE Error", info, false, false)
enableDialog()
}
}
function disableDialog() {
commDialog.open()
column.enabled = false
disableTimeoutTimer.stop()
disableTimeoutTimer.start()
}
function enableDialog() {
commDialog.close()
column.enabled = true
disableTimeoutTimer.stop()
}
Timer {
id: disableTimeoutTimer
running: false
repeat: false
interval: 15000
onTriggered: {
enableDialog()
VescIf.emitMessageDialog("Connect",
"Connection timed out",
false, false)
}
}
Dialog {
id: commDialog
title: "Connecting..."
closePolicy: Popup.NoAutoClose
modal: true
focus: true
Overlay.modal: Rectangle {
color: "#AA000000"
}
width: parent.width - 20 - notchLeft - notchRight
x: parent.width/2 - width/2
y: parent.height / 2 - height / 2
parent: rootItem.parent
ProgressBar {
anchors.fill: parent
indeterminate: visible
}
}
Dialog {
property string addr: ""
id: bleNameDialog
standardButtons: Dialog.Ok | Dialog.Cancel
modal: true
focus: true
title: "Set BLE Device Name"
Overlay.modal: Rectangle {
color: "#AA000000"
}
width: parent.width - 20 - notchLeft - notchRight
height: 200
closePolicy: Popup.CloseOnEscape
x: parent.width/2 - width/2
y: Math.max(parent.height / 4 - height / 2, 20)
parent: rootItem.parent
Rectangle {
anchors.fill: parent
height: stringInput.implicitHeight + 14
border.width: 2
border.color: Utility.getAppHexColor("lightestBackground")
color: Utility.getAppHexColor("normalBackground")
radius: 3
TextInput {
id: stringInput
color: Utility.getAppHexColor("lightText")
anchors.fill: parent
anchors.margins: 7
font.pointSize: 12
focus: true
}
}
onAccepted: {
VescIf.storeBleName(addr, stringInput.text)
VescIf.storeSettings()
bleModel.clear()
mBle.emitScanDone()
}
}
Dialog {
property string uuid: ""
id: hubPassDialog
standardButtons: Dialog.Ok | Dialog.Cancel
modal: true
focus: true
title: "Set TCP Hub Password"
Overlay.modal: Rectangle {
color: "#AA000000"
}
width: parent.width - 20 - notchLeft - notchRight
height: 200
closePolicy: Popup.CloseOnEscape
x: parent.width/2 - width/2
y: Math.max(parent.height / 4 - height / 2, 20)
parent: rootItem.parent
Rectangle {
anchors.fill: parent
height: hubPassInput.implicitHeight + 14
border.width: 2
border.color: Utility.getAppHexColor("lightestBackground")
color: Utility.getAppHexColor("normalBackground")
radius: 3
TextInput {
id: hubPassInput
color: Utility.getAppHexColor("lightText")
anchors.fill: parent
anchors.margins: 7
font.pointSize: 12
focus: true
}
}
onAccepted: {
VescIf.updateTcpHubPassword(uuid, hubPassInput.text)
}
}
Dialog {
id: bleEn
standardButtons: Dialog.Ok
modal: true
focus: true
rightMargin: 10
leftMargin: 10
closePolicy: Popup.CloseOnEscape
y: 10 + parent.height / 2 - height / 2
x: parent.width/2 - width/2
width: parent.width - 20 - notchLeft - notchRight
parent: rootItem.parent
Overlay.modal: Rectangle {
color: "#AA000000"
}
title: "BLE scan"
Text {
color: Utility.getAppHexColor("lightText")
verticalAlignment: Text.AlignVCenter
anchors.fill: parent
wrapMode: Text.WordWrap
text: "BLE scan does not seem to be possible. Make sure that the " +
"location service is enabled on your device."
}
}
Dialog {
id: bleScanStart
standardButtons: Dialog.Ok | Dialog.No
modal: true
focus: true
rightMargin: 10
leftMargin: 10
closePolicy: Popup.CloseOnEscape
y: 10 + parent.height / 2 - height / 2
x: parent.width/2 - width/2
width: parent.width - 20 - notchLeft - notchRight
parent: rootItem.parent
Overlay.modal: Rectangle {
color: "#AA000000"
}
onAccepted: {
scanning = true
scanDotTimer.running = true
bleModel.clear()
vescsUdp = []
mBle.startScan()
if (!Utility.isBleScanEnabled()) {
bleEn.open()
}
}
onRejected: {
VescIf.emitMessageDialog(
"Location Permission",
"VESC Tool cannot scan for bluetoot devices or log data with location information without the " +
"the location permission. Please accept the request in order to use these features.",
false, false)
}
title: "BLE scan"
Text {
color: Utility.getAppHexColor("lightText")
verticalAlignment: Text.AlignVCenter
anchors.fill: parent
wrapMode: Text.WordWrap
text:
"VESC Tool needs to access the location of your device to scan for " +
"Bluetooth devices as well as for recording your location when doing " +
"realtime data logging.\n\n" +
"In order to keep logging when VESC Tool is in the background and/or when the " +
"screen is off, the permission to log data in the background is also required. " +
"Otherwise the logs will only get location information together with the motor " +
"data when the screen is on and VESC Tool is in the foreground."
}
}
}