Skip to content

Commit 512d587

Browse files
committed
Examples: Companion_Radio: UI-New: Utilize battery from millivolts for battery percentage calculations and add it to the sensors page
Signed-off-by: Luis Garcia <[email protected]>
1 parent d8d7b5a commit 512d587

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

examples/companion_radio/ui-new/UITask.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "UITask.h"
22
#include <helpers/TxtDataHelpers.h>
3+
#include <helpers/Battery.h>
34
#include "../MyMesh.h"
45
#include "target.h"
56
#ifdef WIFI_SSID
@@ -102,12 +103,7 @@ class HomeScreen : public UIScreen {
102103

103104

104105
void renderBatteryIndicator(DisplayDriver& display, uint16_t batteryMilliVolts) {
105-
// Convert millivolts to percentage
106-
const int minMilliVolts = 3000; // Minimum voltage (e.g., 3.0V)
107-
const int maxMilliVolts = 4200; // Maximum voltage (e.g., 4.2V)
108-
int batteryPercentage = ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts);
109-
if (batteryPercentage < 0) batteryPercentage = 0; // Clamp to 0%
110-
if (batteryPercentage > 100) batteryPercentage = 100; // Clamp to 100%
106+
int batteryPercentage = batteryPercentFromMilliVolts(batteryMilliVolts);
111107

112108
// battery icon
113109
int iconWidth = 24;
@@ -336,8 +332,20 @@ class HomeScreen : public UIScreen {
336332
strcpy(name, "gps"); sprintf(buf, "%.4f %.4f", lat, lon);
337333
break;
338334
case LPP_VOLTAGE:
339-
r.readVoltage(v);
340-
strcpy(name, "voltage"); sprintf(buf, "%6.2f", v);
335+
r.readVoltage(v); // v is in volts
336+
337+
if (channel == TELEM_CHANNEL_SELF) {
338+
// This is our own battery voltage
339+
uint16_t batteryMilliVolts = (uint16_t)(v * 1000.0f + 0.5f); // convert V -> mV
340+
int pct = batteryPercentFromMilliVolts(batteryMilliVolts);
341+
342+
strcpy(name, "battery");
343+
sprintf(buf, "%4.2fV %3d%%", v, pct);
344+
} else {
345+
// Other voltage sensor
346+
strcpy(name, "voltage");
347+
sprintf(buf, "%6.2f", v);
348+
}
341349
break;
342350
case LPP_CURRENT:
343351
r.readCurrent(v);

0 commit comments

Comments
 (0)