In order to stabilize the battery reading,

I modified the process to make 5 consecutive readings,
as the process is asynchronous,
there is no interference in the main process.
This commit is contained in:
Joaquim 2021-04-16 16:15:38 +01:00
parent c0c37877b5
commit 3c413bdd52
5 changed files with 40 additions and 16 deletions

View File

@ -23,12 +23,20 @@ void Battery::Update() {
isCharging = !nrf_gpio_pin_read(chargingPin);
isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
if ( isReading ) return;
// Non blocking read
SaadcInit();
nrfx_saadc_sample();
samples = 0;
isReading = true;
SaadcInit();
nrfx_saadc_sample();
}
void Battery::adcCallbackStatic(nrfx_saadc_evt_t const *event) {
instance->SaadcEventHandler(event);
}
void Battery::SaadcInit() {
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic));
@ -68,10 +76,13 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const * p_event) {
percentRemainingBuffer.insert(percentRemaining);
nrfx_saadc_uninit();
samples++;
if ( samples > percentRemainingSamples ) {
nrfx_saadc_uninit();
isReading = false;
} else {
nrfx_saadc_sample();
}
}
}
void Battery::adcCallbackStatic(nrfx_saadc_evt_t const *event) {
instance->SaadcEventHandler(event);
}

View File

@ -52,13 +52,13 @@ namespace Pinetime {
float Voltage() const { return voltage; }
bool IsCharging() const { return isCharging; }
bool IsPowerPresent() const { return isPowerPresent; }
bool IsPowerPresent() const { return isPowerPresent; }
private:
static Battery *instance;
nrf_saadc_value_t saadc_value;
static constexpr uint8_t percentRemainingSamples = 10;
static constexpr uint8_t percentRemainingSamples = 5;
CircBuffer<percentRemainingSamples> percentRemainingBuffer {};
static constexpr uint32_t chargingPin = 12;
@ -74,6 +74,9 @@ namespace Pinetime {
void SaadcEventHandler(nrfx_saadc_evt_t const * p_event);
static void adcCallbackStatic(nrfx_saadc_evt_t const *event);
bool isReading = false;
uint8_t samples = 0;
};
}
}

View File

@ -132,7 +132,7 @@ void DisplayApp::Refresh() {
// clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected : Screens::Clock::BleConnectionStates::NotConnected);
break;
case Messages::UpdateBatteryLevel:
// clockScreen.SetBatteryPercentRemaining(batteryController.PercentRemaining());
batteryController.Update();
break;
case Messages::NewNotification:
LoadApp( Apps::NotificationsPreview, DisplayApp::FullRefreshDirections::Down );

View File

@ -84,7 +84,7 @@ void BatteryInfo::UpdateAnim() {
batteryPercent = batteryController.PercentRemaining();
if ( batteryPercent >= 0 ) {
if ( batteryController.IsCharging() ) {
if ( batteryController.IsCharging() and batteryPercent < 100 ) {
animation +=1;
if (animation >= 100) {
animation = 0;
@ -111,12 +111,17 @@ void BatteryInfo::UpdateScreen() {
batteryVoltage = batteryController.Voltage();
if ( batteryPercent >= 0 ) {
if ( batteryController.IsCharging() ) {
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, lv_color_hex(0xFF0000));
if ( batteryController.IsCharging() and batteryPercent < 100 ) {
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_RED);
lv_label_set_text_static(status,"Battery charging");
} else if ( batteryPercent == 100 ) {
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_BLUE);
lv_label_set_text_static(status,"Battery is fully charged");
} else if ( batteryPercent < 10 ) {
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_YELLOW);
lv_label_set_text_static(status,"Battery is low");
} else {
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, lv_color_hex(0x00FF00));
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_GREEN);
lv_label_set_text_static(status,"Battery discharging");
}

View File

@ -98,7 +98,6 @@ void SystemTask::Work() {
heartRateController, settingsController, motionController);
displayApp->Start();
batteryController.Update();
displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
heartRateSensor.Init();
@ -106,7 +105,6 @@ void SystemTask::Work() {
heartRateApp = std::make_unique<Pinetime::Applications::HeartRateTask>(heartRateSensor, heartRateController);
heartRateApp->Start();
nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t)GPIO_PIN_CNF_SENSE_High);
nrf_gpio_cfg_output(15);
nrf_gpio_pin_set(15);
@ -141,7 +139,14 @@ void SystemTask::Work() {
uint8_t msg;
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
// call the battery controller or use the MSG in DisplayApp to get the battery status ???
// it is necessary to validate which is the most efficient
batteryController.Update();
//displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
// analyze a more efficient way to do this refreshment
// this and the UpdateMotion(); can be called on a timer to be independent of the main process ???
Messages message = static_cast<Messages >(msg);
switch(message) {
case Messages::EnableSleeping: