Merge pull request #719 from Riksu9000/improve_battery_reporting

Improve battery percentage calculation and reporting
This commit is contained in:
JF 2021-10-09 20:11:31 +02:00 committed by GitHub
commit b969272c90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 19 deletions

View File

@ -13,7 +13,7 @@ Battery::Battery() {
nrf_gpio_cfg_input(PinMap::Charging, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled); nrf_gpio_cfg_input(PinMap::Charging, static_cast<nrf_gpio_pin_pull_t> GPIO_PIN_CNF_PULL_Disabled);
} }
void Battery::Update() { void Battery::ReadPowerState() {
isCharging = !nrf_gpio_pin_read(PinMap::Charging); isCharging = !nrf_gpio_pin_read(PinMap::Charging);
isPowerPresent = !nrf_gpio_pin_read(PinMap::PowerPresent); isPowerPresent = !nrf_gpio_pin_read(PinMap::PowerPresent);
@ -22,6 +22,10 @@ void Battery::Update() {
} else if (!isPowerPresent) { } else if (!isPowerPresent) {
isFull = false; isFull = false;
} }
}
void Battery::MeasureVoltage() {
ReadPowerState();
if (isReading) { if (isReading) {
return; return;
@ -69,18 +73,23 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024 // p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024; voltage = p_event->data.done.p_buffer[0] * (8 * 600) / 1024;
uint8_t newPercent;
if (isFull) { if (isFull) {
percentRemaining = 100; newPercent = 100;
} else if (voltage < battery_min) { } else if (voltage < battery_min) {
percentRemaining = 0; newPercent = 0;
} else { } else {
percentRemaining = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100); newPercent = std::min((voltage - battery_min) * 100 / (battery_max - battery_min), isCharging ? 99 : 100);
}
if ((isPowerPresent && newPercent > percentRemaining) || (!isPowerPresent && newPercent < percentRemaining) || firstMeasurement) {
firstMeasurement = false;
percentRemaining = newPercent;
systemTask->PushMessage(System::Messages::BatteryPercentageUpdated);
} }
nrfx_saadc_uninit(); nrfx_saadc_uninit();
isReading = false; isReading = false;
systemTask->PushMessage(System::Messages::BatteryMeasurementDone);
} }
} }

View File

@ -10,7 +10,8 @@ namespace Pinetime {
public: public:
Battery(); Battery();
void Update(); void ReadPowerState();
void MeasureVoltage();
void Register(System::SystemTask* systemTask); void Register(System::SystemTask* systemTask);
uint8_t PercentRemaining() const { uint8_t PercentRemaining() const {
@ -42,6 +43,7 @@ namespace Pinetime {
bool isFull = false; bool isFull = false;
bool isCharging = false; bool isCharging = false;
bool isPowerPresent = false; bool isPowerPresent = false;
bool firstMeasurement = true;
void SaadcInit(); void SaadcInit();

View File

@ -24,7 +24,7 @@ namespace Pinetime {
SetOffAlarm, SetOffAlarm,
StopRinging, StopRinging,
MeasureBatteryTimerExpired, MeasureBatteryTimerExpired,
BatteryMeasurementDone, BatteryPercentageUpdated,
}; };
} }
} }

View File

@ -136,7 +136,6 @@ void SystemTask::Work() {
touchPanel.Init(); touchPanel.Init();
dateTimeController.Register(this); dateTimeController.Register(this);
batteryController.Register(this); batteryController.Register(this);
batteryController.Update();
motorController.Init(); motorController.Init();
motionSensor.SoftReset(); motionSensor.SoftReset();
timerController.Register(this); timerController.Register(this);
@ -194,6 +193,8 @@ void SystemTask::Work() {
nrf_gpio_cfg_sense_input(PinMap::PowerPresent, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH); nrf_gpio_cfg_sense_input(PinMap::PowerPresent, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_SENSE_HIGH);
} }
batteryController.MeasureVoltage();
idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback); idleTimer = xTimerCreate("idleTimer", pdMS_TO_TICKS(2000), pdFALSE, this, IdleTimerCallback);
dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback); dimTimer = xTimerCreate("dimTimer", pdMS_TO_TICKS(settingsController.GetScreenTimeOut() - 2000), pdFALSE, this, DimTimerCallback);
measureBatteryTimer = xTimerCreate("measureBattery", batteryMeasurementPeriod, pdTRUE, this, MeasureBatteryTimerCallback); measureBatteryTimer = xTimerCreate("measureBattery", batteryMeasurementPeriod, pdTRUE, this, MeasureBatteryTimerCallback);
@ -345,7 +346,7 @@ void SystemTask::Work() {
stepCounterMustBeReset = true; stepCounterMustBeReset = true;
break; break;
case Messages::OnChargingEvent: case Messages::OnChargingEvent:
batteryController.Update(); batteryController.ReadPowerState();
motorController.RunForDuration(15); motorController.RunForDuration(15);
ReloadIdleTimer(); ReloadIdleTimer();
if (isSleeping && !isWakingUp) { if (isSleeping && !isWakingUp) {
@ -353,14 +354,10 @@ void SystemTask::Work() {
} }
break; break;
case Messages::MeasureBatteryTimerExpired: case Messages::MeasureBatteryTimerExpired:
sendBatteryNotification = true; batteryController.MeasureVoltage();
batteryController.Update();
break; break;
case Messages::BatteryMeasurementDone: case Messages::BatteryPercentageUpdated:
if (sendBatteryNotification) { nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
sendBatteryNotification = false;
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
}
break; break;
default: default:

View File

@ -133,14 +133,12 @@ namespace Pinetime {
TimerHandle_t dimTimer; TimerHandle_t dimTimer;
TimerHandle_t idleTimer; TimerHandle_t idleTimer;
TimerHandle_t measureBatteryTimer; TimerHandle_t measureBatteryTimer;
bool sendBatteryNotification = false;
bool doNotGoToSleep = false; bool doNotGoToSleep = false;
void GoToRunning(); void GoToRunning();
void UpdateMotion(); void UpdateMotion();
bool stepCounterMustBeReset = false; bool stepCounterMustBeReset = false;
static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000); static constexpr TickType_t batteryMeasurementPeriod = pdMS_TO_TICKS(10 * 60 * 1000);
TickType_t lastBatteryNotificationTime = 0;
#if configUSE_TRACE_FACILITY == 1 #if configUSE_TRACE_FACILITY == 1
SystemMonitor<FreeRtosMonitor> monitor; SystemMonitor<FreeRtosMonitor> monitor;