Skip to content

Commit

Permalink
Some cleanup before more surgery
Browse files Browse the repository at this point in the history
  • Loading branch information
Cabooman committed Feb 6, 2024
1 parent 6bc253f commit 32378df
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void loop() {
#ifdef DUAL_CAN
send_can2();
#endif
update_event_timestamps();
run_event_handling();
}

// Initialization functions
Expand Down
1 change: 0 additions & 1 deletion Software/src/devboard/utils/CMakeLists.txt

This file was deleted.

11 changes: 8 additions & 3 deletions Software/src/devboard/utils/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ static uint8_t total_led_color = GREEN;
static char event_message[256];

/* Local function prototypes */
static void update_event_time(void);
static void set_event_message(EVENTS_ENUM_TYPE event);
static void update_led_color(EVENTS_ENUM_TYPE event);

/* Exported functions */
void run_event_handling(void) {
update_event_time();
}

void init_events(void) {
for (uint8_t i = 0; i < EVENT_NOF_EVENTS; i++) {
entries[i].timestamp = 0;
Expand Down Expand Up @@ -49,15 +54,15 @@ void set_event(EVENTS_ENUM_TYPE event, uint8_t data) {
#endif
}

void update_event_timestamps(void) {
/* Local functions */
static void update_event_time(void) {
unsigned long new_millis = millis();
if (new_millis - previous_millis >= 1000) {
time_seconds++;
previous_millis = new_millis;
}
}

/* Local functions */
static void update_led_color(EVENTS_ENUM_TYPE event) {
total_led_color = (total_led_color == RED) ? RED : entries[event].led_color;
}
Expand All @@ -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!");
break;
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;
case EVENT_KWH_PLAUSIBILITY_ERROR:
snprintf(event_message, sizeof(event_message),
Expand Down
2 changes: 1 addition & 1 deletion Software/src/devboard/utils/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ typedef enum {

void init_events(void);
void set_event(EVENTS_ENUM_TYPE event, uint8_t data);
void update_event_timestamps(void);
void run_event_handling(void);

#endif // __MYTIMER_H__
14 changes: 7 additions & 7 deletions test/utils/events_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The test library must be included first!
#include "test_lib.h"
#include "../test_lib.h"

#include "events.cpp"

Expand All @@ -16,35 +16,35 @@ TEST(init_events_test) {
}
}

TEST(update_event_timestamps_test) {
TEST(update_event_time_test) {
// Reset
init_events();
time_seconds = 0;

// No delta, so time shouldn't increase
testlib_millis = 0;
update_event_timestamps();
update_event_time();
ASSERT_EQ(time_seconds, 0);

// Almost time to bump the seconds
testlib_millis = 999;
update_event_timestamps();
update_event_time();
ASSERT_EQ(time_seconds, 0);
ASSERT_EQ(previous_millis, 0);

// millis == 1000, so we should add a second
testlib_millis = 1000;
update_event_timestamps();
update_event_time();
ASSERT_EQ(time_seconds, 1);
ASSERT_EQ(previous_millis, 1000);

// We shouldn't add more seconds until 2000 now
testlib_millis = 1999;
update_event_timestamps();
update_event_time();
ASSERT_EQ(time_seconds, 1);
ASSERT_EQ(previous_millis, 1000);
testlib_millis = 2000;
update_event_timestamps();
update_event_time();
ASSERT_EQ(time_seconds, 2);
ASSERT_EQ(previous_millis, 2000);
}
Expand Down

0 comments on commit 32378df

Please sign in to comment.