Skip to content

Commit

Permalink
Rebased
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed May 5, 2024
1 parent f3c5a1d commit 68e1d49
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 260 deletions.
3 changes: 3 additions & 0 deletions locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"portsSelectManual": {
"message": "Manual Selection"
},
"portsSelectNone": {
"message": "No connection available"
},
"portsSelectVirtual": {
"message": "Virtual Mode (Experimental)",
"description": "Configure a Virtual Flight Controller without the need of a physical FC."
Expand Down
232 changes: 78 additions & 154 deletions src/js/port_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ PortHandler.check_serial_devices = function () {

// auto-select port (only during initialization)
if (!self.initialPorts) {
self.currentPorts = self.updatePortSelect(self.currentPorts);
self.selectPort(self.currentPorts);
self.updatePortSelect(self.currentPorts);
self.selectActivePort();
self.initialPorts = self.currentPorts;
GUI.updateManualPortVisibility();
} else {
Expand All @@ -121,64 +121,43 @@ PortHandler.check_serial_devices = function () {

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) {
// Found device in DFU mode, add it to the list
if (!dfuActive) {
self.clearOptions();

// self.portPickerElement.append($('<option/>', {
// value: "DFU",
// text: usbText,
// /**
// * @deprecated please avoid using `isDFU` and friends for new code.
// */
// data: {isDFU: true},
// }));

// self.portPickerElement.append($('<option/>', {
// value: 'manual',
// text: i18n.getMessage('portsSelectManual'),
// /**
// * @deprecated please avoid using `isDFU` and friends for new code.
// */
// data: {isManual: true},
// }));
const usbText = result[0].productName ? `DFU - ${result[0].productName}` : "DFU";
const dfuOption = document.createElement('option');

dfuOption.value = 'DFU';
dfuOption.text = usbText;

portSelect.add(dfuOption);
// portSelect.value = 'DFU';
if (!dfuElement.length) {
self.portPickerElement.empty();

self.setPortsInputWidth();
const productName = result[0].productName;
const usbText = productName ? `DFU - ${productName}` : 'DFU';

self.portPickerElement.append($('<option/>', {
value: "DFU",
text: usbText,
/**
* @deprecated please avoid using `isDFU` and friends for new code.
*/
data: {isDFU: true},
}));

self.setPortsInputWidth();
self.dfu_available = true;
}
} else {
// 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();
} else if (dfuElement.length) {
// No device in DFU mode, remove it from the list
dfuElement.remove();

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

self.setPortsInputWidth();
GUI.connect_lock = false;

self.dfu_available = false;
}
self.setPortsInputWidth();
self.dfu_available = false;
}

// if ($('option:selected', self.portPickerElement).val() !== 'DFU') {

if (!dfuActive) {
if ($('option:selected', self.portPickerElement).val() !== 'DFU') {
if (!(GUI.connected_to || GUI.connect_lock)) {
FC.resetState();
}
Expand All @@ -194,59 +173,6 @@ PortHandler.check_usb_devices = function (callback) {
});
};

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

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

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

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);
Expand All @@ -258,11 +184,8 @@ PortHandler.removePort = function() {
if (GUI.connected_to) {
for (let i = 0; i < removePorts.length; i++) {
if (removePorts[i].path === GUI.connected_to) {
const button = document.querySelector('div.connect_controls a.connect');
button.click();

const buttonActive = document.querySelector('div.connect_controls a.connect.active');
buttonActive.click();
$('div.connect_controls a.connect').click();
$('div.connect_controls a.connect.active').click();
}
}
}
Expand Down Expand Up @@ -295,13 +218,13 @@ PortHandler.detectPort = function() {
const newPorts = self.array_difference(self.currentPorts, self.initialPorts);

if (newPorts.length) {
self.currentPorts = self.updatePortSelect(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(self.currentPorts);
self.selectActivePort();
}

self.port_available = true;
Expand All @@ -316,8 +239,7 @@ PortHandler.detectPort = function() {
if (GUI.auto_connect && !GUI.connecting_to && !GUI.connected_to) {
// start connect procedure. We need firmware flasher protection over here
if (GUI.active_tab !== 'firmware_flasher') {
const button = document.querySelector('div.connect_controls a.connect');
button.click();
$('div.connect_controls a.connect').click();
}
}
// trigger callbacks
Expand Down Expand Up @@ -349,62 +271,64 @@ PortHandler.sortPorts = function(ports) {
});
};

PortHandler.updatePortSelect = function (ports) {
const portSelect = document.querySelector('#port');
PortHandler.addNoPortSelection = function() {
if (!this.showVirtualMode && !this.showManualMode) {
this.portPickerElement.append($("<option/>", {
value: 'none',
text: i18n.getMessage('portsSelectNone'),
}));
}
};

PortHandler.updatePortSelect = function (ports) {
ports = this.sortPorts(ports);
this.portPickerElement.empty();

for (const port of ports) {
const portText = port.displayName ? `${port.path} - ${port.displayName}` : port.path;

this.portPickerElement.append($("<option/>", {
value: port.path,
text: portText,
/**
* @deprecated please avoid using `isDFU` and friends for new code.
*/
data: {isManual: false},
}));
}

this.clearOptions();

for (let i = 0; i < ports.length; i++) {
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,
// /**
// * @deprecated please avoid using `isDFU` and friends for new code.
// */
// data: {isManual: false},
// }));
// }

// if (this.showVirtualMode) {
// this.portPickerElement.append($("<option/>", {
// value: 'virtual',
// text: i18n.getMessage('portsSelectVirtual'),
// /**
// * @deprecated please avoid using `isDFU` and friends for new code.
// */
// data: {isVirtual: true},
// }));
// }

// this.portPickerElement.append($("<option/>", {
// value: 'manual',
// text: i18n.getMessage('portsSelectManual'),
// /**
// * @deprecated please avoid using `isDFU` and friends for new code.
// */
// data: {isManual: true},
// }));
portOption.value = ports[i].path;
portOption.text = portText;

portSelect.add(portOption);
if (this.showVirtualMode) {
this.portPickerElement.append($("<option/>", {
value: 'virtual',
text: i18n.getMessage('portsSelectVirtual'),
/**
* @deprecated please avoid using `isDFU` and friends for new code.
*/
data: {isVirtual: true},
}));
}

this.addVirtualOption();
this.addManualOption();
if (this.showManualMode) {
this.portPickerElement.append($("<option/>", {
value: 'manual',
text: i18n.getMessage('portsSelectManual'),
/**
* @deprecated please avoid using `isDFU` and friends for new code.
*/
data: {isManual: true},
}));
}

if (!ports.length) { this.addNoPortsOption(); }
if (!ports.length) {
this.addNoPortSelection();
}

this.setPortsInputWidth();
return ports;
this.currentPorts = ports;
};

PortHandler.selectPort = function(ports) {
PortHandler.selectActivePort = function() {
const ports = this.currentPorts;
const OS = GUI.operating_system;
for (let i = 0; i < ports.length; i++) {
const portName = ports[i].displayName;
Expand Down
Loading

0 comments on commit 68e1d49

Please sign in to comment.