Skip to content

refactor(rx): share the WS2812 decoder between Native + LPC SCT (#3035 Phase 3) - #3036

Merged
zackees merged 1 commit into
masterfrom
feat/3035-decode-ws2812-shared
Jun 14, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/3035-decode-ws2812-shared

Conversation

@zackees

@zackees zackees commented Jun 14, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Extract the 4-phase WS2812 edge-pair → byte decoder out of two duplicated bodies (NativeRxDevice::decode, LpcSctRxChannel::decode) into a single header-only function fl::channels::rx::decodeWs2812Edges() at src/fl/channels/rx/decode_ws2812.h.
  • Both call sites now forward to the shared decoder while keeping their pin-aware FL_WARN for the empty-buffer case (the shared function doesn't know the pin number).
  • Closes the inline TODO at src/platforms/arm/lpc/rx_sct_capture.h:14-15 that flagged the consolidation as a follow-up when the SCT driver landed.

Scope decision

The codebase has four EdgeTime-based RX decoders today: Native, LPC SCT, FlexPWM, FlexIO. The first two are verbatim copies of each other and are merged here. The FlexPWM (decodeEdges) and FlexIO (decodeEdgesFlexIo) variants have deliberately different semantics (partial-byte left-shift, no reset detection, no gap tolerance, different error-count target), so folding them into the same function would be a behavior change for those backends. That's intentionally left as a separate change tracked under #3035 Phase 3 with the deltas documented in the new header.

Test plan

  • tests/fl_channels_rx_sct_capture — 5 cases pass (round-trips a synthetic GRB stream through LPC SCT)
  • tests/fl_channels_rx — 15 cases pass (channel-level harness exercising NativeRxDevice)
  • Full 457-target host build green
  • bash lint --cpp clean (zero custom-checker violations)
  • FlexPWM / FlexIO consolidation follow-up — out of scope for this PR (tracked under meta(lpc): LPC SCT-RX bench-validation + Phase-3 follow-ups (post #3021) #3035 Phase 3)

Drive-by lint fixes

Per CLAUDE.md "fix all encountered errors immediately" — bash lint flagged two pre-existing issues that I addressed inline:

  • src/platforms/shared/mock/arm/teensy4/drivers/_build.cpp.hpp: alphabetise the drivers/ includes (the lpuart line was appended out of order when introduced in bbf21f39f).
  • ci/tests/test_streaming_runner_artifact_validation.py: ruff auto-collapse of a multi-line call.

Refs #3035 (Phase 3 — partial).

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Refactor

    • Consolidated WS2812 LED edge decoding into a shared, centralized decoder used across all platform implementations for improved consistency and maintainability.
  • Tests

    • Updated test invocation syntax for artifact validation.

…x/decode_ws2812.h

The NativeRxDevice (host stub) and LpcSctRxChannel (LPC845 SCT input-capture
backend) both carried verbatim copies of the same 4-phase WS2812 edge-pair →
byte decoder — reset-pulse breakout, gap-tolerance fallback, 10 % error gate.
A code comment in rx_sct_capture.h:14-15 already flagged the consolidation
as a follow-up; this lands it. ~150 lines of duplication removed.

The new header-only decoder lives at src/fl/channels/rx/decode_ws2812.h.
Per-driver wrappers keep the device-specific FL_WARN("…pin N…") messages
for the empty-buffer case (the shared decoder doesn't know the pin).

Behavior parity is exact for the Native + LPC paths. The FlexPWM
(decodeEdges) and FlexIO (decodeEdgesFlexIo) drivers use a different
variant (partial-byte left-shift, no reset detection, no gap tolerance,
different error counting) and are intentionally left alone — folding
those in is a separate change tracked under #3035 Phase 3.

Verification:
  * tests/fl_channels_rx_sct_capture (5 cases) — pass
  * tests/fl_channels_rx (15 cases) — pass
  * Full 457-target host build — green
  * bash lint --cpp — clean

Drive-by lint fixes (encountered during the lint pass per CLAUDE.md
"fix all encountered errors immediately"):
  * src/platforms/shared/mock/arm/teensy4/drivers/_build.cpp.hpp:
    alphabetise drivers/ includes (lpuart was appended out of order
    when introduced).
  * ci/tests/test_streaming_runner_artifact_validation.py: ruff
    auto-collapse of a multi-line call.

Refs #3035 (Phase 3 — partial; FlexPWM/FlexIO follow-up still open).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 14, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

A new header-only decodeWs2812Edges function is introduced in src/fl/channels/rx/decode_ws2812.h to centralize WS2812 4-phase edge-pair decoding. Both LpcSctRxChannel::decode() and NativeRxDevice::decode() are refactored to delegate to this shared decoder, removing ~180 lines of duplicated inline logic. A build include order is swapped and a CI test call is reformatted.

Changes

WS2812 Shared Decoder Consolidation

Layer / File(s) Summary
New shared decodeWs2812Edges header
src/fl/channels/rx/decode_ws2812.h
Introduces the complete header-only decodeWs2812Edges inline function with API docs, includes, namespace setup, and full implementation: edge-pair iteration, polarity validation, 4-phase timing-window bit classification, reset/gap tolerance, buffer overflow protection, and 10% error-rate rejection.
LPC SCT and NativeRxDevice backend delegation
src/platforms/arm/lpc/rx_sct_capture.cpp.hpp, src/platforms/arm/lpc/rx_sct_capture.h, src/platforms/shared/rx_device_native.cpp.hpp
Both backends include the shared decoder header and replace their inline ~87–92-line edge-parsing loops with a single decodeWs2812Edges(...) call. NativeRxDevice retains the empty-edges early exit. rx_sct_capture.h doc is updated to reflect the delegation.

Minor Build and CI Fixes

Layer / File(s) Summary
Build include order swap and CI test reformat
src/platforms/shared/mock/arm/teensy4/drivers/_build.cpp.hpp, ci/tests/test_streaming_runner_artifact_validation.py
LPUART driver include is moved before ObjectFLED in the mock Teensy4 Unity build header. Stale-case validate_test_artifact call is collapsed from multi-line to single-line with identical arguments.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant LpcSctRxChannel
    participant NativeRxDevice
    participant decodeWs2812Edges

    Caller->>LpcSctRxChannel: decode(out)
    LpcSctRxChannel->>decodeWs2812Edges: decodeWs2812Edges(timing, mEdges, out)
    decodeWs2812Edges-->>LpcSctRxChannel: result<u32, DecodeError>
    LpcSctRxChannel-->>Caller: result<u32, DecodeError>

    Caller->>NativeRxDevice: decode(out)
    NativeRxDevice->>NativeRxDevice: check mEdges empty → INVALID_ARGUMENT
    NativeRxDevice->>decodeWs2812Edges: decodeWs2812Edges(timing, mEdges, out)
    decodeWs2812Edges-->>NativeRxDevice: result<u32, DecodeError>
    NativeRxDevice-->>Caller: result<u32, DecodeError>
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#3018: The CI test formatting change in test_streaming_runner_artifact_validation.py directly touches the validate_test_artifact staleness-validation and actionable-message test infrastructure added in this PR.
  • FastLED/FastLED#3016: The LPC SCT capture backend's decode() refactoring to delegate WS2812 edge parsing directly overlaps with the original LPC SCT backend addition in that PR.

Poem

🐇 A decoder once lived in three places at once,
Three copies of logic — a bit of a stunt.
Now one shared header does all of the work,
Two backends just call it, no parsing to lurk.
The rabbit hops happily, tidy and neat,
One source of truth makes the codebase complete! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main objective: consolidating duplicate WS2812 decoder logic between two RX backends (Native and LPC SCT) into a shared implementation.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/3035-decode-ws2812-shared

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 and usage tips.

@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: 4

🤖 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 `@src/fl/channels/rx/decode_ws2812.h`:
- Around line 1-21: The header file `src/fl/channels/rx/decode_ws2812.h`
contains non-ASCII Unicode characters (such as the arrow `→` and em dash `—`) in
its comments, which violates the encoding requirement that this file must
contain only 7-bit ASCII. Replace all Unicode glyphs throughout the file with
their ASCII equivalents, specifically converting the right arrow `→` to `->` or
`=>` and the em dash `—` to `--` or `-`, ensuring all comment text uses only
ASCII characters.
- Around line 62-134: The function decodeWs2812Edges is currently defined
directly in the plain .h header file, but the coding guidelines require function
definitions to be kept in .cpp.hpp or .impl.hpp companion files. Move the entire
body of the decodeWs2812Edges function (from the opening brace to the closing
brace) to a new decode_ws2812.impl.hpp file and leave only the function
declaration in the .h file. Include the necessary header files in the .impl.hpp
file to make the function definition valid, and ensure the .h file includes the
.impl.hpp file at the end so the definition is available to callers.
- Around line 129-133: The error-rate threshold check in the condition comparing
error_count to bytes_written * 8 does not account for partial bits that may
exist in bit_index. Update the denominator to include both the complete bytes
and the remaining partial bits by calculating the total bits as bytes_written *
8 + bit_index, then use this total in the error rate comparison. This ensures
frames with low error rates are not incorrectly rejected when the capture ends
mid-byte or when a reset occurs on a partial byte.
- Around line 95-106: The reset pulse detection on line 96 breaks immediately
when low_ns exceeds the reset minimum threshold, preventing the gap-tolerance
logic from evaluating pulses that should be handled under tolerance.
Additionally, the gap_tolerance_ns check on line 99 only validates that low_ns
is below the tolerance threshold, incorrectly accepting short malformed LOW
phases. Fix this by adjusting the reset check to account for the gap-tolerance
window first before breaking on reset, and modify the condition to ensure the
LOW phase duration is within both a minimum and maximum valid range (not just
below tolerance) when evaluating gap-tolerant bits.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 702fa827-6301-4fb8-b724-3d9461870bdc

📥 Commits

Reviewing files that changed from the base of the PR and between cbb8572 and 3f70cd6.

📒 Files selected for processing (6)
  • ci/tests/test_streaming_runner_artifact_validation.py
  • src/fl/channels/rx/decode_ws2812.h
  • src/platforms/arm/lpc/rx_sct_capture.cpp.hpp
  • src/platforms/arm/lpc/rx_sct_capture.h
  • src/platforms/shared/mock/arm/teensy4/drivers/_build.cpp.hpp
  • src/platforms/shared/rx_device_native.cpp.hpp

Comment thread src/fl/channels/rx/decode_ws2812.h Resolved
Comment thread src/fl/channels/rx/decode_ws2812.h Resolved
Comment thread src/fl/channels/rx/decode_ws2812.h Resolved
Comment thread src/fl/channels/rx/decode_ws2812.h Resolved
@zackees
zackees merged commit b99cc8e into master Jun 14, 2026
11 of 13 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jun 14, 2026
zackees added a commit that referenced this pull request Jun 14, 2026
…pp definition

Addresses two CodeRabbit findings on #3036:

1. **ASCII-only header.** The previous header used Unicode glyphs (->, --,
   <=) in comments. Per the path-specific source-encoding rule for
   `src/fl/channels/rx/decode_ws2812.h`, that header must be 7-bit ASCII.
   Replaced all Unicode characters with ASCII equivalents.

2. **No function definitions in plain .h files.** The previous
   implementation defined `decodeWs2812Edges()` inline in the .h. Per the
   project convention used throughout fl/channels/rx (see channel.h
   <-> channel.cpp.hpp), function definitions go in a .cpp.hpp companion
   compiled exactly once into the FastLED unity build, while the .h
   carries only the declaration. Created
   `src/fl/channels/rx/decode_ws2812.cpp.hpp` and added the include
   to `src/fl/channels/rx/_build.cpp.hpp`.

Behaviour unchanged: this is purely a file-layout split. The two
CodeRabbit findings that flagged latent semantic bugs in the
gap-tolerance ordering and the error-rate denominator are preserved
from the original Native + LPC decoders and explicitly documented in
the header's "Preserved-from-original quirks" block; fixing them is
out of scope for the consolidation PR and tracked under #3035 follow-ups.

Verification:
  * tests/fl_channels_rx_sct_capture (5 cases) -- pass
  * tests/fl_channels_rx (15 cases) -- pass
  * bash lint --cpp -- clean

Refs #3035.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
zackees added a commit that referenced this pull request Jun 14, 2026
…pp definition (#3037)

Addresses two CodeRabbit findings on #3036:

1. **ASCII-only header.** The previous header used Unicode glyphs (->, --,
   <=) in comments. Per the path-specific source-encoding rule for
   `src/fl/channels/rx/decode_ws2812.h`, that header must be 7-bit ASCII.
   Replaced all Unicode characters with ASCII equivalents.

2. **No function definitions in plain .h files.** The previous
   implementation defined `decodeWs2812Edges()` inline in the .h. Per the
   project convention used throughout fl/channels/rx (see channel.h
   <-> channel.cpp.hpp), function definitions go in a .cpp.hpp companion
   compiled exactly once into the FastLED unity build, while the .h
   carries only the declaration. Created
   `src/fl/channels/rx/decode_ws2812.cpp.hpp` and added the include
   to `src/fl/channels/rx/_build.cpp.hpp`.

Behaviour unchanged: this is purely a file-layout split. The two
CodeRabbit findings that flagged latent semantic bugs in the
gap-tolerance ordering and the error-rate denominator are preserved
from the original Native + LPC decoders and explicitly documented in
the header's "Preserved-from-original quirks" block; fixing them is
out of scope for the consolidation PR and tracked under #3035 follow-ups.

Verification:
  * tests/fl_channels_rx_sct_capture (5 cases) -- pass
  * tests/fl_channels_rx (15 cases) -- pass
  * bash lint --cpp -- clean

Refs #3035.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant