Public Access
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:
+116
-37
@@ -1,58 +1,137 @@
|
||||
// roro9stack — boot stub: proves toolchain, display and keyboard.
|
||||
// roro9stack — temporary diagnostics screen for M0 hardware checks (replaced by the Launcher in step 7).
|
||||
#include <M5Cardputer.h>
|
||||
|
||||
#include "event_bus.h"
|
||||
#include "platform/nvs_store.h"
|
||||
#include "service_manager.h"
|
||||
#include "services/battery_service.h"
|
||||
#include "services/clock_service.h"
|
||||
#include "services/power_service.h"
|
||||
#include "services/storage_service.h"
|
||||
#include "settings.h"
|
||||
#include "version.h"
|
||||
|
||||
static roro::EventBus bus;
|
||||
static roro::ServiceManager services;
|
||||
using namespace roro;
|
||||
|
||||
static EventBus bus;
|
||||
static NvsStore nvs;
|
||||
static Settings settings(nvs, bus);
|
||||
static ServiceManager services;
|
||||
static BatteryService* battery;
|
||||
static StorageService* storage;
|
||||
static PowerService* power;
|
||||
static ClockService* clockService;
|
||||
|
||||
static String lastKey = "-";
|
||||
static String lastEvent = "-";
|
||||
static bool dirty = true;
|
||||
|
||||
static void draw() {
|
||||
auto& d = M5Cardputer.Display;
|
||||
d.fillScreen(TFT_BLACK);
|
||||
d.setTextSize(1);
|
||||
d.setTextColor(TFT_GREEN);
|
||||
d.drawString(String(kProductName) + " " + versionString(), 4, 2);
|
||||
d.setTextColor(TFT_WHITE);
|
||||
|
||||
const auto& b = battery->estimator();
|
||||
const auto& s = storage->state();
|
||||
int y = 16;
|
||||
auto line = [&](const String& text) {
|
||||
d.drawString(text, 4, y);
|
||||
y += 11;
|
||||
};
|
||||
line("Battery: " + String(b.percent()) + "% " + String(b.millivolts()) + " mV (raw " +
|
||||
String(battery->lastRawMillivolts()) + ")");
|
||||
if (s.present)
|
||||
line("SD: " + String(s.usedPercent) + "% of " + String((uint32_t)(s.totalBytes >> 20)) +
|
||||
" MB logs:" + (s.logsAllowed ? "y" : "n") + " capt:" + (s.capturesAllowed ? "y" : "n"));
|
||||
else
|
||||
line("SD: no card");
|
||||
line("Clock: " + String(clockService->displayTime().c_str()) + " uptime " + String(millis() / 1000) + " s");
|
||||
line("Heap: " + String(ESP.getFreeHeap() / 1024) + " KB free, min " + String(ESP.getMinFreeHeap() / 1024) + " KB");
|
||||
line("Bright: " + String(settings.getInt(Setting::Brightness)) + "% dim " +
|
||||
String(settings.getInt(Setting::DimTimeoutS)) + "s off " + String(settings.getInt(Setting::OffTimeoutS)) + "s");
|
||||
line("Key: " + lastKey);
|
||||
line("Event: " + lastEvent);
|
||||
d.setTextColor(TFT_DARKGREY);
|
||||
line("+/- brightness, d: dim test (5s/10s)");
|
||||
}
|
||||
|
||||
static void logEvent(const String& text) {
|
||||
lastEvent = text;
|
||||
dirty = true;
|
||||
Serial.println("event: " + text);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
auto cfg = M5.config();
|
||||
M5Cardputer.begin(cfg, true); // true = enable keyboard
|
||||
|
||||
auto& d = M5Cardputer.Display;
|
||||
d.setRotation(1);
|
||||
d.fillScreen(TFT_BLACK);
|
||||
d.setTextColor(TFT_GREEN);
|
||||
d.setTextSize(2);
|
||||
d.drawString(roro::kProductName, 8, 8);
|
||||
d.setTextSize(1);
|
||||
d.setTextColor(TFT_WHITE);
|
||||
d.drawString(roro::versionString(), 8, 32);
|
||||
d.drawString("Press keys...", 8, 56);
|
||||
|
||||
M5Cardputer.begin(cfg, true);
|
||||
M5Cardputer.Display.setRotation(1);
|
||||
Serial.begin(115200);
|
||||
|
||||
nvs.begin();
|
||||
settings.load();
|
||||
|
||||
battery = new BatteryService(bus);
|
||||
storage = new StorageService(bus);
|
||||
power = new PowerService(settings);
|
||||
clockService = new ClockService(settings, bus);
|
||||
services.add(*power);
|
||||
services.add(*clockService);
|
||||
services.add(*battery);
|
||||
services.add(*storage);
|
||||
|
||||
bus.subscribe(EventType::BatteryChanged,
|
||||
[](const Event& e) { logEvent("battery " + String(e.a) + "% " + String(e.b) + "mV"); });
|
||||
bus.subscribe(EventType::StorageThreshold,
|
||||
[](const Event& e) { logEvent("storage level " + String(e.b) + " (" + String(e.a) + "%)"); });
|
||||
bus.subscribe(EventType::Notification, [](const Event& e) { logEvent("notify: " + String(e.text)); });
|
||||
bus.subscribe(EventType::SettingChanged, [](const Event& e) { logEvent("setting " + String(e.text)); });
|
||||
|
||||
services.startAll(millis());
|
||||
}
|
||||
|
||||
static void printStatus() {
|
||||
Serial.printf("%s %s, free heap %u, min free heap %u, psram %u\n", roro::kProductName,
|
||||
roro::versionString(), ESP.getFreeHeap(), ESP.getMinFreeHeap(),
|
||||
ESP.getPsramSize());
|
||||
static void handleKeys() {
|
||||
if (!M5Cardputer.Keyboard.isChange() || !M5Cardputer.Keyboard.isPressed()) return;
|
||||
if (power->onKey(millis())) return; // only woke the screen
|
||||
|
||||
auto state = M5Cardputer.Keyboard.keysState();
|
||||
String word;
|
||||
for (auto c : state.word) word += c;
|
||||
lastKey = word + (state.fn ? " +fn" : "") + (state.opt ? " +opt" : "") + (state.ctrl ? " +ctrl" : "") +
|
||||
(state.enter ? " enter" : "") + (state.del ? " del" : "");
|
||||
dirty = true;
|
||||
|
||||
int brightness = settings.getInt(Setting::Brightness);
|
||||
if (word == "=" || word == "+") settings.setInt(Setting::Brightness, brightness + 10);
|
||||
if (word == "-") settings.setInt(Setting::Brightness, brightness - 10);
|
||||
if (word == "d") {
|
||||
// Short timeouts to test dimming quickly; persisted, so they survive a reboot.
|
||||
settings.setInt(Setting::DimTimeoutS, 5);
|
||||
settings.setInt(Setting::OffTimeoutS, 10);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static uint32_t lastStatus = 0;
|
||||
if (millis() - lastStatus > 3000) {
|
||||
lastStatus = millis();
|
||||
printStatus();
|
||||
}
|
||||
|
||||
static uint32_t lastDraw = 0, lastLog = 0;
|
||||
M5Cardputer.update();
|
||||
handleKeys();
|
||||
services.tick(millis());
|
||||
bus.dispatch();
|
||||
|
||||
M5Cardputer.update();
|
||||
if (M5Cardputer.Keyboard.isChange() && M5Cardputer.Keyboard.isPressed()) {
|
||||
auto state = M5Cardputer.Keyboard.keysState();
|
||||
String s;
|
||||
for (auto c : state.word) s += c;
|
||||
auto& d = M5Cardputer.Display;
|
||||
d.fillRect(0, 80, d.width(), 20, TFT_BLACK);
|
||||
d.drawString("key: " + s, 8, 80);
|
||||
Serial.printf("keys: word='%s' fn=%d opt=%d ctrl=%d alt=%d shift=%d enter=%d del=%d tab=%d\n",
|
||||
s.c_str(), state.fn, state.opt, state.ctrl, state.alt, state.shift,
|
||||
state.enter, state.del, state.tab);
|
||||
if (millis() - lastLog > 5000) {
|
||||
lastLog = millis();
|
||||
const auto& s = storage->state();
|
||||
Serial.printf("status: batt %d%% %dmV raw %d | sd %s %d%% total %lluMB | heap %u min %u | screen %d\n",
|
||||
battery->estimator().percent(), battery->estimator().millivolts(),
|
||||
battery->lastRawMillivolts(), s.present ? "yes" : "no", s.usedPercent,
|
||||
(unsigned long long)(s.totalBytes >> 20), ESP.getFreeHeap(), ESP.getMinFreeHeap(),
|
||||
(int)power->screen());
|
||||
}
|
||||
if (power->screen() != ScreenState::Off && (dirty || millis() - lastDraw > 1000)) {
|
||||
lastDraw = millis();
|
||||
dirty = false;
|
||||
draw();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <Preferences.h>
|
||||
|
||||
#include "key_value_store.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
// Settings persistence in the ESP32's internal flash (NVS).
|
||||
class NvsStore : public KeyValueStore {
|
||||
public:
|
||||
void begin() { prefs_.begin("roro", false); }
|
||||
|
||||
bool getInt(const char* key, int32_t& out) override {
|
||||
if (!prefs_.isKey(key)) return false;
|
||||
out = prefs_.getInt(key);
|
||||
return true;
|
||||
}
|
||||
bool getString(const char* key, std::string& out) override {
|
||||
if (!prefs_.isKey(key)) return false;
|
||||
out = prefs_.getString(key).c_str();
|
||||
return true;
|
||||
}
|
||||
void putInt(const char* key, int32_t value) override { prefs_.putInt(key, value); }
|
||||
void putString(const char* key, const std::string& value) override {
|
||||
prefs_.putString(key, value.c_str());
|
||||
}
|
||||
|
||||
private:
|
||||
Preferences prefs_;
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
// Cardputer ADV + Cap LoRa-1262 pin map (see docs.m5stack.com).
|
||||
namespace roro::pins {
|
||||
|
||||
constexpr int kBatteryAdc = 10; // through a 1:2 divider
|
||||
|
||||
constexpr int kButtonG0 = 0;
|
||||
|
||||
// SPI bus shared by the microSD card and the LoRa radio.
|
||||
constexpr int kSpiSck = 40;
|
||||
constexpr int kSpiMiso = 39;
|
||||
constexpr int kSpiMosi = 14;
|
||||
constexpr int kSdCs = 12;
|
||||
constexpr int kLoraCs = 5;
|
||||
|
||||
} // namespace roro::pins
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
#include "pins.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
// The SPI bus used by the microSD card and (from M3) the LoRa radio. The display has its own bus.
|
||||
// Every user must wrap transfers in beginTransaction/endTransaction, which also locks the bus.
|
||||
inline SPIClass& sharedSpi() {
|
||||
static SPIClass spi(HSPI);
|
||||
static bool begun = false;
|
||||
if (!begun) {
|
||||
// Keep the radio deselected so it never answers SD card traffic.
|
||||
pinMode(pins::kLoraCs, OUTPUT);
|
||||
digitalWrite(pins::kLoraCs, HIGH);
|
||||
spi.begin(pins::kSpiSck, pins::kSpiMiso, pins::kSpiMosi, -1);
|
||||
begun = true;
|
||||
}
|
||||
return spi;
|
||||
}
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "battery_service.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "platform/pins.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
void BatteryService::tick(uint32_t) {
|
||||
lastRaw_ = analogReadMilliVolts(pins::kBatteryAdc) * 2;
|
||||
estimator_.addSample(lastRaw_);
|
||||
int percent = estimator_.percent();
|
||||
if (percent != reportedPercent_) {
|
||||
reportedPercent_ = percent;
|
||||
bus_.publish(Event::withValues(EventType::BatteryChanged, percent, estimator_.millivolts()));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "battery_estimator.h"
|
||||
#include "event_bus.h"
|
||||
#include "service.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
class BatteryService : public Service {
|
||||
public:
|
||||
explicit BatteryService(EventBus& bus) : bus_(bus) {}
|
||||
const char* name() const override { return "battery"; }
|
||||
uint32_t tickIntervalMs() const override { return 2000; }
|
||||
void tick(uint32_t nowMs) override;
|
||||
|
||||
const BatteryEstimator& estimator() const { return estimator_; }
|
||||
int lastRawMillivolts() const { return lastRaw_; }
|
||||
|
||||
private:
|
||||
EventBus& bus_;
|
||||
BatteryEstimator estimator_;
|
||||
int lastRaw_ = 0;
|
||||
int reportedPercent_ = -1;
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include "clock_model.h"
|
||||
#include "event_bus.h"
|
||||
#include "service.h"
|
||||
#include "settings.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
// Holds wall-clock time. Sources (GNSS in M2, NTP in M1, mesh in M4) call set().
|
||||
class ClockService : public Service {
|
||||
public:
|
||||
ClockService(const Settings& settings, EventBus& bus) : settings_(settings) {
|
||||
bus.subscribe(EventType::SettingChanged, [this](const Event& e) {
|
||||
if (e.a == static_cast<int32_t>(Setting::Timezone)) applyTimezone();
|
||||
});
|
||||
}
|
||||
const char* name() const override { return "clock"; }
|
||||
void start() override { applyTimezone(); }
|
||||
|
||||
bool set(int64_t utcSeconds, TimeSource source) { return model_.set(utcSeconds, source, millis()); }
|
||||
const ClockModel& model() const { return model_; }
|
||||
|
||||
// "14:05" once set; otherwise "--:--".
|
||||
std::string displayTime() const {
|
||||
return model_.isSet() ? ClockModel::formatLocalTime(model_.utcNow(millis())) : "--:--";
|
||||
}
|
||||
|
||||
private:
|
||||
void applyTimezone() { ClockModel::applyTimezone(settings_.getString(Setting::Timezone).c_str()); }
|
||||
|
||||
const Settings& settings_;
|
||||
ClockModel model_;
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "power_service.h"
|
||||
|
||||
#include <M5Cardputer.h>
|
||||
#include <esp_sleep.h>
|
||||
|
||||
#include "platform/pins.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
PowerService::PowerService(const Settings& settings)
|
||||
: settings_(settings),
|
||||
policy_(settings.getInt(Setting::DimTimeoutS) * 1000, settings.getInt(Setting::OffTimeoutS) * 1000) {}
|
||||
|
||||
void PowerService::start() {
|
||||
pinMode(pins::kButtonG0, INPUT_PULLUP);
|
||||
policy_.activity(millis());
|
||||
applyScreen(ScreenState::On);
|
||||
}
|
||||
|
||||
void PowerService::tick(uint32_t nowMs) {
|
||||
policy_.setTimeouts(settings_.getInt(Setting::DimTimeoutS) * 1000,
|
||||
settings_.getInt(Setting::OffTimeoutS) * 1000);
|
||||
applyScreen(policy_.update(nowMs));
|
||||
|
||||
bool pressed = digitalRead(pins::kButtonG0) == LOW;
|
||||
if (button_.update(pressed, nowMs) == ButtonAction::LongPress) powerOff();
|
||||
}
|
||||
|
||||
void PowerService::applyScreen(ScreenState state) {
|
||||
auto& display = M5Cardputer.Display;
|
||||
int percent = settings_.getInt(Setting::Brightness);
|
||||
int target = state == ScreenState::On ? percent : state == ScreenState::Dimmed ? percent / 4 : 0;
|
||||
if (state == applied_ && target == appliedBrightness_) return;
|
||||
|
||||
if (state == ScreenState::Off)
|
||||
display.sleep();
|
||||
else if (applied_ == ScreenState::Off)
|
||||
display.wakeup();
|
||||
display.setBrightness(target * 255 / 100);
|
||||
applied_ = state;
|
||||
appliedBrightness_ = target;
|
||||
}
|
||||
|
||||
void PowerService::powerOff() {
|
||||
auto& display = M5Cardputer.Display;
|
||||
display.wakeup();
|
||||
display.setBrightness(settings_.getInt(Setting::Brightness) * 255 / 100);
|
||||
display.fillScreen(TFT_BLACK);
|
||||
display.setTextColor(TFT_WHITE);
|
||||
display.drawCenterString("Powering off", display.width() / 2, display.height() / 2 - 8);
|
||||
// G0 wakes the device on a low level, so wait for release or it would wake immediately.
|
||||
while (digitalRead(pins::kButtonG0) == LOW) delay(10);
|
||||
delay(300);
|
||||
display.sleep();
|
||||
display.setBrightness(0);
|
||||
esp_sleep_enable_ext0_wakeup(static_cast<gpio_num_t>(pins::kButtonG0), 0);
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "power_policy.h"
|
||||
#include "service.h"
|
||||
#include "settings.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
// Screen dimming/off from user activity and G0 handling. A long G0 press powers off (deep sleep,
|
||||
// woken by G0); Services stop with it.
|
||||
class PowerService : public Service {
|
||||
public:
|
||||
explicit PowerService(const Settings& settings);
|
||||
const char* name() const override { return "power"; }
|
||||
uint32_t tickIntervalMs() const override { return 50; }
|
||||
void start() override;
|
||||
void tick(uint32_t nowMs) override;
|
||||
|
||||
// Call for every key press before routing it. Returns true if the key must not reach the App.
|
||||
bool onKey(uint32_t nowMs) { return policy_.activity(nowMs); }
|
||||
|
||||
ScreenState screen() const { return applied_; }
|
||||
|
||||
private:
|
||||
void applyScreen(ScreenState state);
|
||||
void powerOff();
|
||||
|
||||
const Settings& settings_;
|
||||
PowerPolicy policy_;
|
||||
PowerButton button_{2000};
|
||||
ScreenState applied_ = ScreenState::On;
|
||||
int appliedBrightness_ = -1;
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "storage_service.h"
|
||||
|
||||
#include <SD.h>
|
||||
|
||||
#include "platform/pins.h"
|
||||
#include "platform/shared_spi.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
bool StorageService::mount() {
|
||||
return SD.begin(pins::kSdCs, sharedSpi(), 20000000, "/sd", 5, false);
|
||||
}
|
||||
|
||||
void StorageService::tick(uint32_t) {
|
||||
if (mounted_) {
|
||||
// Detect removal: the root can no longer be opened.
|
||||
File root = SD.open("/");
|
||||
if (!root) {
|
||||
SD.end();
|
||||
mounted_ = false;
|
||||
}
|
||||
}
|
||||
if (!mounted_) mounted_ = mount();
|
||||
|
||||
if (mounted_)
|
||||
monitor_.update(true, SD.totalBytes(), SD.usedBytes());
|
||||
else
|
||||
monitor_.update(false, 0, 0);
|
||||
}
|
||||
|
||||
void StorageService::stop() {
|
||||
if (mounted_) SD.end();
|
||||
mounted_ = false;
|
||||
}
|
||||
|
||||
} // namespace roro
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "service.h"
|
||||
#include "storage_monitor.h"
|
||||
|
||||
namespace roro {
|
||||
|
||||
// Mounts the microSD card (retrying while absent) and feeds its usage to the StorageMonitor.
|
||||
class StorageService : public Service {
|
||||
public:
|
||||
explicit StorageService(EventBus& bus) : monitor_(bus) {}
|
||||
const char* name() const override { return "storage"; }
|
||||
uint32_t tickIntervalMs() const override { return 15000; }
|
||||
void tick(uint32_t nowMs) override;
|
||||
void stop() override;
|
||||
|
||||
const StorageState& state() const { return monitor_.state(); }
|
||||
|
||||
private:
|
||||
bool mount();
|
||||
StorageMonitor monitor_;
|
||||
bool mounted_ = false;
|
||||
};
|
||||
|
||||
} // namespace roro
|
||||
Reference in New Issue
Block a user