Add M0 services: settings, battery, clock, storage, power

lib/services holds the host-tested logic:
- Settings: typed, validated, persisted, SettingChanged events,
  transmit gated on a confirmed Region
- BatteryEstimator: LiPo curve, median smoothing against TX sags
- ClockModel: source priority GNSS > NTP > mesh, relative ages,
  Europe/Brussels local time via POSIX TZ
- StorageMonitor: warning once per boot at 80%, Logs stop at 90%,
  Captures stop with < 2 MiB left
- PowerPolicy / PowerButton: dim/off timeouts, wake key swallowed only
  when the screen was off, G0 long press

src/services wires them to the hardware (NVS, battery ADC, SD on the
shared SPI bus with the LoRa CS held high, backlight, deep sleep), and
main.cpp is a temporary diagnostics screen for the hardware checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhqxQ49eCju4CzKYNjZzwT
This commit is contained in:
2026-10-02 09:47:36 +02:00
co-authored by Claude Opus 5.5
parent ae0726b7f9
commit 987406fa7c
28 changed files with 1434 additions and 39 deletions
+49
View File
@@ -0,0 +1,49 @@
#include "clock_model.h"
#include <cstdio>
#include <cstdlib>
#include <ctime>
namespace roro {
bool ClockModel::set(int64_t utcSeconds, TimeSource source, uint32_t nowMs) {
if (source < source_) return false;
source_ = source;
utcAtSet_ = utcSeconds;
msAtSet_ = nowMs;
return true;
}
int64_t ClockModel::utcNow(uint32_t nowMs) const {
// Unsigned subtraction handles the millis() wraparound. Uptime beyond 49 days between two
// sets would lose 49 days; sources refresh far more often than that.
return utcAtSet_ + static_cast<int64_t>(static_cast<uint32_t>(nowMs - msAtSet_) / 1000);
}
void ClockModel::applyTimezone(const char* posixTz) {
setenv("TZ", posixTz, 1);
tzset();
}
std::string ClockModel::formatLocalTime(int64_t utcSeconds) {
time_t t = static_cast<time_t>(utcSeconds);
struct tm local;
localtime_r(&t, &local);
char buf[8];
std::snprintf(buf, sizeof(buf), "%02d:%02d", local.tm_hour, local.tm_min);
return buf;
}
std::string ClockModel::formatAge(int64_t seconds) {
char buf[24];
if (seconds < 60) return "now";
if (seconds < 3600)
std::snprintf(buf, sizeof(buf), "%d min ago", static_cast<int>(seconds / 60));
else if (seconds < 86400)
std::snprintf(buf, sizeof(buf), "%d h ago", static_cast<int>(seconds / 3600));
else
std::snprintf(buf, sizeof(buf), "%d d ago", static_cast<int>(seconds / 86400));
return buf;
}
} // namespace roro