Skip to content

Commit 0055450

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 4465edb commit 0055450

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

@@ -99,12 +100,7 @@ class HomeScreen : public UIScreen {
99100

100101

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

109105
// battery icon
110106
int iconWidth = 24;
@@ -326,8 +322,20 @@ class HomeScreen : public UIScreen {
326322
strcpy(name, "gps"); sprintf(buf, "%.4f %.4f", lat, lon);
327323
break;
328324
case LPP_VOLTAGE:
329-
r.readVoltage(v);
330-
strcpy(name, "voltage"); sprintf(buf, "%6.2f", v);
325+
r.readVoltage(v); // v is in volts
326+
327+
if (channel == TELEM_CHANNEL_SELF) {
328+
// This is our own battery voltage
329+
uint16_t batteryMilliVolts = (uint16_t)(v * 1000.0f + 0.5f); // convert V -> mV
330+
int pct = batteryPercentFromMilliVolts(batteryMilliVolts);
331+
332+
strcpy(name, "battery");
333+
sprintf(buf, "%4.2fV %3d%%", v, pct);
334+
} else {
335+
// Other voltage sensor
336+
strcpy(name, "voltage");
337+
sprintf(buf, "%6.2f", v);
338+
}
331339
break;
332340
case LPP_CURRENT:
333341
r.readCurrent(v);

0 commit comments

Comments
 (0)