FastLED 3.9.15
Loading...
Searching...
No Matches
driver.h
Go to the documentation of this file.
1
18
19#pragma once
20
21#include "fl/stl/shared_ptr.h"
22#include "fl/stl/string.h" // IWYU pragma: keep
23#include "fl/stl/noexcept.h"
24#include "fl/stl/atomic.h"
25
26namespace fl {
27
28// Forward declarations
29class ChannelData;
31
50public:
59 using Callback = void (*)(void*) FL_NOEXCEPT;
60
61 Callback callback = nullptr;
62 void* context = nullptr;
63
64 constexpr PollNeededCallback() FL_NOEXCEPT = default;
67
68 bool isValid() const FL_NOEXCEPT { return callback != nullptr; }
69 explicit operator bool() const FL_NOEXCEPT { return isValid(); }
70
71 void invoke() const FL_NOEXCEPT {
72 if (callback != nullptr) {
74 }
75 }
76 };
77
85 private:
93
94 public:
97
99 Snapshot* active =
100 mSnapshot.exchange(nullptr, fl::memory_order_acq_rel);
101 destroySnapshots(active);
103 mRetired = nullptr;
104 }
105
107 if (callback.callback == nullptr) {
108 clear();
109 return;
110 }
111 Snapshot* snapshot = new Snapshot(callback); // ok bare allocation
112 retire(mSnapshot.exchange(snapshot, fl::memory_order_acq_rel));
113 }
114
116 retire(mSnapshot.exchange(nullptr, fl::memory_order_acq_rel));
117 }
118
119 void invoke() const FL_NOEXCEPT {
121 if (snapshot == nullptr) {
122 return;
123 }
124 snapshot->callback.invoke();
125 }
126
127 private:
128 void retire(Snapshot* snapshot) FL_NOEXCEPT {
129 if (snapshot == nullptr) {
130 return;
131 }
132 // An ISR may already have loaded this pointer, so reclaim only when
133 // the slot is destroyed and driver teardown has quiesced callbacks.
134 snapshot->next = mRetired;
135 mRetired = snapshot;
136 }
137
138 static void destroySnapshots(Snapshot* snapshot) FL_NOEXCEPT {
139 while (snapshot != nullptr) {
140 Snapshot* next = snapshot->next;
141 delete snapshot; // ok bare allocation
142 snapshot = next;
143 }
144 }
145
148 };
149
154
157
159 constexpr Capabilities(bool clockless, bool spi) FL_NOEXCEPT
160 : supportsClockless(clockless), supportsSpi(spi) {}
161 };
162
165 struct DriverState {
172
175
178
181
183 operator Value() const FL_NOEXCEPT { return state; }
184
186 bool operator==(Value v) const FL_NOEXCEPT { return state == v; }
187 bool operator!=(Value v) const FL_NOEXCEPT { return state != v; }
188 };
189
195 virtual void enqueue(ChannelDataPtr channelData) FL_NOEXCEPT = 0;
196
200 virtual void show() FL_NOEXCEPT = 0;
201
210
214 virtual fl::string getName() const FL_NOEXCEPT { return fl::string::from_literal(""); }
215
220
226 virtual bool canHandle(const ChannelDataPtr& data) const FL_NOEXCEPT = 0;
227
231 bool waitForReady(u32 timeoutMs = 1000) FL_NOEXCEPT;
232 bool waitForReadyOrDraining(u32 timeoutMs = 1000) FL_NOEXCEPT;
233
241 (void)callback;
242 }
243
253 virtual bool waitDone(u32 timeoutMs = 1000) FL_NOEXCEPT {
254 return waitForReady(timeoutMs);
255 }
256
258 virtual ~IChannelDriver() FL_NOEXCEPT = default;
259
260protected:
262
263 // Non-copyable, non-movable
265 IChannelDriver& operator=(const IChannelDriver&) FL_NOEXCEPT = delete;
268
269 template<typename Condition>
270 bool waitForCondition(Condition condition, u32 timeoutMs = 1000) FL_NOEXCEPT;
271};
272
273} // namespace fl
Transmission data for a single LED channel.
Definition data.h:39
void invoke() const FL_NOEXCEPT
Definition driver.h:119
fl::atomic< Snapshot * > mSnapshot
Definition driver.h:146
void retire(Snapshot *snapshot) FL_NOEXCEPT
Definition driver.h:128
static void destroySnapshots(Snapshot *snapshot) FL_NOEXCEPT
Definition driver.h:138
void set(PollNeededCallback callback) FL_NOEXCEPT
Definition driver.h:106
virtual void enqueue(ChannelDataPtr channelData) FL_NOEXCEPT=0
Enqueue channel data for transmission.
virtual void setPollNeededCallback(PollNeededCallback callback) FL_NOEXCEPT
Install the manager-owned poll-needed callback for ISR wakeups.
Definition driver.h:240
bool waitForReadyOrDraining(u32 timeoutMs=1000) FL_NOEXCEPT
virtual ~IChannelDriver() FL_NOEXCEPT=default
Virtual destructor.
virtual fl::string getName() const FL_NOEXCEPT
Get the driver name for affinity binding.
Definition driver.h:214
IChannelDriver() FL_NOEXCEPT=default
virtual void show() FL_NOEXCEPT=0
Trigger transmission of enqueued data.
virtual bool waitDone(u32 timeoutMs=1000) FL_NOEXCEPT
Block until this driver finishes any in-flight transmit.
Definition driver.h:253
bool waitForCondition(Condition condition, u32 timeoutMs=1000) FL_NOEXCEPT
bool waitForReady(u32 timeoutMs=1000) FL_NOEXCEPT
Wait for driver to become READY.
virtual bool canHandle(const ChannelDataPtr &data) const FL_NOEXCEPT=0
Check if this driver can handle the given channel data.
virtual Capabilities getCapabilities() const FL_NOEXCEPT=0
Get driver capabilities (clockless, SPI, or both)
virtual DriverState poll() FL_NOEXCEPT=0
Query driver state and perform maintenance.
static string from_literal(const char *literal) FL_NOEXCEPT
#define constexpr
Declares that it is possible to evaluate a value at compile time, introduced in C++11.
Definition cpp_compat.h:15
AtomicFake< T > atomic
Definition atomic.h:26
@ memory_order_acquire
Definition atomic.h:41
@ memory_order_acq_rel
Definition atomic.h:43
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:535
constexpr Capabilities() FL_NOEXCEPT
Default constructor (no capabilities)
Definition driver.h:156
bool supportsClockless
Supports clockless protocols (WS2812, SK6812, etc.)
Definition driver.h:152
constexpr Capabilities(bool clockless, bool spi) FL_NOEXCEPT
Constructor with explicit capabilities.
Definition driver.h:159
bool supportsSpi
Supports SPI protocols (APA102, SK9822, etc.)
Definition driver.h:153
Driver capabilities.
Definition driver.h:151
bool operator!=(Value v) const FL_NOEXCEPT
Definition driver.h:187
DriverState(Value v, const fl::string &e) FL_NOEXCEPT
Construct from state and error message.
Definition driver.h:180
fl::string error
Error message (only populated when state == ERROR)
Definition driver.h:174
Value state
Current driver state.
Definition driver.h:173
@ READY
Hardware idle; ready to accept new transmissions.
Definition driver.h:167
@ DRAINING
All channels submitted; still transmitting.
Definition driver.h:169
@ ERROR
Driver encountered an error.
Definition driver.h:170
@ BUSY
Active: channels transmitting or queued.
Definition driver.h:168
DriverState(Value v) FL_NOEXCEPT
Construct from state only (no error)
Definition driver.h:177
bool operator==(Value v) const FL_NOEXCEPT
Comparison operators for backward compatibility.
Definition driver.h:186
Driver state with optional error message.
Definition driver.h:165
void(*)(void *) FL_NOEXCEPT Callback
Definition driver.h:59
constexpr PollNeededCallback() FL_NOEXCEPT=default
void invoke() const FL_NOEXCEPT
Definition driver.h:71
bool isValid() const FL_NOEXCEPT
Definition driver.h:68
ISR-safe callback handle invoked when the manager should poll again.
Definition driver.h:58
Snapshot(PollNeededCallback cb) FL_NOEXCEPT
Definition driver.h:87