1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-24 00:35:39 +03:00

reorganization of several MSP methods, small optimizations

This commit is contained in:
cTn 2014-09-21 20:31:57 +02:00
parent 47135e2bb8
commit a0ed27c161

View file

@ -73,23 +73,7 @@ var MSP = {
callbacks: [], callbacks: [],
packet_error: 0, packet_error: 0,
callbacks_cleanup: function () { read: function (readInfo) {
for (var i = 0; i < this.callbacks.length; i++) {
clearInterval(this.callbacks[i].timer);
}
this.callbacks = [];
},
disconnect_cleanup: function () {
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.callbacks_cleanup();
}
};
MSP.read = function (readInfo) {
var data = new Uint8Array(readInfo.data); var data = new Uint8Array(readInfo.data);
for (var i = 0; i < data.length; i++) { for (var i = 0; i < data.length; i++) {
@ -130,9 +114,11 @@ MSP.read = function (readInfo) {
this.code = data[i]; this.code = data[i];
this.message_checksum ^= data[i]; this.message_checksum ^= data[i];
if (this.message_length_expected != 0) { // standard message if (this.message_length_expected > 0) {
// process payload
this.state++; this.state++;
} else { // MSP_ACC_CALIBRATION, etc... } else {
// no payload
this.state += 2; this.state += 2;
} }
break; break;
@ -162,9 +148,8 @@ MSP.read = function (readInfo) {
break; break;
} }
} }
}; },
process_data: function (code, message_buffer, message_length) {
MSP.process_data = function(code, message_buffer, message_length) {
var data = new DataView(message_buffer, 0); // DataView (allowing us to view arrayBuffer as struct/union) var data = new DataView(message_buffer, 0); // DataView (allowing us to view arrayBuffer as struct/union)
switch (code) { switch (code) {
@ -492,16 +477,15 @@ MSP.process_data = function(code, message_buffer, message_length) {
if (callback) callback({'command': code, 'data': data, 'length': message_length}); if (callback) callback({'command': code, 'data': data, 'length': message_length});
} }
} }
}; },
send_message: function (code, data, callback_sent, callback_msp) {
MSP.send_message = function(code, data, callback_sent, callback_msp) { var bufferOut,
var bufferOut; bufView;
var bufView;
// always reserve 6 bytes for protocol overhead ! // always reserve 6 bytes for protocol overhead !
if (data) { if (data) {
var size = data.length + 6; var size = data.length + 6,
var checksum = 0; checksum = 0;
bufferOut = new ArrayBuffer(size); bufferOut = new ArrayBuffer(size);
bufView = new Uint8Array(bufferOut); bufView = new Uint8Array(bufferOut);
@ -557,6 +541,20 @@ MSP.send_message = function(code, data, callback_sent, callback_msp) {
}); });
return true; return true;
},
callbacks_cleanup: function () {
for (var i = 0; i < this.callbacks.length; i++) {
clearInterval(this.callbacks[i].timer);
}
this.callbacks = [];
},
disconnect_cleanup: function () {
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.callbacks_cleanup();
}
}; };
MSP.crunch = function (code) { MSP.crunch = function (code) {