diff --git a/include/timeseries.h b/include/timeseries.h index bd07bdf..c77ca74 100644 --- a/include/timeseries.h +++ b/include/timeseries.h @@ -2,11 +2,11 @@ #include -#include +#include typedef struct { uint64_t tstart, tend, interval; - std::vector data; + std::deque data; const char *unit; const char *format; } timeseries_t; @@ -14,3 +14,6 @@ typedef struct { void timeseries_init(timeseries_t *ts, uint64_t tstart, uint64_t interval, const char *unit, const char *format); void timeseries_append(timeseries_t *ts, float value); + +// remove oldest values +void timeseries_prune(timeseries_t *ts, float duration); diff --git a/src/epaper.cpp b/src/epaper.cpp index 99e14e7..aa65b90 100644 --- a/src/epaper.cpp +++ b/src/epaper.cpp @@ -65,7 +65,7 @@ void epaper_draw_and_hibernate(epaper_callback cb, bool full_refresh) // find minimum and maximum values in a vector. NaN is ignored. template -void minmax(const std::vector &vec, T *min, T *max) +void minmax(const std::deque &vec, T *min, T *max) { bool first = true; diff --git a/src/main.cpp b/src/main.cpp index 77d96b9..47cbb10 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -157,7 +157,7 @@ static void draw_epaper_initial_callback(GxEPD2_DISPLAY_CLASSsetCursor(0, y); display->setFont(&FreeSans12pt7b); - display->print("Connected to WLAN: "); + display->print("WLAN verbunden: "); y += FreeSans12pt7b.yAdvance; display->setCursor(0, y); @@ -181,7 +181,7 @@ static void draw_epaper_initial_callback(GxEPD2_DISPLAY_CLASSsetCursor(0, y); display->setFont(&FreeSans12pt7b); - display->print("Initial time: "); + display->print("Synchronisierte Zeit: "); y += FreeSans12pt7b.yAdvance; display->setCursor(0, y); @@ -348,6 +348,8 @@ void loop(void) if((now - lastEPaperRefresh) >= 60000) { lastEPaperRefresh = now; epaper_draw_and_hibernate(draw_epaper_callback, false); + + timeseries_prune(&ts_scd30_humidity, 3600); // keep the last hour } /* diff --git a/src/timeseries.cpp b/src/timeseries.cpp index 44687b3..39cace8 100644 --- a/src/timeseries.cpp +++ b/src/timeseries.cpp @@ -16,3 +16,17 @@ void timeseries_append(timeseries_t *ts, float value) ts->data.push_back(value); ts->tend += ts->interval; } + + +void timeseries_prune(timeseries_t *ts, float duration) +{ + std::deque::size_type to_keep = duration / ts->interval; + if(to_keep >= ts->data.size()) { + return; + } + + std::deque::size_type to_delete = ts->data.size() - to_keep; + ts->data.erase(ts->data.begin(), ts->data.begin() + to_delete); + + ts->tstart += to_delete * ts->interval; +}