mirror of
https://github.com/iNavFlight/inav-configurator.git
synced 2025-07-17 05:15:20 +03:00
Frequency analyzer for MSP frames
This commit is contained in:
parent
d1483e5e26
commit
2c334f6c23
3 changed files with 83 additions and 0 deletions
77
js/eventFrequencyAnalyzer.js
Normal file
77
js/eventFrequencyAnalyzer.js
Normal file
|
@ -0,0 +1,77 @@
|
|||
'use s';
|
||||
|
||||
var helper = helper || {};
|
||||
|
||||
/**
|
||||
* Simple analyzer that returns frequency of events using 5s buffer
|
||||
* Usage: register periodic events with 'put', then call 'get' to get results
|
||||
*/
|
||||
helper.eventFrequencyAnalyzer = (function () {
|
||||
|
||||
var privateScope = {},
|
||||
publicScope = {},
|
||||
bufferPeriod = 5000;
|
||||
|
||||
privateScope.data = {};
|
||||
privateScope.output = {};
|
||||
privateScope.intervalHandler;
|
||||
|
||||
/**
|
||||
* Periodically executed aggregation task
|
||||
* @returns {{}|*}
|
||||
*/
|
||||
publicScope.analyze = function () {
|
||||
privateScope.output = {};
|
||||
|
||||
for (var i in privateScope.data) {
|
||||
if (privateScope.data.hasOwnProperty(i)) {
|
||||
privateScope.output[i] = privateScope.data[i] / bufferPeriod * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
privateScope.data = {};
|
||||
return privateScope.output;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return event list with frequencies
|
||||
* @returns {{}|*}
|
||||
*/
|
||||
publicScope.get = function () {
|
||||
return privateScope.output;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns raw data
|
||||
* @returns {{}|*}
|
||||
*/
|
||||
publicScope.getRaw = function () {
|
||||
return privateScope.data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Put event into analyzer
|
||||
* @param {object} event
|
||||
*/
|
||||
publicScope.put = function (event) {
|
||||
if (privateScope.data[event]) {
|
||||
privateScope.data[event]++;
|
||||
} else {
|
||||
privateScope.data[event] = 1;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} buffer buffer length in milliseconds
|
||||
*/
|
||||
publicScope.setBufferPeriod = function (buffer) {
|
||||
bufferPeriod = buffer;
|
||||
clearInterval(privateScope.intervalHandler);
|
||||
privateScope.intervalHandler = setInterval(publicScope.analyze, bufferPeriod);
|
||||
};
|
||||
|
||||
privateScope.intervalHandler = setInterval(publicScope.analyze, bufferPeriod);
|
||||
|
||||
return publicScope;
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue