1
0
Fork 0
mirror of https://github.com/iNavFlight/inav-configurator.git synced 2025-07-13 11:29:53 +03:00
inav-configurator/js/serial_queue.js
Pawel Spychalski (DzikuVx) 72d85ad354 first draft of serial queue
2017-01-20 14:11:26 +01:00

83 lines
No EOL
2.2 KiB
JavaScript

'use strict';
var helper = helper || {};
helper.mspQueue = (function (serial, MSP) {
var publicScope = {},
privateScope = {};
privateScope.queue = [];
privateScope.portInUse = false;
/**
* This method is periodically executed and moves MSP request
* from a queue to serial port. This allows to throttle requests,
* adjust rate of new frames being sent and prohibit situation in which
* serial port is saturated, virtually overloaded, with outgoing data
*
* This also implements serial port sharing problem: only 1 frame can be transmitted
* at once
*
* MSP class no longer implements blocking, it is queue responsibility
*/
publicScope.executor = function () {
if (privateScope.portInUse) {
return false;
}
var request = privateScope.get();
if (request !== undefined) {
/*
* Lock serial port as being in use right now
*/
privateScope.portInUse = true;
/*
* Set receive callback here
*/
MSP.putCallback(request);
//TODO implement timeout scenario
/*
* Send data to serial port
*/
serial.send(request.requestBuffer, function (sendInfo) {
if (sendInfo.bytesSent == request.requestBuffer.byteLength) {
/*
* message has been sent, check callbacks and free resource
*/
if (request.onSend) {
request.onSend();
}
privateScope.portInUse = false;
}
});
}
};
privateScope.get = function () {
return privateScope.queue.shift();
};
publicScope.freeSerialPort = function () {
privateScope.portInUse = false;
};
publicScope.put = function (mspRequest) {
privateScope.queue.push(mspRequest);
};
publicScope.getLength = function () {
return privateScope.queue.length;
};
setInterval(publicScope.executor, 20);
return publicScope;
})(serial, MSP);