mirror of
https://github.com/betaflight/betaflight-configurator.git
synced 2025-07-26 01:35:28 +03:00
improving the async transmission part in serial layer
This commit is contained in:
parent
4b96bb39b5
commit
25edf3a4bf
1 changed files with 23 additions and 11 deletions
34
js/serial.js
34
js/serial.js
|
@ -129,36 +129,48 @@ var serial = {
|
||||||
},
|
},
|
||||||
send: function (data, callback) {
|
send: function (data, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
self.output_buffer.push({'data': data, 'callback': callback});
|
this.output_buffer.push({'data': data, 'callback': callback});
|
||||||
|
|
||||||
if (!self.transmitting) {
|
if (!this.transmitting) {
|
||||||
self.transmitting = true;
|
this.transmitting = true;
|
||||||
|
|
||||||
var sending = function () {
|
var send = function () {
|
||||||
// store inside separate variables in case array gets destroyed
|
// store inside separate variables in case array gets destroyed
|
||||||
var data = self.output_buffer[0].data,
|
var data = self.output_buffer[0].data,
|
||||||
callback = self.output_buffer[0].callback;
|
callback = self.output_buffer[0].callback;
|
||||||
|
|
||||||
chrome.serial.send(self.connectionId, data, function (sendInfo) {
|
chrome.serial.send(self.connectionId, data, function (sendInfo) {
|
||||||
callback(sendInfo);
|
// track sent bytes for statistics
|
||||||
self.output_buffer.shift();
|
|
||||||
|
|
||||||
self.bytes_sent += sendInfo.bytesSent;
|
self.bytes_sent += sendInfo.bytesSent;
|
||||||
|
|
||||||
|
// fire callback
|
||||||
|
callback(sendInfo);
|
||||||
|
|
||||||
|
// remove data for current transmission form the buffer
|
||||||
|
self.output_buffer.shift();
|
||||||
|
|
||||||
|
// if there is any data in the queue fire send immediately, otherwise stop trasmitting
|
||||||
if (self.output_buffer.length) {
|
if (self.output_buffer.length) {
|
||||||
// keep the buffer withing reasonable limits
|
// keep the buffer withing reasonable limits
|
||||||
while (self.output_buffer.length > 500) {
|
if (self.output_buffer.length > 100) {
|
||||||
self.output_buffer.pop();
|
var counter = 0;
|
||||||
|
|
||||||
|
while (self.output_buffer.length > 100) {
|
||||||
|
self.output_buffer.pop();
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('SERIAL: Send buffer overflowing, dropped: ' + counter + ' entries');
|
||||||
}
|
}
|
||||||
|
|
||||||
sending();
|
send();
|
||||||
} else {
|
} else {
|
||||||
self.transmitting = false;
|
self.transmitting = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
sending();
|
send();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onReceive: {
|
onReceive: {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue