Add configurable nRF52 battery profiles with LiFePO4 cutoff

This commit is contained in:
mikecarper committed 2026-09-30 01:11:10 -07:00
1 parent ddcc1de467
commit 11d64b3db3
31 files changed
+692 -32

No files matched your search

+8
View File
@@ -23,6 +23,9 @@
#include <helpers/StatsFormatHelper.h>
#include <helpers/UsbAsciiBinarySwitch.h>
#include <helpers/UsbLogging.h>
#if defined(NRF52_POWER_MANAGEMENT)
#include <helpers/NRF52VoltagePolicy.h>
#endif
#if defined(NRF52_PLATFORM)
#include <helpers/nrf52/RamFallbackFileSystem.h>
#endif
@@ -9316,6 +9319,11 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp,
while (*command == ' ' || *command == '\t') command++;
}
#if defined(NRF52_POWER_MANAGEMENT)
if (mesh::power::handleVoltagePolicyCommand(command, reply,
reply_capacity)) return true;
#endif
if (_radio_profiles.handle(command, reply, reply_capacity, sender_timestamp != 0)) return true;
if (!strncmp(command, "set tempradio ", 14)) command += 4;
if (!strcmp(command, "get tempradio")) command += 4;
+3
View File
@@ -197,6 +197,9 @@ static bool isNetworkTerminalActive();
#else
#if defined(EXTRAFS)
#include "ResilientInternalExtraFS.h"
extern "C" struct lfs_config* meshNrf52VoltagePrimaryFsConfig() {
return ResilientInternalExtraFS::primaryConfig();
}
extern "C" uint32_t __flash_arduino_end[];
ResilientInternalExtraFS ExtraFS(0xD4000, 0x19000, 128);
DataStore store(InternalFS, ExtraFS, rtc_clock);
@@ -1,5 +1,8 @@
#include "UITask.h"
#include <helpers/TxtDataHelpers.h>
#if defined(NRF52_POWER_MANAGEMENT)
#include <helpers/NRF52VoltagePolicy.h>
#endif
#include <helpers/ui/BluetoothPairingUiPolicy.h>
#include <helpers/ui/CompanionHomeLayout.h>
#include <helpers/ui/CompanionMessageHistory.h>
@@ -484,6 +487,10 @@ class HomeScreen : public UIScreen {
int batteryPercentage = showBattery
? ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts)
: 0;
#if defined(NRF52_POWER_MANAGEMENT)
batteryPercentage = showBattery
? mesh::power::configuredBatteryPercent(batteryMilliVolts) : 0;
#endif
if (batteryPercentage < 0) batteryPercentage = 0; // Clamp to 0%
if (batteryPercentage > 100) batteryPercentage = 100; // Clamp to 100%
+13 -1
View File
@@ -4,6 +4,9 @@
#endif
#include <Arduino.h>
#include <helpers/TxtDataHelpers.h>
#if defined(NRF52_POWER_MANAGEMENT)
#include <helpers/NRF52VoltagePolicy.h>
#endif
#include <helpers/ui/BluetoothPairingUiPolicy.h>
#include <helpers/ui/RadioProfileDisplayPage.h>
#include <helpers/ui/RadioProfileSystemStatus.h>
@@ -285,6 +288,9 @@ void UITask::renderBatteryIndicator(uint16_t batteryMilliVolts) {
const int minMilliVolts = BATT_MIN_MILLIVOLTS;
const int maxMilliVolts = BATT_MAX_MILLIVOLTS;
int batteryPercentage = ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts);
#if defined(NRF52_POWER_MANAGEMENT)
batteryPercentage = mesh::power::configuredBatteryPercent(batteryMilliVolts);
#endif
if (batteryPercentage < 0) batteryPercentage = 0; // Clamp to 0%
if (batteryPercentage > 100) batteryPercentage = 100; // Clamp to 100%
@@ -495,7 +501,13 @@ void UITask::userLedHandler() {
}
if (cur_time > next_batt_check) { // battery reads are not free, keep them rare
low_batt = _board->getBattMilliVolts() < LOW_BATT_MILLIVOLTS;
uint16_t low_batt_mv = LOW_BATT_MILLIVOLTS;
#if defined(NRF52_POWER_MANAGEMENT)
low_batt_mv = mesh::power::remapBatteryWarningMillivolts(
LOW_BATT_MILLIVOLTS, BATT_MIN_MILLIVOLTS, BATT_MAX_MILLIVOLTS,
mesh::power::configuredEmpty(), mesh::power::configuredFull());
#endif
low_batt = _board->getBattMilliVolts() < low_batt_mv;
next_batt_check = cur_time + 60000;
}
+7
View File
@@ -7,6 +7,9 @@
#include <new> // std::nothrow (heap-allocated flood rule table)
#include <stdlib.h> // for qsort()
#include <helpers/CLICommandUtils.h>
#if defined(NRF52_POWER_MANAGEMENT)
#include <helpers/NRF52VoltagePolicy.h>
#endif
#include <helpers/ClientACLCLI.h>
#include <helpers/ClockSyncUtils.h>
#include <helpers/ClientLoginPersistence.h>
@@ -455,6 +458,9 @@ static bool verifyFloodSettingsWrite(FILESYSTEM* fs, const char* filename,
}
static uint8_t batteryPercentFromMilliVolts(uint16_t batt_mv) {
#if defined(NRF52_POWER_MANAGEMENT)
return mesh::power::configuredBatteryPercent(batt_mv);
#else
const int min_mv = BATT_MIN_MILLIVOLTS;
const int max_mv = BATT_MAX_MILLIVOLTS;
if (max_mv <= min_mv) return 100;
@@ -463,6 +469,7 @@ static uint8_t batteryPercentFromMilliVolts(uint16_t batt_mv) {
if (pct < 0) return 0;
if (pct > 100) return 100;
return (uint8_t)pct;
#endif
}
static bool parseBatteryAlertPercent(const char* value, uint8_t min_value, uint8_t max_value, uint8_t& result) {
+12
View File
@@ -21,6 +21,9 @@
#include <helpers/StaticPoolPacketManager.h>
#include <helpers/SimpleMeshTables.h>
#include <helpers/IdentityStore.h>
#if defined(NRF52_POWER_MANAGEMENT)
#include <helpers/NRF52VoltagePolicy.h>
#endif
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
#endif
@@ -464,6 +467,14 @@ public:
}
void handleCommand(const char* command) {
#if defined(NRF52_POWER_MANAGEMENT)
char voltage_reply[160];
if (mesh::power::handleVoltagePolicyCommand(command, voltage_reply,
sizeof(voltage_reply))) {
Serial.println(voltage_reply);
return;
}
#endif
char profile_reply[160];
if (_radio_profiles.handle(command, profile_reply, sizeof(profile_reply))) {
Serial.println(profile_reply); return;
@@ -785,6 +796,7 @@ void setup() {
void loop() {
#if defined(NRF52_PLATFORM)
board.feedWatchdog();
board.loop();
#endif
the_mesh.loop();
rtc_clock.tick();