Some cleanup before more surgery

This commit is contained in:
Cabooman 2024-02-06 22:11:02 +01:00
parent 6bc253ff6e
commit 32378df03e
5 changed files with 17 additions and 13 deletions

View file

@ -194,7 +194,7 @@ void loop() {
#ifdef DUAL_CAN #ifdef DUAL_CAN
send_can2(); send_can2();
#endif #endif
update_event_timestamps(); run_event_handling();
} }
// Initialization functions // Initialization functions

View file

@ -1 +0,0 @@
# add_library(utils_library events.cpp)

View file

@ -17,10 +17,15 @@ static uint8_t total_led_color = GREEN;
static char event_message[256]; static char event_message[256];
/* Local function prototypes */ /* Local function prototypes */
static void update_event_time(void);
static void set_event_message(EVENTS_ENUM_TYPE event); static void set_event_message(EVENTS_ENUM_TYPE event);
static void update_led_color(EVENTS_ENUM_TYPE event); static void update_led_color(EVENTS_ENUM_TYPE event);
/* Exported functions */ /* Exported functions */
void run_event_handling(void) {
update_event_time();
}
void init_events(void) { void init_events(void) {
for (uint8_t i = 0; i < EVENT_NOF_EVENTS; i++) { for (uint8_t i = 0; i < EVENT_NOF_EVENTS; i++) {
entries[i].timestamp = 0; entries[i].timestamp = 0;
@ -49,7 +54,8 @@ void set_event(EVENTS_ENUM_TYPE event, uint8_t data) {
#endif #endif
} }
void update_event_timestamps(void) { /* Local functions */
static void update_event_time(void) {
unsigned long new_millis = millis(); unsigned long new_millis = millis();
if (new_millis - previous_millis >= 1000) { if (new_millis - previous_millis >= 1000) {
time_seconds++; time_seconds++;
@ -57,7 +63,6 @@ void update_event_timestamps(void) {
} }
} }
/* Local functions */
static void update_led_color(EVENTS_ENUM_TYPE event) { static void update_led_color(EVENTS_ENUM_TYPE event) {
total_led_color = (total_led_color == RED) ? RED : entries[event].led_color; total_led_color = (total_led_color == RED) ? RED : entries[event].led_color;
} }
@ -81,7 +86,7 @@ static void set_event_message(EVENTS_ENUM_TYPE event) {
"12V battery source below required voltage to safely close contactors. Inspect the supply/battery!"); "12V battery source below required voltage to safely close contactors. Inspect the supply/battery!");
break; break;
case EVENT_SOC_PLAUSIBILITY_ERROR: case EVENT_SOC_PLAUSIBILITY_ERROR:
snprintf(event_message, sizeof(event_message), "ERROR: SOC% reported by battery not plausible. Restart battery!"); snprintf(event_message, sizeof(event_message), "ERROR: SOC reported by battery not plausible. Restart battery!");
break; break;
case EVENT_KWH_PLAUSIBILITY_ERROR: case EVENT_KWH_PLAUSIBILITY_ERROR:
snprintf(event_message, sizeof(event_message), snprintf(event_message, sizeof(event_message),

View file

@ -30,6 +30,6 @@ typedef enum {
void init_events(void); void init_events(void);
void set_event(EVENTS_ENUM_TYPE event, uint8_t data); void set_event(EVENTS_ENUM_TYPE event, uint8_t data);
void update_event_timestamps(void); void run_event_handling(void);
#endif // __MYTIMER_H__ #endif // __MYTIMER_H__

View file

@ -1,5 +1,5 @@
// The test library must be included first! // The test library must be included first!
#include "test_lib.h" #include "../test_lib.h"
#include "events.cpp" #include "events.cpp"
@ -16,35 +16,35 @@ TEST(init_events_test) {
} }
} }
TEST(update_event_timestamps_test) { TEST(update_event_time_test) {
// Reset // Reset
init_events(); init_events();
time_seconds = 0; time_seconds = 0;
// No delta, so time shouldn't increase // No delta, so time shouldn't increase
testlib_millis = 0; testlib_millis = 0;
update_event_timestamps(); update_event_time();
ASSERT_EQ(time_seconds, 0); ASSERT_EQ(time_seconds, 0);
// Almost time to bump the seconds // Almost time to bump the seconds
testlib_millis = 999; testlib_millis = 999;
update_event_timestamps(); update_event_time();
ASSERT_EQ(time_seconds, 0); ASSERT_EQ(time_seconds, 0);
ASSERT_EQ(previous_millis, 0); ASSERT_EQ(previous_millis, 0);
// millis == 1000, so we should add a second // millis == 1000, so we should add a second
testlib_millis = 1000; testlib_millis = 1000;
update_event_timestamps(); update_event_time();
ASSERT_EQ(time_seconds, 1); ASSERT_EQ(time_seconds, 1);
ASSERT_EQ(previous_millis, 1000); ASSERT_EQ(previous_millis, 1000);
// We shouldn't add more seconds until 2000 now // We shouldn't add more seconds until 2000 now
testlib_millis = 1999; testlib_millis = 1999;
update_event_timestamps(); update_event_time();
ASSERT_EQ(time_seconds, 1); ASSERT_EQ(time_seconds, 1);
ASSERT_EQ(previous_millis, 1000); ASSERT_EQ(previous_millis, 1000);
testlib_millis = 2000; testlib_millis = 2000;
update_event_timestamps(); update_event_time();
ASSERT_EQ(time_seconds, 2); ASSERT_EQ(time_seconds, 2);
ASSERT_EQ(previous_millis, 2000); ASSERT_EQ(previous_millis, 2000);
} }