Skip to content

Commit

Permalink
Reduce MIN_TIMEOUT
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed May 1, 2023
1 parent 9663574 commit 7de6e40
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
36 changes: 21 additions & 15 deletions src/js/msp.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const MSP = {
packet_error: 0,
unsupported: 0,

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

Expand Down Expand Up @@ -305,6 +305,10 @@ const MSP = {
bufView[bufferSize - 1] = this.crc8_dvb_s2_data(bufView, 3, bufferSize - 1);
return bufferOut;
},
// function to get name of code
getMSPCodeName: function(code) {
return Object.keys(MSPCodes).find(key => MSPCodes[key] === code);
},
send_message: function (code, data, callback_sent, callback_msp, doCallbackOnError) {
if (code === undefined || !serial.connectionId || CONFIGURATOR.virtualMode) {
if (callback_msp) {
Expand All @@ -315,6 +319,7 @@ const MSP = {

let requestExists = false;

// Check if request already exists in the queue
for (const instance of MSP.callbacks) {
if (instance.code === code) {
// For MSP V1 we replace requests of the same type in the queue
Expand All @@ -325,19 +330,19 @@ const MSP = {
// which request a response belongs to.
// TODO: Implement a way to identify which request a response belongs to
// so that we can skip duplicate requests in the queue.
if (code < 255 && code !== MSPCodes.MSP_MULTIPLE_MSP) {
setTimeout(function () {
const index = MSP.callbacks.indexOf(instance);
if (index > -1) {
if (instance.timer) {
clearInterval(instance.timer);
}
MSP.callbacks.splice(index, 1);
}
}, 10);
} else {
// console.log(`Remove duplicate request ${code} (${this.getMSPCodeName(code)}) from queue and add updated request`);
// if (code < 255 && code !== MSPCodes.MSP_MULTIPLE_MSP) {
// const index = MSP.callbacks.indexOf(instance);
// if (index > -1) {
// if (instance.timer) {
// clearInterval(instance.timer);
// }
// MSP.callbacks.splice(index, 1);
// }
// } else {
requestExists = true;
}
break;
// }
}
}

Expand All @@ -352,14 +357,15 @@ const MSP = {
};

if (!requestExists) {
obj.timer = setInterval(function () {
obj.timer = setTimeout(function () {
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${MSP.timeout} QUEUE: ${MSP.callbacks.length} (${MSP.callbacks.map(function (e) { return e.code; })})`);
serial.send(bufferOut, function (_sendInfo) {
// TODO: handle error
// Determine new timeout
obj.stop = performance.now();
const executionTime = Math.round(obj.stop - obj.start);
MSP.timeout = Math.max(MSP.MIN_TIMEOUT, Math.min(executionTime, MSP.MAX_TIMEOUT));
});

}, MSP.timeout);
}

Expand Down
2 changes: 1 addition & 1 deletion src/js/msp/MSPHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2733,7 +2733,7 @@ MspHelper.prototype.writeConfiguration = async function(callback) {
setTimeout(async function() {
MSP.send_message(MSPCodes.MSP_EEPROM_WRITE, false, false, function() {
gui_log(i18n.getMessage('configurationEepromSaved'));

console.log('Configuration saved to EEPROM');
if (callback) {
callback();
}
Expand Down
14 changes: 7 additions & 7 deletions src/js/protocols/stm32.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ STM32_protocol.prototype.connect = function (port, baud, hex, options, callback)
function reboot() {
const buffer = [];
buffer.push8(rebootMode);
MSP.send_message(MSPCodes.MSP_SET_REBOOT, buffer, () => {

// if firmware doesn't flush MSP/serial send buffers and gracefully shutdown VCP connections we won't get a reply, so don't wait for it.

self.msp_connector.disconnect(disconnectionResult => onDisconnect(disconnectionResult));

}, () => console.log('Reboot request received by device'));
setTimeout(() => {
MSP.send_message(MSPCodes.MSP_SET_REBOOT, buffer, () => {
// if firmware doesn't flush MSP/serial send buffers and gracefully shutdown VCP connections we won't get a reply, so don't wait for it.
self.msp_connector.disconnect(disconnectionResult => onDisconnect(disconnectionResult));
console.log('Reboot request received by device');
});
}, 100);
}

function onAbort() {
Expand Down
12 changes: 6 additions & 6 deletions src/js/tabs/ports.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ ports.initialize = function (callback) {
GUI.content_ready(callback);
}

function on_save_handler() {
function on_save_handler() {
tracking.sendSaveAndChangeEvents(tracking.EVENT_CATEGORIES.FLIGHT_CONTROLLER, self.analyticsChanges, 'ports');
self.analyticsChanges = {};

Expand Down Expand Up @@ -486,11 +486,11 @@ ports.initialize = function (callback) {
MSP.send_message(MSPCodes.MSP_SET_FEATURE_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_FEATURE_CONFIG), false, save_to_eeprom);
}

async function save_to_eeprom() {
await mspHelper.writeConfiguration();

GUI.tab_switch_cleanup(function() {
MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection);
function save_to_eeprom() {
mspHelper.writeConfiguration(function() {
GUI.tab_switch_cleanup(function() {
MSP.send_message(MSPCodes.MSP_SET_REBOOT, false, false, reinitializeConnection);
});
});
}
}
Expand Down

0 comments on commit 7de6e40

Please sign in to comment.