Skip to content

feat(channels): templated Channel::create<Bus B>() / FastLED.add<Bus B>() (#2428, closes #2167) - #2451

Merged
zackees merged 1 commit into
masterfrom
feat/channels-bus-template-2167-2428
May 11, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/channels-bus-template-2167-2428

Conversation

@zackees

@zackees zackees commented May 11, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds the Bus-templated APIs explicitly called out in issue #2428's Phase 3b and closes #2167 (which asked for `Channel::create(config)` to be a real template). Additive — no breaking changes to existing call sites.

```cpp
#include "platforms/esp/32/drivers/rmt/rmt_5/bus_traits.h"
auto ch = fl::Channel::createfl::Bus::RMT(cfg);
// or:
auto ch = FastLED.addfl::Bus::RMT(cfg);

// Mismatches caught at compile time:
FastLED.addfl::Bus::RTM(cfg); // ❌ no enumerator
FastLED.addfl::Bus::AUTO(cfg); // ❌ static_assert: use non-template form
FastLED.addfl::Bus::SOME_HYPOTHETICAL(cfg); // ❌ no BusSupports specialization
```

How it preserves the binary-bloat fix

The template body does `(void) BusTraits::instancePtr();`. That ODR-uses the singleton's storage, so `--gc-sections` keeps the driver's translation unit. Sketches that only ever name one `Bus::X` link only that one driver — the per-bus version of the fix from #2420 / #2421.

If the user calls `FastLED.addfl::Bus::RMT(cfg)` without including `platforms/esp/32/drivers/rmt/rmt_5/bus_traits.h`, the compile fails with `implicit instantiation of undefined template 'BusTraitsBus::RMT'`. That diagnostic IS the explicit opt-in.

What's in the PR

  • `src/fl/channels/bus.h` — `busName(Bus)` switch + drift-prevention `FL_STATIC_ASSERT` on the last enumerator. Single source of truth for the `Bus → driver getName()` mapping. Includes the "no underscore" trio (`Bus::FLEX_IO` → `"FLEXIO"`, `Bus::OBJECT_FLED` → `"OBJECTFLED"`, `Bus::BIT_BANG` → `"BITBANG"`).
  • `src/fl/channels/channel.h` — `template static ChannelPtr create(ChannelConfig)` with the two `FL_STATIC_ASSERT` guards.
  • `src/FastLED.h` — `template<fl::Bus B> static fl::ChannelPtr add(const fl::ChannelConfig& config)` forwarder to `Channel::create`.
  • `tests/fl/channels/channel.cpp` — host tests for both overloads exercising the stub bus, the affinity-overwrite path, the channel-registration path, and driver-identity match against `BusTraitsBus::STUB::instance()`.

Test plan

  • `bash test channel --cpp` — 1/1 pass.
  • `bash test --cpp` — 181/181 pass.
  • `bash lint --cpp` — clean.
  • CI matrix to validate hardware platform builds (ESP32 / Teensy / etc.) — header-only change.

Issue #2428 remaining items

Per the issue author's status comment, what's left after this merges:

  • `mAffinity` field type conversion from `fl::string` to `fl::Bus` (Phase 4 cleanup).
  • Templatizing `ChannelConfig` and `Channel<Bus, Chipset>` for full compile-time chipset/bus pair checking (the bigger Phase 3b refactor — ~17 files).
  • Phase 5 finale — drop the unconditional `_build.cpp.hpp` driver aggregator.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added compile-time channel-to-bus routing so channels can be pinned to a specific bus, ensuring driver affinity is set deterministically and validated at compile time.
  • Tests

    • Expanded tests to verify bus-pinned channel creation, driver binding, affinity overriding, and integration with the draw list.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 11, 2026 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 792d2ba5-b8c4-4b48-876f-891177a65ea2

📥 Commits

Reviewing files that changed from the base of the PR and between 3349091 and 685da3c.

📒 Files selected for processing (4)
  • src/FastLED.h
  • src/fl/channels/bus.h
  • src/fl/channels/channel.h
  • tests/fl/channels/channel.cpp

📝 Walkthrough

Walkthrough

Adds compile-time bus-pinned channel creation: Channel::create<Bus B> and CFastLED::add<Bus B>, fl::busName() helper, supporting includes/static asserts, and tests verifying affinity overwrite and driver binding.

Changes

Compile-Time Bus-Pinned Channel Creation

Layer / File(s) Summary
Bus Name Utility
src/fl/channels/bus.h
fl::busName(Bus) maps Bus enum values to stable driver-name strings via a switch; adds FL_NOEXCEPT include and FL_STATIC_ASSERT for enum-drift detection.
Channel Template API
src/fl/channels/channel.h
Adds Channel::create<Bus B> template that enforces B != Bus::AUTO, requires BusSupports<B> specialization, ODR-uses BusTraits<B>::instancePtr(), and overwrites config.options.mAffinity with busName(B) before delegating to Channel::create(config).
FastLED Convenience Wrapper
src/FastLED.h
Adds CFastLED::add<Bus B> template overload that calls Channel::create<Bus B>, registers the channel with FastLED on success, and returns the ChannelPtr (or nullptr).
Tests
tests/fl/channels/channel.cpp
Adds test includes and helpers plus four FL_TEST_CASEs that verify Channel::create<Bus::STUB> and FastLED.add<Bus::STUB> create non-null channels, overwrite mAffinity, bind the STUB driver on showLeds, and register channels in the draw list.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#2451: Same code-level changes adding templated Channel::create and CFastLED::add with busName().
  • FastLED/FastLED#2438: Related via shared use of BusTraits::instancePtr() and driver singleton wiring.
  • FastLED/FastLED#2429: Provides foundational Bus enum and BusTraits infrastructure used by these template APIs.

Poem

🐰 A bus is born at compile time,
With template magic, routing sublime,
Channels pinned where drivers dwell,
Static checks that guard them well,
FastLED draws without a care!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% 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 accurately and concisely describes the main addition: templated Channel::create() and FastLED.add() overloads for compile-time bus routing.
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/channels-bus-template-2167-2428

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.

…B>() (#2428, closes #2167)

Issue #2167 asked for `Channel::create<EngineType>(config)` to be a real
template; #2428 generalized that into Phase 3b's compile-time bus selection.
This commit adds both APIs as additive overloads — no breaking change.

  #include "platforms/esp/32/drivers/rmt/rmt_5/bus_traits.h"
  auto ch = fl::Channel::create<fl::Bus::RMT>(cfg);
  // ...or:
  auto ch = FastLED.add<fl::Bus::RMT>(cfg);

The template:
- `static_assert`s `B != Bus::AUTO` (use the non-template form for default)
  and that `BusSupports<B, *>` has a specialization (so unsupported buses
  are a compile-time error rather than a runtime warning).
- ODR-uses `BusTraits<B>::instancePtr()` so `--gc-sections` keeps only the
  named driver's TU — the per-bus binary-bloat fix from #2420 / #2421.
- Derives the affinity string from a compile-time `busName(B)` switch so
  typos (`Bus::RTM`) become "no such enumerator" instead of silent runtime
  fall-through.

`busName(Bus)` is the single source of truth mapping each enum value to the
exact string returned by the driver's `getName()`. Drift is guarded by an
`FL_STATIC_ASSERT` on the last enumerator's value, and at test time by
`ChannelManager::getDriverByName(busName(B))` having to find the singleton.

Tests in `tests/fl/channels/channel.cpp` cover both overloads on the host
stub bus, including the affinity overwrite, the channel-registration path,
and driver-identity match against `BusTraits<Bus::STUB>::instance()`.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees force-pushed the feat/channels-bus-template-2167-2428 branch from 3349091 to 685da3c Compare May 11, 2026 18:11
@zackees
zackees merged commit 3ff64f7 into master May 11, 2026
5 of 6 checks passed
zackees added a commit that referenced this pull request May 11, 2026
) (#2454)

Sister API to the templated FastLED.add<fl::Bus B>(cfg) from #2451. The
templated form ODR-uses BusTraits<B>::instancePtr() at the call site to
link the named driver — that is the binary-bloat fix from #2420/#2421. But
that mode is wrong for sketches that have already called
fl::enableAllDrivers() and want runtime dispatch by enum without the per-
call-site linker keep-alive.

The new non-template overload:

  FastLED.add(fl::Bus::RMT, cfg);

- Sets cfg.options.mAffinity = fl::busName(b) so ChannelManager dispatches
  by name (matches each driver's getName() exactly via the bus.h table).
- Maps fl::Bus::AUTO to "clear affinity" so the manager falls back to
  priority dispatch — same contract as not setting an affinity at all.
- Does NOT name BusTraits<B>::instance, so the driver TU is only linked
  when something else in the build named it (enableAllDrivers, the
  templated form, or a per-driver bus_traits.h include + enableDrivers).

Tests cover:
- Runtime dispatch lands on the named driver (Bus::STUB on host).
- Overload overwrites prior mAffinity strings.
- Bus::AUTO clears affinity and falls through to priority dispatch.

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

The channels README hadn't been updated since #2435 (Phase 3a) while the
real API surface grew through #2437/2438/2441/2448/2449/2450/2451/2454/
2456. This refresh aligns the docs with what the code actually does today.

Reviewed in three independent sub-agent passes:

- Pass 1 (broad accuracy): added the new dual-mode dispatch architecture
  section (compile-time `fl::Bus` binding vs runtime `ChannelManager`),
  the templated `Channel::create<Bus B>(cfg)` / `FastLED.add<Bus B>(cfg)`
  examples, the `ChannelConfigOf<Chipset>` + `TypedChannel<Bus, Chipset>`
  Phase 3b API, the `enableDrivers<>()` / `enableAllDrivers()` opt-in
  registry, the `FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY` binary-size
  macro, the one-shot affinity-miss diagnostic (#2456), and the
  driver-name rename callout (BITBANG -> BIT_BANG, FLEXIO -> FLEX_IO,
  OBJECTFLED -> OBJECT_FLED). Updated the registration example to the
  current 2-arg `addDriver(priority, driver)` signature; the name comes
  from `driver->getName()`. Expanded the Reference section to list the
  new headers (`bus.h`, `bus_traits.h`, `all_drivers.h`, `channel_typed.h`,
  `ichannel.h`) and to include `findDriverByName` / `clearAllDrivers` on
  `manager.h`.

- Pass 2 (independent verification): fixed two real bugs the first pass
  left in place. (a) DMA wait-pattern snippet referenced a nonexistent
  `EngineState::READY` — corrected to `IChannelDriver::DriverState::READY`.
  (b) "Overriding Engine Selection" examples called a nonexistent
  `FastLED.setDriverPriority(...)` — the method lives only on
  `ChannelManager::setDriverPriority`; rewrote both call sites to
  `fl::ChannelManager::instance().setDriverPriority(...)` with an
  explanatory comment about the API asymmetry vs the
  `setDriverEnabled` / `setExclusiveDriver` helpers that DO have
  CFastLED forwarders.

- Pass 3 (polish): added missing file-scope `CRGB leds[60];` declarations
  to two example snippets that referenced `leds` without anchoring it.
  Inserted an "alternatives, not a sequence" clarifier above the three
  Override Methods so a careful reader doesn't think calling all three in
  order is intended. Verified the `Bus::AUTO` story end-to-end:
  `FastLED.add<fl::Bus::AUTO>(cfg)` never appears as an example (the
  static_assert from #2451 rejects it); only `FastLED.add(cfg)` (implicit
  default via `ChannelConfigOf`) and `FastLED.add(fl::Bus::AUTO, cfg)`
  (runtime form, clears affinity) are shown.

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

Closes the binary-bloat half of #2421 by making the previously opt-in
"no driver auto-registration" behavior the default and removing the
macro entirely. Drivers now link only when their `BusTraits<Bus::X>::instance`
is named via:

- Legacy `addLeds<>` Phase 5b pre-bind (platform-default driver)
- `addLeds<..., fl::Bus::X>` trailing template parameter (#2460)
- `fl::TypedChannel<Bus::X, Chipset>::create(cfg)` (#2451)
- `fl::enableDrivers<fl::Bus::X, ...>()` opt-in
- `FastLED.enableAllDrivers()` (3.10.3-style universal enrollment)
- `FastLED.setExclusiveDriver<fl::Bus::X>()` (NEW template overload)

Stock `examples/Blink/Blink.ino` on ESP32-S3 drops from ~776 KB to
657 KB with no source change or build flag needed. Only `ChannelEngineRMT`
(the platform default) is linked; UART / LCD_SPI / LCD_CLOCKLESS /
I2S / SPI driver TUs are all garbage-collected.

`setExclusiveDriver` is now also exposed as a template overload that
links + registers a non-default driver at priority above the platform
default. The string-form `setExclusiveDriver(const char*)` is kept for
mock/test scenarios (drivers already registered with the manager that
aren't in the `fl::Bus` enum).

Test harness changes:
- `tests/doctest_main.cpp` calls `fl::enableAllDrivers()` at startup so
  unit tests that depend on runtime driver dispatch keep working.
- `tests/fastled_core.cpp` "Legacy API 4 parallel strips" test rewritten
  to use the Channel API (`FastLED.add(cfg)`) instead of the legacy
  `addLeds<>` path; the legacy path now pre-binds to the platform
  default and bypasses the manager, so the mock-driver pattern only
  works via Channel API.
- `tests/platforms/shared/active_strip_data` updated to probe the
  `BusTraits<Bus::STUB>::instancePtr()` singleton directly rather than
  expecting auto-registration.
- `examples/AutoResearch/AutoResearch.ino` calls
  `FastLED.enableAllDrivers()` in `setup()` for its driver-enumeration
  flow.

Closes #2421 (binary-bloat half). Refs #2428.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
zackees added a commit that referenced this pull request May 12, 2026
…default-as-opt-in) (#2469)

Closes the binary-bloat half of #2421 by making the previously opt-in
"no driver auto-registration" behavior the default and removing the
macro entirely. Drivers now link only when their `BusTraits<Bus::X>::instance`
is named via:

- Legacy `addLeds<>` Phase 5b pre-bind (platform-default driver)
- `addLeds<..., fl::Bus::X>` trailing template parameter (#2460)
- `fl::TypedChannel<Bus::X, Chipset>::create(cfg)` (#2451)
- `fl::enableDrivers<fl::Bus::X, ...>()` opt-in
- `FastLED.enableAllDrivers()` (3.10.3-style universal enrollment)
- `FastLED.setExclusiveDriver<fl::Bus::X>()` (NEW template overload)

Stock `examples/Blink/Blink.ino` on ESP32-S3 drops from ~776 KB to
657 KB with no source change or build flag needed. Only `ChannelEngineRMT`
(the platform default) is linked; UART / LCD_SPI / LCD_CLOCKLESS /
I2S / SPI driver TUs are all garbage-collected.

`setExclusiveDriver` is now also exposed as a template overload that
links + registers a non-default driver at priority above the platform
default. The string-form `setExclusiveDriver(const char*)` is kept for
mock/test scenarios (drivers already registered with the manager that
aren't in the `fl::Bus` enum).

Test harness changes:
- `tests/doctest_main.cpp` calls `fl::enableAllDrivers()` at startup so
  unit tests that depend on runtime driver dispatch keep working.
- `tests/fastled_core.cpp` "Legacy API 4 parallel strips" test rewritten
  to use the Channel API (`FastLED.add(cfg)`) instead of the legacy
  `addLeds<>` path; the legacy path now pre-binds to the platform
  default and bypasses the manager, so the mock-driver pattern only
  works via Channel API.
- `tests/platforms/shared/active_strip_data` updated to probe the
  `BusTraits<Bus::STUB>::instancePtr()` singleton directly rather than
  expecting auto-registration.
- `examples/AutoResearch/AutoResearch.ino` calls
  `FastLED.enableAllDrivers()` in `setup()` for its driver-enumeration
  flow.

Closes #2421 (binary-bloat half). Refs #2428.

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@zackees
zackees deleted the feat/channels-bus-template-2167-2428 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.

FastLED Channels API

1 participant