Skip to content

Commit 63c776a

Browse files
authored
Add array mutation helper and add missing arming disable flags (#4334)
* Add array helper * Apply change to debug * Remove auto imported nonsense * Remove commented code * Extract to separate import * Include new file * Fix order * Change parameter order * Update virtualfc * Add missing disarmFlagElements * Add helper function to add an array instead of single element
1 parent 20fbd87 commit 63c776a

File tree

5 files changed

+109
-35
lines changed

5 files changed

+109
-35
lines changed

src/js/VirtualFC.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import FC from "./fc";
55
import CONFIGURATOR, { API_VERSION_1_47 } from "./data_storage";
66
import { OSD } from "./tabs/osd";
77
import semver from "semver";
8+
import { addArrayElement, addArrayElementAfter } from "./utils/array";
89

910
const VirtualFC = {
1011
// these values are manufactured to unlock all the functionality of the configurator, they dont represent actual hardware
@@ -224,9 +225,9 @@ const VirtualFC = {
224225
];
225226

226227
if (semver.gte(virtualFC.CONFIG.apiVersion, API_VERSION_1_47)) {
227-
virtualFC.AUX_CONFIG.splice(virtualFC.AUX_CONFIG.indexOf("HORIZON") + 1, 0, "ALT_HOLD");
228-
virtualFC.AUX_CONFIG.splice(virtualFC.AUX_CONFIG.indexOf("CAMSTAB") + 1, 0, "POS_HOLD");
229-
virtualFC.AUX_CONFIG.push("CHIRP");
228+
addArrayElementAfter(virtualFC.AUX_CONFIG, "HORIZON", "ALT_HOLD");
229+
addArrayElementAfter(virtualFC.AUX_CONFIG, "CAMSTAB", "POS_HOLD");
230+
addArrayElement(virtualFC.AUX_CONFIG, "CHIRP");
230231
}
231232

232233
FC.AUX_CONFIG_IDS = [

src/js/debug.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import FC from "./fc.js";
22
import { API_VERSION_1_46, API_VERSION_1_47 } from "./data_storage";
33
import semver from "semver";
4+
import { removeArrayElement, addArrayElement, replaceArrayElement, addArrayElementAfter } from "./utils/array";
45

56
const DEBUG = {
67
modes: [
@@ -823,11 +824,12 @@ function update() {
823824
}
824825

825826
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
826-
DEBUG.modes.splice(DEBUG.modes.indexOf("GPS_RESCUE_THROTTLE_PID"), 1, "AUTOPILOT_ALTITUDE");
827-
DEBUG.modes.splice(DEBUG.modes.indexOf("GYRO_SCALED"), 1);
828-
DEBUG.modes.splice(DEBUG.modes.indexOf("RANGEFINDER_QUALITY") + 1, 0, "OPTICALFLOW");
829-
DEBUG.modes.push("AUTOPILOT_POSITION");
830-
DEBUG.modes.push("CHIRP");
827+
replaceArrayElement(DEBUG.modes, "GPS_RESCUE_THROTTLE_PID", "AUTOPILOT_ALTITUDE");
828+
removeArrayElement(DEBUG.modes, "GYRO_SCALED");
829+
addArrayElementAfter(DEBUG.modes, "RANGEFINDER_QUALITY", "OPTICALFLOW");
830+
addArrayElement(DEBUG.modes, "AUTOPILOT_POSITION");
831+
addArrayElement(DEBUG.modes, "CHIRP");
832+
831833
delete DEBUG.fieldNames.GPS_RESCUE_THROTTLE_PID;
832834
delete DEBUG.fieldNames.GYRO_SCALED;
833835

@@ -887,8 +889,8 @@ function update() {
887889
"debug[7]": "pidA",
888890
};
889891

890-
DEBUG.enableFields.splice(DEBUG.enableFields.indexOf("Gyro") + 1, 0, "Attitude");
891-
DEBUG.enableFields.push("Servo");
892+
addArrayElementAfter(DEBUG.enableFields, "Gyro", "Attitude");
893+
addArrayElement(DEBUG.enableFields, "Servo");
892894
}
893895
}
894896

src/js/sensor_types.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import semver from "semver";
22
import FC from "./fc";
33
import { API_VERSION_1_47 } from "./data_storage";
4+
import { removeArrayElement, addArrayElement, addArrayElementAfter } from "./utils/array";
45

56
export function sensorTypes() {
67
const sensorTypes = {
@@ -102,37 +103,23 @@ export function sensorTypes() {
102103
},
103104
};
104105

105-
function removeElement(elements, element) {
106-
const index = elements.indexOf(element);
107-
if (index !== -1) {
108-
elements.splice(index, 1);
109-
}
110-
}
111-
112-
function addElement(elements, element, afterElement) {
113-
const elementIndex = elements.indexOf(element);
114-
if (elementIndex === -1) {
115-
elements.splice(elements.indexOf(afterElement) + 1, 0, element);
116-
}
117-
}
118-
119106
const gyroElements = sensorTypes.gyro.elements;
120107
const accElements = sensorTypes.acc.elements;
121108
const gpsElements = sensorTypes.gps.elements;
122109

123110
// remove deprecated sensors or add new ones
124111
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
125-
removeElement(gyroElements, "L3G4200D");
126-
removeElement(gyroElements, "MPU3050");
127-
addElement(gyroElements, "IIM42653", "LSM6DSV16X");
112+
removeArrayElement(gyroElements, "L3G4200D");
113+
removeArrayElement(gyroElements, "MPU3050");
114+
addArrayElementAfter(gyroElements, "LSM6DSV16X", "IIM42653");
128115

129-
removeElement(accElements, "ADXL345");
130-
removeElement(accElements, "MMA8452");
131-
removeElement(accElements, "BMA280");
132-
removeElement(accElements, "LSM303DLHC");
133-
addElement(accElements, "IIM42653", "LSM6DSV16X");
116+
removeArrayElement(accElements, "ADXL345");
117+
removeArrayElement(accElements, "MMA8452");
118+
removeArrayElement(accElements, "BMA280");
119+
removeArrayElement(accElements, "LSM303DLHC");
120+
addArrayElementAfter(accElements, "LSM6DSV16X", "IIM42653");
134121

135-
addElement(gpsElements, "VIRTUAL", "MSP");
122+
addArrayElement(gpsElements, "VIRTUAL");
136123
}
137124

138125
return sensorTypes;

src/js/tabs/setup.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { gui_log } from "../gui_log";
1313
import $ from "jquery";
1414
import { ispConnected } from "../utils/connection";
1515
import { sensorTypes } from "../sensor_types";
16+
import { addArrayElementsAfter, replaceArrayElement } from "../utils/array";
1617

1718
const setup = {
1819
yaw_fix: 0.0,
@@ -252,11 +253,11 @@ setup.initialize = function (callback) {
252253
];
253254

254255
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_46)) {
255-
disarmFlagElements.splice(disarmFlagElements.indexOf("RPMFILTER"), 1, "DSHOT_TELEM");
256+
replaceArrayElement(disarmFlagElements, "RPMFILTER", "DSHOT_TELEM");
256257
}
257258

258259
if (semver.gte(FC.CONFIG.apiVersion, API_VERSION_1_47)) {
259-
disarmFlagElements.splice(disarmFlagElements.indexOf("MOTOR_PROTOCOL"), 0, "CRASHFLIP");
260+
addArrayElementsAfter(disarmFlagElements, "MOTOR_PROTOCOL", ["CRASHFLIP", "ALTHOLD", "POSHOLD"]);
260261
}
261262

262263
// Arming allowed flag

src/js/utils/array.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Removes a specified element from an array if it exists.
3+
*
4+
* @param {Array} elements - The array from which to remove the element.
5+
* @param {*} element - The element to be removed from the array.
6+
*/
7+
export function removeArrayElement(elements, element) {
8+
const index = elements.indexOf(element);
9+
if (index !== -1) {
10+
elements.splice(index, 1);
11+
}
12+
}
13+
14+
/**
15+
* Adds an element to an array after a specified element if the element is not already present in the array.
16+
*
17+
* @param {Array} elements - The array to which the element will be added.
18+
* @param {*} afterElement - The element after which the new element will be added.
19+
* @param {*} element - The element to be added to the array.
20+
*/
21+
export function addArrayElementAfter(elements, afterElement, element) {
22+
const elementIndex = elements.indexOf(element);
23+
if (elementIndex === -1) {
24+
elements.splice(elements.indexOf(afterElement) + 1, 0, element);
25+
}
26+
}
27+
28+
/**
29+
* Adds new elements to an array after a specified element, ensuring no duplicates.
30+
*
31+
* @param {Array} elements - The original array to modify.
32+
* @param {*} afterElement - The element after which new elements will be added.
33+
* @param {Array} newElements - The new elements to add to the array.
34+
*/
35+
export function addArrayElementsAfter(elements, afterElement, newElements) {
36+
const afterElementIndex = elements.indexOf(afterElement);
37+
if (afterElementIndex === -1) {
38+
return;
39+
}
40+
41+
const newElementsToAdd = newElements.filter((element) => !elements.includes(element));
42+
elements.splice(afterElementIndex + 1, 0, ...newElementsToAdd);
43+
}
44+
45+
/**
46+
* Adds an element to an array before a specified element if the element is not already present in the array.
47+
*
48+
* @param {Array} elements - The array to which the element will be added.
49+
* @param {*} beforeElement - The element before which the new element will be added.
50+
* @param {*} element - The element to be added to the array.
51+
*/
52+
export function addArrayElementBefore(elements, beforeElement, element) {
53+
const elementIndex = elements.indexOf(element);
54+
if (elementIndex === -1) {
55+
elements.splice(elements.indexOf(beforeElement), 0, element);
56+
}
57+
}
58+
59+
/**
60+
* Adds an element to an array if it is not already present.
61+
*
62+
* @param {Array} elements - The array to which the element should be added.
63+
* @param {*} element - The element to add to the array.
64+
*/
65+
export function addArrayElement(elements, element) {
66+
if (!elements.includes(element)) {
67+
elements.push(element);
68+
}
69+
}
70+
71+
/**
72+
* Replaces an element in an array with a new element.
73+
*
74+
* @param {Array} elements - The array containing the element to be replaced.
75+
* @param {*} element - The element to be replaced.
76+
* @param {*} replacement - The new element to replace the old element.
77+
*/
78+
export function replaceArrayElement(elements, element, replacement) {
79+
const index = elements.indexOf(element);
80+
if (index !== -1) {
81+
elements[index] = replacement;
82+
}
83+
}

0 commit comments

Comments
 (0)