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:
parent
c0c37877b5
commit
3c413bdd52
|
@ -23,12 +23,20 @@ void Battery::Update() {
|
||||||
isCharging = !nrf_gpio_pin_read(chargingPin);
|
isCharging = !nrf_gpio_pin_read(chargingPin);
|
||||||
isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
|
isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
|
||||||
|
|
||||||
|
if ( isReading ) return;
|
||||||
// Non blocking read
|
// Non blocking read
|
||||||
SaadcInit();
|
samples = 0;
|
||||||
nrfx_saadc_sample();
|
isReading = true;
|
||||||
|
SaadcInit();
|
||||||
|
|
||||||
|
nrfx_saadc_sample();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Battery::adcCallbackStatic(nrfx_saadc_evt_t const *event) {
|
||||||
|
instance->SaadcEventHandler(event);
|
||||||
|
}
|
||||||
|
|
||||||
void Battery::SaadcInit() {
|
void Battery::SaadcInit() {
|
||||||
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
|
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
|
||||||
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic));
|
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);
|
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);
|
|
||||||
}
|
|
||||||
|
|
|
@ -52,13 +52,13 @@ namespace Pinetime {
|
||||||
float Voltage() const { return voltage; }
|
float Voltage() const { return voltage; }
|
||||||
|
|
||||||
bool IsCharging() const { return isCharging; }
|
bool IsCharging() const { return isCharging; }
|
||||||
bool IsPowerPresent() const { return isPowerPresent; }
|
bool IsPowerPresent() const { return isPowerPresent; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Battery *instance;
|
static Battery *instance;
|
||||||
nrf_saadc_value_t saadc_value;
|
nrf_saadc_value_t saadc_value;
|
||||||
|
|
||||||
static constexpr uint8_t percentRemainingSamples = 10;
|
static constexpr uint8_t percentRemainingSamples = 5;
|
||||||
CircBuffer<percentRemainingSamples> percentRemainingBuffer {};
|
CircBuffer<percentRemainingSamples> percentRemainingBuffer {};
|
||||||
|
|
||||||
static constexpr uint32_t chargingPin = 12;
|
static constexpr uint32_t chargingPin = 12;
|
||||||
|
@ -74,6 +74,9 @@ namespace Pinetime {
|
||||||
|
|
||||||
void SaadcEventHandler(nrfx_saadc_evt_t const * p_event);
|
void SaadcEventHandler(nrfx_saadc_evt_t const * p_event);
|
||||||
static void adcCallbackStatic(nrfx_saadc_evt_t const *event);
|
static void adcCallbackStatic(nrfx_saadc_evt_t const *event);
|
||||||
|
|
||||||
|
bool isReading = false;
|
||||||
|
uint8_t samples = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -132,7 +132,7 @@ void DisplayApp::Refresh() {
|
||||||
// clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected : Screens::Clock::BleConnectionStates::NotConnected);
|
// clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected : Screens::Clock::BleConnectionStates::NotConnected);
|
||||||
break;
|
break;
|
||||||
case Messages::UpdateBatteryLevel:
|
case Messages::UpdateBatteryLevel:
|
||||||
// clockScreen.SetBatteryPercentRemaining(batteryController.PercentRemaining());
|
batteryController.Update();
|
||||||
break;
|
break;
|
||||||
case Messages::NewNotification:
|
case Messages::NewNotification:
|
||||||
LoadApp( Apps::NotificationsPreview, DisplayApp::FullRefreshDirections::Down );
|
LoadApp( Apps::NotificationsPreview, DisplayApp::FullRefreshDirections::Down );
|
||||||
|
|
|
@ -84,7 +84,7 @@ void BatteryInfo::UpdateAnim() {
|
||||||
batteryPercent = batteryController.PercentRemaining();
|
batteryPercent = batteryController.PercentRemaining();
|
||||||
|
|
||||||
if ( batteryPercent >= 0 ) {
|
if ( batteryPercent >= 0 ) {
|
||||||
if ( batteryController.IsCharging() ) {
|
if ( batteryController.IsCharging() and batteryPercent < 100 ) {
|
||||||
animation +=1;
|
animation +=1;
|
||||||
if (animation >= 100) {
|
if (animation >= 100) {
|
||||||
animation = 0;
|
animation = 0;
|
||||||
|
@ -111,12 +111,17 @@ void BatteryInfo::UpdateScreen() {
|
||||||
batteryVoltage = batteryController.Voltage();
|
batteryVoltage = batteryController.Voltage();
|
||||||
|
|
||||||
if ( batteryPercent >= 0 ) {
|
if ( batteryPercent >= 0 ) {
|
||||||
if ( batteryController.IsCharging() ) {
|
if ( batteryController.IsCharging() and batteryPercent < 100 ) {
|
||||||
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, lv_color_hex(0xFF0000));
|
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");
|
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 {
|
} 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");
|
lv_label_set_text_static(status,"Battery discharging");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -98,7 +98,6 @@ void SystemTask::Work() {
|
||||||
heartRateController, settingsController, motionController);
|
heartRateController, settingsController, motionController);
|
||||||
displayApp->Start();
|
displayApp->Start();
|
||||||
|
|
||||||
batteryController.Update();
|
|
||||||
displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
|
||||||
|
|
||||||
heartRateSensor.Init();
|
heartRateSensor.Init();
|
||||||
|
@ -106,7 +105,6 @@ void SystemTask::Work() {
|
||||||
heartRateApp = std::make_unique<Pinetime::Applications::HeartRateTask>(heartRateSensor, heartRateController);
|
heartRateApp = std::make_unique<Pinetime::Applications::HeartRateTask>(heartRateSensor, heartRateController);
|
||||||
heartRateApp->Start();
|
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_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_cfg_output(15);
|
||||||
nrf_gpio_pin_set(15);
|
nrf_gpio_pin_set(15);
|
||||||
|
@ -141,7 +139,14 @@ void SystemTask::Work() {
|
||||||
|
|
||||||
uint8_t msg;
|
uint8_t msg;
|
||||||
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
|
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();
|
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);
|
Messages message = static_cast<Messages >(msg);
|
||||||
switch(message) {
|
switch(message) {
|
||||||
case Messages::EnableSleeping:
|
case Messages::EnableSleeping:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user