Improve battery percentage calculation and reporting
While charging, percentage should only go up, and while discharging, percentage should only go down.
This commit is contained in:
parent
b84a546920
commit
a9f7153fdf
|
@ -69,18 +69,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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,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();
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace Pinetime {
|
||||||
SetOffAlarm,
|
SetOffAlarm,
|
||||||
StopRinging,
|
StopRinging,
|
||||||
MeasureBatteryTimerExpired,
|
MeasureBatteryTimerExpired,
|
||||||
BatteryMeasurementDone,
|
BatteryPercentageUpdated,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -349,14 +349,10 @@ void SystemTask::Work() {
|
||||||
motorController.RunForDuration(15);
|
motorController.RunForDuration(15);
|
||||||
break;
|
break;
|
||||||
case Messages::MeasureBatteryTimerExpired:
|
case Messages::MeasureBatteryTimerExpired:
|
||||||
sendBatteryNotification = true;
|
|
||||||
batteryController.Update();
|
batteryController.Update();
|
||||||
break;
|
break;
|
||||||
case Messages::BatteryMeasurementDone:
|
case Messages::BatteryPercentageUpdated:
|
||||||
if (sendBatteryNotification) {
|
|
||||||
sendBatteryNotification = false;
|
|
||||||
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
|
nimbleController.NotifyBatteryLevel(batteryController.PercentRemaining());
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue