mirror of
https://github.com/iNavFlight/inav-configurator.git
synced 2025-07-14 20:10:11 +03:00
first draft of serial queue
This commit is contained in:
parent
638cf92048
commit
72d85ad354
3 changed files with 123 additions and 27 deletions
66
js/msp.js
66
js/msp.js
|
@ -109,6 +109,10 @@ var MSP = {
|
|||
this.last_received_timestamp = Date.now();
|
||||
},
|
||||
|
||||
putCallback: function (mspData) {
|
||||
MSP.callbacks.push(mspData);
|
||||
},
|
||||
|
||||
send_message: function (code, data, callback_sent, callback_msp) {
|
||||
var bufferOut,
|
||||
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
|
||||
// and was causing trouble while backup/restoring configurations
|
||||
// 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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
helper.mspQueue.put(obj);
|
||||
|
||||
if (!requestExists) {
|
||||
obj.timer = setInterval(function () {
|
||||
console.log('MSP data request timed-out: ' + code);
|
||||
// var requestExists = false;
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
serial.send(bufferOut, false);
|
||||
}, serial.getTimeout()); // we should be able to define timeout in the future
|
||||
}
|
||||
// if (!requestExists) {
|
||||
// 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)
|
||||
if (data || !requestExists) {
|
||||
serial.send(bufferOut, function (sendInfo) {
|
||||
if (sendInfo.bytesSent == bufferOut.byteLength) {
|
||||
if (callback_sent) callback_sent();
|
||||
}
|
||||
});
|
||||
}
|
||||
// if (data || !requestExists) {
|
||||
// serial.send(bufferOut, function (sendInfo) {
|
||||
// if (sendInfo.bytesSent == bufferOut.byteLength) {
|
||||
// if (callback_sent) callback_sent();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
return true;
|
||||
},
|
||||
promise: function(code, data) {
|
||||
|
|
83
js/serial_queue.js
Normal file
83
js/serial_queue.js
Normal 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);
|
|
@ -94,6 +94,7 @@
|
|||
<script type="text/javascript" src="./tabs/profiles.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/serial_queue.js"></script>
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue