@@ -5,7 +5,6 @@ import { generateVirtualApiVersions, getTextWidth } from './utils/common';
5
5
import { get as getConfig } from "./ConfigStorage" ;
6
6
import serial from "./serial" ;
7
7
import MdnsDiscovery from "./mdns_discovery" ;
8
- import $ from 'jquery' ;
9
8
import { isWeb } from "./utils/isWeb" ;
10
9
11
10
const TIMEOUT_CHECK = 500 ; // With 250 it seems that it produces a memory leak and slowdown in some versions, reason unknown
@@ -18,6 +17,7 @@ export const usbDevices = { filters: [
18
17
] } ;
19
18
20
19
const PortHandler = new function ( ) {
20
+ this . currentPorts = [ ] ;
21
21
this . initialPorts = false ;
22
22
this . port_detected_callbacks = [ ] ;
23
23
this . port_removed_callbacks = [ ] ;
@@ -26,6 +26,7 @@ const PortHandler = new function () {
26
26
this . showAllSerialDevices = false ;
27
27
this . useMdnsBrowser = false ;
28
28
this . showVirtualMode = false ;
29
+ this . showManualMode = false ;
29
30
} ;
30
31
31
32
PortHandler . initialize = function ( ) {
@@ -56,6 +57,7 @@ PortHandler.reinitialize = function () {
56
57
}
57
58
58
59
this . showVirtualMode = getConfig ( 'showVirtualMode' ) . showVirtualMode ;
60
+ this . showManualMode = getConfig ( 'showManualMode' ) . showManualMode ;
59
61
this . showAllSerialDevices = getConfig ( 'showAllSerialDevices' ) . showAllSerialDevices ;
60
62
this . useMdnsBrowser = getConfig ( 'useMdnsBrowser' ) . useMdnsBrowser ;
61
63
@@ -86,10 +88,10 @@ PortHandler.check_serial_devices = function () {
86
88
const self = this ;
87
89
88
90
serial . getDevices ( function ( cp ) {
89
- let currentPorts = [ ] ;
91
+ self . currentPorts = [ ] ;
90
92
91
93
if ( self . useMdnsBrowser ) {
92
- currentPorts = [
94
+ self . currentPorts = [
93
95
...cp ,
94
96
...( MdnsDiscovery . mdnsBrowser . services ?. filter ( s => s . txt ?. vendor === 'elrs' && s . txt ?. type === 'rx' && s . ready === true )
95
97
. map ( s => s . addresses . map ( a => ( {
@@ -101,36 +103,35 @@ PortHandler.check_serial_devices = function () {
101
103
} ) ) ) . flat ( ) ?? [ ] ) ,
102
104
] . filter ( Boolean ) ;
103
105
} else {
104
- currentPorts = cp ;
106
+ self . currentPorts = cp ;
105
107
}
106
108
107
109
// auto-select port (only during initialization)
108
110
if ( ! self . initialPorts ) {
109
- currentPorts = self . updatePortSelect ( currentPorts ) ;
110
- self . selectPort ( currentPorts ) ;
111
- self . initialPorts = currentPorts ;
111
+ self . updatePortSelect ( self . currentPorts ) ;
112
+ self . selectActivePort ( ) ;
113
+ self . initialPorts = self . currentPorts ;
112
114
GUI . updateManualPortVisibility ( ) ;
113
115
} else {
114
- self . removePort ( currentPorts ) ;
115
- self . detectPort ( currentPorts ) ;
116
+ self . removePort ( ) ;
117
+ self . detectPort ( ) ;
116
118
}
117
119
} ) ;
118
120
} ;
119
121
120
122
PortHandler . check_usb_devices = function ( callback ) {
121
123
const self = this ;
124
+
122
125
chrome . usb . getDevices ( usbDevices , function ( result ) {
123
126
124
127
const dfuElement = self . portPickerElement . children ( "[value='DFU']" ) ;
125
128
if ( result . length ) {
129
+ // Found device in DFU mode, add it to the list
126
130
if ( ! dfuElement . length ) {
127
131
self . portPickerElement . empty ( ) ;
128
- let usbText ;
129
- if ( result [ 0 ] . productName ) {
130
- usbText = ( `DFU - ${ result [ 0 ] . productName } ` ) ;
131
- } else {
132
- usbText = "DFU" ;
133
- }
132
+
133
+ const productName = result [ 0 ] . productName ;
134
+ const usbText = productName ? `DFU - ${ productName } ` : 'DFU' ;
134
135
135
136
self . portPickerElement . append ( $ ( '<option/>' , {
136
137
value : "DFU" ,
@@ -176,21 +177,17 @@ PortHandler.check_usb_devices = function (callback) {
176
177
} ) ;
177
178
} ;
178
179
179
- PortHandler . removePort = function ( currentPorts ) {
180
+ PortHandler . removePort = function ( ) {
180
181
const self = this ;
181
- const removePorts = self . array_difference ( self . initialPorts , currentPorts ) ;
182
+ const removePorts = self . array_difference ( self . initialPorts , self . currentPorts ) ;
182
183
183
184
if ( removePorts . length ) {
184
185
console . log ( `PortHandler - Removed: ${ JSON . stringify ( removePorts ) } ` ) ;
185
186
self . port_available = false ;
186
187
// disconnect "UI" - routine can't fire during atmega32u4 reboot procedure !!!
187
- if ( GUI . connected_to ) {
188
- for ( let i = 0 ; i < removePorts . length ; i ++ ) {
189
- if ( removePorts [ i ] . path === GUI . connected_to ) {
190
- $ ( 'div.connect_controls a.connect' ) . click ( ) ;
191
- $ ( 'div.connect_controls a.connect.active' ) . click ( ) ;
192
- }
193
- }
188
+ if ( removePorts . some ( port => port . path === GUI . connected_to ) ) {
189
+ $ ( 'div.connect_controls a.connect' ) . click ( ) ;
190
+ $ ( 'div.connect_controls a.connect.active' ) . click ( ) ;
194
191
}
195
192
// trigger callbacks (only after initialization)
196
193
for ( let i = ( self . port_removed_callbacks . length - 1 ) ; i >= 0 ; i -- ) {
@@ -208,26 +205,26 @@ PortHandler.removePort = function(currentPorts) {
208
205
self . port_removed_callbacks . splice ( index , 1 ) ;
209
206
}
210
207
}
211
- for ( let i = 0 ; i < removePorts . length ; i ++ ) {
212
- self . initialPorts . splice ( self . initialPorts . indexOf ( removePorts [ i ] ) , 1 ) ;
208
+ for ( const port of removePorts ) {
209
+ self . initialPorts . splice ( self . initialPorts . indexOf ( port , 1 ) ) ;
213
210
}
214
211
self . updatePortSelect ( self . initialPorts ) ;
215
212
self . portPickerElement . trigger ( 'change' ) ;
216
213
}
217
214
} ;
218
215
219
- PortHandler . detectPort = function ( currentPorts ) {
216
+ PortHandler . detectPort = function ( ) {
220
217
const self = this ;
221
- const newPorts = self . array_difference ( currentPorts , self . initialPorts ) ;
218
+ const newPorts = self . array_difference ( self . currentPorts , self . initialPorts ) ;
222
219
223
220
if ( newPorts . length ) {
224
- currentPorts = self . updatePortSelect ( currentPorts ) ;
221
+ self . updatePortSelect ( self . currentPorts ) ;
225
222
console . log ( `PortHandler - Found: ${ JSON . stringify ( newPorts ) } ` ) ;
226
223
227
224
if ( newPorts . length === 1 ) {
228
225
self . portPickerElement . val ( newPorts [ 0 ] . path ) ;
229
226
} else if ( newPorts . length > 1 ) {
230
- self . selectPort ( currentPorts ) ;
227
+ self . selectActivePort ( ) ;
231
228
}
232
229
233
230
self . port_available = true ;
@@ -239,11 +236,9 @@ PortHandler.detectPort = function(currentPorts) {
239
236
self . portPickerElement . trigger ( 'change' ) ;
240
237
241
238
// auto-connect if enabled
242
- if ( GUI . auto_connect && ! GUI . connecting_to && ! GUI . connected_to ) {
239
+ if ( GUI . auto_connect && ! GUI . connecting_to && ! GUI . connected_to && GUI . active_tab !== 'firmware_flasher' ) {
243
240
// start connect procedure. We need firmware flasher protection over here
244
- if ( GUI . active_tab !== 'firmware_flasher' ) {
245
- $ ( 'div.connect_controls a.connect' ) . click ( ) ;
246
- }
241
+ $ ( 'div.connect_controls a.connect' ) . click ( ) ;
247
242
}
248
243
// trigger callbacks
249
244
for ( let i = ( self . port_detected_callbacks . length - 1 ) ; i >= 0 ; i -- ) {
@@ -261,7 +256,7 @@ PortHandler.detectPort = function(currentPorts) {
261
256
self . port_detected_callbacks . splice ( index , 1 ) ;
262
257
}
263
258
}
264
- self . initialPorts = currentPorts ;
259
+ self . initialPorts = self . currentPorts ;
265
260
}
266
261
} ;
267
262
@@ -274,20 +269,24 @@ PortHandler.sortPorts = function(ports) {
274
269
} ) ;
275
270
} ;
276
271
272
+ PortHandler . addNoPortSelection = function ( ) {
273
+ if ( ! this . showVirtualMode && ! this . showManualMode ) {
274
+ this . portPickerElement . append ( $ ( "<option/>" , {
275
+ value : 'none' ,
276
+ text : i18n . getMessage ( 'portsSelectNone' ) ,
277
+ } ) ) ;
278
+ }
279
+ } ;
280
+
277
281
PortHandler . updatePortSelect = function ( ports ) {
278
282
ports = this . sortPorts ( ports ) ;
279
283
this . portPickerElement . empty ( ) ;
280
284
281
- for ( let i = 0 ; i < ports . length ; i ++ ) {
282
- let portText ;
283
- if ( ports [ i ] . displayName ) {
284
- portText = ( `${ ports [ i ] . path } - ${ ports [ i ] . displayName } ` ) ;
285
- } else {
286
- portText = ports [ i ] . path ;
287
- }
285
+ for ( const port of ports ) {
286
+ const portText = port . displayName ? `${ port . path } - ${ port . displayName } ` : port . path ;
288
287
289
288
this . portPickerElement . append ( $ ( "<option/>" , {
290
- value : ports [ i ] . path ,
289
+ value : port . path ,
291
290
text : portText ,
292
291
/**
293
292
* @deprecated please avoid using `isDFU` and friends for new code.
@@ -307,20 +306,27 @@ PortHandler.updatePortSelect = function (ports) {
307
306
} ) ) ;
308
307
}
309
308
310
- this . portPickerElement . append ( $ ( "<option/>" , {
311
- value : 'manual' ,
312
- text : i18n . getMessage ( 'portsSelectManual' ) ,
313
- /**
314
- * @deprecated please avoid using `isDFU` and friends for new code.
315
- */
316
- data : { isManual : true } ,
317
- } ) ) ;
309
+ if ( this . showManualMode ) {
310
+ this . portPickerElement . append ( $ ( "<option/>" , {
311
+ value : 'manual' ,
312
+ text : i18n . getMessage ( 'portsSelectManual' ) ,
313
+ /**
314
+ * @deprecated please avoid using `isDFU` and friends for new code.
315
+ */
316
+ data : { isManual : true } ,
317
+ } ) ) ;
318
+ }
319
+
320
+ if ( ! ports . length ) {
321
+ this . addNoPortSelection ( ) ;
322
+ }
318
323
319
324
this . setPortsInputWidth ( ) ;
320
- return ports ;
325
+ this . currentPorts = ports ;
321
326
} ;
322
327
323
- PortHandler . selectPort = function ( ports ) {
328
+ PortHandler . selectActivePort = function ( ) {
329
+ const ports = this . currentPorts ;
324
330
const OS = GUI . operating_system ;
325
331
for ( let i = 0 ; i < ports . length ; i ++ ) {
326
332
const portName = ports [ i ] . displayName ;
0 commit comments