| 11 | |
| 12 | template<typename Condition> |
| 13 | bool IChannelDriver::waitForCondition(Condition condition, u32 timeoutMs) { |
| 14 | const u32 startTime = timeoutMs > 0 ? millis() : 0; |
| 15 | |
| 16 | // Tier 1: instant non-blocking check. |
| 17 | if (condition()) { |
| 18 | return true; |
| 19 | } |
| 20 | |
| 21 | // Tier 2: bounded microsecond spin (#2818). Catches short DMA tails |
| 22 | // without paying the >=1-tick floor below. Budget runtime-tunable via |
| 23 | // FastLED.setWaitSpinBudgetUs(N); 0 disables. |
| 24 | { |
| 25 | const u32 spinBudget = fl::detail::getWaitSpinBudgetUs(); |
| 26 | if (spinBudget > 0) { |
| 27 | const u32 spinStart = fl::micros(); |
| 28 | while ((fl::micros() - spinStart) < spinBudget) { |
| 29 | if (condition()) { |
| 30 | return true; |
| 31 | } |
| 32 | if (timeoutMs > 0 && (millis() - startTime) >= timeoutMs) { |
| 33 | FL_ERROR("Timeout occurred while waiting for condition"); |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | while (!condition()) { |
| 41 | // Check timeout if specified |
| 42 | if (timeoutMs > 0 && (millis() - startTime >= timeoutMs)) { |
| 43 | FL_ERROR("Timeout occurred while waiting for condition"); |
| 44 | return false; // Timeout occurred |
| 45 | } |
| 46 | |
| 47 | // Adaptive yield (refs #2815, generalizes the #2493 ESP32-P4 carve-out): |
| 48 | // see the matching comment in ChannelManager::waitForCondition for the |
| 49 | // full rationale. The deep yield is only needed when a radio is |
| 50 | // actually up; otherwise the FreeRTOS tick floor is pure timing drift. |
| 51 | if (fl::NetworkDetector::isAnyNetworkActive()) { |
| 52 | // Radio active: keep WiFi/lwIP/BT alive with the deep yield. |
| 53 | task::run(250, task::ExecFlags::SYSTEM); |
| 54 | } else { |
| 55 | // No radio: fast yield, no FreeRTOS tick floor. |
| 56 | task::run(0, task::ExecFlags::SYSTEM); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return true; // Condition met |
| 61 | } |
| 62 | |
| 63 | bool IChannelDriver::waitForReady(u32 timeoutMs) { |
| 64 | // wait until the driver is in a READY state. |
nothing calls this directly
no test coverage detected