Repository navigation
feat(channels): add enableDrivers<Bus...>() opt-in registry API (Phase 4 of #2428) - #2437
Conversation
…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]>
📝 WalkthroughWalkthroughThis PR introduces a centralized driver registration framework. It adds priority constants via ChangesUnified Bus Driver Registration Framework
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/platforms/shared/bitbang/bus_traits.h (1)
34-36: 🏗️ Heavy liftMake driver registration idempotent during the Phase 4 transition.
registerWithManager()always callsaddDriver(...). While legacy auto-init is still active, laterenableDrivers<...>()calls can re-register the same bus and temporarily break singleton identity assumptions. Please add a dedupe/idempotency guard at registration boundary (likelyChannelManagerlevel) 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
📒 Files selected for processing (16)
src/fl/channels/bus_priorities.hsrc/fl/channels/bus_traits.hsrc/platforms/arm/teensy/teensy4_common/drivers/flexio/bus_traits.hsrc/platforms/arm/teensy/teensy4_common/drivers/objectfled/bus_traits.hsrc/platforms/esp/32/drivers/i2s/bus_traits.hsrc/platforms/esp/32/drivers/i2s_spi/bus_traits.hsrc/platforms/esp/32/drivers/lcd_cam/bus_traits.hsrc/platforms/esp/32/drivers/lcd_spi/bus_traits.hsrc/platforms/esp/32/drivers/parlio/bus_traits.hsrc/platforms/esp/32/drivers/rmt/rmt_4/bus_traits.hsrc/platforms/esp/32/drivers/rmt/rmt_5/bus_traits.hsrc/platforms/esp/32/drivers/spi/bus_traits.hsrc/platforms/esp/32/drivers/uart/bus_traits.hsrc/platforms/shared/bitbang/bus_traits.hsrc/platforms/stub/bus_traits.htests/fl/channels/bus_traits.cpp
| // 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")); |
There was a problem hiding this comment.
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).
…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]>
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 callenableDrivers<...>()explicitly (delivering the binary-bloat fix).What ships
New:
src/fl/channels/bus_priorities.hCentralizes 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 legacyinitChannelDrivers()body. Mirrors the historic values fromchannel_manager_esp32.cpp.hpp:104-132so legacy registrations stay byte-identical.bus_traits.hrefactor — 13 driver headersEach
BusTraits<Bus::X>specialization now exposes:static fl::shared_ptr<Driver> instancePtr()— the underlying singleton (lazy-initialized, returns the sameshared_ptron every call).static Driver& instance()— thin reference-returning wrapper around*instancePtr().static void registerWithManager()— callsChannelManager::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.hadditionsUsage:
New unit tests in
tests/fl/channels/bus_traits.cppinstancePtr()returns a non-nullshared_ptrwhose dereferenced address matchesinstance()— confirms both accessors share the same underlying singleton.enableDrivers<Bus::STUB>()registers the stub driver withChannelManager, andgetDriverByName("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 legacymake_sharedcall vs the BusTraits singleton).Phase 5 will refactor
initChannelDrivers()to delegate toregisterWithManager(), unifying the singletons. For Phase 4, this duplication is functionally invisible to users — the manager always returns the most-recently-registered instance.Verification
bash lintclean.bash test --cpppasses 303/303.instancePtridentity andenableDriversregistration round-trip.Test plan
enableDrivers<Bus::STUB>()exercises the full registration path on the host build.enableDriversas the primary API once auto-init is gated off.Related
🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
Refactor
Tests