Skip to content

Commit

Permalink
Enable or disable features according to port configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
haslinghuis committed Feb 28, 2022
1 parent a8a9ac6 commit 6dbfcf3
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 22 deletions.
42 changes: 31 additions & 11 deletions src/js/Features.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,27 @@ const Features = function (config) {
);
}

if (semver.gte(FC.CONFIG.apiVersion, "1.15.0") && !semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_36)) {
if (semver.gte(config.apiVersion, "1.15.0") && !semver.gte(config.apiVersion, API_VERSION_1_36)) {
features.push(
{bit: 8, group: 'rxFailsafe', name: 'FAILSAFE', haveTip: true},
);
}

if (semver.gte(FC.CONFIG.apiVersion, "1.16.0")) {
if (semver.gte(config.apiVersion, "1.16.0")) {
features.push(
{bit: 21, group: 'other', name: 'TRANSPONDER', haveTip: true},
);
}

if (config.flightControllerVersion !== '') {
if (semver.gte(FC.CONFIG.apiVersion, "1.16.0")) {
if (semver.gte(config.apiVersion, "1.16.0")) {
features.push(
{bit: 22, group: 'other', name: 'AIRMODE'},
);
}

if (semver.gte(FC.CONFIG.apiVersion, "1.16.0")) {
if (semver.lt(FC.CONFIG.apiVersion, "1.20.0")) {
if (semver.gte(config.apiVersion, "1.16.0")) {
if (semver.lt(config.apiVersion, "1.20.0")) {
features.push(
{bit: 23, group: 'superexpoRates', name: 'SUPEREXPO_RATES'},
);
Expand All @@ -64,36 +64,36 @@ const Features = function (config) {
}
}

if (semver.gte(FC.CONFIG.apiVersion, "1.20.0")) {
if (semver.gte(config.apiVersion, "1.20.0")) {
features.push(
{bit: 18, group: 'other', name: 'OSD'},
);
if (!semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_35)) {
if (!semver.gte(config.apiVersion, API_VERSION_1_35)) {
features.push(
{bit: 24, group: 'other', name: 'VTX'},
);
}
}

if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_31)) {
if (semver.gte(config.apiVersion, API_VERSION_1_31)) {
features.push(
{bit: 25, group: 'rxMode', mode: 'select', name: 'RX_SPI'},
{bit: 27, group: 'escSensor', name: 'ESC_SENSOR'},
);
}

if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_36)) {
if (semver.gte(config.apiVersion, API_VERSION_1_36)) {
features.push(
{bit: 28, group: 'antiGravity', name: 'ANTI_GRAVITY', haveTip: true, hideName: true},
);
if (semver.lt(FC.CONFIG.apiVersion, API_VERSION_1_44)) { // DYNAMIC_FILTER got removed from FEATURES in BF 4.3 / API 1.44
if (semver.lt(config.apiVersion, API_VERSION_1_44)) { // DYNAMIC_FILTER got removed from FEATURES in BF 4.3 / API 1.44
features.push(
{bit: 29, group: 'other', name: 'DYNAMIC_FILTER'},
);
}
}

if (!semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_36)) {
if (!semver.gte(config.apiVersion, API_VERSION_1_36)) {
features.push(
{bit: 1, group: 'batteryVoltage', name: 'VBAT'},
{bit: 11, group: 'batteryCurrent', name: 'CURRENT_METER'},
Expand Down Expand Up @@ -133,6 +133,26 @@ Features.prototype.isEnabled = function (featureName) {
return false;
};

Features.prototype.enable = function (featureName) {
const self = this;

for (let i = 0; i < self._features.length; i++) {
if (self._features[i].name === featureName) {
self._featureMask = bit_set(self._featureMask, self._features[i].bit);
}
}
};

Features.prototype.disable = function (featureName) {
const self = this;

for (let i = 0; i < self._features.length; i++) {
if (self._features[i].name === featureName) {
self._featureMask = bit_clear(self._featureMask, self._features[i].bit);
}
}
};

Features.prototype.generateElements = function (featuresElements) {
const self = this;

Expand Down
87 changes: 76 additions & 11 deletions src/js/tabs/ports.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,12 @@ TABS.ports.initialize = function (callback, scrollPosition) {
load_configuration_from_fc();

function load_configuration_from_fc() {
let promise;
if(semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_42)) {
promise = MSP.promise(MSPCodes.MSP_VTX_CONFIG);
} else {
promise = Promise.resolve();
}
promise.then(function() {
mspHelper.loadSerialConfig(on_configuration_loaded_handler);
});

Promise
.resolve(true)
.then(() => MSP.promise(MSPCodes.MSP_FEATURE_CONFIG))
.then(() => semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_42) ? MSP.promise(MSPCodes.MSP_VTX_CONFIG) : true)
.then(() => mspHelper.loadSerialConfig(on_configuration_loaded_handler));

function on_configuration_loaded_handler() {
$('#content').load("./tabs/ports.html", on_tab_loaded_handler);
Expand Down Expand Up @@ -353,7 +350,7 @@ TABS.ports.initialize = function (callback, scrollPosition) {

function on_save_handler() {
analytics.sendSaveAndChangeEvents(analytics.EVENT_CATEGORIES.FLIGHT_CONTROLLER, self.analyticsChanges, 'ports');
self.analyticsChanges = {};
self.analyticsChanges = {};

// update configuration based on current ui state
FC.SERIAL_CONFIG.ports = [];
Expand Down Expand Up @@ -399,10 +396,78 @@ TABS.ports.initialize = function (callback, scrollPosition) {
blackbox_baudrate: blackboxBaudrate,
identifier: serialPort.identifier,
};

FC.SERIAL_CONFIG.ports.push(serialPortConfig);

// enable / disable features based on port configuration
let enableRxSerial = false;
let enableTelemetry = false;
let enableBlackbox = false;
let enableEsc = false;
let enableGps = false;

for (const portConfig of FC.SERIAL_CONFIG.ports) {
portConfig.functions.forEach(e => {
if (e === 'RX_SERIAL') {
enableRxSerial = true;
}
});

portConfig.functions.forEach(e => {
if (e.startsWith("TELEMETRY")) {
enableTelemetry = true;
}
});

if (portConfig.functions.indexOf('BLACKBOX') >= 0) {
enableBlackbox = true;
}

if (portConfig.functions.indexOf('ESC_SENSOR') >= 0) {
enableEsc = true;
}

if (portConfig.functions.indexOf('GPS') >= 0) {
enableGps = true;
}
}

if (enableRxSerial) {
FC.FEATURE_CONFIG.features.enable('RX_SERIAL');
} else {
FC.FEATURE_CONFIG.features.disable('RX_SERIAL');
}

if (enableTelemetry) {
FC.FEATURE_CONFIG.features.enable('TELEMETRY');
} else {
FC.FEATURE_CONFIG.features.disable('TELEMETRY');
}

if (enableBlackbox) {
FC.FEATURE_CONFIG.features.enable('BLACKBOX');
} else {
FC.FEATURE_CONFIG.features.disable('BLACKBOX');
}

if (enableEsc) {
FC.FEATURE_CONFIG.features.enable('ESC_SENSOR');
} else {
FC.FEATURE_CONFIG.features.disable('ESC_SENSOR');
}

if (enableGps) {
FC.FEATURE_CONFIG.features.enable('GPS');
} else {
FC.FEATURE_CONFIG.features.disable('GPS');
}
});

mspHelper.sendSerialConfig(save_to_eeprom);
mspHelper.sendSerialConfig(save_features);

function save_features() {
MSP.send_message(MSPCodes.MSP_SET_FEATURE_CONFIG, mspHelper.crunch(MSPCodes.MSP_SET_FEATURE_CONFIG), false, save_to_eeprom);
}

function save_to_eeprom() {
MSP.send_message(MSPCodes.MSP_EEPROM_WRITE, false, false, on_saved_handler);
Expand Down

0 comments on commit 6dbfcf3

Please sign in to comment.