Fix: Correctly determine number of PID profiles#544
Fix: Correctly determine number of PID profiles#544nerdCopter wants to merge 1 commit intoemuflight:masterfrom
Conversation
The logic for determining the number of PID profiles was flawed, preventing users from copying to profile 3. This commit corrects the logic to trust the `numProfiles` value from the firmware when available (API version >= 1.20.0), and falls back to a default of 3 for older versions. This ensures the correct number of profiles is displayed and the copy functionality works as expected.
📝 WalkthroughWalkthroughModified the conditional logic in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/js/tabs/pid_tuning.js`:
- Around line 1712-1714: The assignment to numberOfProfiles uses
CONFIG.numProfiles directly when semver.gte(CONFIG.apiVersion, "1.20.0"), which
can be undefined/0/NaN and will cause the profile selector loop
(numberOfProfiles) to produce zero options; change the assignment in that branch
to validate and fall back to a safe default (e.g., DEFAULT_NUM_PROFILES or a
constant) by coalescing only valid positive integers—e.g., set numberOfProfiles
= (Number.isFinite(Number(CONFIG.numProfiles)) && Number(CONFIG.numProfiles) >
0) ? Number(CONFIG.numProfiles) : DEFAULT_NUM_PROFILES—so semver.gte(...),
CONFIG.numProfiles, numberOfProfiles and the profile-generation loop are
protected.
| if (semver.gte(CONFIG.apiVersion, "1.20.0")) { | ||
| numberOfProfiles = CONFIG.numProfiles; | ||
| } |
There was a problem hiding this comment.
Guard against falsy CONFIG.numProfiles.
If CONFIG.numProfiles is undefined, 0, or NaN (e.g., firmware returns an unexpected value), numberOfProfiles becomes falsy and the loop on line 1717 produces zero options, breaking the profile selector entirely. Consider falling back to the default.
🛡️ Proposed fix
if (semver.gte(CONFIG.apiVersion, "1.20.0")) {
- numberOfProfiles = CONFIG.numProfiles;
+ numberOfProfiles = CONFIG.numProfiles || 3;
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (semver.gte(CONFIG.apiVersion, "1.20.0")) { | |
| numberOfProfiles = CONFIG.numProfiles; | |
| } | |
| if (semver.gte(CONFIG.apiVersion, "1.20.0")) { | |
| numberOfProfiles = CONFIG.numProfiles || 3; | |
| } |
🤖 Prompt for AI Agents
In `@src/js/tabs/pid_tuning.js` around lines 1712 - 1714, The assignment to
numberOfProfiles uses CONFIG.numProfiles directly when
semver.gte(CONFIG.apiVersion, "1.20.0"), which can be undefined/0/NaN and will
cause the profile selector loop (numberOfProfiles) to produce zero options;
change the assignment in that branch to validate and fall back to a safe default
(e.g., DEFAULT_NUM_PROFILES or a constant) by coalescing only valid positive
integers—e.g., set numberOfProfiles =
(Number.isFinite(Number(CONFIG.numProfiles)) && Number(CONFIG.numProfiles) > 0)
? Number(CONFIG.numProfiles) : DEFAULT_NUM_PROFILES—so semver.gte(...),
CONFIG.numProfiles, numberOfProfiles and the profile-generation loop are
protected.
There was a problem hiding this comment.
@coderabbitai, what is a better way to do this overall? Emuflight is a fork of Betaflight around version 3.8.7 . Emuflight does not have any older semver than 1.20.0, in fact emuflight's first semver is 1.40.0 so essentially the firmware only has 3 profiles during it's lifetime of releases. recommend proper code for the code in this area/span for the goals.
the 1.20.0 is simply for old compatibility.
There was a problem hiding this comment.
@nerdCopter, given that Emuflight's minimum API version is always ≥ 1.40.0, the semver.gte(CONFIG.apiVersion, "1.20.0") check is entirely dead code — that branch will always be taken. The simplest and most correct approach is to remove the version guard entirely and just read CONFIG.numProfiles directly, with a safe fallback for any unexpected firmware value:
function loadProfilesList() {
var numberOfProfiles = CONFIG.numProfiles || 3;
var profileElements = [];
for (var i = 0; i < numberOfProfiles; i++) {
profileElements.push(i18n.getMessage("pidTuningProfileOption", [(i + 1)]));
}
return profileElements;
}Why this is better:
- Removes dead code: The
semver.gte(CONFIG.apiVersion, "1.20.0")check is neverfalsein Emuflight, so it just adds noise. - Uses firmware truth:
CONFIG.numProfilesis the authoritative source — if a future firmware version ever changes it, this handles it automatically. - Safe fallback:
|| 3guards againstundefined,0, orNaNfrom an unexpected firmware response, keeping the profile selector functional.
If you prefer slightly more explicit validation (ruling out negative numbers too):
var numberOfProfiles = (CONFIG.numProfiles > 0) ? CONFIG.numProfiles : 3;Both are equivalent for practical purposes since the firmware will only ever return a positive integer here.
✏️ Learnings added
Learnt from: nerdCopter
Repo: emuflight/EmuConfigurator PR: 544
File: src/js/tabs/pid_tuning.js:1712-1714
Timestamp: 2026-03-09T14:03:37.978Z
Learning: Emuflight is a fork of Betaflight ~3.8.7. Emuflight's minimum API version is 1.40.0 — API versions below 1.40.0 (including 1.20.0) are never present in Emuflight firmware. Legacy semver checks like `semver.gte(CONFIG.apiVersion, "1.20.0")` in `src/js/tabs/pid_tuning.js` are dead code in Emuflight's context. Emuflight always has 3 PID profiles in its firmware lifetime.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
|
Closing, this was a firmware bug fixed in emuflight/EmuFlight#1104 |
The logic for determining the number of PID profiles was flawed, preventing users from copying to profile 3.
This commit corrects the logic to trust the
numProfilesvalue from the firmware when available (API version >= 1.20.0), and falls back to a default of 3 for older versions. This ensures the correct number of profiles is displayed and the copy functionality works as expected.Summary by CodeRabbit