mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-23 08:15:30 +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) {
|
||||
var self = this;
|
||||
self.output_buffer.push({'data': data, 'callback': callback});
|
||||
this.output_buffer.push({'data': data, 'callback': callback});
|
||||
|
||||
if (!self.transmitting) {
|
||||
self.transmitting = true;
|
||||
if (!this.transmitting) {
|
||||
this.transmitting = true;
|
||||
|
||||
var sending = function () {
|
||||
var send = function () {
|
||||
// store inside separate variables in case array gets destroyed
|
||||
var data = self.output_buffer[0].data,
|
||||
callback = self.output_buffer[0].callback;
|
||||
|
||||
chrome.serial.send(self.connectionId, data, function (sendInfo) {
|
||||
callback(sendInfo);
|
||||
self.output_buffer.shift();
|
||||
|
||||
// track sent bytes for statistics
|
||||
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) {
|
||||
// keep the buffer withing reasonable limits
|
||||
while (self.output_buffer.length > 500) {
|
||||
self.output_buffer.pop();
|
||||
if (self.output_buffer.length > 100) {
|
||||
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 {
|
||||
self.transmitting = false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
sending();
|
||||
send();
|
||||
}
|
||||
},
|
||||
onReceive: {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue