diff --git a/Software/Software.ino b/Software/Software.ino index 2ec566d6..eb65591a 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -8,6 +8,7 @@ #include "src/battery/BATTERIES.h" #include "src/charger/CHARGERS.h" #include "src/devboard/config.h" +#include "src/devboard/utils/events.h" #include "src/inverter/INVERTERS.h" #include "src/lib/adafruit-Adafruit_NeoPixel/Adafruit_NeoPixel.h" #include "src/lib/eModbus-eModbus/Logging.h" @@ -129,6 +130,8 @@ void setup() { init_webserver(); #endif + init_events(); + init_CAN(); init_LED(); @@ -183,6 +186,7 @@ void loop() { { previousMillisUpdateVal = millis(); update_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus. + set_event(EVENT_DUMMY, (uint8_t)millis()); } // Output @@ -190,6 +194,7 @@ void loop() { #ifdef DUAL_CAN send_can2(); #endif + update_event_timestamps(); } // Initialization functions diff --git a/Software/src/devboard/utils/events.cpp b/Software/src/devboard/utils/events.cpp new file mode 100644 index 00000000..b888f9e9 --- /dev/null +++ b/Software/src/devboard/utils/events.cpp @@ -0,0 +1,33 @@ +#include "events.h" + +typedef struct { + uint32_t timestamp; // Time in seconds since startup when the event occurred + uint8_t data; // Custom data passed when setting the event, for example cell number for under voltage + uint8_t occurences; // Number of occurrences since startup +} EVENTS_STRUCT_TYPE; + +static EVENTS_STRUCT_TYPE entries[EVENT_NOF_EVENTS]; +static unsigned long previous_millis = 0; +static uint32_t time_seconds = 0; + +void init_events(void) { + for (uint8_t i = 0; i < EVENT_NOF_EVENTS; i++) { + entries[i].timestamp = 0; + entries[i].data = 0; + entries[i].occurences = 0; + } +} + +void set_event(EVENTS_ENUM_TYPE event, uint8_t data) { + entries[event].timestamp = time_seconds; + entries[event].data = data; + entries[event].occurences++; +} + +void update_event_timestamps(void) { + unsigned long new_millis = millis(); + if (new_millis - previous_millis >= 1000) { + time_seconds++; + previous_millis = new_millis; + } +} diff --git a/Software/src/devboard/utils/events.h b/Software/src/devboard/utils/events.h new file mode 100644 index 00000000..a23fa608 --- /dev/null +++ b/Software/src/devboard/utils/events.h @@ -0,0 +1,27 @@ +#ifndef __EVENTS_H__ +#define __EVENTS_H__ + +#include + +typedef enum { + EVENT_CAN_FAILURE = 0u, + EVENT_CAN_WARNING, + EVENT_WATER_INGRESS, + EVENT_12V_LOW, + EVENT_SOC_PLAUSIBILITY_ERROR, + EVENT_KWH_PLAUSIBILITY_ERROR, + EVENT_BATTERY_CHG_DISCHG_STOP_REQ, + EVENT_LOW_SOH, + EVENT_HVIL_FAILURE, + EVENT_INTERNAL_OPEN_FAULT, + EVENT_CELL_UNDER_VOLTAGE, + EVENT_CELL_OVER_VOLTAGE, + EVENT_DUMMY, + EVENT_NOF_EVENTS +} EVENTS_ENUM_TYPE; + +void init_events(void); +void set_event(EVENTS_ENUM_TYPE event, uint8_t data); +void update_event_timestamps(void); + +#endif // __MYTIMER_H__