FastLED 3.10.10
Loading...
Searching...
No Matches
slim_spi_bridge_controller.h
Go to the documentation of this file.
1
40
41#pragma once
42
43#include "fl/stl/noexcept.h"
45
47#include "fl/channels/data.h"
48#include "fl/channels/driver.h"
49#include "fl/channels/manager.h"
50#include "fl/channels/bus.h"
51#include "fl/channels/bus_traits.h" // IWYU pragma: keep (DriverTraits<Bus::X> satisfies the DriverTraits contract)
52#include "fl/channels/channel_typed.h" // IWYU pragma: keep (detail::resolve_bus, BusSupports)
53#include "fl/channels/config.h"
55#include "fl/log/log.h"
56
57namespace fl {
58#if defined(FL_IS_ESP32)
59namespace platforms {
60void registerNativeSpiDriver() FL_NO_EXCEPT;
61}
62#endif
63
78template <fl::SpiChipset CHIPSET, EOrder RGB_ORDER, fl::Bus B, fl::u8 B_WHICH>
80public:
84
85 // Compile-time contract, mirrors TypedChannel<B, SpiChipsetConfig, Which>.
88 "SlimSpiBridgeController: Bus does not support SpiChipsetConfig");
89
91#if defined(FL_IS_ESP32)
92 : mExplicitSpiHost(cfg.spiBus != Esp32SpiBus::AUTO)
93#endif
94 {
96#if defined(FL_IS_ESP32)
97 // The explicit-host addLeds() overload registers the native SPI
98 // driver itself; referencing it here would link the ESP-IDF SPI
99 // master stack (~20 KB) into every AUTO-host sketch.
100 if (mExplicitSpiHost) {
101 return;
102 }
103#endif
104 // Idempotent by contract -- safe to call from every instantiation's
105 // constructor (mirrors TypedChannel::create()'s registerWithManager() call).
107 }
108
109 void init() FL_NO_EXCEPT override {}
110
112 const ChannelDataPtr& channelData() const FL_NO_EXCEPT { return mData; }
113
114protected:
117 if (!driver) {
118 return;
119 }
120 // (A1) Respect runtime driver enable/disable: ChannelManager is the
121 // single source of truth. A disabled driver means "drop this frame",
122 // not "enqueue and hope" -- silently enqueuing to a disabled driver
123 // is the #2517 silent-drop failure mode.
124 if (!ChannelManager::registry().isDriverEnabled(*driver)) {
125 FL_WARN_ONCE("SlimSpiBridgeController: driver '" << driver->getName() << "' is disabled - dropping frame");
126 return;
127 }
128
129 // (A8) If the previous frame's buffer is still in flight, block
130 // (bounded) until the driver reports it can accept more work rather
131 // than racing the driver's in-progress transfer.
132 if (mData->isInUse()) {
133 if (!driver->waitForReady()) {
134 FL_WARN_ONCE("SlimSpiBridgeController: driver '" << driver->getName() << "' did not become ready in time - dropping frame");
135 return;
136 }
137 }
138
139 // (A5) Publish the pixel byte layout so the driver (and any
140 // downstream padding/diagnostic code) knows the encoded stride.
141 if (this->getRgbww().active()) {
142 mData->setPixelFormat(ChannelPixelFormat::RGBWW);
143 } else if (this->getRgbw().active()) {
144 mData->setPixelFormat(ChannelPixelFormat::RGBW);
145 } else {
146 mData->setPixelFormat(ChannelPixelFormat::RGB);
147 }
148
149 // Dither reseed mirrors Channel::encodeFrame(): only reseed when a
150 // non-zero dither mode is set.
151 if (pixels.ditherActive()) {
152 pixels.reseed_binary_dithering(mDitherPhase);
153 }
154
155 // (A3) Re-encode into the same buffer every frame -- clear() keeps
156 // capacity, so steady-state operation is allocation-free.
157 mData->getData().clear();
158 fl::PixelIterator it(&pixels, this->getRgbw(), this->getRgbww());
159 encode(it, mData->getData());
160
161 driver->enqueue(mData);
162 // Presented, per the driver contract: an accepted enqueue is the
163 // frame -- mirrors Channel::submitFrame()'s ++mDitherPhase placement
164 // (dither only advances when a frame is actually handed to the
165 // driver, not on every showPixels() attempt).
166 ++mDitherPhase;
167 }
168
169private:
177#if defined(FASTLED_DISABLE_DYNAMIC_DRIVER) && FASTLED_DISABLE_DYNAMIC_DRIVER
178 return {};
179#else
180#if defined(FL_IS_ESP32)
181 if (mExplicitSpiHost) {
182 // A requested SPI host cannot be honored by the LCD_CAM driver.
183 // Do not fall through to priority selection if the native adapter
184 // is unavailable.
185 return ChannelManager::registry().findDriverByName(
186 fl::string::from_literal("SPI_UNIFIED"));
187 }
188#endif
189 // Build busKey only when we actually need it (kBus != AUTO).
190 fl::string busKey;
191 if (kBus != Bus::AUTO) {
192 busKey = fl::string::from_literal(busDriverName(kBus, B_WHICH, /*spi=*/true));
193 }
194
196 ChannelManager::registry().selectDriverForChannel(mData, busKey);
197
198#if FASTLED_LOG_RUNTIME_ENABLED
199 if (!driver) {
200 FL_WARN_EVERY(1000, "addLeds: no enabled driver can take bus '" << busKey
201 << "' - dropping frame. Fix: FastLED.setDriverEnabled(\"" << busKey
202 << "\", true) or FastLED.enableAllDrivers().");
203 } else if (kBus != Bus::AUTO && driver->getName() != busKey) {
204 FL_WARN_EVERY(1000, "addLeds: driver '" << busKey << "' is not enabled - falling back to '"
205 << driver->getName() << "'. Fix: FastLED.setDriverEnabled(\"" << busKey
206 << "\", true), or name the enabled bus in addLeds<..., fl::Bus::X>().");
207 }
208#endif // FASTLED_LOG_RUNTIME_ENABLED
209 return driver;
210#endif // !FASTLED_DISABLE_DYNAMIC_DRIVER
211 }
212
218 switch (CHIPSET) {
222 it.writeAPA102(&out, false);
223 break;
227 it.writeAPA102(&out, true);
228 break;
230 it.writeSK9822(&out, false);
231 break;
233 it.writeSK9822(&out, true);
234 break;
236 it.writeWS2801(&out);
237 break;
239 it.writeWS2803(&out);
240 break;
242 it.writeP9813(&out);
243 break;
245 it.writeLPD8806(&out);
246 break;
248 it.writeLPD6803(&out);
249 break;
251 it.writeSM16716(&out);
252 break;
254 it.writeHD108(&out, false);
255 break;
257 // Never routed here -- FastLED.h sends MY9221 to the bit-bang
258 // MY9221Controller (see file header comment). Defensive
259 // no-op if this controller is ever instantiated directly
260 // with CHIPSET == MY9221.
261 break;
262 }
263 }
264
265 ChannelDataPtr mData;
266#if defined(FL_IS_ESP32)
267 bool mExplicitSpiHost;
268#endif
270#if FASTLED_LOG_RUNTIME_ENABLED
271#endif
272};
273
274} // namespace fl
#define CHIPSET
Portable compile-time identifiers for LED channel buses.
Per-driver traits keyed on fl::Bus.
Phase 3b templated channel facade – Channel<Bus, Chipset> with compile-time bus/chipset enforcement.
CPixelLEDController(RegistrationMode mode) FL_NO_EXCEPT
Protected constructor with registration mode.
Rgbww getRgbww() const FL_NO_EXCEPT
Rgbw getRgbw() const FL_NO_EXCEPT
static ChannelDataPtr create(const ChipsetVariant &chipset, fl::vector_psram< u8 > &&encodedData=fl::vector_psram< u8 >(), ChannelPixelFormat pixelFormat=ChannelPixelFormat::RGB) FL_NO_EXCEPT
Create channel transmission data (modern variant-based API)
Definition data.cpp.hpp:12
static ChannelManager & registry() FL_NO_EXCEPT
Get the singleton WITHOUT registering platform default drivers.
SlimSpiBridgeController(const SpiChipsetConfig &cfg) FL_NO_EXCEPT
static void encode(fl::PixelIterator &it, fl::vector_psram< u8 > &out) FL_NO_EXCEPT
Encode it into out using the writer selected by CHIPSET.
void init() FL_NO_EXCEPT override
Initialize the LED controller.
static constexpr Bus kBus
The bus actually used after resolving Bus::AUTO – mirrors TypedChannel<B, SpiChipsetConfig,...
const ChannelDataPtr & channelData() const FL_NO_EXCEPT
Expose the underlying ChannelDataPtr for conformance tests.
FL_STATIC_ASSERT(BusSupports< kBus, SpiChipsetConfig, B_WHICH >::value, "SlimSpiBridgeController: Bus does not support SpiChipsetConfig")
void showPixels(PixelController< RGB_ORDER > &pixels) FL_NO_EXCEPT override
fl::shared_ptr< IChannelDriver > resolveDriver() FL_NO_EXCEPT
Resolve the driver for this frame.
static string from_literal(const char *literal) FL_NO_EXCEPT
defines the templated version of the CLEDController class
Channel transmission data - lightweight DTO for driver transmission.
Non-templated low level pixel data writing class.
#define FL_WARN_ONCE(...)
Definition log.h:439
#define FL_WARN_EVERY(MILLIS,...)
Definition log.h:442
Centralized logging categories for FastLED hardware interfaces and subsystems.
Unified manager for channel drivers with priority-based fallback.
unsigned char u8
Definition stdint.h:132
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
const char * busDriverName(Bus b, fl::u8 which=0, bool spi=false) FL_NO_EXCEPT
Concrete ChannelManager driver name for a portable bus.
Definition bus.h:193
fl::variant< ClocklessChipset, SpiChipsetConfig > ChipsetVariant
Definition config.h:157
Bus
Portable public bus selector.
Definition bus.h:31
@ AUTO
Definition bus.h:32
@ SM16716
SM16716 LED chipset.
@ LPD6803
LPD6803 LED chipset.
@ APA102HD
APA102 LED chipset with 5-bit gamma correction.
@ HD107
Same as APA102, but in turbo 40-mhz mode.
@ DOTSTARHD
APA102HD LED chipset alias.
@ SK9822HD
SK9822 LED chipset with 5-bit gamma correction.
@ SK9822
SK9822 LED chipset.
@ DOTSTAR
APA102 LED chipset alias.
@ P9813
P9813 LED chipset.
@ APA102
APA102 LED chipset.
@ HD108
16-bit variant of HD107, always gamma corrected. No SD (standard definition) option available - all H...
@ LPD8806
LPD8806 LED chipset.
@ WS2803
WS2803 LED chipset.
@ MY9221
MY9221 12-channel LED driver (dual-edge clocked; 4 RGB LEDs per chip)
@ WS2801
WS2801 LED chipset.
@ HD107HD
Same as APA102HD, but in turbo 40-mhz mode.
Base definition for an LED controller.
Primary template — intentionally undefined.
Definition bus_traits.h:31
Portable compile-time assertion wrapper.
Pixel controller class.
SPI chipset configuration (data + clock pins)
Definition config.h:103
static constexpr Bus value