From 3f1a5f3730e9a32f5b4789c7c4120dd72c2e291c Mon Sep 17 00:00:00 2001 From: Mark Haslinghuis Date: Tue, 2 May 2023 00:53:25 +0200 Subject: [PATCH] Reduce MIN_TIMEOUT --- src/js/msp.js | 32 ++++++-------------------------- src/js/msp/MSPHelper.js | 6 +++--- src/js/protocols/stm32.js | 14 +++++++------- src/js/tabs/motors.js | 4 +--- src/js/tabs/ports.js | 12 ++++++------ 5 files changed, 23 insertions(+), 45 deletions(-) diff --git a/src/js/msp.js b/src/js/msp.js index 976b5d00a2e..13ac6460e56 100644 --- a/src/js/msp.js +++ b/src/js/msp.js @@ -1,7 +1,6 @@ import GUI from "./gui.js"; import CONFIGURATOR from "./data_storage.js"; import serial from "./serial.js"; -import MSPCodes from "./msp/MSPCodes.js"; const MSP = { symbols: { @@ -53,7 +52,7 @@ const MSP = { packet_error: 0, unsupported: 0, - MIN_TIMEOUT: 200, + MIN_TIMEOUT: 50, MAX_TIMEOUT: 2000, timeout: 200, @@ -313,31 +312,13 @@ const MSP = { return false; } + // Check if request already exists in the queue let requestExists = false; - for (const instance of MSP.callbacks) { if (instance.code === code) { - // For MSP V1 we replace requests of the same type in the queue - // For MSP V2 we allow multiple requests of the same type to be in the queue - // This is because MSP V2 allows for multiple requests of the same type to be sent - // in a single frame, and we don't want to skip any of them. - // This is a workaround for the fact that we don't have a way to identify - // 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 { - requestExists = true; - } + requestExists = true; + + break; } } @@ -352,14 +333,13 @@ 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) { 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); } diff --git a/src/js/msp/MSPHelper.js b/src/js/msp/MSPHelper.js index 24e87ce5b5c..37a2428c0e6 100644 --- a/src/js/msp/MSPHelper.js +++ b/src/js/msp/MSPHelper.js @@ -2729,11 +2729,11 @@ MspHelper.prototype.sendSerialConfig = function(callback) { MSP.send_message(mspCode, mspHelper.crunch(mspCode), false, callback); }; -MspHelper.prototype.writeConfiguration = async function(callback) { - setTimeout(async function() { +MspHelper.prototype.writeConfiguration = function(callback) { + setTimeout(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(); } diff --git a/src/js/protocols/stm32.js b/src/js/protocols/stm32.js index d43ea406553..2f252ce4724 100644 --- a/src/js/protocols/stm32.js +++ b/src/js/protocols/stm32.js @@ -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() { diff --git a/src/js/tabs/motors.js b/src/js/tabs/motors.js index a0387ba3ef5..d19a81a6199 100644 --- a/src/js/tabs/motors.js +++ b/src/js/tabs/motors.js @@ -1163,13 +1163,11 @@ motors.initialize = async function (callback) { await MSP.promise(MSPCodes.MSP_SET_FILTER_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_FILTER_CONFIG)); } - await mspHelper.writeConfiguration(); - tracking.sendSaveAndChangeEvents(tracking.EVENT_CATEGORIES.FLIGHT_CONTROLLER, self.analyticsChanges, 'motors'); self.analyticsChanges = {}; self.configHasChanged = false; - reboot(); + await mspHelper.writeConfiguration(reboot); }); $('a.stop').on('click', () => motorsEnableTestModeElement.prop('checked', false).trigger('change')); diff --git a/src/js/tabs/ports.js b/src/js/tabs/ports.js index f8a0a4b33c9..f5f8b8de573 100644 --- a/src/js/tabs/ports.js +++ b/src/js/tabs/ports.js @@ -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 = {}; @@ -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); + }); }); } }