1
0
Fork 0
mirror of https://github.com/iNavFlight/inav-configurator.git synced 2025-07-14 11:59:51 +03:00

first draft of serial queue

This commit is contained in:
Pawel Spychalski (DzikuVx) 2017-01-20 14:11:26 +01:00
parent 638cf92048
commit 72d85ad354
3 changed files with 123 additions and 27 deletions

View file

@ -109,6 +109,10 @@ var MSP = {
this.last_received_timestamp = Date.now(); this.last_received_timestamp = Date.now();
}, },
putCallback: function (mspData) {
MSP.callbacks.push(mspData);
},
send_message: function (code, data, callback_sent, callback_msp) { send_message: function (code, data, callback_sent, callback_msp) {
var bufferOut, var bufferOut,
bufView, bufView,
@ -157,39 +161,47 @@ var MSP = {
// dev version 0.57 code below got recently changed due to the fact that queueing same MSP codes was unsupported // dev version 0.57 code below got recently changed due to the fact that queueing same MSP codes was unsupported
// and was causing trouble while backup/restoring configurations // and was causing trouble while backup/restoring configurations
// watch out if the recent change create any inconsistencies and then adjust accordingly // watch out if the recent change create any inconsistencies and then adjust accordingly
var obj = {'code': code, 'requestBuffer': bufferOut, 'callback': (callback_msp) ? callback_msp : false, 'timer': false}; var obj = {
'code': code,
'requestBuffer': bufferOut,
'callback': (callback_msp) ? callback_msp : false,
'onSend': callback_sent,
'timer': false
};
var requestExists = false; helper.mspQueue.put(obj);
for (i = 0; i < MSP.callbacks.length; i++) {
if (i < MSP.callbacks.length) {
if (MSP.callbacks[i].code == code) {
// request already exist, we will just attach
requestExists = true;
break;
}
} else {
console.log("Callback index error: "+ i);
}
}
if (!requestExists) { // var requestExists = false;
obj.timer = setInterval(function () { // for (i = 0; i < MSP.callbacks.length; i++) {
console.log('MSP data request timed-out: ' + code); // if (i < MSP.callbacks.length) {
// if (MSP.callbacks[i].code == code) {
// // request already exist, we will just attach
// requestExists = true;
// break;
// }
// } else {
// console.log("Callback index error: "+ i);
// }
// }
serial.send(bufferOut, false); // if (!requestExists) {
}, serial.getTimeout()); // we should be able to define timeout in the future // obj.timer = setInterval(function () {
} // console.log('MSP data request timed-out: ' + code);
//
// serial.send(bufferOut, false);
// }, serial.getTimeout()); // we should be able to define timeout in the future
// }
MSP.callbacks.push(obj); // MSP.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) {
serial.send(bufferOut, function (sendInfo) { // serial.send(bufferOut, function (sendInfo) {
if (sendInfo.bytesSent == bufferOut.byteLength) { // if (sendInfo.bytesSent == bufferOut.byteLength) {
if (callback_sent) callback_sent(); // if (callback_sent) callback_sent();
} // }
}); // });
} // }
return true; return true;
}, },
promise: function(code, data) { promise: function(code, data) {

83
js/serial_queue.js Normal file
View file

@ -0,0 +1,83 @@
'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);

View file

@ -94,6 +94,7 @@
<script type="text/javascript" src="./tabs/profiles.js"></script> <script type="text/javascript" src="./tabs/profiles.js"></script>
<script type="text/javascript" src="./js/eventFrequencyAnalyzer.js"></script> <script type="text/javascript" src="./js/eventFrequencyAnalyzer.js"></script>
<script type="text/javascript" src="./js/periodicStatusUpdater.js"></script> <script type="text/javascript" src="./js/periodicStatusUpdater.js"></script>
<script type="text/javascript" src="./js/serial_queue.js"></script>
<title></title> <title></title>
</head> </head>
<body> <body>