FastLED 3.10.10
Loading...
Searching...
No Matches

◆ waitForState()

bool fl::IChannelDriver::waitForState ( bool allowDraining,
u32 timeoutMs )
protected

Definition at line 28 of file driver.cpp.hpp.

28 {
29 const auto condition = [this, allowDraining]() {
30 const auto state = poll().state;
32 (allowDraining && state == IChannelDriver::DriverState::DRAINING);
33 };
34 const u32 startTime = timeoutMs > 0 ? millis() : 0;
35
36 // Tier 1: instant non-blocking check.
37 if (condition()) {
38 return true;
39 }
40
41 // Tier 2: bounded microsecond spin (#2818). Catches short DMA tails
42 // without paying the >=1-tick floor below. Budget runtime-tunable via
43 // FastLED.setWaitSpinBudgetUs(N); 0 disables.
44 {
45 const u32 spinBudget = fl::detail::getWaitSpinBudgetUs();
46 if (spinBudget > 0) {
47 const u32 spinStart = fl::micros();
48 while ((fl::micros() - spinStart) < spinBudget) {
49 if (condition()) {
50 return true;
51 }
52 if (timeoutMs > 0 && (millis() - startTime) >= timeoutMs) {
53 FL_ERROR("Timeout occurred while waiting for condition");
54 return false;
55 }
56 }
57 }
58 }
59
60 while (!condition()) {
61 // Check timeout if specified
62 if (timeoutMs > 0 && (millis() - startTime >= timeoutMs)) {
63 FL_ERROR("Timeout occurred while waiting for condition");
64 return false; // Timeout occurred
65 }
66
67 // Adaptive yield (refs #2815, generalizes the #2493 ESP32-P4 carve-out):
68 // see the matching comment in ChannelManager::waitForState for the
69 // full rationale. The deep yield is only needed when a radio is
70 // actually up; otherwise the FreeRTOS tick floor is pure timing drift.
72 // Radio active: keep WiFi/lwIP/BT alive with the deep yield.
74 } else {
75 // No radio: fast yield, no FreeRTOS tick floor.
77 }
78 }
79
80 return true; // Condition met
81}
TestState state
virtual DriverState poll() FL_NO_EXCEPT=0
Query driver state and perform maintenance.
static bool isAnyNetworkActive() FL_NO_EXCEPT
#define FL_ERROR(...)
Definition log.h:373
fl::u32 getWaitSpinBudgetUs() FL_NO_EXCEPT
Get the current tiered-wait spin budget (microseconds).
void yield_system(fl::u32 microseconds) FL_NO_EXCEPT
OS-level yield only: same behavior as run(us, ExecFlags::SYSTEM).
fl::u32 millis() FL_NO_EXCEPT
Universal millisecond timer - returns milliseconds since system startup.
fl::u32 micros() FL_NO_EXCEPT
Universal microsecond timer - returns microseconds since system startup.
Value state
Current driver state.
Definition driver.h:177
@ READY
Hardware idle; ready to accept new transmissions.
Definition driver.h:171
@ DRAINING
All channels submitted; still transmitting.
Definition driver.h:173

References fl::IChannelDriver::DriverState::DRAINING, FL_ERROR, fl::FL_NO_EXCEPT, fl::detail::getWaitSpinBudgetUs(), fl::net::NetworkDetector::isAnyNetworkActive(), fl::micros(), fl::millis(), poll(), fl::IChannelDriver::DriverState::READY, state, and fl::task::yield_system().

Referenced by operator=(), waitForReady(), and waitForReadyOrDraining().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: