From 358f67f53bdc8eae9e5541f43be1de9c8772ec95 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niccol=C3=B2=20Betto?= <niccolo.betto@skywarder.eu>
Date: Sun, 23 Mar 2025 00:19:05 +0100
Subject: [PATCH] [ConRIG][ConRIGv2] Play audio feedback quicker after startup
 and disarming

---
 src/ConRIG/Configs/RadioConfig.h   |  6 ++++++
 src/ConRIG/Radio/Radio.cpp         | 17 +++++++++++++----
 src/ConRIG/Radio/Radio.h           |  3 ++-
 src/ConRIGv2/Configs/RadioConfig.h |  6 ++++++
 src/ConRIGv2/Radio/Radio.cpp       | 19 +++++++++++++------
 src/ConRIGv2/Radio/Radio.h         |  3 ++-
 6 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/src/ConRIG/Configs/RadioConfig.h b/src/ConRIG/Configs/RadioConfig.h
index 735ae3f51..af7f66cde 100644
--- a/src/ConRIG/Configs/RadioConfig.h
+++ b/src/ConRIG/Configs/RadioConfig.h
@@ -50,6 +50,12 @@ constexpr uint8_t MAV_COMPONENT_ID = 96;
 // Periodic telemetries frequency
 constexpr Hertz PING_GSE_PERIOD = 2_hz;
 
+// Audio feedback message threshold
+constexpr auto AUDIO_FEEDBACK_THRESHOLD = 10;
+// Value to reset the message counter to, to avoid long pauses without audio
+// feedback after startup or disarming
+constexpr auto AUDIO_FEEDBACK_RESET_VALUE = AUDIO_FEEDBACK_THRESHOLD * 4 / 5;
+
 }  // namespace Radio
 }  // namespace Config
 }  // namespace ConRIG
diff --git a/src/ConRIG/Radio/Radio.cpp b/src/ConRIG/Radio/Radio.cpp
index cb88b002b..f9e218372 100644
--- a/src/ConRIG/Radio/Radio.cpp
+++ b/src/ConRIG/Radio/Radio.cpp
@@ -63,10 +63,18 @@ void Radio::handleMessage(const mavlink_message_t& msg)
         case MAVLINK_MSG_ID_GSE_TM:
         {
             int armingState = mavlink_msg_gse_tm_get_arming_state(&msg);
-            messagesReceived += 1;
+            bool wasArmed   = isArmed;
+            bool isNowArmed = armingState == 1;
+            isArmed         = isNowArmed;
+
+            // Reset the message counter to a value higher than zero to avoid
+            // long pauses without audio feedback after disarming
+            if (wasArmed && !isNowArmed)
+                messagesReceived = Config::Radio::AUDIO_FEEDBACK_RESET_VALUE;
+            else
+                messagesReceived += 1;
 
-            isArmed = armingState == 1;
-            if (armingState == 1)
+            if (isNowArmed)
                 getModule<Buttons>()->enableIgnition();
             else
                 getModule<Buttons>()->disableIgnition();
@@ -144,7 +152,8 @@ void Radio::loopBuzzer()
         Thread::sleep(100);
         // Doesn't matter the precision, the important thing is
         // the beep, this is because an atomic is used
-        if ((!isArmed && messagesReceived > 10) ||
+        if ((!isArmed &&
+             messagesReceived > Config::Radio::AUDIO_FEEDBACK_THRESHOLD) ||
             (isArmed && messagesReceived > 0))
         {
             messagesReceived = 0;
diff --git a/src/ConRIG/Radio/Radio.h b/src/ConRIG/Radio/Radio.h
index 949df5f99..29197d280 100644
--- a/src/ConRIG/Radio/Radio.h
+++ b/src/ConRIG/Radio/Radio.h
@@ -79,7 +79,8 @@ private:
 
     std::thread buzzerLooper;
 
-    std::atomic<uint8_t> messagesReceived{0};
+    std::atomic<uint8_t> messagesReceived{
+        Config::Radio::AUDIO_FEEDBACK_RESET_VALUE};
     std::atomic<bool> isArmed{false};
 
     Boardcore::PrintLogger logger = Boardcore::Logging::getLogger("radio");
diff --git a/src/ConRIGv2/Configs/RadioConfig.h b/src/ConRIGv2/Configs/RadioConfig.h
index 8f2695e67..b6065aa5e 100644
--- a/src/ConRIGv2/Configs/RadioConfig.h
+++ b/src/ConRIGv2/Configs/RadioConfig.h
@@ -50,6 +50,12 @@ constexpr uint8_t MAV_COMPONENT_ID = 96;
 // Periodic telemetries frequency
 constexpr Hertz PING_GSE_PERIOD = 2_hz;
 
+// Audio feedback message threshold
+constexpr auto AUDIO_FEEDBACK_THRESHOLD = 10;
+// Value to reset the message counter to, to avoid long pauses without audio
+// feedback after startup or disarming
+constexpr auto AUDIO_FEEDBACK_RESET_VALUE = AUDIO_FEEDBACK_THRESHOLD * 4 / 5;
+
 }  // namespace Radio
 }  // namespace Config
 }  // namespace ConRIGv2
diff --git a/src/ConRIGv2/Radio/Radio.cpp b/src/ConRIGv2/Radio/Radio.cpp
index 328df566a..3091b2a7c 100644
--- a/src/ConRIGv2/Radio/Radio.cpp
+++ b/src/ConRIGv2/Radio/Radio.cpp
@@ -63,10 +63,18 @@ void Radio::handleMessage(const mavlink_message_t& msg)
         case MAVLINK_MSG_ID_GSE_TM:
         {
             int armingState = mavlink_msg_gse_tm_get_arming_state(&msg);
-            messagesReceived += 1;
+            bool wasArmed   = isArmed;
+            bool isNowArmed = armingState == 1;
+            isArmed         = isNowArmed;
+
+            // Reset the message counter to a value higher than zero to avoid
+            // long pauses without audio feedback after disarming
+            if (wasArmed && !isNowArmed)
+                messagesReceived = Config::Radio::AUDIO_FEEDBACK_RESET_VALUE;
+            else
+                messagesReceived += 1;
 
-            isArmed = armingState == 1;
-            if (armingState == 1)
+            if (isNowArmed)
                 getModule<Buttons>()->enableIgnition();
             else
                 getModule<Buttons>()->disableIgnition();
@@ -149,9 +157,8 @@ void Radio::buzzerOff()
 
 void Radio::buzzerTask()
 {
-    constexpr int beepPeriod = 5;  // seconds
-
-    if ((!isArmed && messagesReceived > beepPeriod * 2) ||
+    if ((!isArmed &&
+         messagesReceived > Config::Radio::AUDIO_FEEDBACK_THRESHOLD) ||
         (isArmed && messagesReceived > 0))
     {
         messagesReceived = 0;
diff --git a/src/ConRIGv2/Radio/Radio.h b/src/ConRIGv2/Radio/Radio.h
index 9d8c8bb47..71120d3fb 100644
--- a/src/ConRIGv2/Radio/Radio.h
+++ b/src/ConRIGv2/Radio/Radio.h
@@ -88,7 +88,8 @@ private:
     miosix::FastMutex buttonsMutex;
     mavlink_conrig_state_tc_t buttonState{};
 
-    std::atomic<uint8_t> messagesReceived{0};
+    std::atomic<uint8_t> messagesReceived{
+        Config::Radio::AUDIO_FEEDBACK_RESET_VALUE};
     std::atomic<bool> isArmed{false};
 
     Boardcore::PrintLogger logger = Boardcore::Logging::getLogger("radio");
-- 
GitLab