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 6, 2023
1 parent 9663574 commit 3f1a5f3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 45 deletions.
32 changes: 6 additions & 26 deletions 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: {
Expand Down Expand Up @@ -53,7 +52,7 @@ const MSP = {
packet_error: 0,
unsupported: 0,

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

Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/js/msp/MSPHelper.js
Expand Up @@ -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();
}
Expand Down
14 changes: 7 additions & 7 deletions src/js/protocols/stm32.js
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
4 changes: 1 addition & 3 deletions src/js/tabs/motors.js
Expand Up @@ -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'));
Expand Down
12 changes: 6 additions & 6 deletions src/js/tabs/ports.js
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 3f1a5f3

Please sign in to comment.