mirror of
https://github.com/betaflight/betaflight-configurator.git
synced 2025-07-19 14:25:14 +03:00
Refactor and Sonar clean of Beeper.js
This commit is contained in:
parent
59076c0c3a
commit
9c6ad8500c
3 changed files with 113 additions and 116 deletions
|
@ -1,120 +1,117 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var Beepers = function (config, supportedConditions) {
|
class Beepers {
|
||||||
var self = this;
|
constructor(config, supportedConditions) {
|
||||||
|
const self = this;
|
||||||
|
|
||||||
var beepers = [
|
const beepers = [
|
||||||
{bit: 0, name: 'GYRO_CALIBRATED', visible: true},
|
{ bit: 0, name: 'GYRO_CALIBRATED', visible: true },
|
||||||
{bit: 1, name: 'RX_LOST', visible: true},
|
{ bit: 1, name: 'RX_LOST', visible: true },
|
||||||
{bit: 2, name: 'RX_LOST_LANDING', visible: true},
|
{ bit: 2, name: 'RX_LOST_LANDING', visible: true },
|
||||||
{bit: 3, name: 'DISARMING', visible: true},
|
{ bit: 3, name: 'DISARMING', visible: true },
|
||||||
{bit: 4, name: 'ARMING', visible: true},
|
{ bit: 4, name: 'ARMING', visible: true },
|
||||||
{bit: 5, name: 'ARMING_GPS_FIX', visible: true},
|
{ bit: 5, name: 'ARMING_GPS_FIX', visible: true },
|
||||||
{bit: 6, name: 'BAT_CRIT_LOW', visible: true},
|
{ bit: 6, name: 'BAT_CRIT_LOW', visible: true },
|
||||||
{bit: 7, name: 'BAT_LOW', visible: true},
|
{ bit: 7, name: 'BAT_LOW', visible: true },
|
||||||
{bit: 8, name: 'GPS_STATUS', visible: true},
|
{ bit: 8, name: 'GPS_STATUS', visible: true },
|
||||||
{bit: 9, name: 'RX_SET', visible: true},
|
{ bit: 9, name: 'RX_SET', visible: true },
|
||||||
{bit: 10, name: 'ACC_CALIBRATION', visible: true},
|
{ bit: 10, name: 'ACC_CALIBRATION', visible: true },
|
||||||
{bit: 11, name: 'ACC_CALIBRATION_FAIL', visible: true},
|
{ bit: 11, name: 'ACC_CALIBRATION_FAIL', visible: true },
|
||||||
{bit: 12, name: 'READY_BEEP', visible: true},
|
{ bit: 12, name: 'READY_BEEP', visible: true },
|
||||||
{bit: 13, name: 'MULTI_BEEPS', visible: false}, // do not show
|
{ bit: 13, name: 'MULTI_BEEPS', visible: false },
|
||||||
{bit: 14, name: 'DISARM_REPEAT', visible: true},
|
{ bit: 14, name: 'DISARM_REPEAT', visible: true },
|
||||||
{bit: 15, name: 'ARMED', visible: true},
|
{ bit: 15, name: 'ARMED', visible: true },
|
||||||
{bit: 16, name: 'SYSTEM_INIT', visible: true},
|
{ bit: 16, name: 'SYSTEM_INIT', visible: true },
|
||||||
{bit: 17, name: 'USB', visible: true},
|
{ bit: 17, name: 'USB', visible: true },
|
||||||
{bit: 18, name: 'BLACKBOX_ERASE', visible: true},
|
{ bit: 18, name: 'BLACKBOX_ERASE', visible: true },
|
||||||
];
|
];
|
||||||
|
|
||||||
if (semver.gte(config.apiVersion, "1.37.0")) {
|
if (semver.gte(config.apiVersion, "1.37.0")) {
|
||||||
beepers.push(
|
beepers.push(
|
||||||
{bit: 19, name: 'CRASH_FLIP', visible: true},
|
{ bit: 19, name: 'CRASH_FLIP', visible: true },
|
||||||
{bit: 20, name: 'CAM_CONNECTION_OPEN', visible: true},
|
{ bit: 20, name: 'CAM_CONNECTION_OPEN', visible: true },
|
||||||
{bit: 21, name: 'CAM_CONNECTION_CLOSE', visible: true},
|
{ bit: 21, name: 'CAM_CONNECTION_CLOSE', visible: true }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (semver.gte(config.apiVersion, "1.39.0")) {
|
if (semver.gte(config.apiVersion, "1.39.0")) {
|
||||||
beepers.push(
|
beepers.push(
|
||||||
{bit: 22, name: 'RC_SMOOTHING_INIT_FAIL', visible: true},
|
{ bit: 22, name: 'RC_SMOOTHING_INIT_FAIL', visible: true }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (supportedConditions) {
|
if (supportedConditions) {
|
||||||
self._beepers = [];
|
self._beepers = [];
|
||||||
beepers.forEach(function (beeper) {
|
beepers.forEach(function (beeper) {
|
||||||
if (supportedConditions.some(function (supportedCondition) {
|
if (supportedConditions.some(function (supportedCondition) {
|
||||||
return supportedCondition === beeper.name;
|
return supportedCondition === beeper.name;
|
||||||
})) {
|
})) {
|
||||||
self._beepers.push(beeper);
|
self._beepers.push(beeper);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
self._beepers = beepers.slice();
|
|
||||||
}
|
|
||||||
|
|
||||||
self._beeperMask = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
Beepers.prototype.getMask = function () {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
return self._beeperMask;
|
|
||||||
};
|
|
||||||
|
|
||||||
Beepers.prototype.setMask = function (beeperMask) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
self._beeperMask = beeperMask;
|
|
||||||
};
|
|
||||||
|
|
||||||
Beepers.prototype.isEnabled = function (beeperName) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
for (var i = 0; i < self._beepers.length; i++) {
|
|
||||||
if (self._beepers[i].name === beeperName && bit_check(self._beeperOfMask, self._beepers[i].bit)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
Beepers.prototype.generateElements = function (template, destination) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
for (var i = 0; i < self._beepers.length; i++) {
|
|
||||||
if (self._beepers[i].visible) {
|
|
||||||
var element = template.clone();
|
|
||||||
destination.append(element);
|
|
||||||
|
|
||||||
var input_e = $(element).find('input');
|
|
||||||
var label_e = $(element).find('div');
|
|
||||||
var span_e = $(element).find('span');
|
|
||||||
|
|
||||||
input_e.attr('id', 'beeper-' + i);
|
|
||||||
input_e.attr('name', self._beepers[i].name);
|
|
||||||
input_e.attr('title', self._beepers[i].name);
|
|
||||||
input_e.prop('checked', bit_check(self._beeperMask, self._beepers[i].bit) == 0);
|
|
||||||
input_e.data('bit', self._beepers[i].bit);
|
|
||||||
|
|
||||||
label_e.text(self._beepers[i].name);
|
|
||||||
|
|
||||||
span_e.attr('i18n', 'beeper' + self._beepers[i].name);
|
|
||||||
|
|
||||||
element.show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Beepers.prototype.updateData = function (beeperElement) {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
if (beeperElement.attr('type') === 'checkbox') {
|
|
||||||
var bit = beeperElement.data('bit');
|
|
||||||
|
|
||||||
if (beeperElement.is(':checked')) {
|
|
||||||
self._beeperMask = bit_clear(self._beeperMask, bit);
|
|
||||||
} else {
|
} else {
|
||||||
self._beeperMask = bit_set(self._beeperMask, bit);
|
self._beepers = beepers.slice();
|
||||||
|
}
|
||||||
|
|
||||||
|
self._beeperDisabledMask = 0;
|
||||||
|
}
|
||||||
|
getDisabledMask() {
|
||||||
|
const self = this;
|
||||||
|
|
||||||
|
return self._beeperDisabledMask;
|
||||||
|
}
|
||||||
|
setDisabledMask(beeperDisabledMask) {
|
||||||
|
const self = this;
|
||||||
|
|
||||||
|
self._beeperDisabledMask = beeperDisabledMask;
|
||||||
|
}
|
||||||
|
isEnabled(beeperName) {
|
||||||
|
const self = this;
|
||||||
|
|
||||||
|
for (let i = 0; i < self._beepers.length; i++) {
|
||||||
|
if (self._beepers[i].name === beeperName && bit_check(self._beeperOfMask, self._beepers[i].bit)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
generateElements(template, destination) {
|
||||||
|
const self = this;
|
||||||
|
|
||||||
|
for (let i = 0; i < self._beepers.length; i++) {
|
||||||
|
if (self._beepers[i].visible) {
|
||||||
|
const element = template.clone();
|
||||||
|
destination.append(element);
|
||||||
|
|
||||||
|
const inputElement = $(element).find('input');
|
||||||
|
const labelElement = $(element).find('div');
|
||||||
|
const spanElement = $(element).find('span');
|
||||||
|
|
||||||
|
inputElement.attr('id', `beeper-${i}`);
|
||||||
|
inputElement.attr('name', self._beepers[i].name);
|
||||||
|
inputElement.attr('title', self._beepers[i].name);
|
||||||
|
inputElement.prop('checked', !bit_check(self._beeperDisabledMask, self._beepers[i].bit));
|
||||||
|
inputElement.data('bit', self._beepers[i].bit);
|
||||||
|
|
||||||
|
labelElement.text(self._beepers[i].name);
|
||||||
|
|
||||||
|
spanElement.attr('i18n', `beeper${self._beepers[i].name}`);
|
||||||
|
|
||||||
|
element.show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
updateData(beeperElement) {
|
||||||
|
const self = this;
|
||||||
|
|
||||||
|
if (beeperElement.attr('type') === 'checkbox') {
|
||||||
|
const bit = beeperElement.data('bit');
|
||||||
|
|
||||||
|
if (beeperElement.is(':checked')) {
|
||||||
|
self._beeperDisabledMask = bit_clear(self._beeperDisabledMask, bit);
|
||||||
|
} else {
|
||||||
|
self._beeperDisabledMask = bit_set(self._beeperDisabledMask, bit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -858,10 +858,10 @@ function configuration_restore(callback) {
|
||||||
FC.PID_ADVANCED_CONFIG = configuration.PID_ADVANCED_CONFIG;
|
FC.PID_ADVANCED_CONFIG = configuration.PID_ADVANCED_CONFIG;
|
||||||
|
|
||||||
FC.BEEPER_CONFIG.beepers = new Beepers(FC.CONFIG);
|
FC.BEEPER_CONFIG.beepers = new Beepers(FC.CONFIG);
|
||||||
FC.BEEPER_CONFIG.beepers.setMask(configuration.BEEPER_CONFIG.beepers._beeperMask);
|
FC.BEEPER_CONFIG.beepers.setDisabledMask(configuration.BEEPER_CONFIG.beepers._beeperDisabledMask);
|
||||||
FC.BEEPER_CONFIG.dshotBeaconTone = configuration.BEEPER_CONFIG.dshotBeaconTone;
|
FC.BEEPER_CONFIG.dshotBeaconTone = configuration.BEEPER_CONFIG.dshotBeaconTone;
|
||||||
FC.BEEPER_CONFIG.dshotBeaconConditions = new Beepers(FC.CONFIG, [ "RX_LOST", "RX_SET" ]);
|
FC.BEEPER_CONFIG.dshotBeaconConditions = new Beepers(FC.CONFIG, [ "RX_LOST", "RX_SET" ]);
|
||||||
FC.BEEPER_CONFIG.dshotBeaconConditions.setMask(configuration.BEEPER_CONFIG.dshotBeaconConditions._beeperMask);
|
FC.BEEPER_CONFIG.dshotBeaconConditions.setDisabledMask(configuration.BEEPER_CONFIG.dshotBeaconConditions._beeperDisabledMask);
|
||||||
}
|
}
|
||||||
|
|
||||||
function send_unique_data_item() {
|
function send_unique_data_item() {
|
||||||
|
|
|
@ -693,12 +693,12 @@ MspHelper.prototype.process_data = function(dataHandler) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MSPCodes.MSP_BEEPER_CONFIG:
|
case MSPCodes.MSP_BEEPER_CONFIG:
|
||||||
FC.BEEPER_CONFIG.beepers.setMask(data.readU32());
|
FC.BEEPER_CONFIG.beepers.setDisabledMask(data.readU32());
|
||||||
if (semver.gte(FC.CONFIG.apiVersion, "1.37.0")) {
|
if (semver.gte(FC.CONFIG.apiVersion, "1.37.0")) {
|
||||||
FC.BEEPER_CONFIG.dshotBeaconTone = data.readU8();
|
FC.BEEPER_CONFIG.dshotBeaconTone = data.readU8();
|
||||||
}
|
}
|
||||||
if (semver.gte(FC.CONFIG.apiVersion, "1.39.0")) {
|
if (semver.gte(FC.CONFIG.apiVersion, "1.39.0")) {
|
||||||
FC.BEEPER_CONFIG.dshotBeaconConditions.setMask(data.readU32());
|
FC.BEEPER_CONFIG.dshotBeaconConditions.setDisabledMask(data.readU32());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -1650,13 +1650,13 @@ MspHelper.prototype.crunch = function(code) {
|
||||||
buffer.push32(featureMask);
|
buffer.push32(featureMask);
|
||||||
break;
|
break;
|
||||||
case MSPCodes.MSP_SET_BEEPER_CONFIG:
|
case MSPCodes.MSP_SET_BEEPER_CONFIG:
|
||||||
var beeperMask = FC.BEEPER_CONFIG.beepers.getMask();
|
var beeperDisabledMask = FC.BEEPER_CONFIG.beepers.getDisabledMask();
|
||||||
buffer.push32(beeperMask);
|
buffer.push32(beeperDisabledMask);
|
||||||
if (semver.gte(FC.CONFIG.apiVersion, "1.37.0")) {
|
if (semver.gte(FC.CONFIG.apiVersion, "1.37.0")) {
|
||||||
buffer.push8(FC.BEEPER_CONFIG.dshotBeaconTone);
|
buffer.push8(FC.BEEPER_CONFIG.dshotBeaconTone);
|
||||||
}
|
}
|
||||||
if (semver.gte(FC.CONFIG.apiVersion, "1.39.0")) {
|
if (semver.gte(FC.CONFIG.apiVersion, "1.39.0")) {
|
||||||
buffer.push32(FC.BEEPER_CONFIG.dshotBeaconConditions.getMask());
|
buffer.push32(FC.BEEPER_CONFIG.dshotBeaconConditions.getDisabledMask());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MSPCodes.MSP_SET_MIXER_CONFIG:
|
case MSPCodes.MSP_SET_MIXER_CONFIG:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue