// power_mgr.h — the deep-sleep on/off switch and everything it interferes with. // // TWO MODES, ONE FLAG (`g_cfg.deep_sleep_enabled`, pushable from the WebUI) // // AWAKE (default, mains powered) // Camera stays initialised, the PSRAM ring holds ~10 s of pre-trigger // history, commands execute within a loop tick, streaming works on demand. // This is the mode that makes good bird photos. // // SLEEP (battery / solar) // Wake on the LD2410 OUT pin (EXT1) or the timer, capture a post-trigger // burst, upload, drain the command queue, sleep again. // You give up, unavoidably: // - the pre-trigger ring: the camera is off, so there is no history. // Back to SPEC C4's 1-3 s latency; expect to miss fast arrivals. // - on-demand streaming: a stream_start command is only seen at the next // wake. The device then holds itself awake for the session and sleeps // after (see powerInhibit below). // - command latency: worst case equals deep_sleep_s (SPEC C1). // // HONEST NOTE ON WHAT DEEP SLEEP ACTUALLY SAVES HERE // The XIAO ESP32S3 sleeps at tens of µA, but the LD2410 draws roughly 80 mA // continuously and must stay powered to act as a wake source. So with // sleep_wake_on_radar = true, system current is dominated by the radar and the // ESP sleeping buys you very little. Deep sleep is only a real power win if you // either (a) set sleep_wake_on_radar = false and wake on the timer alone, with // the radar power-gated via PIN_RADAR_EN, or (b) fit a low-quiescent PIR such // as the AM312 on PIN_AUX_ADC as the wake source and use the radar only for // confirmation once awake. The switch is implemented either way; this is a // wiring decision, not a firmware one. #pragma once #include enum WakeCause : uint8_t { WAKE_POWER_ON = 0, // cold boot, or reset WAKE_RADAR = 1, // EXT1 on the LD2410 OUT pin WAKE_TIMER = 2, WAKE_OTHER = 3, }; // Reasons the device must not sleep right now. Refcounted, so nested holders // (a stream inside a wake_and_hold, say) cannot cut each other short. enum SleepInhibit : uint8_t { INHIBIT_BOOT = 0, // released once the first heartbeat succeeds INHIBIT_UPLOAD = 1, INHIBIT_STREAM = 2, // the "stream interferes with sleep" case INHIBIT_OTA = 3, // sleeping mid-flash bricks the device (SPEC C6) INHIBIT_HOLD = 4, // wake_and_hold command INHIBIT_COUNT }; // Reads the wake cause and restores the counters kept in RTC memory. // Call first in setup(), before anything else looks at boot state. void powerBegin(); WakeCause powerWakeCause(); const char* powerWakeCauseName(); uint32_t powerBootCount(); // survives deep sleep, resets on power loss uint32_t powerLastSleepSeconds(); // how long the most recent sleep lasted uint32_t powerTotalSleepSeconds(); // cumulative since power-on // Is sleeping permitted at all right now, ignoring the idle timer? // // This is where streaming and sleeping are reconciled. A sleeping device // cannot be told to start a stream (SPEC C1), so with `stream_blocks_sleep` // set — the default — merely *enabling* streaming keeps the device awake and // reachable, whether or not a session is running. Clear that flag to let it // sleep anyway and accept that `stream_start` waits for the next wake. // // Returns false, with a reason, when: deep sleep is switched off, streaming // is enabled and blocks sleep, or an inhibit is held. bool powerSleepAllowed(); const char* powerSleepBlockedReason(); // Sleep inhibits. Always pair them; prefer the RAII guard below. void powerInhibit(SleepInhibit reason); void powerRelease(SleepInhibit reason); bool powerIsInhibited(); const char* powerInhibitSummary(); // for logs and heartbeats struct SleepGuard { SleepInhibit r; explicit SleepGuard(SleepInhibit reason) : r(reason) { powerInhibit(r); } ~SleepGuard() { powerRelease(r); } }; // Mark that something worth staying awake for just happened. Resets the idle // timer, so a busy feeder keeps the device up instead of thrashing sleep/wake. void powerNoteActivity(); // Hold awake for at least `seconds` (the wake_and_hold command). void powerHoldAwake(uint32_t seconds); // Would the next powerMaybeSleep() actually sleep? Lets the caller do the // things that must happen before the radio goes off — a final status push, // say — without doing them on every loop tick. bool powerWouldSleepNow(); // Called every loop tick. Sleeps if, and only if: // deep_sleep_enabled && no inhibit && idle longer than sleep_idle_timeout_s // Returns false if it decided to stay awake; does not return if it slept. bool powerMaybeSleep(); // Sleep now regardless of the idle timer, but still never while inhibited. // `seconds` == 0 uses g_cfg.deep_sleep_s. Used by the `sleep_now` command and // by the "every failure path ends in sleep" rule of SPEC §4.3. void powerSleepNow(uint32_t seconds = 0); // Sleep through the night (NIGHT_SLEEP). Unlike powerSleepNow() this works // even when deep_sleep_enabled is false, because "sleep at night" is a // separate decision from "sleep between events" — a mains-powered camera // still has no reason to be awake at 03:00 with no illuminator fitted. // Never wakes on the radar: the point is to ignore the night, not to // photograph every cat that walks past. No-op if it is not night. void powerSleepUntilMorning(); // True while the device is running in SLEEP mode. Other subsystems consult // this to decide whether the ring buffer is worth maintaining. bool powerSleepModeActive(); // Applies a change to deep_sleep_enabled that arrived from the endpoint. // Turning sleep ON never takes effect instantly: the device first finishes the // current wake so the ack for the config command still reaches the Pi. void powerOnConfigChanged(bool was_enabled);