From 24aad99533e122df946b3eb007f11b950058ea3e Mon Sep 17 00:00:00 2001 From: electron-rare <108685187+electron-rare@users.noreply.github.com> Date: Wed, 11 Mar 2026 06:48:30 +0100 Subject: [PATCH] refactor(esp32): extract warning siren scene block from main.cpp Co-Authored-By: easter_egg <108685187+electron-rare@users.noreply.github.com> --- ui_freenove_allinone/src/app/main.cpp | 71 +------------------ .../src/app/scene_warning_siren_fx.inc | 70 ++++++++++++++++++ 2 files changed, 71 insertions(+), 70 deletions(-) create mode 100644 ui_freenove_allinone/src/app/scene_warning_siren_fx.inc diff --git a/ui_freenove_allinone/src/app/main.cpp b/ui_freenove_allinone/src/app/main.cpp index 9726265..4c2016a 100644 --- a/ui_freenove_allinone/src/app/main.cpp +++ b/ui_freenove_allinone/src/app/main.cpp @@ -1001,76 +1001,7 @@ void applyLcdBacklight(uint8_t level) { #include "scene_ws2812_fx.inc" -void updateWarningSirenFx(uint32_t now_ms) { - SceneWarningSirenFxState& siren = g_scene_warning_siren_fx; - - if (!siren.enabled) { - if (siren.remote_started) { - const bool stop_ok = sendWarningSirenSync("STOP", 0U); - ZACUS_RL_LOG_MS(1200U, "[WARN_SIREN] remote stop ok=%u\n", stop_ok ? 1U : 0U); - } - siren.remote_started = false; - siren.started_ms = 0U; - siren.next_beat_ms = 0U; - siren.next_retry_ms = 0U; - siren.local_fallback_logged = false; - return; - } - - if (!siren.local_backend_available && !siren.local_fallback_logged) { - Serial.println("[WARN_SIREN] local backend unavailable, syncing siren to A252 only"); - siren.local_fallback_logged = true; - } - - if (!siren.remote_started) { - if (!warningSirenRemoteSyncReady()) { - siren.next_retry_ms = now_ms + 2000U; - ZACUS_RL_LOG_MS(2000U, "[WARN_SIREN] remote sync waiting peer\n"); - return; - } - if (siren.next_retry_ms != 0U && static_cast(now_ms - siren.next_retry_ms) < 0) { - return; - } - const bool start_ok = sendWarningSirenSync("START", 220U); - if (!start_ok) { - siren.next_retry_ms = now_ms + 2000U; - ZACUS_RL_LOG_MS(1000U, "[WARN_SIREN] remote start failed, retrying\n"); - return; - } - siren.remote_started = true; - siren.started_ms = now_ms; - siren.next_retry_ms = 0U; - siren.next_beat_ms = now_ms + kWarningSirenBeatIntervalMs; - Serial.println("[WARN_SIREN] remote start"); - } - - if (siren.next_beat_ms != 0U && static_cast(now_ms - siren.next_beat_ms) < 0) { - return; - } - - if (!warningSirenRemoteSyncReady()) { - siren.remote_started = false; - siren.next_beat_ms = 0U; - siren.next_retry_ms = now_ms + 2000U; - ZACUS_RL_LOG_MS(2000U, "[WARN_SIREN] remote link lost, rearm\n"); - return; - } - - const uint32_t period_ms = 1400U; - const uint32_t half_period_ms = period_ms / 2U; - const uint32_t elapsed_ms = now_ms - siren.started_ms; - const uint32_t phase_ms = elapsed_ms % period_ms; - const uint32_t ramp = (phase_ms <= half_period_ms) ? phase_ms : (period_ms - phase_ms); - int strength = 160 + static_cast((95U * ramp) / (half_period_ms == 0U ? 1U : half_period_ms)); - if (strength > 255) { - strength = 255; - } - const bool beat_ok = sendWarningSirenSync("BEAT", static_cast(strength)); - if (!beat_ok) { - ZACUS_RL_LOG_MS(1000U, "[WARN_SIREN] remote beat send failed\n"); - } - siren.next_beat_ms = now_ms + kWarningSirenBeatIntervalMs; -} +#include "scene_warning_siren_fx.inc" #include "scene_uson_proto_audio.inc" diff --git a/ui_freenove_allinone/src/app/scene_warning_siren_fx.inc b/ui_freenove_allinone/src/app/scene_warning_siren_fx.inc new file mode 100644 index 0000000..5582efb --- /dev/null +++ b/ui_freenove_allinone/src/app/scene_warning_siren_fx.inc @@ -0,0 +1,70 @@ +void updateWarningSirenFx(uint32_t now_ms) { + SceneWarningSirenFxState& siren = g_scene_warning_siren_fx; + + if (!siren.enabled) { + if (siren.remote_started) { + const bool stop_ok = sendWarningSirenSync("STOP", 0U); + ZACUS_RL_LOG_MS(1200U, "[WARN_SIREN] remote stop ok=%u\n", stop_ok ? 1U : 0U); + } + siren.remote_started = false; + siren.started_ms = 0U; + siren.next_beat_ms = 0U; + siren.next_retry_ms = 0U; + siren.local_fallback_logged = false; + return; + } + + if (!siren.local_backend_available && !siren.local_fallback_logged) { + Serial.println("[WARN_SIREN] local backend unavailable, syncing siren to A252 only"); + siren.local_fallback_logged = true; + } + + if (!siren.remote_started) { + if (!warningSirenRemoteSyncReady()) { + siren.next_retry_ms = now_ms + 2000U; + ZACUS_RL_LOG_MS(2000U, "[WARN_SIREN] remote sync waiting peer\n"); + return; + } + if (siren.next_retry_ms != 0U && static_cast(now_ms - siren.next_retry_ms) < 0) { + return; + } + const bool start_ok = sendWarningSirenSync("START", 220U); + if (!start_ok) { + siren.next_retry_ms = now_ms + 2000U; + ZACUS_RL_LOG_MS(1000U, "[WARN_SIREN] remote start failed, retrying\n"); + return; + } + siren.remote_started = true; + siren.started_ms = now_ms; + siren.next_retry_ms = 0U; + siren.next_beat_ms = now_ms + kWarningSirenBeatIntervalMs; + Serial.println("[WARN_SIREN] remote start"); + } + + if (siren.next_beat_ms != 0U && static_cast(now_ms - siren.next_beat_ms) < 0) { + return; + } + + if (!warningSirenRemoteSyncReady()) { + siren.remote_started = false; + siren.next_beat_ms = 0U; + siren.next_retry_ms = now_ms + 2000U; + ZACUS_RL_LOG_MS(2000U, "[WARN_SIREN] remote link lost, rearm\n"); + return; + } + + const uint32_t period_ms = 1400U; + const uint32_t half_period_ms = period_ms / 2U; + const uint32_t elapsed_ms = now_ms - siren.started_ms; + const uint32_t phase_ms = elapsed_ms % period_ms; + const uint32_t ramp = (phase_ms <= half_period_ms) ? phase_ms : (period_ms - phase_ms); + int strength = 160 + static_cast((95U * ramp) / (half_period_ms == 0U ? 1U : half_period_ms)); + if (strength > 255) { + strength = 255; + } + const bool beat_ok = sendWarningSirenSync("BEAT", static_cast(strength)); + if (!beat_ok) { + ZACUS_RL_LOG_MS(1000U, "[WARN_SIREN] remote beat send failed\n"); + } + siren.next_beat_ms = now_ms + kWarningSirenBeatIntervalMs; +}