From 2d63d95a2e6919d76f8414769e382f071e1ab47b Mon Sep 17 00:00:00 2001 From: lenvm Date: Fri, 13 Dec 2024 16:48:02 +0100 Subject: [PATCH] move equipment stop button related functions from Software.ino to equipmentstopbutton folder --- Software/Software.ino | 48 +---------------- .../comm_equipmentstopbutton.cpp | 51 +++++++++++++++++++ .../comm_equipmentstopbutton.h | 14 +++++ 3 files changed, 66 insertions(+), 47 deletions(-) create mode 100644 Software/src/communication/equipmentstopbutton/comm_equipmentstopbutton.cpp create mode 100644 Software/src/communication/equipmentstopbutton/comm_equipmentstopbutton.h diff --git a/Software/Software.ino b/Software/Software.ino index 63ad5e714..42e5f3884 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -13,6 +13,7 @@ #include "src/charger/CHARGERS.h" #include "src/communication/can/comm_can.h" #include "src/communication/contactorcontrol/comm_contactorcontrol.h" +#include "src/communication/equipmentstopbutton/comm_equipmentstopbutton.h" #include "src/communication/rs485/comm_rs485.h" #include "src/communication/seriallink/comm_seriallink.h" #include "src/datalayer/datalayer.h" @@ -45,10 +46,6 @@ #endif // MQTT #endif // WIFI -#ifdef EQUIPMENT_STOP_BUTTON -#include "src/devboard/utils/debounce_button.h" -#endif - Preferences settings; // Store user settings // The current software version, shown on webserver const char* version_number = "8.0.dev"; @@ -84,14 +81,6 @@ MyTimer loop_task_timer_10s(INTERVAL_10_S); MyTimer check_pause_2s(INTERVAL_2_S); -#ifdef EQUIPMENT_STOP_BUTTON -const unsigned long equipment_button_long_press_duration = - 15000; // 15 seconds for long press in case of MOMENTARY_SWITCH -const unsigned long equipment_button_debounce_duration = 200; // 250ms for debouncing the button -unsigned long timeSincePress = 0; // Variable to store the time since the last press -DebouncedButton equipment_stop_button; // Debounced button object -#endif - TaskHandle_t main_loop_task; TaskHandle_t connectivity_loop_task; @@ -352,41 +341,6 @@ void init_stored_settings() { settings.end(); } -#ifdef EQUIPMENT_STOP_BUTTON -void monitor_equipment_stop_button() { - - ButtonState changed_state = debounceButton(equipment_stop_button, timeSincePress); - - if (equipment_stop_behavior == LATCHING_SWITCH) { - if (changed_state == PRESSED) { - // Changed to ON – initiating equipment stop. - setBatteryPause(true, false, true); - } else if (changed_state == RELEASED) { - // Changed to OFF – ending equipment stop. - setBatteryPause(false, false, false); - } - } else if (equipment_stop_behavior == MOMENTARY_SWITCH) { - if (changed_state == RELEASED) { // button is released - - if (timeSincePress < equipment_button_long_press_duration) { - // Short press detected, trigger equipment stop - setBatteryPause(true, false, true); - } else { - // Long press detected, reset equipment stop state - setBatteryPause(false, false, false); - } - } - } -} - -void init_equipment_stop_button() { - //using external pullup resistors NC - pinMode(EQUIPMENT_STOP_PIN, INPUT); - // Initialize the debounced button with NC switch type and equipment_button_debounce_duration debounce time - initDebouncedButton(equipment_stop_button, EQUIPMENT_STOP_PIN, NC, equipment_button_debounce_duration); -} -#endif // EQUIPMENT_STOP_BUTTON - #ifdef DOUBLE_BATTERY void check_interconnect_available() { if (datalayer.battery.status.voltage_dV == 0 || datalayer.battery2.status.voltage_dV == 0) { diff --git a/Software/src/communication/equipmentstopbutton/comm_equipmentstopbutton.cpp b/Software/src/communication/equipmentstopbutton/comm_equipmentstopbutton.cpp new file mode 100644 index 000000000..9de1d6e93 --- /dev/null +++ b/Software/src/communication/equipmentstopbutton/comm_equipmentstopbutton.cpp @@ -0,0 +1,51 @@ +#include "comm_equipmentstopbutton.h" +#include "../../include.h" + +// Parameters +#ifdef EQUIPMENT_STOP_BUTTON +const unsigned long equipment_button_long_press_duration = + 15000; // 15 seconds for long press in case of MOMENTARY_SWITCH +const unsigned long equipment_button_debounce_duration = 200; // 200ms for debouncing the button +unsigned long timeSincePress = 0; // Variable to store the time since the last press +DebouncedButton equipment_stop_button; // Debounced button object +#endif + +// Initialization functions +#ifdef EQUIPMENT_STOP_BUTTON +void init_equipment_stop_button() { + //using external pullup resistors NC + pinMode(EQUIPMENT_STOP_PIN, INPUT); + // Initialize the debounced button with NC switch type and equipment_button_debounce_duration debounce time + initDebouncedButton(equipment_stop_button, EQUIPMENT_STOP_PIN, NC, equipment_button_debounce_duration); +} +#endif // EQUIPMENT_STOP_BUTTON + +// Main functions + +#ifdef EQUIPMENT_STOP_BUTTON +void monitor_equipment_stop_button() { + + ButtonState changed_state = debounceButton(equipment_stop_button, timeSincePress); + + if (equipment_stop_behavior == LATCHING_SWITCH) { + if (changed_state == PRESSED) { + // Changed to ON – initiating equipment stop. + setBatteryPause(true, false, true); + } else if (changed_state == RELEASED) { + // Changed to OFF – ending equipment stop. + setBatteryPause(false, false, false); + } + } else if (equipment_stop_behavior == MOMENTARY_SWITCH) { + if (changed_state == RELEASED) { // button is released + + if (timeSincePress < equipment_button_long_press_duration) { + // Short press detected, trigger equipment stop + setBatteryPause(true, false, true); + } else { + // Long press detected, reset equipment stop state + setBatteryPause(false, false, false); + } + } + } +} +#endif // EQUIPMENT_STOP_BUTTON diff --git a/Software/src/communication/equipmentstopbutton/comm_equipmentstopbutton.h b/Software/src/communication/equipmentstopbutton/comm_equipmentstopbutton.h new file mode 100644 index 000000000..cde04df42 --- /dev/null +++ b/Software/src/communication/equipmentstopbutton/comm_equipmentstopbutton.h @@ -0,0 +1,14 @@ +#ifndef _COMM_EQUIPMENTSTOPBUTTON_H_ +#define _COMM__COMM_EQUIPMENTSTOPBUTTON_H__H_ + +#include "../../include.h" + +#ifdef EQUIPMENT_STOP_BUTTON +#include "../../devboard/utils/debounce_button.h" +#endif + +void init_equipment_stop_button(); + +void monitor_equipment_stop_button(); + +#endif