Files
Arduino/WeatherPredictor/history.cpp
T

39 lines
1.1 KiB
C++

#include "config.h"
#include "history.h"
static Sample s_buf[HISTORY_SIZE];
static int s_head = 0; // next write position
static int s_count = 0;
void historyBegin() { s_head = 0; s_count = 0; }
void historyAdd(uint32_t epoch, float mslHpa, float tempC) {
s_buf[s_head] = Sample{ epoch, mslHpa, tempC };
s_head = (s_head + 1) % HISTORY_SIZE;
if (s_count < HISTORY_SIZE) s_count++;
}
int historyCount() { return s_count; }
Sample historyGet(int i) {
int start = (s_head - s_count + HISTORY_SIZE) % HISTORY_SIZE;
return s_buf[(start + i) % HISTORY_SIZE];
}
Sample historyLatest() { return historyGet(s_count - 1); }
bool historyTrendDelta(float& outDelta) {
if (s_count < 2) return false;
Sample latest = historyLatest();
uint32_t target = latest.epoch - 10800UL; // 3 h earlier
int idx = -1;
for (int i = 0; i < s_count; i++) {
if (historyGet(i).epoch <= target) idx = i; else break;
}
if (idx < 0) return false;
Sample past = historyGet(idx);
if (latest.epoch - past.epoch < 9000UL) return false; // need >= 2.5 h span
outDelta = latest.mslHpa - past.mslHpa;
return true;
}