mirror of
https://github.com/betaflight/betaflight-configurator.git
synced 2025-07-25 09:15:49 +03:00
refactor: MSP binding to this
(#3557)
This commit is contained in:
parent
20c37642b3
commit
c82116d633
1 changed files with 28 additions and 31 deletions
|
@ -61,7 +61,7 @@ const MSP = {
|
||||||
|
|
||||||
JUMBO_FRAME_SIZE_LIMIT: 255,
|
JUMBO_FRAME_SIZE_LIMIT: 255,
|
||||||
|
|
||||||
read: function (readInfo) {
|
read(readInfo) {
|
||||||
if (CONFIGURATOR.virtualMode) {
|
if (CONFIGURATOR.virtualMode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -209,11 +209,11 @@ const MSP = {
|
||||||
}
|
}
|
||||||
this.last_received_timestamp = Date.now();
|
this.last_received_timestamp = Date.now();
|
||||||
},
|
},
|
||||||
_initialize_read_buffer: function() {
|
_initialize_read_buffer() {
|
||||||
this.message_buffer = new ArrayBuffer(this.message_length_expected);
|
this.message_buffer = new ArrayBuffer(this.message_length_expected);
|
||||||
this.message_buffer_uint8_view = new Uint8Array(this.message_buffer);
|
this.message_buffer_uint8_view = new Uint8Array(this.message_buffer);
|
||||||
},
|
},
|
||||||
_dispatch_message: function(expectedChecksum) {
|
_dispatch_message(expectedChecksum) {
|
||||||
if (this.message_checksum === expectedChecksum) {
|
if (this.message_checksum === expectedChecksum) {
|
||||||
// message received, store dataview
|
// message received, store dataview
|
||||||
this.dataView = new DataView(this.message_buffer, 0, this.message_length_expected);
|
this.dataView = new DataView(this.message_buffer, 0, this.message_length_expected);
|
||||||
|
@ -229,21 +229,20 @@ const MSP = {
|
||||||
this.messageIsJumboFrame = false;
|
this.messageIsJumboFrame = false;
|
||||||
this.crcError = false;
|
this.crcError = false;
|
||||||
},
|
},
|
||||||
notify: function() {
|
notify() {
|
||||||
const self = this;
|
this.listeners.forEach((listener) => {
|
||||||
self.listeners.forEach(function(listener) {
|
listener(this);
|
||||||
listener(self);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
listen: function(listener) {
|
listen(listener) {
|
||||||
if (this.listeners.indexOf(listener) == -1) {
|
if (this.listeners.indexOf(listener) == -1) {
|
||||||
this.listeners.push(listener);
|
this.listeners.push(listener);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
clearListeners: function() {
|
clearListeners() {
|
||||||
this.listeners = [];
|
this.listeners = [];
|
||||||
},
|
},
|
||||||
crc8_dvb_s2: function(crc, ch) {
|
crc8_dvb_s2(crc, ch) {
|
||||||
crc ^= ch;
|
crc ^= ch;
|
||||||
for (let ii = 0; ii < 8; ii++) {
|
for (let ii = 0; ii < 8; ii++) {
|
||||||
if (crc & 0x80) {
|
if (crc & 0x80) {
|
||||||
|
@ -254,14 +253,14 @@ const MSP = {
|
||||||
}
|
}
|
||||||
return crc;
|
return crc;
|
||||||
},
|
},
|
||||||
crc8_dvb_s2_data: function(data, start, end) {
|
crc8_dvb_s2_data(data, start, end) {
|
||||||
let crc = 0;
|
let crc = 0;
|
||||||
for (let ii = start; ii < end; ii++) {
|
for (let ii = start; ii < end; ii++) {
|
||||||
crc = this.crc8_dvb_s2(crc, data[ii]);
|
crc = this.crc8_dvb_s2(crc, data[ii]);
|
||||||
}
|
}
|
||||||
return crc;
|
return crc;
|
||||||
},
|
},
|
||||||
encode_message_v1: function(code, data) {
|
encode_message_v1(code, data) {
|
||||||
const dataLength = data ? data.length : 0;
|
const dataLength = data ? data.length : 0;
|
||||||
// always reserve 6 bytes for protocol overhead !
|
// always reserve 6 bytes for protocol overhead !
|
||||||
const bufferSize = dataLength + 6;
|
const bufferSize = dataLength + 6;
|
||||||
|
@ -284,7 +283,7 @@ const MSP = {
|
||||||
bufView[5 + dataLength] = checksum;
|
bufView[5 + dataLength] = checksum;
|
||||||
return bufferOut;
|
return bufferOut;
|
||||||
},
|
},
|
||||||
encode_message_v2: function (code, data) {
|
encode_message_v2(code, data) {
|
||||||
const dataLength = data ? data.length : 0;
|
const dataLength = data ? data.length : 0;
|
||||||
// 9 bytes for protocol overhead
|
// 9 bytes for protocol overhead
|
||||||
const bufferSize = dataLength + 9;
|
const bufferSize = dataLength + 9;
|
||||||
|
@ -304,7 +303,7 @@ const MSP = {
|
||||||
bufView[bufferSize - 1] = this.crc8_dvb_s2_data(bufView, 3, bufferSize - 1);
|
bufView[bufferSize - 1] = this.crc8_dvb_s2_data(bufView, 3, bufferSize - 1);
|
||||||
return bufferOut;
|
return bufferOut;
|
||||||
},
|
},
|
||||||
send_message: function (code, data, callback_sent, callback_msp, doCallbackOnError) {
|
send_message(code, data, callback_sent, callback_msp, doCallbackOnError) {
|
||||||
if (code === undefined || !serial.connectionId || CONFIGURATOR.virtualMode) {
|
if (code === undefined || !serial.connectionId || CONFIGURATOR.virtualMode) {
|
||||||
if (callback_msp) {
|
if (callback_msp) {
|
||||||
callback_msp();
|
callback_msp();
|
||||||
|
@ -314,7 +313,7 @@ const MSP = {
|
||||||
|
|
||||||
// Check if request already exists in the queue
|
// Check if request already exists in the queue
|
||||||
let requestExists = false;
|
let requestExists = false;
|
||||||
for (const instance of MSP.callbacks) {
|
for (const instance of this.callbacks) {
|
||||||
if (instance.code === code) {
|
if (instance.code === code) {
|
||||||
requestExists = true;
|
requestExists = true;
|
||||||
|
|
||||||
|
@ -333,25 +332,25 @@ const MSP = {
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!requestExists) {
|
if (!requestExists) {
|
||||||
obj.timer = setTimeout(function () {
|
obj.timer = setTimeout(() => {
|
||||||
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; })})`);
|
console.warn(`MSP: data request timed-out: ${code} ID: ${serial.connectionId} TAB: ${GUI.active_tab} TIMEOUT: ${this.timeout} QUEUE: ${this.callbacks.length} (${this.callbacks.map((e) => e.code)})`);
|
||||||
serial.send(bufferOut, function (_sendInfo) {
|
serial.send(bufferOut, (_sendInfo) => {
|
||||||
obj.stop = performance.now();
|
obj.stop = performance.now();
|
||||||
const executionTime = Math.round(obj.stop - obj.start);
|
const executionTime = Math.round(obj.stop - obj.start);
|
||||||
MSP.timeout = Math.max(MSP.MIN_TIMEOUT, Math.min(executionTime, MSP.MAX_TIMEOUT));
|
this.timeout = Math.max(this.MIN_TIMEOUT, Math.min(executionTime, this.MAX_TIMEOUT));
|
||||||
});
|
});
|
||||||
}, MSP.timeout);
|
}, this.timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
MSP.callbacks.push(obj);
|
this.callbacks.push(obj);
|
||||||
|
|
||||||
// always send messages with data payload (even when there is a message already in the queue)
|
// always send messages with data payload (even when there is a message already in the queue)
|
||||||
if (data || !requestExists) {
|
if (data || !requestExists) {
|
||||||
if (MSP.timeout > MSP.MIN_TIMEOUT) {
|
if (this.timeout > this.MIN_TIMEOUT) {
|
||||||
MSP.timeout--;
|
this.timeout--;
|
||||||
}
|
}
|
||||||
|
|
||||||
serial.send(bufferOut, function (sendInfo) {
|
serial.send(bufferOut, (sendInfo) => {
|
||||||
if (sendInfo.bytesSent === bufferOut.byteLength) {
|
if (sendInfo.bytesSent === bufferOut.byteLength) {
|
||||||
if (callback_sent) {
|
if (callback_sent) {
|
||||||
callback_sent();
|
callback_sent();
|
||||||
|
@ -366,23 +365,21 @@ const MSP = {
|
||||||
/**
|
/**
|
||||||
* resolves: {command: code, data: data, length: message_length}
|
* resolves: {command: code, data: data, length: message_length}
|
||||||
*/
|
*/
|
||||||
promise: async function(code, data) {
|
async promise(code, data) {
|
||||||
const self = this;
|
return new Promise((resolve) => {
|
||||||
|
this.send_message(code, data, false, (_data) => {
|
||||||
return new Promise(function(resolve) {
|
|
||||||
self.send_message(code, data, false, function(_data) {
|
|
||||||
resolve(_data);
|
resolve(_data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
callbacks_cleanup: function () {
|
callbacks_cleanup() {
|
||||||
for (const callback of this.callbacks) {
|
for (const callback of this.callbacks) {
|
||||||
clearInterval(callback.timer);
|
clearInterval(callback.timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.callbacks = [];
|
this.callbacks = [];
|
||||||
},
|
},
|
||||||
disconnect_cleanup: function () {
|
disconnect_cleanup() {
|
||||||
this.state = 0; // reset packet state for "clean" initial entry (this is only required if user hot-disconnects)
|
this.state = 0; // reset packet state for "clean" initial entry (this is only required if user hot-disconnects)
|
||||||
this.packet_error = 0; // reset CRC packet error counter for next session
|
this.packet_error = 0; // reset CRC packet error counter for next session
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue