FastLED 3.10.8
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
23#include "fl/stl/atomic.h"
24#include "fl/stl/noexcept.h"
25#include "fl/stl/shared_ptr.h"
26#include "fl/stl/span.h"
27#include "fl/stl/string.h" // IWYU pragma: keep
28
29namespace fl {
30
31// Forward declarations
32class ChannelData;
34
53public:
62 using Callback = void (*)(void*);
63
64 Callback callback = nullptr;
65 void* context = nullptr;
66
67 constexpr PollNeededCallback() FL_NO_EXCEPT = default;
70
71 bool isValid() const FL_NO_EXCEPT { return callback != nullptr; }
72 explicit operator bool() const FL_NO_EXCEPT { return isValid(); }
73
74 void invoke() const FL_NO_EXCEPT {
75 if (callback != nullptr) {
77 }
78 }
79 };
80
88 private:
96
97 public:
100
102 Snapshot* active =
103 mSnapshot.exchange(nullptr, fl::memory_order_acq_rel);
104 destroySnapshots(active);
106 mRetired = nullptr;
107 }
108
110 if (callback.callback == nullptr) {
111 clear();
112 return;
113 }
114 Snapshot* snapshot = new Snapshot(callback); // ok bare allocation
115 retire(mSnapshot.exchange(snapshot, fl::memory_order_acq_rel));
116 }
117
119 retire(mSnapshot.exchange(nullptr, fl::memory_order_acq_rel));
120 }
121
122 void invoke() const FL_NO_EXCEPT {
124 if (snapshot == nullptr) {
125 return;
126 }
127 snapshot->callback.invoke();
128 }
129
130 private:
131 void retire(Snapshot* snapshot) FL_NO_EXCEPT {
132 if (snapshot == nullptr) {
133 return;
134 }
135 // An ISR may already have loaded this pointer, so reclaim only when
136 // the slot is destroyed and driver teardown has quiesced callbacks.
137 snapshot->next = mRetired;
138 mRetired = snapshot;
139 }
140
141 static void destroySnapshots(Snapshot* snapshot) FL_NO_EXCEPT {
142 while (snapshot != nullptr) {
143 Snapshot* next = snapshot->next;
144 delete snapshot; // ok bare allocation
145 snapshot = next;
146 }
147 }
148
151 };
152
157
160
162 constexpr Capabilities(bool clockless, bool spi) FL_NO_EXCEPT
163 : supportsClockless(clockless), supportsSpi(spi) {}
164 };
165
168 struct DriverState {
175
178
181
184
186 operator Value() const FL_NO_EXCEPT { return state; }
187
189 bool operator==(Value v) const FL_NO_EXCEPT { return state == v; }
190 bool operator!=(Value v) const FL_NO_EXCEPT { return state != v; }
191 };
192
198 virtual void enqueue(ChannelDataPtr channelData) FL_NO_EXCEPT = 0;
199
203 virtual void show() FL_NO_EXCEPT = 0;
204
213
217 virtual fl::string getName() const FL_NO_EXCEPT { return fl::string::from_literal(""); }
218
223
229 virtual bool canHandle(const ChannelDataPtr& data) const FL_NO_EXCEPT = 0;
230
231 // ================================================================
232 // #3186 declarative-capabilities surface — drivers publish data;
233 // the base class implements the matching virtuals by delegating to
234 // free fl::canMatch / fl::canMatchBulk against the published data.
235 // ================================================================
236
244 Capabilities legacy = getCapabilities();
247 caps.supports_spi = legacy.supportsSpi;
248 return caps;
249 }
250
261
271 return fl::canMatch(getDriverCapabilities(), getPinGroups(), request);
272 }
273
277 bool waitForReady(u32 timeoutMs = 1000) FL_NO_EXCEPT;
278 bool waitForReadyOrDraining(u32 timeoutMs = 1000) FL_NO_EXCEPT;
279
287 (void)callback;
288 }
289
299 virtual bool waitDone(u32 timeoutMs = 1000) FL_NO_EXCEPT {
300 return waitForReady(timeoutMs);
301 }
302
304 virtual ~IChannelDriver() FL_NO_EXCEPT = default;
305
306protected:
308
309 // Non-copyable, non-movable
311 IChannelDriver& operator=(const IChannelDriver&) FL_NO_EXCEPT = delete;
314
315 template<typename Condition>
316 bool waitForCondition(Condition condition, u32 timeoutMs = 1000) FL_NO_EXCEPT;
317};
318
319} // 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:55
void invoke() const FL_NO_EXCEPT
Definition driver.h:122
fl::atomic< Snapshot * > mSnapshot
Definition driver.h:149
void set(PollNeededCallback callback) FL_NO_EXCEPT
Definition driver.h:109
void retire(Snapshot *snapshot) FL_NO_EXCEPT
Definition driver.h:131
static void destroySnapshots(Snapshot *snapshot) FL_NO_EXCEPT
Definition driver.h:141
IChannelDriver() FL_NO_EXCEPT=default
virtual ~IChannelDriver() FL_NO_EXCEPT=default
Virtual destructor.
virtual void setPollNeededCallback(PollNeededCallback callback) FL_NO_EXCEPT
Install the manager-owned poll-needed callback for ISR wakeups.
Definition driver.h:286
virtual DriverCapabilities getDriverCapabilities() const FL_NO_EXCEPT
Driver-wide capability descriptor (declarative form).
Definition driver.h:243
virtual fl::string getName() const FL_NO_EXCEPT
Get the driver name for affinity binding.
Definition driver.h:217
bool waitForCondition(Condition condition, u32 timeoutMs=1000) FL_NO_EXCEPT
virtual Capabilities getCapabilities() const FL_NO_EXCEPT=0
Get driver capabilities (clockless, SPI, or both)
HandleResult canMatch(const ChannelRequest &request) const FL_NO_EXCEPT
Predicate: can this driver match a (single- or multi-pin) request?
Definition driver.h:270
virtual bool canHandle(const ChannelDataPtr &data) const FL_NO_EXCEPT=0
Check if this driver can handle the given channel data.
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:258
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:299
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.
Definition crgb.hpp:179
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:577
bool supportsClockless
Supports clockless protocols (WS2812, SK6812, etc.)
Definition driver.h:155
constexpr Capabilities() FL_NO_EXCEPT
Default constructor (no capabilities)
Definition driver.h:159
bool supportsSpi
Supports SPI protocols (APA102, SK9822, etc.)
Definition driver.h:156
constexpr Capabilities(bool clockless, bool spi) FL_NO_EXCEPT
Constructor with explicit capabilities.
Definition driver.h:162
Driver capabilities.
Definition driver.h:154
DriverState(Value v) FL_NO_EXCEPT
Construct from state only (no error)
Definition driver.h:180
bool operator!=(Value v) const FL_NO_EXCEPT
Definition driver.h:190
fl::string error
Error message (only populated when state == ERROR)
Definition driver.h:177
Value state
Current driver state.
Definition driver.h:176
bool operator==(Value v) const FL_NO_EXCEPT
Comparison operators for backward compatibility.
Definition driver.h:189
@ READY
Hardware idle; ready to accept new transmissions.
Definition driver.h:170
@ DRAINING
All channels submitted; still transmitting.
Definition driver.h:172
@ ERROR
Driver encountered an error.
Definition driver.h:173
@ BUSY
Active: channels transmitting or queued.
Definition driver.h:171
DriverState(Value v, const fl::string &e) FL_NO_EXCEPT
Construct from state and error message.
Definition driver.h:183
Driver state with optional error message.
Definition driver.h:168
constexpr PollNeededCallback() FL_NO_EXCEPT=default
bool isValid() const FL_NO_EXCEPT
Definition driver.h:71
void invoke() const FL_NO_EXCEPT
Definition driver.h:74
ISR-safe callback handle invoked when the manager should poll again.
Definition driver.h:61
Snapshot(PollNeededCallback cb) FL_NO_EXCEPT
Definition driver.h:90