Public Access
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
61 lines
2.0 KiB
C++
61 lines
2.0 KiB
C++
#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
|