tdse-tp3_04-interactive_menu

Index Commits Files Refs
commit 53a6bfe39829a1db6a15e22fde63a571ffb26298
parent 743a9823b4980cc5239b28f338d3b2437c09ecd4
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Wed, 15 Oct 2025 17:21:21 -0300

send `EV_MEN_INACTIVE_TIMEOUT` if no buttons pressed after time

Diffstat:
Mapp/inc/task_menu_attribute.h | 4+++-
Mapp/inc/task_sensor.h | 6+++---
Mapp/src/app.c | 396++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mapp/src/task_menu.c | 8++++++++
Mapp/src/task_sensor.c | 30+++++++++++++++++++++++++-----
5 files changed, 237 insertions(+), 207 deletions(-)
diff --git a/app/inc/task_menu_attribute.h b/app/inc/task_menu_attribute.h
@@ -92,7 +92,9 @@ typedef enum {
     EV_MEN_NEX_ACTIVE,
 
     EV_MEN_ESC_IDLE,
-    EV_MEN_ESC_ACTIVE
+    EV_MEN_ESC_ACTIVE,
+
+    EV_MEN_INACTIVE_TIMEOUT
 } task_menu_ev_t;
 
 /* State of Task Menu */
diff --git a/app/inc/task_sensor.h b/app/inc/task_sensor.h
@@ -53,9 +53,9 @@ extern "C" {
 extern uint32_t g_task_sensor_cnt;
 extern volatile uint32_t g_task_sensor_tick_cnt;
 
-/********************** external functions declaration ***********************/
-extern void task_sensor_init(void *parameters);
-extern void task_sensor_update(void *parameters);
+/********************** functions declaration *********************************/
+void task_sensor_init(void *parameters);
+void task_sensor_update(void *parameters);
 
 /********************** End of CPP guard *************************************/
 #ifdef __cplusplus
diff --git a/app/src/app.c b/app/src/app.c
@@ -1,198 +1,198 @@
-/*
- * Copyright (c) 2023 Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * 3. Neither the name of the copyright holder nor the names of its
- *    contributors may be used to endorse or promote products derived from
- *    this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
- * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- * @file   : app.c
- * @date   : Set 26, 2023
- * @author : Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>
- * @version    v1.0.0
- */
-
-/********************** inclusions *******************************************/
-/* Project includes */
-#include "main.h"
-
-/* Demo includes */
-#include "logger.h"
-#include "dwt.h"
-
-/* Application & Tasks includes */
-#include "board.h"
-#include "task_sensor.h"
-#include "task_menu.h"
-
-/********************** macros and definitions *******************************/
-#define G_APP_CNT_INI        0ul
-#define G_APP_TICK_CNT_INI    0ul
-
-#define TASK_X_WCET_INI        0ul
-#define TASK_X_DELAY_MIN    0ul
-
-typedef struct {
-    void (*task_init)(void *);        // Pointer to task (must be a
-                                    // 'void (void *)' function)
-    void (*task_update)(void *);    // Pointer to task (must be a
-                                    // 'void (void *)' function)
-    void *parameters;                // Pointer to parameters
-} task_cfg_t;
-
-typedef struct {
-    uint32_t WCET;            // Worst-case execution time (microseconds)
-} task_dta_t;
-
-/********************** internal data declaration ****************************/
-const task_cfg_t task_cfg_list[]    = {
-        {task_sensor_init,    task_sensor_update,     NULL},
-        {task_menu_init,    task_menu_update,         NULL}
-};
-
-#define TASK_QTY    (sizeof(task_cfg_list)/sizeof(task_cfg_t))
-
-/********************** internal functions declaration ***********************/
-
-/********************** internal data definition *****************************/
-const char *p_sys    = " Bare Metal - Event-Triggered Systems (ETS)";
-const char *p_app    = " App - Interactive Menu";
-
-/********************** external data declaration ****************************/
-uint32_t g_app_cnt;
-uint32_t g_app_runtime_us;
-
-volatile uint32_t g_app_tick_cnt;
-
-task_dta_t task_dta_list[TASK_QTY];
-
-/********************** external functions definition ************************/
-void app_init(void)
-{
-    uint32_t index;
-
-    /* Print out: Application Initialized */
-    LOGGER_INFO(" ");
-    LOGGER_INFO("%s is running - Tick [mS] = %lu", GET_NAME(app_init), HAL_GetTick());
-
-    LOGGER_INFO(p_sys);
-    LOGGER_INFO(p_app);
-
-    /* Init & Print out: Application execution counter */
-    g_app_cnt = G_APP_CNT_INI;
-    LOGGER_INFO(" %s = %lu", GET_NAME(g_app_cnt), g_app_cnt);
-
-    /* Init Cycle Counter */
-    cycle_counter_init();
-
-    /* Go through the task arrays */
-    for (index = 0; TASK_QTY > index; index++)
-    {
-        /* Run task_x_init */
-        (*task_cfg_list[index].task_init)(task_cfg_list[index].parameters);
-
-        /* Init variables */
-        task_dta_list[index].WCET = TASK_X_WCET_INI;
-    }
-
-    /* Protect shared resource */
-    __asm("CPSID i");    /* disable interrupts */
-    /* Init Tick Counter */
-    g_app_tick_cnt = G_APP_TICK_CNT_INI;
-
-    g_task_sensor_tick_cnt = G_APP_TICK_CNT_INI;
-    g_task_menu_tick_cnt = G_APP_TICK_CNT_INI;
-    __asm("CPSIE i");    /* enable interrupts */
-}
-
-void app_update(void)
-{
-    uint32_t index;
-    bool b_time_update_required = false;
-    uint32_t cycle_counter_time_us;
-
-    /* Protect shared resource */
-    __asm("CPSID i");    /* disable interrupts */
-    if (G_APP_TICK_CNT_INI < g_app_tick_cnt)
-    {
-        /* Update Tick Counter */
-        g_app_tick_cnt--;
-        b_time_update_required = true;
-    }
-    __asm("CPSIE i");    /* enable interrupts */
-
-    /* Check if it's time to run tasks */
-    while (b_time_update_required)
-    {
-        /* Update App Counter */
-        g_app_cnt++;
-        g_app_runtime_us = 0;
-
-        /* Go through the task arrays */
-        for (index = 0; TASK_QTY > index; index++)
-        {
-            cycle_counter_reset();
-
-            /* Run task_x_update */
-            (*task_cfg_list[index].task_update)(task_cfg_list[index].parameters);
-
-            cycle_counter_time_us = cycle_counter_get_time_us();
-
-            /* Update variables */
-            g_app_runtime_us += cycle_counter_time_us;
-
-            if (task_dta_list[index].WCET < cycle_counter_time_us)
-            {
-                task_dta_list[index].WCET = cycle_counter_time_us;
-            }
-        }
-
-        /* Protect shared resource */
-        __asm("CPSID i");    /* disable interrupts */
-        if (G_APP_TICK_CNT_INI < g_app_tick_cnt)
-        {
-            /* Update Tick Counter */
-            g_app_tick_cnt--;
-            b_time_update_required = true;
-        }
-        else
-        {
-            b_time_update_required = false;
-        }
-        __asm("CPSIE i");    /* enable interrupts */
-    }
-}
-
-void HAL_SYSTICK_Callback(void)
-{
-    /* Update Tick Counter */
-    g_app_tick_cnt++;
-
-    g_task_sensor_tick_cnt++;
-    g_task_menu_tick_cnt++;
-}
-
-/********************** end of file ******************************************/
+/*
+ * Copyright (c) 2023 Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @file   : app.c
+ * @date   : Set 26, 2023
+ * @author : Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>
+ * @version    v1.0.0
+ */
+
+/********************** inclusions *******************************************/
+/* Project includes */
+#include "main.h"
+
+/* Demo includes */
+#include "logger.h"
+#include "dwt.h"
+
+/* Application & Tasks includes */
+#include "board.h"
+#include "task_sensor.h"
+#include "task_menu.h"
+
+/********************** macros and definitions *******************************/
+#define G_APP_CNT_INI        0ul
+#define G_APP_TICK_CNT_INI    0ul
+
+#define TASK_X_WCET_INI        0ul
+#define TASK_X_DELAY_MIN    0ul
+
+typedef struct {
+    void (*task_init)(void *);        // Pointer to task (must be a
+                                    // 'void (void *)' function)
+    void (*task_update)(void *);    // Pointer to task (must be a
+                                    // 'void (void *)' function)
+    void *parameters;                // Pointer to parameters
+} task_cfg_t;
+
+typedef struct {
+    uint32_t WCET;            // Worst-case execution time (microseconds)
+} task_dta_t;
+
+/********************** internal data declaration ****************************/
+const task_cfg_t task_cfg_list[]    = {
+        {task_sensor_init,    task_sensor_update,     NULL},
+        {task_menu_init,    task_menu_update,         NULL}
+};
+
+#define TASK_QTY    (sizeof(task_cfg_list)/sizeof(task_cfg_t))
+
+/********************** internal functions declaration ***********************/
+
+/********************** internal data definition *****************************/
+const char *p_sys    = " Bare Metal - Event-Triggered Systems (ETS)";
+const char *p_app    = " App - Interactive Menu";
+
+/********************** external data declaration ****************************/
+uint32_t g_app_cnt;
+uint32_t g_app_runtime_us;
+
+volatile uint32_t g_app_tick_cnt;
+
+task_dta_t task_dta_list[TASK_QTY];
+
+/********************** external functions definition ************************/
+void app_init(void)
+{
+    uint32_t index;
+
+    /* Print out: Application Initialized */
+    LOGGER_INFO(" ");
+    LOGGER_INFO("%s is running - Tick [mS] = %lu", GET_NAME(app_init), HAL_GetTick());
+
+    LOGGER_INFO(p_sys);
+    LOGGER_INFO(p_app);
+
+    /* Init & Print out: Application execution counter */
+    g_app_cnt = G_APP_CNT_INI;
+    LOGGER_INFO(" %s = %lu", GET_NAME(g_app_cnt), g_app_cnt);
+
+    /* Init Cycle Counter */
+    cycle_counter_init();
+
+    /* Go through the task arrays */
+    for (index = 0; TASK_QTY > index; index++)
+    {
+        /* Run task_x_init */
+        (*task_cfg_list[index].task_init)(task_cfg_list[index].parameters);
+
+        /* Init variables */
+        task_dta_list[index].WCET = TASK_X_WCET_INI;
+    }
+
+    /* Protect shared resource */
+    __asm("CPSID i");    /* disable interrupts */
+    /* Init Tick Counter */
+    g_app_tick_cnt = G_APP_TICK_CNT_INI;
+
+    g_task_sensor_tick_cnt = G_APP_TICK_CNT_INI;
+    g_task_menu_tick_cnt = G_APP_TICK_CNT_INI;
+    __asm("CPSIE i");    /* enable interrupts */
+}
+
+void app_update(void)
+{
+    uint32_t index;
+    bool b_time_update_required = false;
+    uint32_t cycle_counter_time_us;
+
+    /* Protect shared resource */
+    __asm("CPSID i");    /* disable interrupts */
+    if (G_APP_TICK_CNT_INI < g_app_tick_cnt)
+    {
+        /* Update Tick Counter */
+        g_app_tick_cnt--;
+        b_time_update_required = true;
+    }
+    __asm("CPSIE i");    /* enable interrupts */
+
+    /* Check if it's time to run tasks */
+    while (b_time_update_required)
+    {
+        /* Update App Counter */
+        g_app_cnt++;
+        g_app_runtime_us = 0;
+
+        /* Go through the task arrays */
+        for (index = 0; TASK_QTY > index; index++)
+        {
+            cycle_counter_reset();
+
+            /* Run task_x_update */
+            (*task_cfg_list[index].task_update)(task_cfg_list[index].parameters);
+
+            cycle_counter_time_us = cycle_counter_get_time_us();
+
+            /* Update variables */
+            g_app_runtime_us += cycle_counter_time_us;
+
+            if (task_dta_list[index].WCET < cycle_counter_time_us)
+            {
+                task_dta_list[index].WCET = cycle_counter_time_us;
+            }
+        }
+
+        /* Protect shared resource */
+        __asm("CPSID i");    /* disable interrupts */
+        if (G_APP_TICK_CNT_INI < g_app_tick_cnt)
+        {
+            /* Update Tick Counter */
+            g_app_tick_cnt--;
+            b_time_update_required = true;
+        }
+        else
+        {
+            b_time_update_required = false;
+        }
+        __asm("CPSIE i");    /* enable interrupts */
+    }
+}
+
+void HAL_SYSTICK_Callback(void)
+{
+    /* Update Tick Counter */
+    g_app_tick_cnt++;
+
+    g_task_sensor_tick_cnt++;
+    g_task_menu_tick_cnt++;
+}
+
+/********************** end of file ******************************************/
diff --git a/app/src/task_menu.c b/app/src/task_menu.c
@@ -193,6 +193,14 @@ void task_menu_statechart(void)
         p_task_menu_dta->event = get_event_task_menu();
     }
 
