Skip to content

Commit 6105210

Browse files
authored
Add manual connection option (#3703)
* Hide manual connection and refactor port_handler * Rebased
1 parent d354567 commit 6105210

File tree

5 files changed

+132
-113
lines changed

5 files changed

+132
-113
lines changed

locales/en/messages.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"portsSelectManual": {
5656
"message": "Manual Selection"
5757
},
58+
"portsSelectNone": {
59+
"message": "No connection available"
60+
},
5861
"portsSelectVirtual": {
5962
"message": "Virtual Mode (Experimental)",
6063
"description": "Configure a Virtual Flight Controller without the need of a physical FC."
@@ -113,6 +116,10 @@
113116
"message": "Use mDNS Browser Device discovery on network (experimental)",
114117
"description": "Enable mDNS Browser Device discovery in PortHandler (experimental)"
115118
},
119+
"showManualMode": {
120+
"message": "Enable manual connection mode",
121+
"description": "Text for the option to enable or disable manual connection mode"
122+
},
116123
"showVirtualMode": {
117124
"message": "Enable virtual connection mode",
118125
"description": "Text for the option to enable or disable the virtual FC"

src/js/port_handler.js

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { generateVirtualApiVersions, getTextWidth } from './utils/common';
55
import { get as getConfig } from "./ConfigStorage";
66
import serial from "./serial";
77
import MdnsDiscovery from "./mdns_discovery";
8-
import $ from 'jquery';
98
import { isWeb } from "./utils/isWeb";
109

1110
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: [
1817
] };
1918

2019
const PortHandler = new function () {
20+
this.currentPorts = [];
2121
this.initialPorts = false;
2222
this.port_detected_callbacks = [];
2323
this.port_removed_callbacks = [];
@@ -26,6 +26,7 @@ const PortHandler = new function () {
2626
this.showAllSerialDevices = false;
2727
this.useMdnsBrowser = false;
2828
this.showVirtualMode = false;
29+
this.showManualMode = false;
2930
};
3031

3132
PortHandler.initialize = function () {
@@ -56,6 +57,7 @@ PortHandler.reinitialize = function () {
5657
}
5758

5859
this.showVirtualMode = getConfig('showVirtualMode').showVirtualMode;
60+
this.showManualMode = getConfig('showManualMode').showManualMode;
5961
this.showAllSerialDevices = getConfig('showAllSerialDevices').showAllSerialDevices;
6062
this.useMdnsBrowser = getConfig('useMdnsBrowser').useMdnsBrowser;
6163

@@ -86,10 +88,10 @@ PortHandler.check_serial_devices = function () {
8688
const self = this;
8789

8890
serial.getDevices(function(cp) {
89-
let currentPorts = [];
91+
self.currentPorts = [];
9092

9193
if (self.useMdnsBrowser) {
92-
currentPorts = [
94+
self.currentPorts = [
9395
...cp,
9496
...(MdnsDiscovery.mdnsBrowser.services?.filter(s => s.txt?.vendor === 'elrs' && s.txt?.type === 'rx' && s.ready === true)
9597
.map(s => s.addresses.map(a => ({
@@ -101,36 +103,35 @@ PortHandler.check_serial_devices = function () {
101103
}))).flat() ?? []),
102104
].filter(Boolean);
103105
} else {
104-
currentPorts = cp;
106+
self.currentPorts = cp;
105107
}
106108

107109
// auto-select port (only during initialization)
108110
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;
112114
GUI.updateManualPortVisibility();
113115
} else {
114-
self.removePort(currentPorts);
115-
self.detectPort(currentPorts);
116+
self.removePort();
117+
self.detectPort();
116118
}
117119
});
118120
};
119121

120122
PortHandler.check_usb_devices = function (callback) {
121123
const self = this;
124+
122125
chrome.usb.getDevices(usbDevices, function (result) {
123126

124127
const dfuElement = self.portPickerElement.children("[value='DFU']");
125128
if (result.length) {
129+
// Found device in DFU mode, add it to the list
126130
if (!dfuElement.length) {
127131
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';
134135

135136
self.portPickerElement.append($('<option/>', {
136137
value: "DFU",
@@ -176,21 +177,17 @@ PortHandler.check_usb_devices = function (callback) {
176177
});
177178
};
178179

179-
PortHandler.removePort = function(currentPorts) {
180+
PortHandler.removePort = function() {
180181
const self = this;
181-
const removePorts = self.array_difference(self.initialPorts, currentPorts);
182+
const removePorts = self.array_difference(self.initialPorts, self.currentPorts);
182183

183184
if (removePorts.length) {
184185
console.log(`PortHandler - Removed: ${JSON.stringify(removePorts)}`);
185186
self.port_available = false;
186187
// 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();
194191
}
195192
// trigger callbacks (only after initialization)
196193
for (let i = (self.port_removed_callbacks.length - 1); i >= 0; i--) {
@@ -208,26 +205,26 @@ PortHandler.removePort = function(currentPorts) {
208205
self.port_removed_callbacks.splice(index, 1);
209206
}
210207
}
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));
213210
}
214211
self.updatePortSelect(self.initialPorts);
215212
self.portPickerElement.trigger('change');
216213
}
217214
};
218215

219-
PortHandler.detectPort = function(currentPorts) {
216+
PortHandler.detectPort = function() {
220217
const self = this;
221-
const newPorts = self.array_difference(currentPorts, self.initialPorts);
218+
const newPorts = self.array_difference(self.currentPorts, self.initialPorts);
222219

223220
if (newPorts.length) {
224-
currentPorts = self.updatePortSelect(currentPorts);
221+
self.updatePortSelect(self.currentPorts);
225222
console.log(`PortHandler - Found: ${JSON.stringify(newPorts)}`);
226223

227224
if (newPorts.length === 1) {
228225
self.portPickerElement.val(newPorts[0].path);
229226
} else if (newPorts.length > 1) {
230-
self.selectPort(currentPorts);
227+
self.selectActivePort();
231228
}
232229

233230
self.port_available = true;
@@ -239,11 +236,9 @@ PortHandler.detectPort = function(currentPorts) {
239236
self.portPickerElement.trigger('change');
240237

241238
// 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') {
243240
// 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();
247242
}
248243
// trigger callbacks
249244
for (let i = (self.port_detected_callbacks.length - 1); i >= 0; i--) {
@@ -261,7 +256,7 @@ PortHandler.detectPort = function(currentPorts) {
261256
self.port_detected_callbacks.splice(index, 1);
262257
}
263258
}
264-
self.initialPorts = currentPorts;
259+
self.initialPorts = self.currentPorts;
265260
}
266261
};
267262

@@ -274,20 +269,24 @@ PortHandler.sortPorts = function(ports) {
274269
});
275270
};
276271

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+
277281
PortHandler.updatePortSelect = function (ports) {
278282
ports = this.sortPorts(ports);
279283
this.portPickerElement.empty();
280284

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;
288287

289288
this.portPickerElement.append($("<option/>", {
290-
value: ports[i].path,
289+
value: port.path,
291290
text: portText,
292291
/**
293292
* @deprecated please avoid using `isDFU` and friends for new code.
@@ -307,20 +306,27 @@ PortHandler.updatePortSelect = function (ports) {
307306
}));
308307
}
309308

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+
}
318323

319324
this.setPortsInputWidth();
320-
return ports;
325+
this.currentPorts = ports;
321326
};
322327

323-
PortHandler.selectPort = function(ports) {
328+
PortHandler.selectActivePort = function() {
329+
const ports = this.currentPorts;
324330
const OS = GUI.operating_system;
325331
for (let i = 0; i < ports.length; i++) {
326332
const portName = ports[i].displayName;

0 commit comments

Comments
 (0)