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/helpers.js
Darren Lines bd70d431c0 Add version number to CLI save
This PR adds the version number to the CLI filename. It also adds a secondary way to get the data from the FC if it is out of range. If it fails, it appears as the previous save filename looked.
2022-07-29 14:18:30 +01:00

81 lines
2 KiB
JavaScript

/*global $*/
'use strict';
function checkChromeRuntimeError() {
if (chrome.runtime.lastError) {
console.error(
`Chrome API Error: ${chrome.runtime.lastError.message}.\n Traced ${
new Error().stack
}`
);
return true;
}
return false;
}
function constrain(input, min, max) {
if (input < min) {
return min;
}
if (input > max) {
return max;
}
return input;
}
function zeroPad(value, width) {
value = "" + value;
while (value.length < width) {
value = "0" + value;
}
return value;
}
function generateFilename(prefix, suffix) {
var date = new Date();
var filename = prefix;
if (CONFIG) {
if (CONFIG.flightControllerIdentifier) {
filename = CONFIG.flightControllerIdentifier + '_' + CONFIG.flightControllerVersion + "_" + filename;
}
if (CONFIG.name && CONFIG.name.trim() !== '') {
filename = filename + '_' + CONFIG.name.trim().replace(' ', '_');
}
} else {
// Attempt to get the version number and
MSPHelper.getSetting("version").then(function (data) {
if (data.value) {
let version = data.value.split('/')[0] + "_" + data.value.split(' ')[1];
filename = "" + version + "_" + filename;
}
});
MSPHelper.getSetting("name").then(function (data) {
if (data.value && data.value.trim() !== '') {
filename += "_" + data.value.trim().replace(' ', '_');
}
});
}
filename = filename + '_' + date.getFullYear()
+ zeroPad(date.getMonth() + 1, 2)
+ zeroPad(date.getDate(), 2)
+ '_' + zeroPad(date.getHours(), 2)
+ zeroPad(date.getMinutes(), 2)
+ zeroPad(date.getSeconds(), 2);
return filename + '.' + suffix;
}
function scaleRangeInt(x, srcMin, srcMax, destMin, destMax) {
let a = (destMax - destMin) * (x - srcMin);
let b = srcMax - srcMin;
return Math.round((a / b) + destMin);
}