FastLED 3.10.10
Loading...
Searching...
No Matches
slim_bridge_controller.h
Go to the documentation of this file.
1
12
13#pragma once
14
17#include "fl/stl/noexcept.h"
18#include "fl/stl/vector.h"
19
21#include "fl/channels/data.h"
22#include "fl/channels/driver.h"
23#include "fl/channels/manager.h"
24#include "fl/channels/bus_traits.h" // IWYU pragma: keep (DriverTraits<Bus::X> satisfies the DriverTraits contract)
25#include "fl/channels/config.h"
27#include "fl/log/log.h"
29
30namespace fl {
31
34template <bool PLATFORM_DEFAULT = !FL_PLATFORM_HAS_LARGE_MEMORY>
36#ifdef FL_SLIM_BRIDGE_DIRECT_SHOW
37 static constexpr bool value = (FL_SLIM_BRIDGE_DIRECT_SHOW != 0);
38#else
39 static constexpr bool value = PLATFORM_DEFAULT;
40#endif
41};
42
43namespace detail {
44template <typename Traits>
45auto reject_legacy_frame(int) FL_NO_EXCEPT -> decltype(Traits::rejectLegacyFrame(), void()) {
46 Traits::rejectLegacyFrame();
47}
48
49template <typename Traits>
51
52template <typename Traits>
53auto show_batched_legacy(int) FL_NO_EXCEPT -> decltype(Traits::showLegacy(), void()) {
54 Traits::showLegacy();
55}
56
57template <typename Traits>
59 IChannelDriver& driver = Traits::instance();
60 driver.show();
61 driver.waitForReady();
62}
63} // namespace detail
64
67template <bool DIRECT_SHOW, bool BATCHED = false>
69
72template <bool BATCHED>
73struct SlimBridgeShowPolicy<false, BATCHED> {
74 template <typename DriverTraits>
75 static void attach() FL_NO_EXCEPT { DriverTraits::registerWithManager(); }
76 static bool enabled(IChannelDriver& driver) FL_NO_EXCEPT {
78 }
79
84 template <typename DriverTraits>
85 static void endShow() FL_NO_EXCEPT {}
86};
87
90template <>
91struct SlimBridgeShowPolicy<true, false> {
92 template <typename DriverTraits>
93 static void attach() FL_NO_EXCEPT {}
94 static bool enabled(IChannelDriver&) FL_NO_EXCEPT { return true; }
95 static fl::shared_ptr<IChannelDriver> fallback(const ChannelDataPtr&) FL_NO_EXCEPT { return {}; }
96 static void afterEnqueue(IChannelDriver& driver) FL_NO_EXCEPT { driver.show(); }
97 template <typename DriverTraits>
98 static void endShow() FL_NO_EXCEPT {}
99};
100
103template <>
104struct SlimBridgeShowPolicy<true, true> : SlimBridgeShowPolicy<true, false> {
106 template <typename DriverTraits>
107 static void endShow() FL_NO_EXCEPT {
108 // RMT4 supplies autonomous completion. Other batched drivers retain
109 // the bounded synchronous fallback until they supply that capability.
111 }
112};
113
151template <int DATA_PIN, typename TIMING, EOrder RGB_ORDER, int WAIT_TIME, typename DriverTraits,
152 int XTRA0 = 0, bool DIRECT_SHOW = SlimBridgeDirectShow<>::value,
153 bool BATCHED_DIRECT_SHOW = false>
154class SlimBridgeController : public CPixelLEDController<RGB_ORDER> {
156
157public:
160 template<Bus B>
162 WAIT_TIME, typename fl::conditional<B == Bus::AUTO, DriverTraits,
164 XTRA0, DIRECT_SHOW, BATCHED_DIRECT_SHOW>;
165
168 // (A?) Fold WAIT_TIME (microseconds) into reset_us, mirroring the
169 // historical ClocklessIdf4 behavior: the driver must honor whichever
170 // is longer -- the chipset's own latch/reset requirement, or the
171 // caller-requested minimum inter-frame wait.
172 if (WAIT_TIME > 0 && static_cast<u32>(WAIT_TIME) > timing.reset_us) {
173 timing.reset_us = static_cast<u32>(WAIT_TIME);
174 }
175 // A clockless controller bound to a unified parallel-IO bus needs
176 // that engine's clockless pipeline linked (#4793).
180 FL_STATIC_ASSERT(XTRA0 >= 0 && XTRA0 <= 32, "XTRA0 out of range");
181 if (XTRA0 != 0) {
182 mData->setExtraZeroBitsPerByte(static_cast<u8>(XTRA0));
183 }
184 // Idempotent by contract -- safe to call from every instantiation's
185 // constructor (mirrors ClocklessIdf5's registerWithManager() call).
186 // A no-op in direct-show mode.
187 Policy::template attach<DriverTraits>();
188 }
189
190 void init() FL_NO_EXCEPT override {}
191
192 void endShowLeds(void* data) FL_NO_EXCEPT override {
194 // FastLED enqueues every controller before calling any endShowLeds,
195 // so the first direct controller flushes the complete parallel batch.
196 Policy::template endShow<DriverTraits>();
197 }
198
200 const ChannelDataPtr& channelData() const FL_NO_EXCEPT { return mData; }
201
202protected:
204 // (A1) Respect runtime driver enable/disable: ChannelManager is the
205 // single source of truth. A disabled driver is never enqueued to (the
206 // #2517 silent-drop failure mode); the frame goes to an enabled driver
207 // that can handle it instead, with a warning naming the fix.
209 if (!Policy::enabled(DriverTraits::instance())) {
210 fallback = Policy::fallback(mData);
211 IChannelDriver& wanted = DriverTraits::instance();
212 FL_UNUSED(wanted); // FL_WARN_EVERY compiles out on low-memory targets
213 if (!fallback) {
214 FL_WARN_EVERY(1000, "addLeds: driver '" << wanted.getName().c_str()
215 << "' is not enabled and no enabled driver can take this strip - dropping frame. "
216 "Fix: FastLED.setDriverEnabled(\"" << wanted.getName().c_str()
217 << "\", true), or drop the setExclusiveDriver() that disabled it.");
218 return;
219 }
220 FL_WARN_EVERY(1000, "addLeds: driver '" << wanted.getName().c_str()
221 << "' is not enabled - falling back to '" << fallback->getName().c_str()
222 << "'. Fix: FastLED.setDriverEnabled(\"" << wanted.getName().c_str()
223 << "\", true), or name the enabled bus in addLeds<..., fl::Bus::X>().");
224 }
225 IChannelDriver& driver = fallback ? *fallback : DriverTraits::instance();
226
227 // (A8) If the previous frame's buffer is still in flight, block
228 // (bounded) until the driver reports it can accept more work rather
229 // than racing the driver's in-progress DMA/RMT transfer.
230 if (mData->isInUse()) {
231 if (!driver.waitForReady()) {
232 FL_WARN_ONCE("SlimBridgeController: driver '" << driver.getName().c_str() << "' did not become ready in time - dropping frame");
233 return;
234 }
235 }
236
237 // (A5) Publish the pixel byte layout so the driver (and any
238 // downstream padding/diagnostic code) knows the encoded stride.
239 if (this->getRgbww().active()) {
240 mData->setPixelFormat(ChannelPixelFormat::RGBWW);
241 } else if (this->getRgbw().active()) {
242 mData->setPixelFormat(ChannelPixelFormat::RGBW);
243 } else {
244 mData->setPixelFormat(ChannelPixelFormat::RGB);
245 }
246
247 const int pixelCount = pixels.size();
248 const fl::size bytesPerPixel = channelPixelFormatBytesPerPixel(mData->getPixelFormat());
249 if (pixelCount < 0 || bytesPerPixel == 0 ||
250 static_cast<fl::size>(pixelCount) > static_cast<fl::size>(-1) / bytesPerPixel) {
252 FL_WARN_ONCE("SlimBridgeController: invalid encoded frame size - dropping frame");
253 return;
254 }
255 const fl::size expectedSize = static_cast<fl::size>(pixelCount) * bytesPerPixel;
256 auto &encoded = mData->getData();
257 // Reserve the complete snapshot before clearing the old one. Byte-wise
258 // geometric growth could fail partway through encoding and enqueue a
259 // silently truncated frame; exact reservation also bounds peak heap.
260 encoded.reserve(expectedSize);
261 if (encoded.capacity() < expectedSize) {
263 FL_WARN_ONCE("SlimBridgeController: cannot allocate encoded frame - dropping frame");
264 return;
265 }
266
267 // Observe accepted frames only after allocation succeeds (e.g. feeding
268 // ActiveStripTracker for stub/WASM channels).
269 onBeforeEncode(pixels);
270
271 // (A3) Re-encode into the same buffer every frame -- clear() keeps
272 // capacity, so steady-state operation is allocation-free.
273 encoded.clear();
274 fl::PixelIterator iterator(&pixels, this->getRgbw(), this->getRgbww());
275 iterator.writeWS2812(&encoded);
276 if (encoded.size() != expectedSize) {
277 encoded.clear();
279 FL_WARN_ONCE("SlimBridgeController: incomplete encoded frame - dropping frame");
280 return;
281 }
282
283 driver.enqueue(mData);
284 Policy::afterEnqueue(driver);
285 }
286
289 virtual void onBeforeEncode(PixelController<RGB_ORDER>& pixels) FL_NO_EXCEPT { (void)pixels; }
290
291private:
292 ChannelDataPtr mData;
293};
294
295} // namespace fl
#define DATA_PIN
Definition ClientReal.h:82
#define RGB_ORDER
Per-driver traits keyed on fl::Bus.
CPixelLEDController(RegistrationMode mode) FL_NO_EXCEPT
Protected constructor with registration mode.
Rgbww getRgbww() const FL_NO_EXCEPT
VIRTUAL_IF_NOT_AVR void endShowLeds(void *data) 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
fl::shared_ptr< IChannelDriver > selectEnabledDriverForChannel(const ChannelDataPtr &data) const FL_NO_EXCEPT
Highest-priority enabled driver that can handle data, or null.
static ChannelManager & registry() FL_NO_EXCEPT
Get the singleton WITHOUT registering platform default drivers.
bool isDriverEnabled(const char *name) const FL_NO_EXCEPT
Check if a driver is enabled by name.
virtual fl::string getName() const FL_NO_EXCEPT
Get the driver name for affinity binding.
Definition driver.h:218
virtual void enqueue(ChannelDataPtr channelData) FL_NO_EXCEPT=0
Enqueue channel data for transmission.
virtual void show() FL_NO_EXCEPT=0
Trigger transmission of enqueued data.
bool waitForReady(u32 timeoutMs=1000) FL_NO_EXCEPT
Wait for driver to become READY.
Minimal interface for LED channel transmission drivers.
Definition driver.h:53
void writeWS2812(CONTAINER_UIN8_T *out) FL_NO_EXCEPT
void endShowLeds(void *data) FL_NO_EXCEPT override
const ChannelDataPtr & channelData() const FL_NO_EXCEPT
Expose the underlying ChannelDataPtr for conformance tests.
SlimBridgeShowPolicy< DIRECT_SHOW, BATCHED_DIRECT_SHOW > Policy
virtual void onBeforeEncode(PixelController< RGB_ORDER > &pixels) FL_NO_EXCEPT
Called once per accepted frame, after the enabled/ready gates pass and BEFORE the frame is encoded an...
void showPixels(PixelController< RGB_ORDER > &pixels) FL_NO_EXCEPT override
void init() FL_NO_EXCEPT override
Initialize the LED controller.
SlimBridgeController< DATA_PIN, TIMING, RGB_ORDER, WAIT_TIME, typename fl::conditional< B==Bus::AUTO, DriverTraits, BusTraits< B > >::type, XTRA0, DIRECT_SHOW, BATCHED_DIRECT_SHOW > WithBus
Rebind only the bus; retain the compile-time chipset timing and slim storage rather than constructing...
const char * c_str() const FL_NO_EXCEPT
defines the templated version of the CLEDController class
Channel transmission data - lightweight DTO for driver transmission.
fl::EOrder EOrder
Definition eorder.h:12
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.
auto reject_legacy_frame(int) FL_NO_EXCEPT -> decltype(Traits::rejectLegacyFrame(), void())
auto show_batched_legacy(int) FL_NO_EXCEPT -> decltype(Traits::showLegacy(), void())
void enable_clockless() FL_NO_EXCEPT
Definition bus_traits.h:69
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_info.h:57
unsigned char u8
Definition stdint.h:132
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
constexpr ChipsetTimingConfig makeTimingConfig() FL_NO_EXCEPT
Convert compile-time CHIPSET type to runtime timing config.
@ PLATFORM_DEFAULT
Use the recommended backend for the active platform (RMT on ESP32; FlexPWM on Teensy 4....
Definition types.h:10
@ AUTO
Definition bus.h:32
u8 channelPixelFormatBytesPerPixel(ChannelPixelFormat format) FL_NO_EXCEPT
Definition data.h:33
Base definition for an LED controller.
Primary template — intentionally undefined.
Definition bus_traits.h:31
How a SlimBridgeController reaches its driver.
#define FL_STATIC_ASSERT(...)
#define FL_UNUSED(x)
Portable compile-time assertion wrapper.
Pixel controller class.
u32 reset_us
Reset/latch time (microseconds)
Runtime bit-period timing for a clockless chipset.
Preserve explicit build overrides while allowing a backend to choose its legacy default without chang...
static void afterEnqueue(IChannelDriver &) FL_NO_EXCEPT
static fl::shared_ptr< IChannelDriver > fallback(const ChannelDataPtr &data) FL_NO_EXCEPT
An enabled driver that can take data, in priority order.
static bool enabled(IChannelDriver &driver) FL_NO_EXCEPT
static bool enabled(IChannelDriver &) FL_NO_EXCEPT
static void afterEnqueue(IChannelDriver &driver) FL_NO_EXCEPT
static fl::shared_ptr< IChannelDriver > fallback(const ChannelDataPtr &) FL_NO_EXCEPT
static void afterEnqueue(IChannelDriver &) FL_NO_EXCEPT