Skip to content

Commit b969272

Browse files
authored
Merge pull request #719 from Riksu9000/improve_battery_reporting
Improve battery percentage calculation and reporting
2 parents c99feee + 1777b9d commit b969272

5 files changed

Lines changed: 25 additions & 19 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Battery::Battery() {
1313
nrf_gpio_cfg_input(PinMap::Charging, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled);
1414
}
1515

16-
void Battery::Update() {
16+
void Battery::ReadPowerState() {
1717
isCharging = !nrf_gpio_pin_read(PinMap::Charging);
1818
isPowerPresent = !nrf_gpio_pin_read(PinMap::PowerPresent);
1919

@@ -22,6 +22,10 @@ void Battery::Update() {
2222
} else if (!isPowerPresent) {
2323
isFull = false;
2424
}
25+
}
26+
27+
void Battery::MeasureVoltage() {
28+
ReadPowerState();
2529

2630
if (isReading) {
2731
return;
@@ -69,18 +73,23 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
6973
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
7074
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024;
7175

76+
uint8_t newPercent;
7277
if (isFull) {
73-
percentRemaining = 100;
78+
newPercent = 100;
7479
} else if (voltage < battery_min) {
75-
percentRemaining = 0;
80+
newPercent = 0;
7681
} else {
77-
percentRemaining = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100);
82+
newPercent = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100);
83+
}
84+
85+
if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) {
86+
firstMeasurement = false;
87+
percentRemaining = newPercent;
88+
systemTask->PushMessage(System::Messages::BatteryPercentageUpdated);
7889
}
7990

8091
nrfx_saadc_uninit();
8192
isReading = false;
82-
83-
systemTask->PushMessage(System::Messages::BatteryMeasurementDone);
8493
}
8594
}
8695

src/components/battery/BatteryController.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace Pinetime {
1010
public:
1111
Battery();
1212

13-
void Update();
13+
void ReadPowerState();
14+
void MeasureVoltage();
1415
void Register(System::SystemTask* systemTask);
1516

1617
uint8_t PercentRemaining() const {
@@ -42,6 +43,7 @@ namespace Pinetime {
4243
bool isFull = false;
4344
bool isCharging = false;
4445
bool isPowerPresent = false;
46+
bool firstMeasurement = true;
4547

4648
void SaadcInit();
4749

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: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ void SystemTask::Work() {
136136
touchPanel.Init();
137137
dateTimeController.Register(this);
138138
batteryController.Register(this);
139-
batteryController.Update();
140139
motorController.Init();
141140
motionSensor.SoftReset();
142141
timerController.Register(this);
@@ -194,6 +193,8 @@ void SystemTask::Work() {
194193
nrf_gpio_cfg_sense_input(PinMap::PowerPresent, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH);
195194
}
196195

196+
batteryController.MeasureVoltage();
197+
197198
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback);
198199
dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback);
199200
measureBatteryTimer = xTimerCreate("measureBattery", batteryMeasurementPeriod, pdTRUE, this, MeasureBatteryTimerCallback);
@@ -345,22 +346,18 @@ void SystemTask::Work() {
345346
stepCounterMustBeReset = true;
346347
break;
347348
case Messages::OnChargingEvent:
348-
batteryController.Update();
349+
batteryController.ReadPowerState();
349350
motorController.RunForDuration(15);
350351
ReloadIdleTimer();
351352
if (isSleeping && !isWakingUp) {
352353
GoToRunning();
353354
}
354355
break;
355356
case Messages::MeasureBatteryTimerExpired:
356-
sendBatteryNotification = true;
357-
batteryController.Update();
357+
batteryController.MeasureVoltage();
358358
break;
359-
case Messages::BatteryMeasurementDone:
360-
if (sendBatteryNotification) {
361-
sendBatteryNotification = false;
362-
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
363-
}
359+
case Messages::BatteryPercentageUpdated:
360+
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
364361
break;
365362

366363
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)