1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-23 00:05:22 +03:00

Improve msp send

This commit is contained in:
Mark Haslinghuis 2025-06-10 20:10:56 +02:00
parent f068b1a67c
commit 2f6140b6ba

View file

@ -374,6 +374,32 @@ const MSP = {
serial.send(bufferOut); serial.send(bufferOut);
}, },
send_message(code, data, callback_sent, callback_msp, doCallbackOnError) { send_message(code, data, callback_sent, callback_msp, doCallbackOnError) {
// Early validation
if (!this._validateSendMessage(code, callback_msp)) {
return false;
}
const isDuplicateRequest = this._isDuplicateRequest(code);
const bufferOut = this._encodeMessage(code, data);
const requestObj = this._createRequestObject(code, bufferOut, callback_msp, doCallbackOnError);
// Set up timeout only for new requests
if (!isDuplicateRequest) {
this._setupTimeout(requestObj, bufferOut);
}
this.callbacks.push(requestObj);
// Send message if it has data or is a new request
if (this._shouldSendMessage(data, isDuplicateRequest)) {
this._sendBuffer(bufferOut, callback_sent);
}
return true;
},
_validateSendMessage(code, callback_msp) {
const connected = serial.connected; const connected = serial.connected;
if (code === undefined || !connected || CONFIGURATOR.virtualMode) { if (code === undefined || !connected || CONFIGURATOR.virtualMode) {
@ -383,58 +409,62 @@ const MSP = {
return false; return false;
} }
let requestExists = false; return true;
for (const instance of this.callbacks) { },
if (instance.code === code) {
requestExists = true;
break; _isDuplicateRequest(code) {
} return this.callbacks.some((instance) => instance.code === code);
} },
const bufferOut = code <= 254 ? this.encode_message_v1(code, data) : this.encode_message_v2(code, data); _encodeMessage(code, data) {
return code <= 254 ? this.encode_message_v1(code, data) : this.encode_message_v2(code, data);
},
const obj = { _createRequestObject(code, bufferOut, callback_msp, doCallbackOnError) {
code: code, return {
code,
requestBuffer: bufferOut, requestBuffer: bufferOut,
callback: callback_msp, callback: callback_msp,
callbackOnError: doCallbackOnError, callbackOnError: doCallbackOnError,
start: performance.now(), start: performance.now(),
}; };
},
if (!requestExists) { _setupTimeout(requestObj, bufferOut) {
obj.timer = setTimeout(() => { requestObj.timer = setTimeout(() => {
console.warn( this._handleTimeout(requestObj, bufferOut);
`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${ }, this.timeout);
this.timeout },
} QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`,
); _handleTimeout(requestObj, bufferOut) {
serial.send(bufferOut, (_sendInfo) => { console.warn(
obj.stop = performance.now(); `MSP: data request timed-out: ${requestObj.code} ID: ${serial.connectionId} ` +
const executionTime = Math.round(obj.stop - obj.start); `TAB: ${GUI.active_tab} TIMEOUT: ${this.timeout} ` +
this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT)); `QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`,
}); );
}, this.timeout);
serial.send(bufferOut, (_sendInfo) => {
requestObj.stop = performance.now();
const executionTime = Math.round(requestObj.stop - requestObj.start);
this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT));
});
},
_shouldSendMessage(data, isDuplicateRequest) {
return data || !isDuplicateRequest;
},
_sendBuffer(bufferOut, callback_sent) {
// Optimize timeout for frequent requests
if (this.timeout > this.MIN_TIMEOUT) {
this.timeout--;
} }
this.callbacks.push(obj); serial.send(bufferOut, (sendInfo) => {
if (sendInfo.bytesSent === bufferOut.byteLength && callback_sent) {
// always send messages with data payload (even when there is a message already in the queue) callback_sent();
if (data || !requestExists) {
if (this.timeout > this.MIN_TIMEOUT) {
this.timeout--;
} }
});
serial.send(bufferOut, (sendInfo) => {
if (sendInfo.bytesSent === bufferOut.byteLength) {
if (callback_sent) {
callback_sent();
}
}
});
}
return true;
}, },
/** /**