From 237d732f52a5ae93ab4077a6512b969d7a15017a Mon Sep 17 00:00:00 2001 From: nick Date: Mon, 15 Jun 2026 15:44:19 -0600 Subject: [PATCH] dashboard: queue latest relay target states --- .../esp32-s3-dashboard/dashboard_config.h | 2 +- .../esp32-s3-dashboard/esp32-s3-dashboard.ino | 55 +++++++++++-------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/firmware/esp32-s3-dashboard/dashboard_config.h b/firmware/esp32-s3-dashboard/dashboard_config.h index 3d6426d..34b6250 100644 --- a/firmware/esp32-s3-dashboard/dashboard_config.h +++ b/firmware/esp32-s3-dashboard/dashboard_config.h @@ -1,6 +1,6 @@ #pragma once -static const char *DASHBOARD_VERSION = "0.1.73-relay-command-priority"; +static const char *DASHBOARD_VERSION = "0.1.74-relay-latest-state-queue"; // Update these if your Cargo ESP AP credentials are different. static const char *CARGO_WIFI_SSID = "OverlandController"; diff --git a/firmware/esp32-s3-dashboard/esp32-s3-dashboard.ino b/firmware/esp32-s3-dashboard/esp32-s3-dashboard.ino index bee664f..2abd020 100644 --- a/firmware/esp32-s3-dashboard/esp32-s3-dashboard.ino +++ b/firmware/esp32-s3-dashboard/esp32-s3-dashboard.ino @@ -62,11 +62,9 @@ static bool metadata_refresh_requested = true; static bool was_wifi_connected = false; static bool overview_screen_created = false; -static bool relay_command_pending = false; -static String pending_relay_id; -static bool pending_relay_state = false; -static int pending_relay_index = -1; -static bool pending_relay_previous_state = false; +static bool relay_command_pending[6] = {false}; +static bool pending_relay_state[6] = {false}; +static bool pending_relay_previous_state[6] = {false}; static String last_battery_current_text; static String last_battery_estimate_text; @@ -529,7 +527,7 @@ static void relay_button_event_cb(lv_event_t *event) intptr_t index_value = reinterpret_cast(lv_event_get_user_data(event)); int index = static_cast(index_value); - if (index < 0 || index >= relay_count || relay_command_pending) { + if (index < 0 || index >= relay_count) { return; } @@ -539,11 +537,13 @@ static void relay_button_event_cb(lv_event_t *event) relay_states[index] = next_state; update_relay_buttons(); - pending_relay_id = relay_ids[index]; - pending_relay_state = next_state; - pending_relay_index = index; - pending_relay_previous_state = previous_state; - relay_command_pending = true; + if (!relay_command_pending[index]) { + pending_relay_previous_state[index] = previous_state; + } + + // Latest-state queue: rapid taps overwrite this relay's desired target. + pending_relay_state[index] = next_state; + relay_command_pending[index] = true; String text = "Queued "; text += relay_names[index]; @@ -555,26 +555,37 @@ static void relay_button_event_cb(lv_event_t *event) static void process_pending_relay_command() { - if (!relay_command_pending || api_request_in_progress) { + if (api_request_in_progress) { return; } - String relay_id = pending_relay_id; - bool next_state = pending_relay_state; - int index = pending_relay_index; - bool previous_state = pending_relay_previous_state; + int index = -1; + for (int i = 0; i < relay_count; i++) { + if (relay_command_pending[i]) { + index = i; + break; + } + } + + if (index < 0) { + return; + } + + String relay_id = relay_ids[index]; + bool next_state = pending_relay_state[index]; + bool previous_state = pending_relay_previous_state[index]; + + // Clear before posting. If the user taps again after this command returns, + // the new desired state will be queued as a fresh command. + relay_command_pending[index] = false; bool ok = post_relay_state(relay_id, next_state); - relay_command_pending = false; - pending_relay_id = ""; - pending_relay_index = -1; - if (ok) { set_label_text_if_changed(system_status_label, last_system_status_text, "Relay command sent"); - request_fast_status_refresh(); } else { - if (index >= 0 && index < relay_count) { + // Only revert if no newer desired state was queued for this relay. + if (!relay_command_pending[index] && index >= 0 && index < relay_count) { relay_states[index] = previous_state; update_relay_buttons(); }