mirror of
https://github.com/iNavFlight/inav-configurator.git
synced 2025-07-24 16:55:22 +03:00
adaptive intervals
This commit is contained in:
parent
dc40b96238
commit
37738ac269
12 changed files with 87 additions and 9 deletions
72
js/msp_balanced_interval.js
Normal file
72
js/msp_balanced_interval.js
Normal file
|
@ -0,0 +1,72 @@
|
|||
'use strict';
|
||||
|
||||
var helper = helper || {};
|
||||
|
||||
helper.mspBalancedInterval = (function (mspQueue, intervalHandler) {
|
||||
|
||||
var publicScope = {},
|
||||
privateScope = {};
|
||||
|
||||
/**
|
||||
* How often balancing should be executed [Hz]
|
||||
* @type {number}
|
||||
*/
|
||||
privateScope.balancingFrequency = 0.5;
|
||||
|
||||
privateScope.intervals = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {number} requestedInterval
|
||||
* @param {number} messagesInInterval
|
||||
* @param {function} code
|
||||
*/
|
||||
publicScope.add = function (name, requestedInterval, messagesInInterval, code) {
|
||||
privateScope.intervals.push({
|
||||
name: name,
|
||||
requestedInterval: requestedInterval,
|
||||
messagesInInterval: messagesInInterval,
|
||||
code: code
|
||||
});
|
||||
|
||||
intervalHandler.add(name, code, mspQueue.getIntervalPrediction(requestedInterval, messagesInInterval));
|
||||
};
|
||||
|
||||
/**
|
||||
* Periodically executed balancing handler
|
||||
*/
|
||||
publicScope.balancer = function () {
|
||||
|
||||
var interval;
|
||||
|
||||
for (var i in privateScope.intervals) {
|
||||
if (privateScope.intervals.hasOwnProperty(i)) {
|
||||
interval = privateScope.intervals[i];
|
||||
|
||||
intervalHandler.remove(interval.name);
|
||||
intervalHandler.add(
|
||||
interval.name,
|
||||
interval.code,
|
||||
mspQueue.getIntervalPrediction(
|
||||
interval.requestedInterval,
|
||||
interval.messagesInInterval
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Real interval cleaning happens win helper.interval.killAll method
|
||||
* both methods have to be executed
|
||||
*/
|
||||
publicScope.flush = function () {
|
||||
privateScope.intervals = [];
|
||||
};
|
||||
|
||||
setInterval(publicScope.balancer, Math.round(1000 / privateScope.balancingFrequency));
|
||||
|
||||
return publicScope;
|
||||
})(helper.mspQueue, helper.interval);
|
Loading…
Add table
Add a link
Reference in a new issue