DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
sleep_sched.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file sleep_sched.cpp
6 * @brief Dynamic sleep-cycle scheduler decision core (see sleep_sched.h).
7 */
8
10
11#if DETWS_ENABLE_SLEEP_SCHED
12
13uint32_t detws_sleep_next(uint32_t now, uint32_t last_active_ms, const DetwsSleepCfg *cfg)
14{
15 if (!cfg)
16 return 0;
17
18 uint32_t idle = (uint32_t)(now - last_active_ms); // wrap-safe unsigned delta
19 if (idle < cfg->idle_ms)
20 return 0; // active recently: stay awake
21
22 uint32_t ceil_ms = cfg->max_ms < cfg->min_ms ? cfg->min_ms : cfg->max_ms;
23 if (cfg->ramp_ms == 0)
24 return ceil_ms; // no ramp: go straight to the deepest window
25
26 // Grow the window by doubling for every ramp_ms of idle past the threshold, clamped to the ceiling.
27 // The pre-shift ceiling check keeps the doubling from overflowing.
28 uint32_t doublings = (idle - cfg->idle_ms) / cfg->ramp_ms;
29 uint32_t window = cfg->min_ms ? cfg->min_ms : 1;
30 for (uint32_t i = 0; i < doublings; i++)
31 {
32 if (window >= ceil_ms || window > ceil_ms / 2)
33 {
34 window = ceil_ms;
35 break;
36 }
37 window <<= 1;
38 }
39 if (window > ceil_ms)
40 window = ceil_ms;
41 if (window < cfg->min_ms)
42 window = cfg->min_ms;
43 return window;
44}
45
46#endif // DETWS_ENABLE_SLEEP_SCHED
Dynamic sleep-cycle scheduler (DETWS_ENABLE_SLEEP_SCHED).