From 1d6cbb222ced226fcb482c0b19130774d181c8f8 Mon Sep 17 00:00:00 2001 From: Luke Hoersten Date: Sat, 13 Jun 2026 23:01:23 -0500 Subject: M7: touch + outbound /state POST to Scrypted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit state_client.{h,c}: - Worker task drains a depth-1 queue (xQueueOverwrite gives the replace-on-full semantics from the spec — in-flight POST is never cancelled; the next queued entry is overwritten by newer state). - esp_http_client POST to /state with Content-Type application/ json, User-Agent ScryptedViewport/, Connection: close, 1s timeout. Body: {"viewport":"","state":"wake"|"sleep"}. - Any non-2xx or transport error increments state_post_failures and is otherwise ignored. Silently drops if no Scrypted URL is configured. touch.{h,c}: - FT5426 capacitive touch on the shared I2C bus at 0x38. - 30ms polling task; tracks down/up transitions, detects taps as down-then-up within 500ms with a 150ms debounce. - On tap, toggles wake/sleep via state_machine_set_local(), which drives the local transition AND fires state_client_post() at Scrypted. state_machine adds state_machine_set_local(): runs the same transition as state_machine_set() then enqueues an outbound POST. Idle-timer expiry now uses this path so Scrypted sees idle-driven sleeps too (the spec's "tighten the race with /frame 409" path stays in place as the fallback). display.h exposes display_i2c_bus(); touch.c uses it instead of re-initializing the same I2C port. app_main starts state_client right after the state machine and starts touch after display init (touch is skipped if display isn't up since they share the bus). CMakeLists.txt: add esp_http_client to REQUIRES. Build clean against ESP-IDF 5.4 (binary ~860 KB; jumped ~210 KB from M6 because esp_http_client + cJSON path pulls in tcp_transport, mbedtls, http_parser). TESTING.md M7: flask-based test receiver, tap dispatch verification, failure-path check (kill receiver, confirm counter increments), queue-coalescing behavior, no-POST-on-Scrypted-initiated, no-POST- when-unconfigured. --- main/state_machine.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'main/state_machine.c') diff --git a/main/state_machine.c b/main/state_machine.c index 22f3f56..2808738 100644 --- a/main/state_machine.c +++ b/main/state_machine.c @@ -5,6 +5,7 @@ #include "esp_timer.h" #include "display.h" +#include "state_client.h" static const char *TAG = "state"; @@ -28,8 +29,7 @@ static void disarm_idle_timer(void) static void idle_timer_fired(void *arg) { ESP_LOGI(TAG, "idle timer expired — sleeping"); - state_machine_set(VIEWPORT_STATE_ASLEEP); - // TODO M7: state_client_post(VIEWPORT_STATE_ASLEEP); // tell Scrypted + state_machine_set_local(VIEWPORT_STATE_ASLEEP); } esp_err_t state_machine_init(void) @@ -80,6 +80,16 @@ void state_machine_frame_painted(void) if (awake) arm_idle_timer_unlocked(); } +void state_machine_set_local(viewport_run_state_t target) +{ + // For idempotent no-op (already in target), state_machine_set returns OK + // without changing state; we still call state_client to keep Scrypted in + // sync if it's drifted. State POSTs are idempotent on both ends. + esp_err_t err = state_machine_set(target); + if (err != ESP_OK) return; // unconfigured / invalid + state_client_post(target); +} + viewport_run_state_t state_machine_current(void) { viewport_state_lock(); -- cgit v1.2.3