FastLED 3.10.6
Loading...
Searching...
No Matches

◆ waitForCondition()

template<typename Condition>
bool fl::ChannelManager::waitForCondition ( Condition condition,
u32 timeoutMs = 1000 )
private

Wait until a condition is met, with check-pump-delay logic.

Parameters
conditionFunction that returns true when waiting should stop
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 450 of file manager.cpp.hpp.

450 {
451 const u32 startTime = timeoutMs > 0 ? millis() : 0;
452
453 // Tier 1: instant non-blocking check (avoid micros() / millis() cost on
454 // the common already-ready path).
455 if (condition()) {
456 return true;
457 }
458
459 // Tier 2: bounded microsecond spin (#2818). Catches short DMA tails
460 // (APA102 small strips, WS2812B <=8 LEDs) without paying the >=1-tick
461 // floor of the cooperator yield below. Budget is runtime-tunable via
462 // FastLED.setWaitSpinBudgetUs(N); set to 0 to disable.
463 {
464 const u32 spinBudget = fl::detail::getWaitSpinBudgetUs();
465 if (spinBudget > 0) {
466 const u32 spinStart = fl::micros();
467 while ((fl::micros() - spinStart) < spinBudget) {
468 if (condition()) {
469 return true;
470 }
471 if (timeoutMs > 0 && (millis() - startTime) >= timeoutMs) {
472 FL_ERROR("ChannelManager: Timeout occurred while waiting for condition");
473 return false;
474 }
475 }
476 }
477 }
478
479 while (!condition()) {
480 // Check timeout if specified
481 if (timeoutMs > 0 && (millis() - startTime >= timeoutMs)) {
482 FL_ERROR("ChannelManager: Timeout occurred while waiting for condition");
483 return false; // Timeout occurred
484 }
485
486 const u32 sliceMs = pollNeededWaitSliceMs(startTime, timeoutMs);
487 if (sliceMs == 0) {
488 return false;
489 }
490 if (waitForPollNeededSignal(sliceMs)) {
491 continue;
492 }
493
494 // Adaptive yield (refs #2815, generalizes the #2493 ESP32-P4 carve-out):
495 //
496 // The 1-tick (>=1 ms at CONFIG_FREERTOS_HZ=1000) floor only exists to
497 // keep WiFi / lwIP / BT controller tasks alive while we are inside the
498 // channel wait loop (#2254). When no radio is actually up, that floor
499 // is pure timing drift -- visible as the regression reported in #2420
500 // and as the per-frame cost the #2493 ESP32-P4 carve-out was avoiding.
501 //
502 // NetworkDetector::isAnyNetworkActive() is the runtime version of the
503 // "is a radio up?" question. On non-ESP32 platforms and on ESP32-P4
504 // (no radio silicon) it folds to a constant `false`, so this is
505 // strictly a perf win for the common single-strip / no-WiFi case
506 // without losing the WiFi-friendly behavior when a radio is active.
508 // Radio active: keep WiFi/lwIP/BT alive with the deep yield.
510 } else {
511 // No radio: fast yield, no FreeRTOS tick floor.
513 }
514 }
515
516 return true; // Condition met
517}
u32 pollNeededWaitSliceMs(u32 startTime, u32 timeoutMs) const FL_NO_EXCEPT
bool waitForPollNeededSignal(u32 timeoutMs) FL_NO_EXCEPT
static bool isAnyNetworkActive() FL_NO_EXCEPT
#define FL_ERROR(...)
Definition log.h:372
fl::u32 getWaitSpinBudgetUs() FL_NO_EXCEPT
Get the current tiered-wait spin budget (microseconds).
void run(fl::u32 microseconds, ExecFlags flags)
Run selected task subsystems.
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
fl::u32 micros()
Universal microsecond timer - returns microseconds since system startup.

References FL_ERROR, fl::detail::getWaitSpinBudgetUs(), fl::net::NetworkDetector::isAnyNetworkActive(), fl::micros(), fl::millis(), pollNeededWaitSliceMs(), fl::task::run(), fl::task::SYSTEM, and waitForPollNeededSignal().

Referenced by waitForReady(), and waitForReadyOrDraining().

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