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

◆ waitForState()

bool fl::ChannelManager::waitForState ( bool allowDraining,
u32 timeoutMs )
private

Wait for READY, optionally accepting DRAINING, with shared wait logic.

Parameters
allowDrainingWhether DRAINING also satisfies the wait
timeoutMsOptional timeout in milliseconds (0 = no timeout)
Returns
true if condition was met, false if timeout occurred
Note
Runs async_run() on each iteration and delays intelligently to avoid busy-waiting

Definition at line 476 of file manager.cpp.hpp.

476 {
477 const auto condition = [this, allowDraining]() {
478 const auto state = poll().state;
480 (allowDraining && state == IChannelDriver::DriverState::DRAINING);
481 };
482 const u32 startTime = timeoutMs > 0 ? millis() : 0;
483
484 // Tier 1: instant non-blocking check (avoid micros() / millis() cost on
485 // the common already-ready path).
486 if (condition()) {
487 return true;
488 }
489
490 // Tier 2: bounded microsecond spin (#2818). Catches short DMA tails
491 // (APA102 small strips, WS2812B <=8 LEDs) without paying the >=1-tick
492 // floor of the cooperator yield below. Budget is runtime-tunable via
493 // FastLED.setWaitSpinBudgetUs(N); set to 0 to disable.
494 {
495 const u32 spinBudget = fl::detail::getWaitSpinBudgetUs();
496 if (spinBudget > 0) {
497 const u32 spinStart = fl::micros();
498 while ((fl::micros() - spinStart) < spinBudget) {
499 if (condition()) {
500 return true;
501 }
502 if (timeoutMs > 0 && (millis() - startTime) >= timeoutMs) {
503 FL_ERROR("ChannelManager: Timeout occurred while waiting for condition");
504 return false;
505 }
506 }
507 }
508 }
509
510 while (!condition()) {
511 // Check timeout if specified
512 if (timeoutMs > 0 && (millis() - startTime >= timeoutMs)) {
513 FL_ERROR("ChannelManager: Timeout occurred while waiting for condition");
514 return false; // Timeout occurred
515 }
516
517 const u32 sliceMs = pollNeededWaitSliceMs(startTime, timeoutMs);
518 if (sliceMs == 0) {
519 return false;
520 }
521 if (waitForPollNeededSignal(sliceMs)) {
522 continue;
523 }
524
525 // Adaptive yield (refs #2815, generalizes the #2493 ESP32-P4 carve-out):
526 //
527 // The 1-tick (>=1 ms at CONFIG_FREERTOS_HZ=1000) floor only exists to
528 // keep WiFi / lwIP / BT controller tasks alive while we are inside the
529 // channel wait loop (#2254). When no radio is actually up, that floor
530 // is pure timing drift -- visible as the regression reported in #2420
531 // and as the per-frame cost the #2493 ESP32-P4 carve-out was avoiding.
532 //
533 // NetworkDetector::isAnyNetworkActive() is the runtime version of the
534 // "is a radio up?" question. On non-ESP32 platforms and on ESP32-P4
535 // (no radio silicon) it folds to a constant `false`, so this is
536 // strictly a perf win for the common single-strip / no-WiFi case
537 // without losing the WiFi-friendly behavior when a radio is active.
539 // Radio active: keep WiFi/lwIP/BT alive with the deep yield.
541 } else {
542 // No radio: fast yield, no FreeRTOS tick floor.
544 }
545 }
546
547 return true; // Condition met
548}
TestState state
u32 pollNeededWaitSliceMs(u32 startTime, u32 timeoutMs) const FL_NO_EXCEPT
IChannelDriver::DriverState poll() FL_NO_EXCEPT
Poll all registered drivers and return aggregate state.
bool waitForPollNeededSignal(u32 timeoutMs) FL_NO_EXCEPT
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(), pollNeededWaitSliceMs(), fl::IChannelDriver::DriverState::READY, state, waitForPollNeededSignal(), and fl::task::yield_system().

Referenced by waitForReady(), and waitForReadyOrDraining().

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