Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions js/outputMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,38 @@
return Object.keys(used).sort((a, b) => a - b);
}

function getTimerMap(isMR, motors, servos) {

Check failure on line 82 in js/outputMapping.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 21 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=iNavFlight_inav-configurator&issues=AZ0Hx028NcBFoDE9Fkog&open=AZ0Hx028NcBFoDE9Fkog&pullRequest=2596
let timerMap = [],
motorsToGo = motors,
servosToGo = servos;

for (let i = 0; i < data.length; i++) {
timerMap[i] = null;
}

if (servosToGo > 0 && BitHelper.bit_check(data[i]['usageFlags'], TIM_USE_LED)) {
console.log(i + ": LED");
timerMap[i] = OUTPUT_TYPE_LED;
} else if (servosToGo > 0 && BitHelper.bit_check(data[i]['usageFlags'], TIM_USE_SERVO)) {
console.log(i + ": SERVO");
servosToGo--;
timerMap[i] = OUTPUT_TYPE_SERVO;
} else if (motorsToGo > 0 && BitHelper.bit_check(data[i]['usageFlags'], TIM_USE_MOTOR)) {
console.log(i + ": MOTOR");
motorsToGo--;
timerMap[i] = OUTPUT_TYPE_MOTOR;
// Two priority passes: dedicated outputs first, then auto.
// Matches firmware pwmBuildTimerOutputList() behavior.
for (let priority = 0; priority < 2; priority++) {
let isDedicated = (priority === 0);

for (let i = 0; i < data.length; i++) {
if (timerMap[i] !== null) continue;

let flags = data[i]['usageFlags'];
let timerId = data[i]['timerId'];
let mode = timerOverrides[timerId] || self.TIMER_OUTPUT_MODE_AUTO;

if (motorsToGo > 0 && BitHelper.bit_check(flags, TIM_USE_MOTOR)
&& (isDedicated ? mode === self.TIMER_OUTPUT_MODE_MOTORS : mode !== self.TIMER_OUTPUT_MODE_MOTORS)) {
timerMap[i] = OUTPUT_TYPE_MOTOR;
motorsToGo--;
} else if (servosToGo > 0 && BitHelper.bit_check(flags, TIM_USE_SERVO)
&& (isDedicated ? mode === self.TIMER_OUTPUT_MODE_SERVOS : mode !== self.TIMER_OUTPUT_MODE_SERVOS)) {
timerMap[i] = OUTPUT_TYPE_SERVO;
servosToGo--;
} else if (!isDedicated && BitHelper.bit_check(flags, TIM_USE_LED)) {
timerMap[i] = OUTPUT_TYPE_LED;
}
}
}

Expand Down
Loading