Skip to content

feat(channels): non-template FastLED.add() + typed mBus + remove mAffinity (closes #2459) - #2461

Merged
zackees merged 1 commit into
masterfrom
feat/channels-non-template-add-2459
May 11, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/channels-non-template-add-2459

Conversation

@zackees

@zackees zackees commented May 11, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #2459. Collapses the Channel API's compile-time selection layer to one template entry point and one runtime entry point, per the SSOT direction from the design discussion.

Before (three templates + a runtime overload + a string field):

After (one of each):

  • `fl::TypedChannel<B, Chipset>::create(cfg)` — the single template entry point (already existed; kept).
  • `FastLED.add(cfg)` — the single runtime entry point. Non-template. Reads `cfg.options.mBus`.
  • `ChannelOptions::mBus` (`fl::Bus` enum) — typed, the only affinity field.

Behaviour changes

Non-template `FastLED.add(cfg)`

  • Auto-enrolls every driver on the platform via `fl::enableAllDrivers()` so any runtime `cfg.options.mBus` value can be dispatched.

  • Emits one-shot `FL_WARN_ONCE` explaining the binary-size trade-off:

    FastLED.add(cfg): runtime-selection mode — enrolling every available driver via fl::enableAllDrivers(). For minimum binary size, prefer FastLED.addLeds<CHIPSET, PIN, ORDER, fl::Bus::X>(leds, n) which links only the named driver. Suppress this warning with -DFASTLED_SUPPRESS_RUNTIME_DRIVER_WARNING.

  • Suppress macro: `-DFASTLED_SUPPRESS_RUNTIME_DRIVER_WARNING`.

Typed `mBus` dispatch

  • `cfg.options.mBus = fl::Bus::RMT` pins the channel to the RMT driver. The dispatch path calls `ChannelManager::findDriverByName(busName(mBus))`.
  • `cfg.options.mBus = fl::Bus::AUTO` (default) falls through to `ChannelManager` priority dispatch.
  • On miss, one-shot `FL_ERROR` lists the two currently-shipping remediations: `fl::enableDriversfl::Bus::X()` or `FastLED.enableAllDrivers()`. The third remediation (`addLeds<..., fl::Bus::X>`) is deferred to Channel API: legacy FastLED.addLeds<..., fl::Bus B> trailing template parameter #2460.

Removed APIs

  • `Channel::create(ChannelConfig)` (template) — superseded by `TypedChannel<B, Chipset>::create()`.
  • `FastLED.add(ChannelConfig)` (template) — superseded by `FastLED.add(cfg)` with `mBus = B`.
  • `FastLED.add<B = AUTO, Chipset>(ChannelConfigOf)` (template) — superseded by `TypedChannel<B, Chipset>::create()`.
  • `FastLED.add(Bus, ChannelConfig)` (runtime) — superseded by `FastLED.add(cfg)` with `mBus = B`.
  • `ChannelOptions::mAffinity` (string field) — superseded by `mBus`.
  • `fl::isKnownBusName(string)` (predicate) — no longer needed; dispatch only sees typed-Bus misses now.

Custom / mock driver binding

The pre-PR pattern was `opts.mAffinity = "MOCK"; FastLED.add(cfg)`. With no string field, custom drivers bind by:

  1. Priority dispatch — `mgr.clearAllDrivers(); mgr.addDriver(prio, mock); FastLED.add(cfg)` — the mock is the only candidate, priority dispatch picks it.
  2. `setExclusiveDriver` — when multiple competing drivers are registered, `mgr.setExclusiveDriver("MOCK")` disables all others.

Tests

  • 11 `fastled_core` test cases + 14 channel/driver test cases had `mAffinity = "..."` lines deleted.
  • Two `channel.cpp` tests that explicitly verified the `mAffinity`/`mBus` precedence behaviour were deleted (no precedence anymore).
  • Two more were rewritten to use `mBus` only.
  • One `fastled_core` low-priority-binding test lost half its coverage; documented as a gap that a future `clearExclusiveDriver()` addition can restore.
  • `examples/AutoResearch/AutoResearchRemote.cpp` converted its string driver-name lookup to an inline `fl::Bus` mapping.

Diff stats

```
13 files changed, 189 insertions(+), 501 deletions(-)
```

Net -312 lines — the simplification is real.

Test plan

  • `bash test --cpp` — 304/304 pass.
  • `bash lint --cpp` — clean.
  • CI matrix to validate hardware platform builds.

Related

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved channel driver selection with typed bus identification instead of string-based matching.
    • Added one-time warning when using runtime driver selection.
  • API Changes

    • Removed compile-time bus selection template overloads from Channel API.
    • Updated channel configuration to use typed bus selection field.
  • Documentation

    • Updated Channels API documentation to reflect typed bus selection model and driver dispatch behavior.
  • Tests

    • Updated test suite to align with new typed bus selection approach.

Review Change Stack

…inity (closes #2459)

Collapses the Channel API's compile-time selection layer per the design
direction in #2459. Previously there were THREE templated entry points doing
similar work (Channel::create<B>, FastLED.add<B>, FastLED.add<B,Chipset>),
plus a runtime FastLED.add(Bus, cfg), plus a stringly-typed mAffinity field
on ChannelOptions. After this PR:

  * FastLED.add(cfg) is the ONLY user-facing add overload. Non-template.
  * cfg.options.mBus (fl::Bus enum, default Bus::AUTO) selects the driver.
  * fl::TypedChannel<B, Chipset>::create(cfg) is the SINGLE template entry
    point for compile-time link-and-bind (the addLeds<..., fl::Bus B>
    trailing-template-arg path is planned in #2460).
  * mAffinity string field on ChannelOptions is gone.

Production changes:

  * src/fl/channels/options.h - delete mAffinity, add mBus (fl::Bus).
  * src/fl/channels/channel.h / .cpp.hpp - drop mAffinity member; add mBus
    + mBusWarned; delete templated Channel::create<B>(); simplify dispatch
    to read mBus only.
  * src/fl/channels/bus.h - drop the now-unused isKnownBusName predicate
    (since dispatch only sees typed-Bus misses, the message is always the
    actionable hint - no need to distinguish "third-party name" branch).
  * src/FastLED.h - delete the three templated add<> overloads (templated
    add<B>, templated add<B,Chipset>(ChannelConfigOf), and runtime
    add(Bus, cfg)). Document mBus in the FastLED.enableAllDrivers() example.
  * src/FastLED.cpp.hpp - non-template FastLED.add(cfg) now auto-calls
    fl::enableAllDrivers() so any runtime mBus value can dispatch; emits
    one-shot FL_WARN_ONCE explaining the binary-size trade-off. Suppress
    with -DFASTLED_SUPPRESS_RUNTIME_DRIVER_WARNING.

Diagnostic message: the typed-Bus miss in Channel::showPixels still emits
the one-shot FL_ERROR with the two currently-shipping remediations
(enableDrivers<Bus::X>() and enableAllDrivers()). The third remediation
(addLeds<..., Bus::X>) is deferred to #2460 because it requires touching
23 legacy addLeds template variants and a design choice between the
Channel-API route vs the legacy-CLEDController pre-bind route.

Tests:

  * 11 fastled_core tests + 3 channel tests + 11 driver tests + 2 spi
    tests had mAffinity = "MOCK_X" lines deleted. Each test that registers
    a single mock now relies on priority dispatch (mock is the only
    registered driver after clearAllDrivers); tests that need to force a
    specific driver among several use setExclusiveDriver(name).
  * Two channel.cpp tests testing the dual mAffinity/mBus precedence got
    deleted; two got rewritten to use mBus only.
  * One fastled_core test (low-priority-binding via affinity) lost half
    its coverage - the setExclusiveDriver path persists state between
    tests and breaks the next case. Captured as a documented coverage
    gap. A future clearExclusiveDriver() addition can restore it.
  * examples/AutoResearch/AutoResearchRemote.cpp converts its string
    driver-name lookup to an inline fl::Bus mapping.

Linker contract: nothing changed at the binary-bloat layer. enableDrivers
and addLeds<...> remain the only compile-time linker-keep-alive paths;
FastLED.add(cfg) explicitly trades binary size for ergonomics by
auto-enabling all drivers (the FL_WARN_ONCE makes this trade-off visible).

Verified: 304/304 cpp tests pass, bash lint clean.

Closes #2459. Follow-up: #2460 (addLeds<..., fl::Bus B>).

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

coderabbitai Bot commented May 11, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR migrates the FastLED channel driver selection model from string-based ChannelOptions::mAffinity to a typed fl::Bus enum field (mBus). It removes compile-time Bus-templated API overloads, refactors runtime dispatch logic with one-shot bus-miss diagnostics, and updates documentation and tests throughout.

Changes

String mAffinity to Typed mBus Migration

Layer / File(s) Summary
ChannelOptions Bus-typed field
src/fl/channels/options.h
Replace mAffinity (fl::string) with mBus (fl::Bus enum, default AUTO); add bus.h include and remove string.h; expand docs for typed driver selection.
Bus helper documentation
src/fl/channels/bus.h
Update busName(Bus) documentation to explain Bus::AUTO and driver-name lookup via ChannelOptions::mBus; remove string.h include.
Channel member state & includes
src/fl/channels/channel.h
Replace mAffinity/mAffinityWarned with mBus/mBusWarned; reorder includes for ichannel.h/options.h/shared_ptr.h; remove compile-time Channel::create<Bus B>() template and bus_traits.h.
Channel constructors using mBus
src/fl/channels/channel.cpp.hpp
Update ChipsetVariant+span and deprecated pin+timing constructors to initialize mBus from ChannelOptions instead of mAffinity.
showPixels() driver dispatch with bus-based selection
src/fl/channels/channel.cpp.hpp
Refactor showPixels() to compute busKey from mBus (when not AUTO), select driver via ChannelManager::selectDriverForChannel(busKey), probe findDriverByName(busKey) to distinguish registration vs canHandle miss, emit one-shot mBusWarned diagnostic, then fallback to AUTO/priority dispatch.
CFastLED.add() API simplification
src/FastLED.h, src/FastLED.cpp.hpp
Remove template<Bus B> add(ChannelConfig), add(Bus b, ChannelConfig), and template<Bus B, Chipset> add(ChannelConfigOf) overloads; add FL_WARN_ONCE diagnostic and fl::enableAllDrivers() to non-template runtime path; update doc example from mAffinity string to typed mBus.
README dispatch modes and examples
src/fl/channels/README.md
Rewrite dispatch modes section; update compile-time and runtime selection examples; refresh busName reference and affinity/bus diagnostics guidance.
AutoResearchRemote driver mapping
examples/AutoResearch/AutoResearchRemote.cpp
Replace per-driver mAffinity assignment with explicit Bus enum mapping (RMT, PARLIO, SPI, I2S, LCD_*, UART) via switch logic; unmapped names fallback to AUTO.
fastled_core.cpp test cleanup
tests/fastled_core.cpp
Remove explicit mAffinity assignments from 12 mock/event-driven test cases; simplify affinity-priority test to focus on empty-affinity/priority behavior; rely on default priority-based binding.
channel.cpp typed mBus tests
tests/fl/channels/channel.cpp
Remove mAffinity from byte-order tests; add mBus=Bus::STUB and Bus::AUTO dispatch tests; update bus-miss diagnostic test to use mBus=fl::Bus::RMT with one-shot suppression; remove unknown-affinity fallback test.
channel_typed.cpp and driver tests
tests/fl/channels/channel_typed.cpp, tests/fl/channels/driver.cpp, tests/fl/channels/spi_channel.cpp
Add TypedChannel and runtime mBus tests; remove all mAffinity string assignments across 13 test setups; channels now use priority-based binding.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related issues

  • FastLED/FastLED#2459: This PR directly implements the core objective: replace string-based ChannelOptions::mAffinity with typed fl::Bus mBus, remove templated Bus-based FastLED.add<>() overloads, and migrate to non-template FastLED.add(cfg) with runtime enableAllDrivers() and one-shot bus-miss diagnostics.

Possibly related PRs

  • FastLED/FastLED#2448: Introduces fl::enableAllDrivers() and CFastLED::enableAllDrivers() that this PR invokes from the runtime driver-selection path.
  • FastLED/FastLED#2456: Modifies the same driver-dispatch layer (bus.h, Channel::showPixels, ChannelManager diagnostics) with one-shot warnings and bus-name handling that this PR refactors from string to Bus enum.
  • FastLED/FastLED#2458: Updates src/fl/channels/README.md with the new fl::Bus-based dispatch model and dual-mode (compile-time vs runtime) API documentation alongside this PR.

Poem

A string once held the driver's name,
But types now guide us toward the flame—
mBus in enum, dispatch runs clear,
No template fog, the path is here! 🚀
One warning when the driver's gone,
Auto-fallback carries on.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% 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 PR title accurately describes the primary change: introducing non-template FastLED.add(), replacing string-based mAffinity with typed mBus, and removing the old mAffinity field, directly addressing issue #2459.
Linked Issues check ✅ Passed The PR fully implements the required objectives from #2459: replaces ChannelOptions::mAffinity with typed fl::Bus mBus, removes templated FastLED.add overloads, provides non-template FastLED.add(cfg) with runtime driver selection, emits appropriate diagnostics on bus misses, updates documentation, and maintains full test suite passing.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the objectives in #2459: updating Channel API, ChannelOptions, internal dispatch logic, test files to accommodate mBus migration, and documentation. No unrelated refactoring or feature additions were introduced.

✏️ 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/channels-non-template-add-2459

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.

🧹 Nitpick comments (1)
examples/AutoResearch/AutoResearchRemote.cpp (1)

653-669: ⚡ Quick win

Consider refactoring the manual string-to-Bus mapping.

The manual if-else chain mapping driver names to fl::Bus enum values duplicates knowledge that already exists in the busName() helper. This creates a maintenance burden — if the fl::Bus enum is extended, this code must be updated manually.

A more maintainable approach would be to iterate through all fl::Bus enum values and use busName() to match the driver name, eliminating the duplication.

♻️ Proposed refactor using busName() iteration
             fl::ChannelOptions opts;
-            {
-                const fl::string& n = de.name;
-                if      (n == "RMT")           opts.mBus = fl::Bus::RMT;
-                else if (n == "PARLIO")        opts.mBus = fl::Bus::PARLIO;
-                else if (n == "SPI")           opts.mBus = fl::Bus::SPI;
-                else if (n == "I2S")           opts.mBus = fl::Bus::I2S;
-                else if (n == "I2S_SPI")       opts.mBus = fl::Bus::I2S_SPI;
-                else if (n == "LCD_RGB")       opts.mBus = fl::Bus::LCD_RGB;
-                else if (n == "LCD_SPI")       opts.mBus = fl::Bus::LCD_SPI;
-                else if (n == "LCD_CLOCKLESS") opts.mBus = fl::Bus::LCD_CLOCKLESS;
-                else if (n == "UART")          opts.mBus = fl::Bus::UART;
-                else if (n == "FLEX_IO")       opts.mBus = fl::Bus::FLEX_IO;
-                else if (n == "OBJECT_FLED")   opts.mBus = fl::Bus::OBJECT_FLED;
-                else if (n == "BIT_BANG")      opts.mBus = fl::Bus::BIT_BANG;
-                else if (n == "STUB")          opts.mBus = fl::Bus::STUB;
-                // else: leave Bus::AUTO; priority dispatch will pick.
-            }
+            // Iterate Bus enum values and match against busName()
+            opts.mBus = fl::Bus::AUTO;  // default
+            for (fl::u8 b = static_cast<fl::u8>(fl::Bus::RMT); 
+                 b <= static_cast<fl::u8>(fl::Bus::STUB); ++b) {
+                fl::Bus bus = static_cast<fl::Bus>(b);
+                if (de.name == fl::busName(bus)) {
+                    opts.mBus = bus;
+                    break;
+                }
+            }

This eliminates 17 lines and automatically handles future Bus enum additions.

🤖 Prompt for 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.

In `@examples/AutoResearch/AutoResearchRemote.cpp` around lines 653 - 669, The
long if-else chain mapping de.name to opts.mBus duplicates fl::Bus knowledge;
replace it by iterating over fl::Bus enum values, calling busName(busVal) and
comparing to the string n (de.name), and set opts.mBus to the matching busVal
(leave Bus::AUTO if none match); use the existing helper busName() to drive the
mapping so future fl::Bus additions are handled automatically — update the block
around de.name/opts.mBus to perform this enum iteration and comparison instead
of the manual checks.
🤖 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.

Nitpick comments:
In `@examples/AutoResearch/AutoResearchRemote.cpp`:
- Around line 653-669: The long if-else chain mapping de.name to opts.mBus
duplicates fl::Bus knowledge; replace it by iterating over fl::Bus enum values,
calling busName(busVal) and comparing to the string n (de.name), and set
opts.mBus to the matching busVal (leave Bus::AUTO if none match); use the
existing helper busName() to drive the mapping so future fl::Bus additions are
handled automatically — update the block around de.name/opts.mBus to perform
this enum iteration and comparison instead of the manual checks.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: bcb83261-7b55-4dcd-b7a5-8a4d9d43cae8

📥 Commits

Reviewing files that changed from the base of the PR and between 0c6adf5 and e89c231.

📒 Files selected for processing (13)
  • examples/AutoResearch/AutoResearchRemote.cpp
  • src/FastLED.cpp.hpp
  • src/FastLED.h
  • src/fl/channels/README.md
  • src/fl/channels/bus.h
  • src/fl/channels/channel.cpp.hpp
  • src/fl/channels/channel.h
  • src/fl/channels/options.h
  • tests/fastled_core.cpp
  • tests/fl/channels/channel.cpp
  • tests/fl/channels/channel_typed.cpp
  • tests/fl/channels/driver.cpp
  • tests/fl/channels/spi_channel.cpp
💤 Files with no reviewable changes (2)
  • tests/fl/channels/spi_channel.cpp
  • tests/fl/channels/driver.cpp

@zackees
zackees merged commit 4afad42 into master May 11, 2026
7 of 9 checks passed
zackees added a commit that referenced this pull request May 11, 2026
After #2461 merged (non-template FastLED.add + typed mBus + mAffinity
removal), four small spots in the channels README still described the old
shape. This pass aligns the wording with what shipped:

  * "Passing fl::Bus::AUTO clears the affinity" -> "skips the pinning step"
    (matches the now-typed mBus field, not the deleted mAffinity string).
  * Dispatch-flow bullet list under "Engine selection" rewritten to show
    that Channel::showPixels derives the bus key from cfg.options.mBus via
    busName(mBus) before passing it to selectDriverForChannel. Fixed the
    "Affinity-miss diagnostic" cross-reference to match the actual section
    title "Bus-miss diagnostic".
  * Reference -> channel.h line dropped the false "non-template create(cfg)
    and templated create<Bus B>(cfg) factories" claim. Only the non-
    template form survives; compile-time bus pinning lives on TypedChannel.
  * Reference -> options.h line replaced "affinity" with "mBus" and added
    "rgbw" to the field summary.

No code changes; README only. bash lint clean.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
zackees added a commit that referenced this pull request May 12, 2026
) (#2465)

Adds an optional trailing `fl::Bus B = fl::Bus::AUTO` template parameter
to every `FastLED.addLeds<>` variant. `B = AUTO` (the default) leaves
existing call sites byte-for-byte unchanged; `B != AUTO` ODR-uses
`fl::BusTraits<B>::instance` via a new `fl::busKeepAlive<B>()` helper so
`--gc-sections` retains the named driver TU.

For the `FASTLED_SPI_USES_CHANNEL_API` SPI variants (which already
construct a `ChannelConfig`), the body also sets
`config.options.mBus = B` so the channel routes through the named
driver at runtime. Legacy non-Channel-API controllers keep their
platform-default routing and rely on linker keep-alive alone — for
full runtime routing through a specific Bus, prefer `FastLED.add(cfg)`
with `cfg.options.mBus = B`.

The runtime miss diagnostic in `channel.cpp.hpp` now lists three
remediations: `fl::enableDrivers<>()`, `FastLED.enableAllDrivers()`,
and the new `FastLED.addLeds<..., fl::Bus::X>(...)` shape.

Tests cover the helper plus one clockless (WS2812) and one SPI
(APA102) variant in `tests/fl/channels/bus_traits.cpp`, with default-
AUTO regression guards.

Verification:
- bash test --cpp: 303/303 host tests pass
- bash lint: clean
- bash compile uno --examples Blink: AVR C++11 path compiles
- ESP32 platform compile blocked by unrelated toolchain cache
  breakage (missing Arduino.h / WString.h / soc_caps.h in the
  framework-arduinoespressif32 cache); CHANNEL_API SPI path body
  change is textually a single-line write to the existing
  `mBus` field added in #2461

Closes #2460. Refs #2459, #2428.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees deleted the feat/channels-non-template-add-2459 branch May 30, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Channel API: collapse to non-template FastLED.add(), Bus-typed affinity, addLeds<..., fl::Bus> trailing parameter

1 participant