#include #include "config.h" #include "flog.h" static FcastEntry s_buf[FORECAST_LOG_SIZE]; static int s_head = 0; static int s_count = 0; static char s_lastLetter = 0; // last logged letter, to detect changes void flogBegin() { s_head = 0; s_count = 0; s_lastLetter = 0; } static void push(uint32_t ep, char letter, uint8_t cat) { s_buf[s_head] = FcastEntry{ ep, letter, cat }; s_head = (s_head + 1) % FORECAST_LOG_SIZE; if (s_count < FORECAST_LOG_SIZE) s_count++; } void flogConsider(uint32_t epoch, const Forecast& f) { if (f.letter == s_lastLetter) return; // no change -> nothing to log s_lastLetter = f.letter; push(epoch, f.letter, (uint8_t)f.category); flogSave(); } int flogCount() { return s_count; } FcastEntry flogGet(int i) { int start = (s_head - s_count + FORECAST_LOG_SIZE) % FORECAST_LOG_SIZE; return s_buf[(start + i) % FORECAST_LOG_SIZE]; } static const char* FPATH = "/forecasts.dat"; bool flogSave() { File f = LittleFS.open(FPATH, "w"); if (!f) return false; int32_t n = s_count; f.write((const uint8_t*)&n, sizeof(n)); for (int i = 0; i < s_count; i++) { FcastEntry e = flogGet(i); f.write((const uint8_t*)&e, sizeof(e)); } f.close(); return true; } bool flogLoad() { File f = LittleFS.open(FPATH, "r"); if (!f) return false; int32_t n = 0; if (f.read((uint8_t*)&n, sizeof(n)) != sizeof(n)) { f.close(); return false; } if (n < 0 || n > FORECAST_LOG_SIZE) { f.close(); return false; } s_head = 0; s_count = 0; for (int i = 0; i < n; i++) { FcastEntry e; if (f.read((uint8_t*)&e, sizeof(e)) != sizeof(e)) break; push(e.epoch, e.letter, e.cat); } f.close(); if (s_count > 0) s_lastLetter = flogGet(s_count - 1).letter; // avoid re-logging on boot return true; }