+
+    if ((true == p_task_menu_dta->flag)
+        && (p_task_menu_dta->event == EV_MEN_INACTIVE_TIMEOUT)
+        && (p_task_menu_dta->state != ST_MEN_MOTOR_SELECT))
+    {
+        menu_set_state(ST_MEN_MOTOR_SELECT);
+    }
+
     switch (p_task_menu_dta->state)
     {
         case ST_MEN_MOTOR_SELECT:
diff --git a/app/src/task_sensor.c b/app/src/task_sensor.c
@@ -51,12 +51,13 @@
 #include "task_menu_interface.h"
 
 /********************** macros and definitions *******************************/
-#define G_TASK_SEN_CNT_INIT            0ul
-#define G_TASK_SEN_TICK_CNT_INI        0ul
+#define G_TASK_SEN_CNT_INIT                0ul
+#define G_TASK_SEN_TICK_CNT_INI            0ul
+#define G_TASK_SEN_INACTIVE_MAX_TICK   10000ul // 10 seconds
 
-#define DEL_BTN_XX_MIN                0ul
-#define DEL_BTN_XX_MED                25ul
-#define DEL_BTN_XX_MAX                50ul
+#define DEL_BTN_XX_MIN  0ul
+#define DEL_BTN_XX_MED 25ul
+#define DEL_BTN_XX_MAX 50ul
 
 /********************** internal data declaration ****************************/
 const task_sensor_cfg_t task_sensor_cfg_list[] = {
@@ -85,6 +86,8 @@ void task_sensor_statechart(void);
 const char *p_task_sensor         = "Task Sensor (Sensor Statechart)";
 const char *p_task_sensor_         = "Non-Blocking & Update By Time Code";
 
+uint32_t g_task_sensor_last_active_ticks;
+
 /********************** external data declaration ****************************/
 uint32_t g_task_sensor_cnt;
 volatile uint32_t g_task_sensor_tick_cnt;
@@ -106,6 +109,9 @@ void task_sensor_init(void *parameters)
     g_task_sensor_cnt = G_TASK_SEN_CNT_INIT;
     LOGGER_INFO("   %s = %lu", GET_NAME(g_task_sensor_cnt), g_task_sensor_cnt);
 
+
+    g_task_sensor_last_active_ticks = G_TASK_SEN_INACTIVE_MAX_TICK;
+
     for (index = 0; SENSOR_DTA_QTY > index; index++)
     {
         /* Update Task Sensor Data Pointer */
@@ -130,6 +136,17 @@ void task_sensor_update(void *parameters)
 {
     bool b_time_update_required = false;
 
+    if (0 < g_task_sensor_last_active_ticks)
+    {
+        g_task_sensor_last_active_ticks--;
+
+        if (1 == g_task_sensor_last_active_ticks)
+        {
+            g_task_sensor_last_active_ticks = 0;
+            put_event_task_menu(EV_MEN_INACTIVE_TIMEOUT);
+        }
+    }
+
     /* Protect shared resource */
     __asm("CPSID i");    /* disable interrupts */
     if (G_TASK_SEN_TICK_CNT_INI < g_task_sensor_tick_cnt)
@@ -206,6 +223,7 @@ void task_sensor_statechart(void)
                     {
                         put_event_task_menu(p_task_sensor_cfg->signal_down);
                         p_task_sensor_dta->state = ST_BTN_XX_DOWN;
+                        g_task_sensor_last_active_ticks = G_TASK_SEN_INACTIVE_MAX_TICK;
                     }
                     else
                     {
@@ -234,6 +252,7 @@ void task_sensor_statechart(void)
                     {
                         put_event_task_menu(p_task_sensor_cfg->signal_up);
                         p_task_sensor_dta->state = ST_BTN_XX_UP;
+                        g_task_sensor_last_active_ticks = G_TASK_SEN_INACTIVE_MAX_TICK;
                     }
                     else
                     {
@@ -253,4 +272,5 @@ void task_sensor_statechart(void)
         }
     }
 }
+
 /********************** end of file ******************************************/