1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-23 08:15:22 +03:00
betaflight-configurator/src/js/utils/common.js
2021-01-01 22:26:49 +04:00

87 lines
2.2 KiB
JavaScript

import semver from "semver";
export function microtime() {
return new Date().getTime() / 1000;
}
export function millitime() {
return new Date().getTime();
}
const DEGREE_TO_RADIAN_RATIO = Math.PI / 180;
export function degToRad(degrees) {
return degrees * DEGREE_TO_RADIAN_RATIO;
}
export function bytesToSize(bytes) {
let outputBytes;
if (bytes < 1024) {
outputBytes = `${bytes} Bytes`;
} else if (bytes < 1048576) {
outputBytes = `${(bytes / 1024).toFixed(3)} KB`;
} else if (bytes < 1073741824) {
outputBytes = `${(bytes / 1048576).toFixed(3)} MB`;
} else {
outputBytes = `${(bytes / 1073741824).toFixed(3)} GB`;
}
return outputBytes;
}
/*
* checkChromeRuntimeError() has to be called after each chrome API call
*/
export function checkChromeRuntimeError() {
if (chrome.runtime.lastError) {
console.error(
`Chrome API Error: ${chrome.runtime.lastError.message}.\n Traced ${
new Error().stack
}`
);
return true;
}
return false;
}
const majorFirmwareVersions = {
"1.43": "4.2.*",
"1.42": "4.1.*",
"1.41": "4.0.*",
"1.40": "3.5.*",
"1.39": "3.4.*",
"1.37": "3.3.0",
"1.36": "3.2.*",
"1.31": "3.1.0"
};
export function generateVirtualApiVersions() {
const firmwareVersionDropdown = document.getElementById(
"firmware-version-dropdown"
);
const max = semver.minor(CONFIGURATOR.API_VERSION_MAX_SUPPORTED);
for (let i = max; i > 0; i--) {
const option = document.createElement("option");
const verNum = `1.${i}`;
option.value = `${verNum}.0`;
option.text = `MSP: ${verNum} `;
if (majorFirmwareVersions.hasOwnProperty(verNum)) {
option.text += ` | Firmware: ${majorFirmwareVersions[verNum]}`;
} else if (i === max) {
option.text += ` | Latest Firmware`;
}
firmwareVersionDropdown.appendChild(option);
}
}
// TODO: these are temp binding while transition to module happens
window.microtime = microtime;
window.degToRad = degToRad;
window.bytesToSize = bytesToSize;
window.checkChromeRuntimeError = checkChromeRuntimeError;
window.generateVirtualApiVersions = generateVirtualApiVersions;