FastLED 3.10.10
Loading...
Searching...
No Matches
driver.h
Go to the documentation of this file.
1
18
19#pragma once
20
21#include "fl/channels/can_match.h" // free fl::canMatch / canMatchBulk
22#include "fl/channels/capabilities.h" // DriverCapabilities, PinGroup, ChannelRequest, HandleResult
24#include "fl/stl/atomic.h"
25#include "fl/stl/noexcept.h"
26#include "fl/stl/shared_ptr.h"
27#include "fl/stl/span.h"
28#include "fl/stl/string.h" // IWYU pragma: keep
29
30namespace fl {
31
32// Forward declarations
33class ChannelData;
35
54public:
63 using Callback = void (*)(void*);
64
65 Callback callback = nullptr;
66 void* context = nullptr;
67
68 constexpr PollNeededCallback() FL_NO_EXCEPT = default;
71
72 bool isValid() const FL_NO_EXCEPT { return callback != nullptr; }
73 explicit operator bool() const FL_NO_EXCEPT { return isValid(); }
74
75 void invoke() const FL_NO_EXCEPT {
76 if (callback != nullptr) {
78 }
79 }
80 };
81
89 private:
97
98 public:
101
103 Snapshot* active =
104 mSnapshot.exchange(nullptr, fl::memory_order_acq_rel);
105 destroySnapshots(active);
107 mRetired = nullptr;
108 }
109
111 if (callback.callback == nullptr) {
112 clear();
113 return;
114 }
115 Snapshot* snapshot = new Snapshot(callback); // ok bare allocation
116 retire(mSnapshot.exchange(snapshot, fl::memory_order_acq_rel));
117 }
118
120 retire(mSnapshot.exchange(nullptr, fl::memory_order_acq_rel));
121 }
122
123 void invoke() const FL_NO_EXCEPT {
125 if (snapshot == nullptr) {
126 return;
127 }
128 snapshot->callback.invoke();
129 }
130
131 private:
132 void retire(Snapshot* snapshot) FL_NO_EXCEPT {
133 if (snapshot == nullptr) {
134 return;
135 }
136 // An ISR may already have loaded this pointer, so reclaim only when
137 // the slot is destroyed and driver teardown has quiesced callbacks.
138 snapshot->next = mRetired;
139 mRetired = snapshot;
140 }
141
142 static void destroySnapshots(Snapshot* snapshot) FL_NO_EXCEPT {
143 while (snapshot != nullptr) {
144 Snapshot* next = snapshot->next;
145 delete snapshot; // ok bare allocation
146 snapshot = next;
147 }
148 }
149
152 };
153
158
161
163 constexpr Capabilities(bool clockless, bool spi) FL_NO_EXCEPT
164 : supportsClockless(clockless), supportsSpi(spi) {}
165 };
166
169 struct DriverState {
176
179
182
185
187 operator Value() const FL_NO_EXCEPT { return state; }
188
190 bool operator==(Value v) const FL_NO_EXCEPT { return state == v; }
191 bool operator!=(Value v) const FL_NO_EXCEPT { return state != v; }
192 };
193
199 virtual void enqueue(ChannelDataPtr channelData) FL_NO_EXCEPT = 0;
200
204 virtual void show() FL_NO_EXCEPT = 0;
205
214
218 virtual fl::string getName() const FL_NO_EXCEPT { return fl::string::from_literal(""); }
219
224
230 virtual bool canHandle(const ChannelDataPtr& data) const FL_NO_EXCEPT = 0;
231
232 // ================================================================
233 // #3186 declarative-capabilities surface — drivers publish data;
234 // the base class implements the matching virtuals by delegating to
235 // free fl::canMatch / fl::canMatchBulk against the published data.
236 // ================================================================
237
245 Capabilities legacy = getCapabilities();
248 caps.supports_spi = legacy.supportsSpi;
249 return caps;
250 }
251
262
272 return fl::canMatch(getDriverCapabilities(), getPinGroups(), request);
273 }
274
278 bool waitForReady(u32 timeoutMs = 1000) FL_NO_EXCEPT;
279 bool waitForReadyOrDraining(u32 timeoutMs = 1000) FL_NO_EXCEPT;
280
288 (void)callback;
289 }
290
300 virtual bool waitDone(u32 timeoutMs = 1000) FL_NO_EXCEPT {
301 return waitForReady(timeoutMs);
302 }
303
305 virtual ~IChannelDriver() FL_NO_EXCEPT = default;
306
309 bool configureWorker(const WorkerConfig& config) FL_NO_EXCEPT;
310
314
315private:
316 // Keep default aggregate construction and configuration checks out of
317 // unused vtable entries. Only S3 and stub implementations have a worker
318 // backend; other platforms carry no worker vtable entries.
319#if defined(FL_IS_ESP_32S3) || defined(FASTLED_STUB_IMPL)
320 virtual
321#endif
323
324#if defined(FL_IS_ESP_32S3) || defined(FASTLED_STUB_IMPL)
325 virtual
326#endif
328
329protected:
331
332 // Non-copyable, non-movable
334 IChannelDriver& operator=(const IChannelDriver&) FL_NO_EXCEPT = delete;
337
338 FL_NO_INLINE bool waitForState(bool allowDraining, u32 timeoutMs) FL_NO_EXCEPT;
339};
340
341} // namespace fl
Free function that decides whether a ChannelRequest can be served by a driver's published capability ...
Declarative driver capabilities + channel-request types for the Channel Manager's predicate / depende...
Transmission data for a single LED channel.
Definition data.h:56
void invoke() const FL_NO_EXCEPT
Definition driver.h:123
fl::atomic< Snapshot * > mSnapshot
Definition driver.h:150
void set(PollNeededCallback callback) FL_NO_EXCEPT
Definition driver.h:110
void retire(Snapshot *snapshot) FL_NO_EXCEPT
Definition driver.h:132
static void destroySnapshots(Snapshot *snapshot) FL_NO_EXCEPT
Definition driver.h:142
IChannelDriver() FL_NO_EXCEPT=default
virtual ~IChannelDriver() FL_NO_EXCEPT=default
Virtual destructor.
bool configureWorker(const WorkerConfig &config) FL_NO_EXCEPT
Configure shared worker settings before transmission.
void fillWorkerDiagnostics(WorkerDiagnostics &) const FL_NO_EXCEPT
virtual void setPollNeededCallback(PollNeededCallback callback) FL_NO_EXCEPT
Install the manager-owned poll-needed callback for ISR wakeups.
Definition driver.h:287
virtual DriverCapabilities getDriverCapabilities() const FL_NO_EXCEPT
Driver-wide capability descriptor (declarative form).
Definition driver.h:244
virtual fl::string getName() const FL_NO_EXCEPT
Get the driver name for affinity binding.
Definition driver.h:218
virtual Capabilities getCapabilities() const FL_NO_EXCEPT=0
Get driver capabilities (clockless, SPI, or both)
FL_NO_INLINE bool waitForState(bool allowDraining, u32 timeoutMs) FL_NO_EXCEPT
HandleResult canMatch(const ChannelRequest &request) const FL_NO_EXCEPT
Predicate: can this driver match a (single- or multi-pin) request?
Definition driver.h:271
bool configureWorkerImpl(const WorkerConfig &) FL_NO_EXCEPT
virtual bool canHandle(const ChannelDataPtr &data) const FL_NO_EXCEPT=0
Check if this driver can handle the given channel data.
WorkerDiagnostics workerDiagnostics() const FL_NO_EXCEPT
Snapshot worker evidence after serializing with transmission completion.
virtual void enqueue(ChannelDataPtr channelData) FL_NO_EXCEPT=0
Enqueue channel data for transmission.
virtual fl::span< const PinGroup > getPinGroups() const FL_NO_EXCEPT
Per-resource group enumeration.
Definition driver.h:259
virtual void show() FL_NO_EXCEPT=0
Trigger transmission of enqueued data.
virtual DriverState poll() FL_NO_EXCEPT=0
Query driver state and perform maintenance.
virtual bool waitDone(u32 timeoutMs=1000) FL_NO_EXCEPT
Block until this driver finishes any in-flight transmit.
Definition driver.h:300
bool waitForReady(u32 timeoutMs=1000) FL_NO_EXCEPT
Wait for driver to become READY.
bool waitForReadyOrDraining(u32 timeoutMs=1000) FL_NO_EXCEPT
static string from_literal(const char *literal) FL_NO_EXCEPT
#define constexpr
Declares that it is possible to evaluate a value at compile time, introduced in C++11.
Definition cpp_compat.h:15
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
AtomicFake< T > atomic
Definition atomic.h:26
@ memory_order_acquire
Definition atomic.h:41
@ memory_order_acq_rel
Definition atomic.h:43
HandleResult canMatch(const DriverCapabilities &caps, fl::span< const PinGroup > groups, const ChannelRequest &request) FL_NO_EXCEPT
Test a request (single-pin OR multi-pin ganged) against a driver's published capability data.
HandleResult
Base definition for an LED controller.
Worker scheduling evidence.
#define FL_NO_INLINE
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:577
bool supportsClockless
Supports clockless protocols (WS2812, SK6812, etc.)
Definition driver.h:156
constexpr Capabilities() FL_NO_EXCEPT
Default constructor (no capabilities)
Definition driver.h:160
bool supportsSpi
Supports SPI protocols (APA102, SK9822, etc.)
Definition driver.h:157
constexpr Capabilities(bool clockless, bool spi) FL_NO_EXCEPT
Constructor with explicit capabilities.
Definition driver.h:163
Driver capabilities.
Definition driver.h:155
DriverState(Value v) FL_NO_EXCEPT
Construct from state only (no error)
Definition driver.h:181
bool operator!=(Value v) const FL_NO_EXCEPT
Definition driver.h:191
fl::string error
Error message (only populated when state == ERROR)
Definition driver.h:178
Value state
Current driver state.
Definition driver.h:177
bool operator==(Value v) const FL_NO_EXCEPT
Comparison operators for backward compatibility.
Definition driver.h:190
@ READY
Hardware idle; ready to accept new transmissions.
Definition driver.h:171
@ DRAINING
All channels submitted; still transmitting.
Definition driver.h:173
@ ERROR
Driver encountered an error.
Definition driver.h:174
@ BUSY
Active: channels transmitting or queued.
Definition driver.h:172
DriverState(Value v, const fl::string &e) FL_NO_EXCEPT
Construct from state and error message.
Definition driver.h:184
Driver state with optional error message.
Definition driver.h:169
constexpr PollNeededCallback() FL_NO_EXCEPT=default
bool isValid() const FL_NO_EXCEPT
Definition driver.h:72
void invoke() const FL_NO_EXCEPT
Definition driver.h:75
ISR-safe callback handle invoked when the manager should poll again.
Definition driver.h:62
Snapshot(PollNeededCallback cb) FL_NO_EXCEPT
Definition driver.h:91
Shared engine settings, configured before the first transmission.