Skip to content

Commit

Permalink
Only retain last duplicate MSP command in queue
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed Apr 11, 2023
1 parent a4d876d commit adf7a9f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
33 changes: 23 additions & 10 deletions src/js/msp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 @@ -47,7 +48,6 @@ const MSP = {
message_checksum: 0,
messageIsJumboFrame: false,
crcError: false,
sequence: 0,

callbacks: [],
packet_error: 0,
Expand All @@ -62,6 +62,10 @@ const MSP = {

JUMBO_FRAME_SIZE_LIMIT: 255,

// function to get name of code
getMSPCodeName: function(code) {
return Object.keys(MSPCodes).find(key => MSPCodes[key] === code);
},
read: function (readInfo) {
if (CONFIGURATOR.virtualMode) {
return;
Expand Down Expand Up @@ -330,11 +334,22 @@ const MSP = {

for (const instance of MSP.callbacks) {
if (instance.code === code) {
// request already exists in queue, don't add it again
if (callback_msp) {
callback_msp();
}
return false;
// request already exists in queue, remove existing instance from queue
// and add new instance to the end of the queue
// this is done to prevent the queue from getting clogged with the same request
// and to ensure that the callback is called in the order that the requests were made
// we allow existing request to timeout and be removed from the queue
setTimeout(function () {
const index = MSP.callbacks.indexOf(instance);
if (index > -1) {
if (instance.timer) {
clearInterval(instance.timer);
}
MSP.callbacks.splice(index, 1);
}
}, MSP.timeout);

console.log(`MSP.send_message: code: ${code} ${this.getMSPCodeName(code)} QUEUE: ${MSP.callbacks.length} updated in queue`);
}
}

Expand All @@ -343,15 +358,13 @@ const MSP = {
const obj = {
'code': code,
'requestBuffer': bufferOut,
'callback': callback_msp ? callback_msp : false,
'timer': false,
'callback': callback_msp,
'callbackOnError': doCallbackOnError,
'start': performance.now(),
'sequence': MSP.sequence++,
};

obj.timer = setInterval(function () {
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${MSP.timeout} QUEUE: ${MSP.callbacks.length} SEQUENCE: ${obj.sequence}`);
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${MSP.timeout} QUEUE: ${MSP.callbacks.length}`);
serial.send(bufferOut, function (_sendInfo) {
obj.stop = performance.now();
const executionTime = Math.round(obj.stop - obj.start);
Expand Down
2 changes: 1 addition & 1 deletion src/tabs/presets/presets.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ presets.preselectFilterFields = function() {
const currentVersion = FC.CONFIG.flightControllerVersion;
const selectedVersions = [];

for(const bfVersion of this.presetsRepo.index.uniqueValues.firmware_version) {
for (const bfVersion of this.presetsRepo.index.uniqueValues.firmware_version) {
if (currentVersion.startsWith(bfVersion)) {
selectedVersions.push(bfVersion);
}
Expand Down

0 comments on commit adf7a9f

Please sign in to comment.