diff --git a/firmware/inc/backlog.h b/firmware/inc/backlog.h index fced2d7..60270f2 100644 --- a/firmware/inc/backlog.h +++ b/firmware/inc/backlog.h @@ -45,6 +45,10 @@ * Address offset in memory */ #define BACKLOG_ADDRESS 0 +/** + * Defines the number of backlogs that should be valid before we start replaying + */ +#define BACKLOG_REPLAY_THRESHOLD 24 /* One day */ void record_backlog(tracker_datapoint* dp); struct tracker_datapoint* get_backlog(void); diff --git a/firmware/src/backlog.c b/firmware/src/backlog.c index 2266551..3395673 100644 --- a/firmware/src/backlog.c +++ b/firmware/src/backlog.c @@ -226,6 +226,23 @@ struct tracker_datapoint* read_check_backlog_item(uint16_t index) return (struct tracker_datapoint*)buffer; } +/** + * Returns the number of backlogs indicated as valid in is_backlog_valid. + * + * Should correspond with the actual number of valid backlogs + */ +uint16_t is_backlog_valid_count(void) +{ + uint16_t i, count = 0; + + for (i = 0; i < BACKLOG_COUNT; i++) { + if (is_backlog_valid[i] == BACKLOG_VALID_FLAG) { + count++; + } + } + + return count; +} /** * Gets a valid backlog item. Returns NULL if none available. */ @@ -238,6 +255,11 @@ struct tracker_datapoint* get_backlog(void) load_is_backlog_valid(); } + /* Return early if we haven't reached our replay threshold */ + if (is_backlog_valid_count() < BACKLOG_REPLAY_THRESHOLD) { + return NULL; + } + for (i = 0; i < BACKLOG_COUNT; i++) { /* Find an read index we think is valid */