Skip to content

fix(switches): widen fsPreviousState to 32-bit to fix toggle detection for more than 16 switches - #7560

Merged
pfeerick merged 3 commits into
EdgeTX:mainfrom
norulers:fix/fsPreviousState-uint32
Jul 15, 2026
Merged

fix(switches): widen fsPreviousState to 32-bit to fix toggle detection for more than 16 switches#7560
pfeerick merged 3 commits into
EdgeTX:mainfrom
norulers:fix/fsPreviousState-uint32

Conversation

@norulers

@norulers norulers commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

The fsPreviousState variable was declared as uint16_t, which only supports up to 16 switches. On radios with more than 16 switches (like the T22), switches beyond the 16th (SW5/SW6) would have their states lost or incorrectly tracked.

This change:

  • Expands fsPreviousState from uint16_t to uint32_t to support more switches
  • Simplifies the bit extraction in getFSPreviousPhysicalState() using a direct shift-and-mask pattern

Summary by CodeRabbit

  • Bug Fixes
    • Improved switch change detection by ensuring prior physical-state tracking supports the full range of switch inputs.
    • Corrected how previous switch-state bits are read and updated, improving reliability of toggle behavior across repeated evaluations while a switch is held.
  • Tests
    • Added coverage to verify toggle-type function switches only change once per press (and not again when evaluation runs while the switch remains held).

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d29594c9-5738-4e9f-b53f-0372aed07915

📥 Commits

Reviewing files that changed from the base of the PR and between a7f7fe8 and e564c8c.

📒 Files selected for processing (1)
  • radio/src/tests/switches.cpp

📝 Walkthrough

Walkthrough

The switch previous-state bitset is widened from 16-bit to 32-bit storage. Indexed lookup and state toggling use 32-bit-compatible operations, with tests covering single toggles while switches remain held.

Changes

Switch State Tracking

Layer / File(s) Summary
Widen previous-state tracking
radio/src/switches.cpp
fsPreviousState uses 32-bit storage, indexed state bits use direct shift-and-mask extraction, and toggles use a 32-bit mask.
Validate held toggle behavior
radio/src/tests/switches.cpp
A conditional test verifies that configured function switches toggle once on press and do not re-toggle during repeated evaluations while held.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: widening fsPreviousState to 32-bit to support more than 16 switches.
Description check ✅ Passed The description covers the change and rationale, though it omits the template's Fixes # reference and exact section formatting.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@radio/src/switches.cpp`:
- Line 95: Update the widened bitset mutation near fsPreviousState to construct
the mask from uint32_t before shifting, replacing the signed 1 << i expression
so bit index 31 is handled safely.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 32d3fa60-309e-4347-aa80-dec0bc8c5123

📥 Commits

Reviewing files that changed from the base of the PR and between c966bee and f74e773.

📒 Files selected for processing (1)
  • radio/src/switches.cpp

Comment thread radio/src/switches.cpp
@pfeerick pfeerick added the bug 🪲 Something isn't working label Jul 15, 2026
@pfeerick pfeerick added this to the 2.12.3 milestone Jul 15, 2026
@pfeerick pfeerick added the backport/2.12 To be backported to a 2.12 release also. label Jul 15, 2026
@pfeerick pfeerick changed the title fix: expand fsPreviousState to uint32_t to support >16 switches (fixes T22 SW5/SW6 toggling) fix(switches): widen fsPreviousState to 32-bit to fix toggle detection for switches beyond index 15 Jul 15, 2026
@pfeerick pfeerick changed the title fix(switches): widen fsPreviousState to 32-bit to fix toggle detection for switches beyond index 15 fix(switches): widen fsPreviousState to 32-bit to fix toggle detection for more than 16 switches Jul 15, 2026
Verifies that holding a custom switch does not generate phantom
toggle events. Fails on the pre-fix code for T22 SW5/SW6 (switch
indices 16/17), where fsPreviousState was truncated to 16 bits.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@pfeerick

Copy link
Copy Markdown
Member

Thanks for catching this. Also added a unit test that would have caught this if it had been there at the time, which will protect against future regressions.

@pfeerick
pfeerick merged commit 0a6d3d0 into EdgeTX:main Jul 15, 2026
40 checks passed
@norulers
norulers deleted the fix/fsPreviousState-uint32 branch July 16, 2026 00:14
pfeerick pushed a commit that referenced this pull request Jul 19, 2026
…n for more than 16 switches (#7560)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/2.12 To be backported to a 2.12 release also. bug 🪲 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants