Skip to content

Commit

Permalink
Hide manual connection and refactor port_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed Dec 29, 2023
1 parent 6577697 commit 82d9197
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 136 deletions.
4 changes: 4 additions & 0 deletions locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@
"message": "Use mDNS Browser Device discovery on network (experimental)",
"description": "Enable mDNS Browser Device discovery in PortHandler (experimental)"
},
"showManualMode": {
"message": "Enable manual connection mode",
"description": "Text for the option to enable or disable manual connection mode"
},
"showVirtualMode": {
"message": "Enable virtual connection mode",
"description": "Text for the option to enable or disable the virtual FC"
Expand Down
2 changes: 1 addition & 1 deletion src/js/msp.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const MSP = {
packet_error: 0,
unsupported: 0,

MIN_TIMEOUT: 50,
MIN_TIMEOUT: 100,
MAX_TIMEOUT: 2000,
timeout: 200,

Expand Down
180 changes: 114 additions & 66 deletions src/js/port_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const usbDevices = { filters: [
] };

const PortHandler = new function () {
this.currentPorts = [];
this.initialPorts = false;
this.port_detected_callbacks = [];
this.port_removed_callbacks = [];
Expand All @@ -26,6 +27,7 @@ const PortHandler = new function () {
this.showAllSerialDevices = false;
this.useMdnsBrowser = false;
this.showVirtualMode = false;
this.showManualMode = false;
};

PortHandler.initialize = function () {
Expand Down Expand Up @@ -56,6 +58,7 @@ PortHandler.reinitialize = function () {
}

this.showVirtualMode = getConfig('showVirtualMode').showVirtualMode;
this.showManualMode = getConfig('showManualMode').showManualMode;
this.showAllSerialDevices = getConfig('showAllSerialDevices').showAllSerialDevices;
this.useMdnsBrowser = getConfig('useMdnsBrowser').useMdnsBrowser;

Expand Down Expand Up @@ -86,10 +89,10 @@ PortHandler.check_serial_devices = function () {
const self = this;

serial.getDevices(function(cp) {
let currentPorts = [];
self.currentPorts = [];

if (self.useMdnsBrowser) {
currentPorts = [
self.currentPorts = [
...cp,
...(MdnsDiscovery.mdnsBrowser.services?.filter(s => s.txt?.vendor === 'elrs' && s.txt?.type === 'rx' && s.ready === true)
.map(s => s.addresses.map(a => ({
Expand All @@ -101,77 +104,133 @@ PortHandler.check_serial_devices = function () {
}))).flat() ?? []),
].filter(Boolean);
} else {
currentPorts = cp;
self.currentPorts = cp;
}

// auto-select port (only during initialization)
if (!self.initialPorts) {
currentPorts = self.updatePortSelect(currentPorts);
self.selectPort(currentPorts);
self.initialPorts = currentPorts;
self.currentPorts = self.updatePortSelect(self.currentPorts);
self.selectPort(self.currentPorts);
self.initialPorts = self.currentPorts;
GUI.updateManualPortVisibility();
} else {
self.removePort(currentPorts);
self.detectPort(currentPorts);
self.removePort();
self.detectPort();
}
});
};

PortHandler.check_usb_devices = function (callback) {
const self = this;
const portSelect = document.querySelector('#port');

chrome.usb.getDevices(usbDevices, function (result) {
const dfuActive = !!portSelect.value.startsWith('DFU');

const dfuElement = self.portPickerElement.children("[value='DFU']");
if (result.length) {
if (!dfuElement.length) {
self.portPickerElement.empty();
let usbText;
if (result[0].productName) {
usbText = (`DFU - ${result[0].productName}`);
} else {
usbText = "DFU";
}
// Found device in DFU mode, add it to the list
if (!dfuActive) {
self.clearOptions();

const usbText = (result[0].productName) ? (`DFU - ${result[0].productName}`) : "DFU";
const dfuOption = document.createElement('option');

self.portPickerElement.append($('<option/>', {
value: "DFU",
text: usbText,
data: {isDFU: true},
}));
dfuOption.value = 'DFU';
dfuOption.text = usbText;

self.portPickerElement.append($('<option/>', {
value: 'manual',
text: i18n.getMessage('portsSelectManual'),
data: {isManual: true},
}));
portSelect.add(dfuOption);

self.portPickerElement.val('DFU').trigger('change');
// self.portPickerElement.val('DFU').trigger('change');
portSelect.value = 'DFU';
self.setPortsInputWidth();
}
self.dfu_available = true;
} else {
if (dfuElement.length) {
dfuElement.remove();
self.setPortsInputWidth();
// We are not in DFU mode anymore. Add items here as check_serial_devices() will not be called without a change in serial devices.
if (dfuActive) {
self.clearOptions();

self.addManualOption();
self.addVirtualOption();
self.addNoPortsOption();

self.setPortsInputWidth();
}
self.dfu_available = false;
}
if (callback) {
callback(self.dfu_available);
}
if (!$('option:selected', self.portPickerElement).data().isDFU) {

if (!dfuActive) {
if (!(GUI.connected_to || GUI.connect_lock)) {
FC.resetState();
}
if (self.dfu_available) {
self.portPickerElement.trigger('change');
}
}

if (callback) {
callback(self.dfu_available);
}
});
};

PortHandler.removePort = function(currentPorts) {
PortHandler.clearOptions = function () {
const self = this;
const portSelect = document.querySelector('#port');
let index = portSelect.length;

while (index--) {
if (portSelect.options[index]) {
portSelect.remove(index);
}
}
};

PortHandler.addManualOption = function() {
const self = this;
const removePorts = self.array_difference(self.initialPorts, currentPorts);

if (self.showManualMode) {
const portSelect = document.querySelector('#port');
const manualOption = document.createElement('option');

manualOption.value = 'manual';
manualOption.text = i18n.getMessage('portsSelectManual');

portSelect.add(manualOption);
}
};

PortHandler.addVirtualOption = function() {
const self = this;

if (self.showVirtualMode) {
const portSelect = document.querySelector('#port');
const virtualOption = document.createElement('option');

virtualOption.value = 'virtual';
virtualOption.text = i18n.getMessage('portsSelectVirtual');

portSelect.add(virtualOption);
}
};

PortHandler.addNoPortsOption = function() {
const self = this;

if (!self.showVirtualMode && !self.showManualMode) {
const portSelect = document.querySelector('#port');
const noPortsOption = document.createElement('option');

noPortsOption.value = 'none';
noPortsOption.text = 'No connections available';

portSelect.add(noPortsOption);
}
};

PortHandler.removePort = function() {
const self = this;
const removePorts = self.array_difference(self.initialPorts, self.currentPorts);

if (removePorts.length) {
console.log(`PortHandler - Removed: ${JSON.stringify(removePorts)}`);
Expand Down Expand Up @@ -209,18 +268,18 @@ PortHandler.removePort = function(currentPorts) {
}
};

PortHandler.detectPort = function(currentPorts) {
PortHandler.detectPort = function() {
const self = this;
const newPorts = self.array_difference(currentPorts, self.initialPorts);
const newPorts = self.array_difference(self.currentPorts, self.initialPorts);

if (newPorts.length) {
currentPorts = self.updatePortSelect(currentPorts);
self.currentPorts = self.updatePortSelect(self.currentPorts);
console.log(`PortHandler - Found: ${JSON.stringify(newPorts)}`);

if (newPorts.length === 1) {
self.portPickerElement.val(newPorts[0].path);
} else if (newPorts.length > 1) {
self.selectPort(currentPorts);
self.selectPort(self.currentPorts);
}

self.port_available = true;
Expand Down Expand Up @@ -254,7 +313,7 @@ PortHandler.detectPort = function(currentPorts) {
self.port_detected_callbacks.splice(index, 1);
}
}
self.initialPorts = currentPorts;
self.initialPorts = self.currentPorts;
}
};

Expand All @@ -268,37 +327,26 @@ PortHandler.sortPorts = function(ports) {
};

PortHandler.updatePortSelect = function (ports) {
const portSelect = document.querySelector('#port');

ports = this.sortPorts(ports);
this.portPickerElement.empty();

this.clearOptions();

for (let i = 0; i < ports.length; i++) {
let portText;
if (ports[i].displayName) {
portText = (`${ports[i].path} - ${ports[i].displayName}`);
} else {
portText = ports[i].path;
}
const portOption = document.createElement('option');
const portText = ports[i].displayName ? `${ports[i].path} - ${ports[i].displayName}` : ports[i].path;

this.portPickerElement.append($("<option/>", {
value: ports[i].path,
text: portText,
data: {isManual: false},
}));
}
portOption.value = ports[i].path;
portOption.text = portText;

if (this.showVirtualMode) {
this.portPickerElement.append($("<option/>", {
value: 'virtual',
text: i18n.getMessage('portsSelectVirtual'),
data: {isVirtual: true},
}));
portSelect.add(portOption);
}

this.portPickerElement.append($("<option/>", {
value: 'manual',
text: i18n.getMessage('portsSelectManual'),
data: {isManual: true},
}));
this.addVirtualOption();
this.addManualOption();

if (!ports.length) this.addNoPortsOption();

this.setPortsInputWidth();
return ports;
Expand Down
Loading

0 comments on commit 82d9197

Please sign in to comment.