From 57c93fd2978ed5b5e64f4620803b7a2cd7767ed2 Mon Sep 17 00:00:00 2001 From: Luke Hoersten Date: Sat, 13 Jun 2026 22:52:41 -0500 Subject: M6: state machine — POST /state, idle timer, /frame 409 guard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit state_machine.{h,c} — central wake/sleep transitions: - state_machine_init() creates the esp_timer one-shot for the idle timer. - state_machine_set(target) is idempotent and atomic. On AWAKE: backlight on, idle timer (re)armed. On ASLEEP: idle timer cancelled, backlight off. Rejects with INVALID_STATE when the device is unconfigured. - state_machine_frame_painted() restarts the idle timer if awake; called by /frame after each successful paint. - Idle-timer callback transitions to ASLEEP. TODO M7 hook: outbound POST {viewport, state:sleep} to /state. http_api.c: - POST /state: parse {state}, accept "wake"/"sleep", reject others 400. Unconfigured device → 409 "device unconfigured". Already-in-state → 204 (idempotent no-op). Successful transition → 204. - POST /frame: 409 Conflict when state != AWAKE. After successful paint, call state_machine_frame_painted() so the idle clock keeps resetting while frames stream. app_main: - Initialize state_machine before http_api so the route handler can drive it from request 0. - After display_init(), reconcile the panel with the boot state: UNCONFIGURED → test pattern (placeholder until M8 IP screen) ASLEEP → display_sleep() so a configured device boots dark AWAKE → leave on (not reached on fresh boot) Disable path: idle_timeout_ms=0 in /config means the timer is never armed and a wake state persists until /state {sleep} or a power cycle. Build clean against ESP-IDF 5.4 (binary ~645 KB). TESTING.md M6 expands with idempotency checks, 409-when-asleep, 409-when- unconfigured, idle-timer firing within idle_timeout_ms+slack, /frame restarting the idle timer, idle-timer disable via idle_timeout_ms=0, and the M7 dependency note about the missing outbound sleep POST. --- main/state_machine.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 main/state_machine.h (limited to 'main/state_machine.h') diff --git a/main/state_machine.h b/main/state_machine.h new file mode 100644 index 0000000..176b93e --- /dev/null +++ b/main/state_machine.h @@ -0,0 +1,20 @@ +#pragma once + +#include "esp_err.h" +#include "viewport_state.h" + +esp_err_t state_machine_init(void); + +// Transition to AWAKE or ASLEEP. Idempotent: no-op if already there. +// Returns ESP_ERR_INVALID_STATE if the device is unconfigured. +// Side-effects (under one critical section): +// AWAKE -> backlight on, idle timer (re)armed +// ASLEEP -> backlight off, idle timer cancelled, framebuffer discarded +esp_err_t state_machine_set(viewport_run_state_t target); + +// Called by /frame after a successful paint. Restarts the idle timer +// while awake; no-op otherwise. +void state_machine_frame_painted(void); + +// Snapshot of current state. +viewport_run_state_t state_machine_current(void); -- cgit v1.2.3