Repository navigation
Description
Phase 1 of #2815 — Tiered wait + NetworkDetector-gated yield
Tactical, no-API-change first phase of the channel-manager wait redesign. Eliminates the unconditional ≥1 ms FreeRTOS-tick floor by inserting a 250 µs micro-spin before the cooperator-friendly deep yield, and gates the deep yield on runtime radio activity via the existing fl::NetworkDetector.
Design rationale and the multi-strategy comparison live in #2815. This issue is the implementation spec.
Scope (one PR)
Files modified
src/fl/channels/manager.cpp.hpp— replace the body ofChannelManager::waitForCondition(lines 364-388).src/fl/channels/driver.cpp.hpp— replace the body ofIChannelDriver::waitForCondition(lines 11-35) with the identical tiered logic. Extract a shared helper (e.g.fl::detail::tieredWaitYield(elapsed_us)insrc/fl/channels/detail/wait_yield.h) to keep them lock-step.src/platforms/esp/32/util/network_detector.{h,cpp.hpp}— promoted target forfl::NetworkDetector. Move fromsrc/platforms/esp/32/drivers/rmt/rmt_5/network_detector.{h,cpp.hpp}. Leave a forwarding shim at the old path so RMT5-internal callers compile unchanged.src/FastLED.h— declareCFastLED::setWaitSpinBudgetUs(u32)per the Public Settings Pattern. New compile-time macroFASTLED_WAIT_SPIN_BUDGET_US(default250).src/FastLED.cpp.hpp— implementsetWaitSpinBudgetUs. Static storage for the runtime budget.
Files deleted / cleaned
- The
#if defined(FL_IS_ESP_32P4)carve-out in both wait sites is removed. On P4,NetworkDetector::isAnyNetworkActive()returnsfalsevia weak-symbol fallback (no WiFi/BT components linked), so the runtime gate selects the fast path automatically — same effect, single code path.
Tiered wait pseudocode
// Shared helper, called by both ChannelManager::waitForCondition and
// IChannelDriver::waitForCondition.
template<typename Condition>
bool tieredWait(Condition condition, u32 timeoutMs) {
const u32 start_ms = millis();
// Tier 1: instant non-blocking check (already-READY case).
if (condition()) return true;
// Tier 2: bounded microsecond spin — catches DMA tail without
// paying the FreeRTOS tick floor.
const u32 spin_budget_us = fl::detail::getWaitSpinBudgetUs(); // 250 default
const u32 spin_start_us = fl::micros();
while ((fl::micros() - spin_start_us) < spin_budget_us) {
if (condition()) return true;
}
// Tier 3: cooperator-friendly deep yield. NetworkDetector gates this
// at runtime — when no radio is up, drop the tick floor entirely.
#if FL_IS_ESP32
const bool radio_up = fl::NetworkDetector::isAnyNetworkActive();
#else
constexpr bool radio_up = false; // bare-metal: no radio cooperator needed
#endif
while (!condition()) {
if (timeoutMs > 0 && (millis() - start_ms >= timeoutMs)) {
FL_ERROR("waitForCondition timeout");
return false;
}
if (radio_up) {
task::run(250, task::ExecFlags::SYSTEM); // vTaskDelay(>=1 tick) — keeps WiFi/lwIP cooperator alive
} else {
task::run(0, task::ExecFlags::SYSTEM); // fl::yield() — no tick floor
}
}
return true;
}Public API surface
Per the project's Public Settings Pattern (CLAUDE.md → agents/docs/cpp-standards.md), the runtime knob lives on CFastLED:
// src/FastLED.h
class CFastLED {
public:
// Set the micro-spin budget used by waitForCondition before falling
// through to the cooperator yield. Default: FASTLED_WAIT_SPIN_BUDGET_US (250).
void setWaitSpinBudgetUs(fl::u32 budget_us) FL_NOEXCEPT;
};FASTLED_WAIT_SPIN_BUDGET_US may be #defined before #include <FastLED.h> to change the compile-time default.
Test plan
Host-side (tests/test_channel_wait_tiered.cpp, new file):
- Drive
tieredWaitwith acondition()lambda that flips true after a configurable simulated delay. - Assert: condition flipping at 100 µs → caught in Tier 2, returns within 200 µs.
- Assert: condition flipping at 5 ms → reaches Tier 3, returns within 6 ms (no tick-floor in host build).
- Assert: timeout path returns false after
timeoutMs.
On-device (extension to examples/AutoResearch/AutoResearch.ino, no new .ino — project policy):
- Add a
--timing-drifttest mode that mirrors the [Bug] Compounded timing inaccuracy on master compared to 3.10.3 #2420 reporter's sketch (35-LED WS2812B, paced sequence with target wall time). - Report per-frame
FastLED.show()latency and total sequence wall time. - Run via
bash autoresearch <board> --timing-drift. - Acceptance: sequence completes within ±10 ms of expected (2500 ms target → 2490–2510 ms).
Acceptance criteria
- ESP32-S3 single-strip, no
WiFi.begin()→FastLED.show()for 35-LED WS2812B completes within ~1.2× transmit time (≤1.3 ms wall, vs current ~2 ms). - ESP32-S3 with
WiFi.begin()connected → no lwIP throughput regression vs current master (manual re-validation of the ESP32-S3: I2S driver starves websockets, SPI output incorrect, Audio driver FPS impact #2254 websocket scenario). - ESP32-P4 PARLIO 12×256 → no regression vs current
task::run(0)fast-path performance. - The [Bug] Compounded timing inaccuracy on master compared to 3.10.3 #2420 reporter's sketch produces a 2500 ms sequence within ±10 ms.
NetworkDetectorbuilds and links correctly fromsrc/platforms/esp/32/util/network_detector.h; oldrmt/rmt_5/network_detector.his a forwarding shim that still compiles.FL_IS_ESP_32P4carve-out is removed from bothwaitForConditionsites.- Teensy 4.x build: no regression;
task::run(SYSTEM)falls through toyield()as today. - AVR / RP2040 builds: no regression.
Out of scope (Phase 2+)
- Per-driver
waitDone()virtual and ISR-Take fast path → Phase 2. - Multi-driver fan-in (shared sem or task notification) → Phase 3.
- Decoupled async
show()→ Phase 4.
Cross-references
- perf(channels): replace =1 FreeRTOS-tick busy-sleep in waitForReady with ISR-completion + NetworkDetector-gated yield (refs #2420, #2254, #2493) #2815 — design rationale and strategy comparison
- [Bug] Compounded timing inaccuracy on master compared to 3.10.3 #2420 — reporter's bug (target fix)
- ESP32-S3: I2S driver starves websockets, SPI output incorrect, Audio driver FPS impact #2254 — origin of the deep-yield-for-WiFi rationale (preserved via NetworkDetector gate)
- ESP32-P4 PARLIO: FastLED.show() appears to block for ~16.7 ms before WS2812 data burst begins #2493 — PARLIO 16.7 ms block (origin of the
FL_IS_ESP_32P4carve-out, now removed)
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
perf(channels): add waitDone() virtual + manager N=1 fast path (Phase…
perf(channels): add Tier-2 microsecond spin to waitForCondition (Phas…
fix(core): adaptive yield in show()'s refresh throttle (FastLED#3762, f…