Skip to content

feat(channels): unify singletons + opt-in disable macro (Phase 5a of #2428) - #2438

Merged
zackees merged 1 commit into
masterfrom
feat/channels-registry-optin
May 10, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/channels-registry-optin

Conversation

@zackees

@zackees zackees commented May 10, 2026 •

Copy link
Copy Markdown
Member

Summary

Phase 5a of #2428. Two changes, both backward-compatible (default behavior unchanged):

  1. Unify legacy auto-init with the new BusTraits singletons — fixes the duplication caveat documented in Phase 4 (feat(channels): add enableDrivers<Bus...>() opt-in registry API (Phase 4 of #2428) #2437).
  2. Add FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY opt-in macro — sets up Phase 5b (the actual binary-bloat fix). Default 0, no behavior change.

Change 1: Singleton unification

Pre-Phase-5a, addParlioIfPossible() etc. in channel_manager_esp32.cpp.hpp constructed fresh driver instances via fl::make_shared<ChannelDriverPARLIO>(), distinct from the singleton owned by BusTraits<Bus::PARLIO>. Calling fl::enableDrivers<fl::Bus::PARLIO>() after the legacy auto-init created a second PARLIO driver that tried to claim the PARLIO peripheral concurrently.

All 9 addXxxIfPossible() helpers now delegate to BusTraits<Bus::X>::instancePtr(). The legacy and new APIs share the same singleton — mgr.getDriverByName(\"PARLIO\") returns the same shared_ptr whether you reach it via auto-init or explicit enableDrivers.

Same change applied to src/platforms/stub/init_channel_driver.h.

The unified-SPI hardware adapter (SpiHw1 via SpiChannelEngineAdapter) is intentionally not unified — it has no Bus enum value because it wraps a runtime-discovered set of controllers. Stays on its existing path.

Change 2: FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY

When defined to 1, initChannelDrivers() becomes a no-op (on both ESP32 and stub). Users opting in must call fl::enableDrivers<fl::Bus::X...>() explicitly. This is the pathway that lets the linker drop unused driver TUs (#2420 binary-bloat fix). Default 0 — behavior unchanged in this PR.

Phase 5b (next, not in this PR) will

  • Flip the default of FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY to 1.
  • Retarget the legacy addLeds<> clockless path to call BusTraits<DefaultBus<ClocklessChipset>::value>::instance() directly, bypassing ChannelManager::selectDriverForChannel() so no manager drivers need to be registered for legacy sketches.
  • Gate the _build.cpp.hpp driver aggregator behind the same flag so unused driver TUs don't link.
  • Measure binary size on the [Bug] Compounded timing inaccuracy on master compared to 3.10.3 #2420 reproducer (NEOPIXEL on ESP32-S3 — expect ~783 KB → ~344 KB).

New regression test

tests/fl/channels/bus_traits.cpp adds \"Legacy initChannelDrivers() and enableDrivers<Bus::STUB>() share singleton\" — would have failed pre-5a (legacy registered a different instance than the BusTraits singleton). Verifies pointer identity in both directions.

Verification

  • bash lint clean.
  • bash test --cpp passes 303/303.
  • New singleton-identity test passes.
  • Default behavior byte-for-byte unchanged on ESP32 and stub auto-init paths.

Test plan

  • Host tests pass.
  • Singleton-identity test verifies the unification directly.
  • CI verifies all platforms still build and behave identically (default macro 0).
  • Phase 5b will exercise FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY=1 and measure binary size.

Related

🤖 Generated with Claude Code

Summary by CodeRabbit

Release Notes

  • New Features

    • Added opt-in configuration to disable legacy driver auto-registration via new macro.
  • Refactor

    • Unified driver registration system across platform implementations for consistency.
    • Improved driver initialization to use singleton pattern across legacy and modern driver APIs.

Review Change Stack

…2428)

Two changes, both backward-compatible (default behavior unchanged):

1) Unify legacy auto-init with the new BusTraits singletons.

   Pre-Phase-5a, `addParlioIfPossible()` etc. in channel_manager_esp32.cpp.hpp
   constructed fresh driver instances via `fl::make_shared<ChannelDriverPARLIO>()`,
   distinct from the singleton owned by `BusTraits<Bus::PARLIO>`. If a user
   called `fl::enableDrivers<fl::Bus::PARLIO>()` after the legacy auto-init,
   a *second* PARLIO driver was constructed which then tried to claim the
   PARLIO peripheral concurrently. Phase 4 documented this caveat; Phase 5a
   fixes it.

   All 9 `addXxxIfPossible()` helpers now delegate to
   `BusTraits<Bus::X>::instancePtr()`. The legacy and new APIs share the
   same singleton — `mgr.getDriverByName("PARLIO")` returns the same
   `shared_ptr` whether you reach it via auto-init or explicit
   `enableDrivers`. Same change applied to `src/platforms/stub/init_channel_driver.h`.

   The unified-SPI hardware adapter (SpiHw1 via SpiChannelEngineAdapter)
   is intentionally *not* unified -- it has no Bus enum value because it
   wraps a runtime-discovered set of controllers. Stays on its existing
   path.

2) Add `FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY` opt-in macro.

   When defined to 1, `initChannelDrivers()` becomes a no-op (on both ESP32
   and stub). Users opting in must call `fl::enableDrivers<fl::Bus::X...>()`
   explicitly. This is the pathway that lets the linker drop unused driver
   TUs (#2420 binary-bloat fix). Default 0 -- behavior unchanged.

   Phase 5b will:
   - Flip the default to 1.
   - Retarget the legacy `addLeds<>` clockless path to call
     `BusTraits<DefaultBus<ClocklessChipset>::value>::instance()` directly,
     bypassing `ChannelManager::selectDriverForChannel()` so no manager
     drivers need to be registered for legacy sketches.
   - Gate the `_build.cpp.hpp` driver aggregator behind the same flag so
     unused driver TUs don't link.

3) New regression test: tests/fl/channels/bus_traits.cpp adds
   "Legacy initChannelDrivers() and enableDrivers<Bus::STUB>() share singleton"
   which would have failed pre-5a (legacy registered a different instance than
   the BusTraits singleton). Verifies pointer identity in both directions.

Verification:
- bash lint clean.
- bash test --cpp passes 303/303.
- New singleton-identity test passes.
- Default behavior is byte-for-byte unchanged on ESP32 and stub auto-init paths.

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

coderabbitai Bot commented May 10, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR unifies ESP32 and stub channel driver initialization by replacing direct driver construction with BusTraits singleton access. Both platforms now conditionally support disabling legacy auto-registration via FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY macro. Helper functions obtain drivers from BusTraits::instancePtr() instead of calling create*Engine() or constructing instances directly. Tests verify singleton identity across registration paths.

Changes

Driver Registration via BusTraits

Layer / File(s) Summary
Macro & Headers
src/platforms/esp/32/drivers/channel_manager_esp32.cpp.hpp, src/platforms/stub/init_channel_driver.h
Define FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY (default 0) and add bus/bus_traits headers for singleton driver access.
Include Strategy
src/platforms/esp/32/drivers/channel_manager_esp32.cpp.hpp
Switch from directly including concrete driver implementations to conditionally including per-bus bus_traits.h headers guarded by capability macros.
Stub Driver Registration
src/platforms/stub/init_channel_driver.h
Register BusTraits<fl::Bus::STUB>::instancePtr() instead of constructing local static engine.
ESP32 Bus Helpers
src/platforms/esp/32/drivers/channel_manager_esp32.cpp.hpp
Nine per-bus functions now obtain drivers from BusTraits<Bus::...>::instancePtr() instead of create*Engine().
Conditional Initialization
src/platforms/esp/32/drivers/channel_manager_esp32.cpp.hpp
Wrap initChannelDrivers() body with #if !FASTLED_DISABLE_LEGACY_DRIVER_REGISTRY guard; disabled mode becomes no-op with debug log.
Tests
tests/fl/channels/bus_traits.cpp
New test asserts that legacy initChannelDrivers() and explicit enableDrivers API register identical singletons.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

  • FastLED/FastLED#2429: Introduces Bus and BusTraits types; this PR applies them to stub BusTraits specialization and legacy registration paths.
  • FastLED/FastLED#2437: Adds instancePtr()/registerWithManager() and enableDrivers<>() API that this PR now uses in legacy ESP32/stub initialization paths.

Poem

🐰 A driver singleton hops through traits,
No more constructing late,
BusTraits brings the instance state—
Legacy paths unite their fate,
With optional disable, we create.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: unifying singletons and adding an opt-in disable macro, with clear reference to the phase number.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
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-registry-optin

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.

@zackees
zackees merged commit 8e0ef5b into master May 10, 2026
5 of 7 checks passed
@zackees
zackees deleted the feat/channels-registry-optin 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