Add configurable battery and USB display behavior
Persist separate display modes and timeouts in seconds for battery and USB power, expose four controls in WebConfig and CLI, and apply one shared policy across display-equipped roles. Detect R8 external power from a USB host or calibrated battery voltage above 4.21 V, with cached sampling and hysteresis. Add policy, persistence, browser, and R8 power-detection regression coverage.
This commit is contained in:
1 parent
d83153e935
commit
a814a00ce7
39 files changed
+3187
-2488
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
#include "CommonCLI.h"
|
||||
#include <helpers/ui/DisplayPowerSettings.h>
|
||||
#include "CLICommandUtils.h"
|
||||
#include "FloodAdvertCLI.h"
|
||||
#include "StorageLayout.h"
|
||||
@@ -731,6 +732,8 @@ static void formatSnrDbX4Short(char* dest, size_t dest_len, int16_t snr_x4) {
|
||||
}
|
||||
|
||||
void CommonCLI::loadPrefs(FILESYSTEM* fs) {
|
||||
const bool display_settings_loaded = mesh::ui::loadDisplayPowerSettings(fs, false);
|
||||
(void)display_settings_loaded;
|
||||
bool is_fresh_install = false;
|
||||
bool is_upgrade = false;
|
||||
bool loaded = false;
|
||||
@@ -785,6 +788,8 @@ void CommonCLI::loadPrefs(FILESYSTEM* fs) {
|
||||
MQTTPrefsAtomicStore::LegacyUpgradeGate legacy_upgrade(
|
||||
_com_prefs_needs_upgrade || node_prefs_needs_migration);
|
||||
loadMQTTPrefs(fs, &legacy_upgrade);
|
||||
if (!display_settings_loaded && _mqtt_prefs.display_timeout_secs != DISPLAY_TIMEOUT_DEFAULT_SECS)
|
||||
mesh::ui::migrateLegacyDisplayTimeout(_mqtt_prefs.display_timeout_secs);
|
||||
if (_mqtt_prefs_hold) legacy_upgrade.holdMqttSource();
|
||||
|
||||
// For MQTT bridge, migrate bridge.source to RX (logRx) only on fresh installs or upgrades
|
||||
@@ -2457,6 +2462,7 @@ uint8_t CommonCLI::buildAdvertData(uint8_t node_type, uint8_t* app_data) {
|
||||
|
||||
void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* reply) {
|
||||
mesh::cli::normalizeCommandVerb(command);
|
||||
if (mesh::ui::handleDisplayPowerCommand(command, reply, 160)) return;
|
||||
|
||||
// Observer-only top-level commands (ota check/update, tls.bundletest, alert test)
|
||||
// live in CommonCLI_Observer.cpp.
|
||||
|
||||
@@ -22,6 +22,7 @@ static const char* const WC_ALLOWED_SET_KEYS[] = {
|
||||
"lat", "lon", "radio", "tx", "af", "rxdelay", "txdelay",
|
||||
"cad", "radio.rxgain", "radio.fem.rxgain", "radio.rxps", "powersaving",
|
||||
"repeat",
|
||||
"display.mode", "display.timeout", "display.usb.mode", "display.usb.timeout",
|
||||
"advert.interval", "flood.advert.interval",
|
||||
"flood.max", "flood.max.advert", "flood.max.unscoped", "loop.detect",
|
||||
// MQTTPrefs (WiFi / MQTT / misc observer)
|
||||
|
||||
+2171
-2137
File diff suppressed because it is too large.
Load diff
@@ -10,6 +10,8 @@ static_assert(sizeof(WEBCONFIG_AP_PREFIX) <= 28,
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <DNSServer.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <helpers/ui/DisplayPowerSettings.h>
|
||||
#include <helpers/ui/DisplayBuildFlags.h>
|
||||
#include <Preferences.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_system.h>
|
||||
@@ -1791,6 +1793,15 @@ void WebConfigServer::handleConfigGet(AsyncWebServerRequest* req) {
|
||||
radio["rxps_rx_us"] = node.rx_ps_rx_us;
|
||||
radio["rxps_sleep_us"] = node.rx_ps_sleep_us;
|
||||
radio["powersaving"] = (bool)node.power_saving;
|
||||
#ifdef MESHCORE_HAS_REAL_DISPLAY
|
||||
JsonObject screen = doc["display"].to<JsonObject>();
|
||||
const auto& display_prefs = mesh::ui::displayPowerPrefs();
|
||||
screen["mode"] = mesh::ui::displayModeName(display_prefs.battery.mode);
|
||||
screen["timeout"] = display_prefs.battery.seconds;
|
||||
screen["usb_mode"] = mesh::ui::displayModeName(display_prefs.usb.mode);
|
||||
screen["usb_timeout"] = display_prefs.usb.seconds;
|
||||
screen["pairing"] = mesh::ui::displayPairingSupported();
|
||||
#endif
|
||||
radio["repeat"] = (bool)node.repeat;
|
||||
radio["flood_max"] = node.flood_max;
|
||||
radio["flood_max_advert"] = node.flood_max_advert;
|
||||
|
||||
@@ -1,4 +1,23 @@
|
||||
#include "DisplayDriver.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
bool DisplayDriver::servicePower(bool usb_power, bool app_connected, bool pairing) {
|
||||
_power_policy.update(mesh::ui::displayPowerPrefs(), usb_power, app_connected, pairing, millis());
|
||||
const bool changed = isOn() != _power_policy.on();
|
||||
if (changed) { if (_power_policy.on()) turnOn(); else turnOff(); }
|
||||
return changed;
|
||||
}
|
||||
|
||||
bool DisplayDriver::wake(mesh::ui::DisplayWake reason) {
|
||||
if (!_power_policy.wake(reason, millis())) return false;
|
||||
if (_power_policy.on() && !isOn()) turnOn();
|
||||
return true;
|
||||
}
|
||||
|
||||
void DisplayDriver::dismiss() {
|
||||
_power_policy.dismiss();
|
||||
if (!_power_policy.on() && isOn()) turnOff();
|
||||
}
|
||||
|
||||
#if defined(ESP32_PLATFORM) && defined(MESHCORE_HAS_REAL_DISPLAY)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "DisplayBuildFlags.h"
|
||||
#include "DisplayPowerPolicy.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
@@ -15,6 +16,7 @@ public:
|
||||
|
||||
class DisplayDriver {
|
||||
int _w, _h;
|
||||
mesh::ui::DisplayPowerPolicy _power_policy;
|
||||
protected:
|
||||
DisplayDriver(int w, int h) { _w = w; _h = h; }
|
||||
void setDimensions(int w, int h) { _w = w; _h = h; }
|
||||
@@ -52,6 +54,10 @@ public:
|
||||
virtual void forceFullRefresh() {} // next refresh will be full for eink
|
||||
virtual void turnOn() = 0;
|
||||
virtual void turnOff() = 0;
|
||||
// Returns whether the panel state changed, allowing cached UIs to redraw.
|
||||
bool servicePower(bool usb_power, bool app_connected = false, bool pairing = false);
|
||||
bool wake(mesh::ui::DisplayWake reason);
|
||||
void dismiss();
|
||||
virtual void clear() = 0;
|
||||
virtual void startFrame(ColorVal bkg = UIColor::window_bkg) = 0;
|
||||
virtual void setTextSize(int sz) = 0;
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace mesh { namespace ui {
|
||||
|
||||
enum class DisplayMode : uint8_t { Off, On, Button, Pairing, ButtonPairing, Automatic };
|
||||
enum class DisplayWake : uint8_t { Boot, Button, Message };
|
||||
|
||||
struct DisplayPowerProfile {
|
||||
DisplayMode mode = DisplayMode::Button;
|
||||
uint16_t seconds = 15;
|
||||
};
|
||||
struct DisplayPowerPrefs {
|
||||
DisplayPowerProfile battery;
|
||||
DisplayPowerProfile usb;
|
||||
};
|
||||
|
||||
inline DisplayPowerPrefs& displayPowerPrefs() {
|
||||
static DisplayPowerPrefs prefs;
|
||||
return prefs;
|
||||
}
|
||||
inline const char* displayModeName(DisplayMode mode) {
|
||||
switch (mode) {
|
||||
case DisplayMode::Off: return "off";
|
||||
case DisplayMode::On: return "on";
|
||||
case DisplayMode::Button: return "button";
|
||||
case DisplayMode::Pairing: return "pairing";
|
||||
case DisplayMode::ButtonPairing: return "button-pairing";
|
||||
case DisplayMode::Automatic: return "automatic";
|
||||
}
|
||||
return "off";
|
||||
}
|
||||
inline bool parseDisplayMode(const char* text, DisplayMode& mode) {
|
||||
for (uint8_t i = 0; i <= uint8_t(DisplayMode::Automatic); ++i) {
|
||||
if (strcmp(text, displayModeName(DisplayMode(i))) == 0) {
|
||||
mode = DisplayMode(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
inline bool displayModeSupportsPairing(DisplayMode mode) {
|
||||
return mode == DisplayMode::Pairing || mode == DisplayMode::ButtonPairing;
|
||||
}
|
||||
inline bool validDisplayProfile(const DisplayPowerProfile& profile) {
|
||||
return uint8_t(profile.mode) <= uint8_t(DisplayMode::Automatic)
|
||||
&& profile.seconds >= 1 && profile.seconds <= 3600;
|
||||
}
|
||||
|
||||
// Pure, wrap-safe policy shared by every UI. Radio traffic, refreshes and USB
|
||||
// presence are not user activity. An active pairing request has its own expiry.
|
||||
class DisplayPowerPolicy {
|
||||
DisplayPowerProfile _profile;
|
||||
bool _initialized = false, _usb = false, _connected = false, _pairing = false;
|
||||
bool _timed = false;
|
||||
uint32_t _deadline = 0;
|
||||
public:
|
||||
void update(const DisplayPowerPrefs& prefs, bool usb, bool connected,
|
||||
bool pairing, uint32_t now) {
|
||||
const auto& selected = usb ? prefs.usb : prefs.battery;
|
||||
const bool reset = !_initialized || _usb != usb || _profile.mode != selected.mode;
|
||||
const bool timeout_changed = _profile.seconds != selected.seconds;
|
||||
const bool just_connected = connected && !_connected;
|
||||
_profile = selected;
|
||||
_usb = usb;
|
||||
_connected = connected;
|
||||
_pairing = pairing;
|
||||
if (reset) {
|
||||
_timed = false;
|
||||
_initialized = true;
|
||||
wake(DisplayWake::Boot, now);
|
||||
} else if (timeout_changed && _timed) {
|
||||
_deadline = now + uint32_t(_profile.seconds) * 1000;
|
||||
}
|
||||
if (just_connected && _profile.mode == DisplayMode::Automatic) _timed = false;
|
||||
if (_timed && int32_t(now - _deadline) >= 0) _timed = false;
|
||||
}
|
||||
bool wake(DisplayWake reason, uint32_t now) {
|
||||
if (!_initialized) return false;
|
||||
bool allowed = _profile.mode == DisplayMode::On;
|
||||
if (reason == DisplayWake::Button) {
|
||||
allowed |= _profile.mode == DisplayMode::Button
|
||||
|| _profile.mode == DisplayMode::ButtonPairing
|
||||
|| _profile.mode == DisplayMode::Automatic;
|
||||
} else {
|
||||
allowed |= _profile.mode == DisplayMode::Automatic && !_connected;
|
||||
}
|
||||
if (!allowed) return false;
|
||||
_timed = true;
|
||||
uint32_t duration = uint32_t(_profile.seconds) * 1000;
|
||||
if (reason == DisplayWake::Boot && duration > 4000) duration = 4000;
|
||||
_deadline = now + duration;
|
||||
return true;
|
||||
}
|
||||
void dismiss() { _timed = false; }
|
||||
bool on() const {
|
||||
if (!_initialized || _profile.mode == DisplayMode::Off) return false;
|
||||
if (_profile.mode == DisplayMode::On) return true;
|
||||
if (_pairing && (_profile.mode == DisplayMode::Automatic
|
||||
|| displayModeSupportsPairing(_profile.mode))) return true;
|
||||
return _profile.mode != DisplayMode::Pairing && _timed;
|
||||
}
|
||||
};
|
||||
|
||||
} }
|
||||
@@ -0,0 +1,159 @@
|
||||
#include "DisplayPowerSettings.h"
|
||||
#include "DisplayBuildFlags.h"
|
||||
#include <stdio.h>
|
||||
|
||||
namespace mesh { namespace ui {
|
||||
namespace {
|
||||
FILESYSTEM* settings_fs = nullptr;
|
||||
bool pairing_available = false;
|
||||
const char* const path = "/display_prefs";
|
||||
const char* const backup = "/display_prefs.bak";
|
||||
const char* const temporary = "/display_prefs.tmp";
|
||||
|
||||
// Explicit bytes avoid compiler padding and preserve a small fixed format.
|
||||
void encode(const DisplayPowerPrefs& prefs, uint8_t (&data)[12]) {
|
||||
data[0] = 'D'; data[1] = 'P'; data[2] = 1;
|
||||
data[3] = uint8_t(prefs.battery.mode);
|
||||
data[4] = prefs.battery.seconds & 255; data[5] = prefs.battery.seconds >> 8;
|
||||
data[6] = uint8_t(prefs.usb.mode);
|
||||
data[7] = prefs.usb.seconds & 255; data[8] = prefs.usb.seconds >> 8;
|
||||
data[9] = 0; data[10] = 0; data[11] = 0;
|
||||
for (unsigned i = 0; i < 11; ++i) data[11] ^= data[i];
|
||||
}
|
||||
bool readPrefs(const char* name, DisplayPowerPrefs& prefs) {
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
File file(*settings_fs);
|
||||
if (!file.open(name, FILE_O_READ)) return false;
|
||||
#else
|
||||
File file = settings_fs->open(name, "r");
|
||||
if (!file) return false;
|
||||
#endif
|
||||
uint8_t data[12], expected[12];
|
||||
const bool complete = file.size() == sizeof(data)
|
||||
&& file.read(data, sizeof(data)) == sizeof(data);
|
||||
file.close();
|
||||
if (!complete) return false;
|
||||
prefs.battery.mode = DisplayMode(data[3]);
|
||||
prefs.battery.seconds = uint16_t(data[4]) | (uint16_t(data[5]) << 8);
|
||||
prefs.usb.mode = DisplayMode(data[6]);
|
||||
prefs.usb.seconds = uint16_t(data[7]) | (uint16_t(data[8]) << 8);
|
||||
encode(prefs, expected);
|
||||
return memcmp(data, expected, sizeof(data)) == 0
|
||||
&& validDisplayProfile(prefs.battery) && validDisplayProfile(prefs.usb);
|
||||
}
|
||||
bool savePrefs(const DisplayPowerPrefs& prefs) {
|
||||
if (!settings_fs) return false;
|
||||
// Preserve the previous complete file until the replacement is verified.
|
||||
settings_fs->remove(temporary);
|
||||
if (settings_fs->exists(temporary)) return false;
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
File file(*settings_fs);
|
||||
if (!file.open(temporary, FILE_O_WRITE)) return false;
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
File file = settings_fs->open(temporary, "w");
|
||||
if (!file) return false;
|
||||
#else
|
||||
File file = settings_fs->open(temporary, "w", true);
|
||||
if (!file) return false;
|
||||
#endif
|
||||
uint8_t data[12];
|
||||
encode(prefs, data);
|
||||
const bool written = file.write(data, sizeof(data)) == sizeof(data);
|
||||
file.close();
|
||||
DisplayPowerPrefs checked;
|
||||
if (!written || !readPrefs(temporary, checked)) return false;
|
||||
if (settings_fs->exists(backup) && !settings_fs->remove(backup)) return false;
|
||||
if (settings_fs->exists(path) && !settings_fs->rename(path, backup)) return false;
|
||||
if (!settings_fs->rename(temporary, path)) {
|
||||
if (settings_fs->exists(backup)) settings_fs->rename(backup, path);
|
||||
return false;
|
||||
}
|
||||
settings_fs->remove(backup);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool loadDisplayPowerSettings(FILESYSTEM* fs, bool pairing_supported) {
|
||||
settings_fs = fs;
|
||||
pairing_available = pairing_supported;
|
||||
DisplayPowerPrefs prefs;
|
||||
if (pairing_supported) prefs.battery.mode = prefs.usb.mode = DisplayMode::ButtonPairing;
|
||||
displayPowerPrefs() = prefs;
|
||||
if (!fs) return false;
|
||||
bool loaded = readPrefs(path, prefs);
|
||||
if (!loaded && readPrefs(backup, prefs)) {
|
||||
loaded = true;
|
||||
fs->remove(path);
|
||||
fs->rename(backup, path);
|
||||
}
|
||||
if (!loaded) return false;
|
||||
if (!pairing_supported) {
|
||||
if (displayModeSupportsPairing(prefs.battery.mode)) prefs.battery.mode = DisplayMode::Button;
|
||||
if (displayModeSupportsPairing(prefs.usb.mode)) prefs.usb.mode = DisplayMode::Button;
|
||||
}
|
||||
displayPowerPrefs() = prefs;
|
||||
return true;
|
||||
}
|
||||
bool displayPairingSupported() { return pairing_available; }
|
||||
|
||||
void migrateLegacyDisplayTimeout(uint16_t seconds) {
|
||||
if (seconds > 3600) return;
|
||||
auto prefs = displayPowerPrefs();
|
||||
prefs.battery.mode = prefs.usb.mode = seconds == 0 ? DisplayMode::On : DisplayMode::Automatic;
|
||||
if (seconds) prefs.battery.seconds = prefs.usb.seconds = seconds;
|
||||
// Keep the old behavior in memory even if flash is temporarily unavailable.
|
||||
displayPowerPrefs() = prefs;
|
||||
savePrefs(prefs);
|
||||
}
|
||||
|
||||
bool handleDisplayPowerCommand(const char* command, char* reply, size_t reply_size) {
|
||||
const bool get = strncmp(command, "get ", 4) == 0;
|
||||
const bool set = strncmp(command, "set ", 4) == 0;
|
||||
if (!get && !set) return false;
|
||||
const char* key = command + 4;
|
||||
const char* keys[] = {"display.mode", "display.timeout", "display.usb.mode", "display.usb.timeout"};
|
||||
int index = -1;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const size_t len = strlen(keys[i]);
|
||||
if (strncmp(key, keys[i], len) == 0 && (key[len] == 0 || key[len] == ' ')) {
|
||||
index = i; break;
|
||||
}
|
||||
}
|
||||
if (index < 0) return false;
|
||||
#ifndef MESHCORE_HAS_REAL_DISPLAY
|
||||
snprintf(reply, reply_size, "Error: display unsupported");
|
||||
return true;
|
||||
#else
|
||||
auto candidate = displayPowerPrefs();
|
||||
auto& profile = index < 2 ? candidate.battery : candidate.usb;
|
||||
const char* value = key + strlen(keys[index]);
|
||||
if (get) {
|
||||
if (*value) snprintf(reply, reply_size, "Error: unexpected argument");
|
||||
else if (index & 1) snprintf(reply, reply_size, "> %u", unsigned(profile.seconds));
|
||||
else snprintf(reply, reply_size, "> %s", displayModeName(profile.mode));
|
||||
return true;
|
||||
}
|
||||
while (*value == ' ') ++value;
|
||||
if (index & 1) {
|
||||
uint32_t seconds = 0;
|
||||
bool valid = *value != 0;
|
||||
for (const char* p = value; *p && valid; ++p) {
|
||||
valid = *p >= '0' && *p <= '9';
|
||||
if (valid) { seconds = seconds * 10 + (*p - '0'); valid = seconds <= 3600; }
|
||||
}
|
||||
if (!valid || seconds == 0) {
|
||||
snprintf(reply, reply_size, "Error: timeout must be 1-3600 seconds"); return true;
|
||||
}
|
||||
profile.seconds = uint16_t(seconds);
|
||||
} else if (!parseDisplayMode(value, profile.mode)) {
|
||||
snprintf(reply, reply_size, "Error: use off|on|button|pairing|button-pairing|automatic"); return true;
|
||||
} else if (displayModeSupportsPairing(profile.mode) && !pairing_available) {
|
||||
snprintf(reply, reply_size, "Error: BLE pairing display unsupported"); return true;
|
||||
}
|
||||
if (!savePrefs(candidate)) snprintf(reply, reply_size, "Error: display settings save failed");
|
||||
else { displayPowerPrefs() = candidate; snprintf(reply, reply_size, "OK"); }
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
} }
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <helpers/IdentityStore.h>
|
||||
#include "DisplayPowerPolicy.h"
|
||||
|
||||
namespace mesh { namespace ui {
|
||||
|
||||
// Separate, versioned file: existing node and MQTT preference layouts stay
|
||||
// readable by older firmware. Returns true when a saved profile was loaded.
|
||||
bool loadDisplayPowerSettings(FILESYSTEM* fs, bool pairing_supported);
|
||||
bool handleDisplayPowerCommand(const char* command, char* reply, size_t reply_size);
|
||||
bool displayPairingSupported();
|
||||
void migrateLegacyDisplayTimeout(uint16_t seconds);
|
||||
|
||||
} }
|
||||
Reference in new issue
Block a user