Skip to content

Commit a9f7153

Browse files
committed
Improve battery percentage calculation and reporting
While charging, percentage should only go up, and while discharging, percentage should only go down.
1 parent b84a546 commit a9f7153

5 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,23 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
6969
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
7070
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024;
7171

72+
uint8_t newPercent;
7273
if (isFull) {
73-
percentRemaining = 100;
74+
newPercent = 100;
7475
} else if (voltage < battery_min) {
75-
percentRemaining = 0;
76+
newPercent = 0;
7677
} else {
77-
percentRemaining = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100);
78+
newPercent = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100);
79+
}
80+
81+
if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) {
82+
firstMeasurement = false;
83+
percentRemaining = newPercent;
84+
systemTask->PushMessage(System::Messages::BatteryPercentageUpdated);
7885
}
7986

8087
nrfx_saadc_uninit();
8188
isReading = false;
82-
83-
systemTask->PushMessage(System::Messages::BatteryMeasurementDone);
8489
}
8590
}
8691

src/components/battery/BatteryController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace Pinetime {
4242
bool isFull = false;
4343
bool isCharging = false;
4444
bool isPowerPresent = false;
45+
bool firstMeasurement = true;
4546

4647
void SaadcInit();
4748

src/systemtask/Messages.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Pinetime {
2424
SetOffAlarm,
2525
StopRinging,
2626
MeasureBatteryTimerExpired,
27-
BatteryMeasurementDone,
27+
BatteryPercentageUpdated,
2828
};
2929
}
3030
}

src/systemtask/SystemTask.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,10 @@ void SystemTask::Work() {
349349
motorController.RunForDuration(15);
350350
break;
351351
case Messages::MeasureBatteryTimerExpired:
352-
sendBatteryNotification = true;
353352
batteryController.Update();
354353
break;
355-
case Messages::BatteryMeasurementDone:
356-
if (sendBatteryNotification) {
357-
sendBatteryNotification = false;
358-
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
359-
}
354+
case Messages::BatteryPercentageUpdated:
355+
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
360356
break;
361357

362358
default:

src/systemtask/SystemTask.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,12 @@ namespace Pinetime {
133133
TimerHandle_t dimTimer;
134134
TimerHandle_t idleTimer;
135135
TimerHandle_t measureBatteryTimer;
136-
bool sendBatteryNotification = false;
137136
bool doNotGoToSleep = false;
138137

139138
void GoToRunning();
140139
void UpdateMotion();
141140
bool stepCounterMustBeReset = false;
142141
static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000);
143-
TickType_t lastBatteryNotificationTime = 0;
144142

145143
#if configUSE_TRACE_FACILITY == 1
146144
SystemMonitor<FreeRtosMonitor> monitor;

0 commit comments

Comments
 (0)