Skip to content

Commit

Permalink
move equipment stop button related functions from Software.ino to equ…
Browse files Browse the repository at this point in the history
…ipmentstopbutton folder
  • Loading branch information
lenvm committed Dec 13, 2024
1 parent f1fa36a commit 2d63d95
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 47 deletions.
48 changes: 1 addition & 47 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2d63d95

Please sign in to comment.