Skip to content

Commit 3c413bd

Browse files
committed
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.
1 parent c0c3787 commit 3c413bd

5 files changed

Lines changed: 40 additions & 16 deletions

File tree

src/components/battery/BatteryController.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,20 @@ void Battery::Update() {
2323
isCharging = !nrf_gpio_pin_read(chargingPin);
2424
isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
2525

26+
if ( isReading ) return;
2627
// Non blocking read
27-
SaadcInit();
28-
nrfx_saadc_sample();
28+
samples = 0;
29+
isReading = true;
30+
SaadcInit();
31+
32+
nrfx_saadc_sample();
2933

3034
}
3135

36+
void Battery::adcCallbackStatic(nrfx_saadc_evt_t const *event) {
37+
instance->SaadcEventHandler(event);
38+
}
39+
3240
void Battery::SaadcInit() {
3341
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
3442
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic));
@@ -68,10 +76,13 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const * p_event) {
6876

6977
percentRemainingBuffer.insert(percentRemaining);
7078

71-
nrfx_saadc_uninit();
79+
samples++;
80+
if ( samples > percentRemainingSamples ) {
81+
nrfx_saadc_uninit();
82+
isReading = false;
83+
} else {
84+
nrfx_saadc_sample();
85+
}
7286
}
7387
}
7488

75-
void Battery::adcCallbackStatic(nrfx_saadc_evt_t const *event) {
76-
instance->SaadcEventHandler(event);
77-
}

src/components/battery/BatteryController.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ namespace Pinetime {
5252
float Voltage() const { return voltage; }
5353

5454
bool IsCharging() const { return isCharging; }
55-
bool IsPowerPresent() const { return isPowerPresent; }
55+
bool IsPowerPresent() const { return isPowerPresent; }
5656

5757
private:
5858
static Battery *instance;
5959
nrf_saadc_value_t saadc_value;
6060

61-
static constexpr uint8_t percentRemainingSamples = 10;
61+
static constexpr uint8_t percentRemainingSamples = 5;
6262
CircBuffer<percentRemainingSamples> percentRemainingBuffer {};
6363

6464
static constexpr uint32_t chargingPin = 12;
@@ -74,6 +74,9 @@ namespace Pinetime {
7474

7575
void SaadcEventHandler(nrfx_saadc_evt_t const * p_event);
7676
static void adcCallbackStatic(nrfx_saadc_evt_t const *event);
77+
78+
bool isReading = false;
79+
uint8_t samples = 0;
7780
};
7881
}
7982
}

src/displayapp/DisplayApp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void DisplayApp::Refresh() {
132132
// clockScreen.SetBleConnectionState(bleController.IsConnected() ? Screens::Clock::BleConnectionStates::Connected : Screens::Clock::BleConnectionStates::NotConnected);
133133
break;
134134
case Messages::UpdateBatteryLevel:
135-
// clockScreen.SetBatteryPercentRemaining(batteryController.PercentRemaining());
135+
batteryController.Update();
136136
break;
137137
case Messages::NewNotification:
138138
LoadApp( Apps::NotificationsPreview, DisplayApp::FullRefreshDirections::Down );

src/displayapp/screens/BatteryInfo.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void BatteryInfo::UpdateAnim() {
8484
batteryPercent = batteryController.PercentRemaining();
8585

8686
if ( batteryPercent >= 0 ) {
87-
if ( batteryController.IsCharging() ) {
87+
if ( batteryController.IsCharging() and batteryPercent < 100 ) {
8888
animation +=1;
8989
if (animation >= 100) {
9090
animation = 0;
@@ -111,12 +111,17 @@ void BatteryInfo::UpdateScreen() {
111111
batteryVoltage = batteryController.Voltage();
112112

113113
if ( batteryPercent >= 0 ) {
114-
if ( batteryController.IsCharging() ) {
115-
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, lv_color_hex(0xFF0000));
114+
if ( batteryController.IsCharging() and batteryPercent < 100 ) {
115+
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_RED);
116116
lv_label_set_text_static(status,"Battery charging");
117-
117+
} else if ( batteryPercent == 100 ) {
118+
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_BLUE);
119+
lv_label_set_text_static(status,"Battery is fully charged");
120+
} else if ( batteryPercent < 10 ) {
121+
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_YELLOW);
122+
lv_label_set_text_static(status,"Battery is low");
118123
} else {
119-
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, lv_color_hex(0x00FF00));
124+
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC , LV_STATE_DEFAULT, LV_COLOR_GREEN);
120125
lv_label_set_text_static(status,"Battery discharging");
121126
}
122127

src/systemtask/SystemTask.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,13 @@ void SystemTask::Work() {
9898
heartRateController, settingsController, motionController);
9999
displayApp->Start();
100100

101-
batteryController.Update();
102101
displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
103102

104103
heartRateSensor.Init();
105104
heartRateSensor.Disable();
106105
heartRateApp = std::make_unique<Pinetime::Applications::HeartRateTask>(heartRateSensor, heartRateController);
107106
heartRateApp->Start();
108107

109-
110108
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);
111109
nrf_gpio_cfg_output(15);
112110
nrf_gpio_pin_set(15);
@@ -141,7 +139,14 @@ void SystemTask::Work() {
141139

142140
uint8_t msg;
143141
if (xQueueReceive(systemTasksMsgQueue, &msg, 100)) {
142+
143+
// call the battery controller or use the MSG in DisplayApp to get the battery status ???
144+
// it is necessary to validate which is the most efficient
144145
batteryController.Update();
146+
//displayApp->PushMessage(Pinetime::Applications::Display::Messages::UpdateBatteryLevel);
147+
// analyze a more efficient way to do this refreshment
148+
// this and the UpdateMotion(); can be called on a timer to be independent of the main process ???
149+
145150
Messages message = static_cast<Messages >(msg);
146151
switch(message) {
147152
case Messages::EnableSleeping:

0 commit comments

Comments
 (0)