Skip to content

feat(channels): add enableDrivers<Bus...>() opt-in registry API (Phase 4 of #2428) - #2437

Merged
zackees merged 1 commit into
masterfrom
feat/channels-enable-drivers
May 10, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/channels-enable-drivers

Conversation

@zackees

@zackees zackees commented May 10, 2026 •

Copy link
Copy Markdown
Member

Summary

Phase 4 of #2428. Adds the opt-in driver-registration API surface ratified in the issue. Pure additive — default behavior unchanged. The legacy initChannelDrivers() auto-registration still runs and registers every available driver as before. Phase 5 will switch the default off and require users to call enableDrivers<...>() explicitly (delivering the binary-bloat fix).

What ships

New: src/fl/channels/bus_priorities.h

Centralizes per-Bus priority constants (PARLIO=4, RMT=2, etc.) so each BusTraits<X>::registerWithManager() reads from a single table rather than duplicating values across per-driver headers and the legacy initChannelDrivers() body. Mirrors the historic values from channel_manager_esp32.cpp.hpp:104-132 so legacy registrations stay byte-identical.

bus_traits.h refactor — 13 driver headers

Each BusTraits<Bus::X> specialization now exposes:

  • static fl::shared_ptr<Driver> instancePtr() — the underlying singleton (lazy-initialized, returns the same shared_ptr on every call).
  • static Driver& instance() — thin reference-returning wrapper around *instancePtr().
  • static void registerWithManager() — calls ChannelManager::addDriver(default_bus_priority(B), instancePtr()).

Pattern is uniform across all 13 driver headers: PARLIO, RMT5, RMT4, SPI, I2S, I2S_SPI, LCD_RGB, LCD_SPI, LCD_CLOCKLESS, UART, FLEX_IO, OBJECT_FLED, BIT_BANG, STUB.

bus_traits.h additions

template<Bus... Buses>
inline void enableDrivers() FL_NOEXCEPT {
    using expand = int[];
    (void)expand{0, detail::bus_register_one<Buses>()...};  // C++11-clean
}

Usage:

#include "platforms/esp/32/drivers/rmt/rmt_5/bus_traits.h"
#include "platforms/esp/32/drivers/parlio/bus_traits.h"
fl::enableDrivers<fl::Bus::RMT, fl::Bus::PARLIO>();

New unit tests in tests/fl/channels/bus_traits.cpp

  • instancePtr() returns a non-null shared_ptr whose dereferenced address matches instance() — confirms both accessors share the same underlying singleton.
  • enableDrivers<Bus::STUB>() registers the stub driver with ChannelManager, and getDriverByName("STUB") returns the BusTraits singleton (identity verified, not just by-name).

Caveat (deferred to Phase 5)

With the legacy auto-init still active, calling enableDrivers<Bus::X>() after startup re-registers the driver by name. The manager replaces by name (its existing behavior), but two singleton instances temporarily exist (the legacy make_shared call vs the BusTraits singleton).

Phase 5 will refactor initChannelDrivers() to delegate to registerWithManager(), unifying the singletons. For Phase 4, this duplication is functionally invisible to users — the manager always returns the most-recently-registered instance.

Verification

  • bash lint clean.
  • bash test --cpp passes 303/303.
  • New bus_traits tests cover instancePtr identity and enableDrivers registration round-trip.

Test plan

  • Host tests pass.
  • enableDrivers<Bus::STUB>() exercises the full registration path on the host build.
  • CI verifies all platforms still build (no behavior change on legacy auto-init path).
  • Phase 5 will exercise enableDrivers as the primary API once auto-init is gated off.

Related

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

Refactor

  • Centralized driver registration and instance management patterns across all supported bus types for improved consistency.
  • Added standardized helpers for driver singleton initialization and channel manager registration.

Tests

  • Added test coverage for the new driver registration infrastructure.

Review Change Stack

…e 4 of #2428)

Adds the opt-in driver-registration API surface ratified in #2428. Phase 4
is purely additive — default behavior unchanged. The legacy
`initChannelDrivers()` auto-registration still runs and registers every
available driver as before. Phase 5 will switch the default off and require
users to call `enableDrivers<...>()` explicitly.

What ships:

- New: src/fl/channels/bus_priorities.h. Centralizes per-Bus priority
  constants (PARLIO=4, RMT=2, etc.) so each `BusTraits<X>::registerWithManager()`
  reads from a single table instead of duplicating values across per-driver
  headers and the legacy initChannelDrivers body. Mirrors the historic
  values in channel_manager_esp32.cpp.hpp so legacy registrations stay
  identical.

- bus_traits.h refactor (13 driver headers): each `BusTraits<Bus::X>`
  specialization now exposes `instancePtr()` returning `fl::shared_ptr<Driver>`
  (the underlying singleton) and a new `registerWithManager()` static method
  that calls `ChannelManager::addDriver(default_bus_priority(B), instancePtr())`.
  `instance()` becomes a thin reference-returning wrapper. Pattern is uniform
  across PARLIO, RMT5, RMT4, SPI, I2S, I2S_SPI, LCD_RGB, LCD_SPI, LCD_CLOCKLESS,
  UART, FLEX_IO, OBJECT_FLED, BIT_BANG, STUB.

- bus_traits.h additions: `template<Bus...> inline void enableDrivers()`
  and a `detail::bus_register_one<B>()` expander helper. C++11-clean (uses
  the `int[]` initializer trick instead of C++17 fold expressions).

- New unit tests in tests/fl/channels/bus_traits.cpp: instancePtr() returns
  a non-null shared_ptr matching instance(); enableDrivers<Bus::STUB>()
  registers the stub driver and the registered shared_ptr identity matches
  the BusTraits singleton.

Caveat (deferred to Phase 5): with the legacy auto-init still active,
calling `enableDrivers<Bus::X>()` after startup re-registers the driver by
name. The manager replaces by name, but two singleton instances temporarily
exist (the legacy make_shared call vs the BusTraits singleton). Phase 5
will refactor `initChannelDrivers()` to delegate to `registerWithManager()`,
unifying the singletons and dropping the duplication.

Verification:
- bash lint clean.
- bash test --cpp passes 303/303.
- New bus_traits tests cover instancePtr identity and enableDrivers
  registration round-trip.

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

coderabbitai Bot commented May 10, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR introduces a centralized driver registration framework. It adds priority constants via default_bus_priority(Bus), a public enableDrivers<Bus...>() API, and systematically refactors all platform BusTraits specializations to expose singleton pointers and registration hooks for unified ChannelManager integration.

Changes

Unified Bus Driver Registration Framework

Layer / File(s) Summary
Priority & Registration API
src/fl/channels/bus_priorities.h, src/fl/channels/bus_traits.h
Introduces constexpr default_bus_priority(Bus) function mapping each bus to a priority constant, and adds public enableDrivers<Bus...>() template that registers multiple buses via bus_register_one<Bus>() helper.
ARM Teensy Bus Drivers
src/platforms/arm/teensy/teensy4_common/drivers/flexio/bus_traits.h, src/platforms/arm/teensy/teensy4_common/drivers/objectfled/bus_traits.h
FlexIO and ObjectFLED BusTraits refactored to expose instancePtr() returning cached shared_ptr<Driver>, delegate instance() to it, and add registerWithManager() for ChannelManager integration.
ESP32 I2S & SPI Drivers
src/platforms/esp/32/drivers/i2s/bus_traits.h, src/platforms/esp/32/drivers/i2s_spi/bus_traits.h
I2S and I2S_SPI BusTraits refactored to centralize singleton via instancePtr(), update instance() and registerWithManager() methods, and include bus priority headers.
ESP32 LCD Drivers
src/platforms/esp/32/drivers/lcd_cam/bus_traits.h, src/platforms/esp/32/drivers/lcd_spi/bus_traits.h
LCD_RGB, LCD_SPI, and LCD_CLOCKLESS BusTraits extract singleton management into instancePtr(), delegate instance access, and wire driver registration with default_bus_priority().
ESP32 Other Buses
src/platforms/esp/32/drivers/parlio/bus_traits.h, src/platforms/esp/32/drivers/rmt/rmt_4/bus_traits.h, src/platforms/esp/32/drivers/rmt/rmt_5/bus_traits.h, src/platforms/esp/32/drivers/spi/bus_traits.h, src/platforms/esp/32/drivers/uart/bus_traits.h
PARLIO, RMT (both variants), SPI, and UART BusTraits specializations refactored consistently: introduce instancePtr() for shared pointer ownership, update instance() delegation, add registerWithManager() hooks with priorities.
Shared & Stub Platform Drivers
src/platforms/shared/bitbang/bus_traits.h, src/platforms/stub/bus_traits.h
BIT_BANG and STUB BusTraits centralize singleton management via instancePtr(), remove SingletonShared dependency (STUB), and wire registration with shared pointers and priorities.
Tests
tests/fl/channels/bus_traits.cpp
Added test cases validating instancePtr() returns non-null pointer aliasing instance(), and enableDrivers<Bus::STUB>() correctly registers driver with matching identity in ChannelManager.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

  • #2428: This PR implements the proposed driver-registration API design, adding default_bus_priority(), enableDrivers<>() helper, and per-Bus BusTraits::registerWithManager()/instancePtr() changes that directly realize the registration framework outlined in that issue.

Possibly related PRs

  • FastLED/FastLED#2429: Related — this PR builds directly on #2429's fl::Bus/BusTraits foundation, extending it with default_bus_priority, enableDrivers/bus_register_one, and adding instancePtr()/registerWithManager() changes to many BusTraits specializations.

Poem

🐰 The buses now hop in line,
Each driver finds its place so fine,
Priority mapped, singleton clean,
Registration's never seen
Such harmony in the channel scene! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 9.72% 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 specifically describes the main change: adding a new opt-in driver-registration API called enableDrivers<Bus...>() as Phase 4 of a larger refactoring effort.
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-enable-drivers

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: 1

🧹 Nitpick comments (1)
src/platforms/shared/bitbang/bus_traits.h (1)

34-36: 🏗️ Heavy lift

Make driver registration idempotent during the Phase 4 transition.

registerWithManager() always calls addDriver(...). While legacy auto-init is still active, later enableDrivers<...>() calls can re-register the same bus and temporarily break singleton identity assumptions. Please add a dedupe/idempotency guard at registration boundary (likely ChannelManager level) before Phase 5.

🤖 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 `@src/platforms/shared/bitbang/bus_traits.h` around lines 34 - 36,
registerWithManager() currently unconditionally calls
ChannelManager::instance().addDriver(...), allowing duplicate registration; make
the registration idempotent by checking for an existing driver before adding.
Either add a contains/hasDriver or addDriverIfAbsent API to ChannelManager and
use it from registerWithManager(), or have addDriver internally deduplicate by
comparing the existing drivers against instancePtr() / Bus::BIT_BANG / priority
(default_bus_priority(Bus::BIT_BANG)) and skip adding if a matching driver
already exists; update registerWithManager() to call the new/updated API (or
rely on addDriver's internal guard) so repeated calls during Phase
4/enableDrivers(...) are no-ops.
🤖 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 `@tests/fl/channels/bus_traits.cpp`:
- Around line 82-85: Tighten the assertion that checks driver count growth:
replace the broad FL_CHECK(after >= before) with a check that enforces the
documented expectation that the count either stays equal or increases by exactly
one; i.e. assert (after == before || after == before + 1). Apply this change in
the test around the enableDrivers/driver-registration check that uses variables
after, before and the FL_CHECK macro (and keeps the subsequent lookup via
mgr.getDriverByName("STUB") unchanged).

---

Nitpick comments:
In `@src/platforms/shared/bitbang/bus_traits.h`:
- Around line 34-36: registerWithManager() currently unconditionally calls
ChannelManager::instance().addDriver(...), allowing duplicate registration; make
the registration idempotent by checking for an existing driver before adding.
Either add a contains/hasDriver or addDriverIfAbsent API to ChannelManager and
use it from registerWithManager(), or have addDriver internally deduplicate by
comparing the existing drivers against instancePtr() / Bus::BIT_BANG / priority
(default_bus_priority(Bus::BIT_BANG)) and skip adding if a matching driver
already exists; update registerWithManager() to call the new/updated API (or
rely on addDriver's internal guard) so repeated calls during Phase
4/enableDrivers(...) are no-ops.
🪄 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: 50ed7520-0b32-4938-8721-b7d87ec8026c

📥 Commits

Reviewing files that changed from the base of the PR and between 19a01a3 and 9bf7164.

📒 Files selected for processing (16)
  • src/fl/channels/bus_priorities.h
  • src/fl/channels/bus_traits.h
  • src/platforms/arm/teensy/teensy4_common/drivers/flexio/bus_traits.h
  • src/platforms/arm/teensy/teensy4_common/drivers/objectfled/bus_traits.h
  • src/platforms/esp/32/drivers/i2s/bus_traits.h
  • src/platforms/esp/32/drivers/i2s_spi/bus_traits.h
  • src/platforms/esp/32/drivers/lcd_cam/bus_traits.h
  • src/platforms/esp/32/drivers/lcd_spi/bus_traits.h
  • src/platforms/esp/32/drivers/parlio/bus_traits.h
  • src/platforms/esp/32/drivers/rmt/rmt_4/bus_traits.h
  • src/platforms/esp/32/drivers/rmt/rmt_5/bus_traits.h
  • src/platforms/esp/32/drivers/spi/bus_traits.h
  • src/platforms/esp/32/drivers/uart/bus_traits.h
  • src/platforms/shared/bitbang/bus_traits.h
  • src/platforms/stub/bus_traits.h
  • tests/fl/channels/bus_traits.cpp

Comment on lines +82 to +85
// After enableDrivers the manager must contain the stub driver. Count
// either grows by one (fresh registration) or stays equal (replace by name).
FL_CHECK(after >= before);
auto stubDriver = mgr.getDriverByName(fl::string::from_literal("STUB"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Tighten the driver-count assertion

On Line 84, after >= before is broader than the documented expectation. Assert after == before || after == before + 1 to catch unintended multi-add behavior.

Proposed test assertion tweak
-    FL_CHECK(after >= before);
+    FL_CHECK(after == before || after == (before + 1));
🤖 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 `@tests/fl/channels/bus_traits.cpp` around lines 82 - 85, Tighten the assertion
that checks driver count growth: replace the broad FL_CHECK(after >= before)
with a check that enforces the documented expectation that the count either
stays equal or increases by exactly one; i.e. assert (after == before || after
== before + 1). Apply this change in the test around the
enableDrivers/driver-registration check that uses variables after, before and
the FL_CHECK macro (and keeps the subsequent lookup via
mgr.getDriverByName("STUB") unchanged).

@zackees
zackees merged commit b0be2b4 into master May 10, 2026
7 of 9 checks passed
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
zackees deleted the feat/channels-enable-drivers 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.

1 participant