Skip to content

Commit b931689

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 <git@luigi311.com>
1 parent 67ae6d1 commit b931689

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

examples/companion_radio/ui-new/UITask.cpp

Lines changed: 16 additions & 14 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
@@ -110,18 +111,7 @@ class HomeScreen : public UIScreen {
110111

111112

112113
void renderBatteryIndicator(DisplayDriver& display, uint16_t batteryMilliVolts) {
113-
// Convert millivolts to percentage
114-
#ifndef BATT_MIN_MILLIVOLTS
115-
#define BATT_MIN_MILLIVOLTS 3000
116-
#endif
117-
#ifndef BATT_MAX_MILLIVOLTS
118-
#define BATT_MAX_MILLIVOLTS 4200
119-
#endif
120-
const int minMilliVolts = BATT_MIN_MILLIVOLTS;
121-
const int maxMilliVolts = BATT_MAX_MILLIVOLTS;
122-
int batteryPercentage = ((batteryMilliVolts - minMilliVolts) * 100) / (maxMilliVolts - minMilliVolts);
123-
if (batteryPercentage < 0) batteryPercentage = 0; // Clamp to 0%
124-
if (batteryPercentage > 100) batteryPercentage = 100; // Clamp to 100%
114+
int batteryPercentage = batteryPercentFromMilliVolts(batteryMilliVolts);
125115

126116
// battery icon
127117
int iconWidth = 24;
@@ -358,8 +348,20 @@ class HomeScreen : public UIScreen {
358348
strcpy(name, "gps"); sprintf(buf, "%.4f %.4f", lat, lon);
359349
break;
360350
case LPP_VOLTAGE:
361-
r.readVoltage(v);
362-
strcpy(name, "voltage"); sprintf(buf, "%6.2f", v);
351+
r.readVoltage(v); // v is in volts
352+
353+
if (channel == TELEM_CHANNEL_SELF) {
354+
// This is our own battery voltage
355+
uint16_t batteryMilliVolts = (uint16_t)(v * 1000.0f + 0.5f); // convert V -> mV
356+
int pct = batteryPercentFromMilliVolts(batteryMilliVolts);
357+
358+
strcpy(name, "battery");
359+
sprintf(buf, "%4.2fV %3d%%", v, pct);
360+
} else {
361+
// Other voltage sensor
362+
strcpy(name, "voltage");
363+
sprintf(buf, "%6.2f", v);
364+
}
363365
break;
364366
case LPP_CURRENT:
365367
r.readCurrent(v);

0 commit comments

Comments
 (0)