Skip to content

Commit af10747

Browse files
committed
Merge branch 'notify-battery-level' into develop
2 parents 4f378e8 + 3e70554 commit af10747

17 files changed

Lines changed: 71 additions & 68 deletions

src/components/battery/BatteryController.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include "BatteryController.h"
22
#include <hal/nrf_gpio.h>
33
#include <nrfx_saadc.h>
4-
#include <libraries/log/nrf_log.h>
54
#include <algorithm>
6-
#include <math.h>
75

86
using namespace Pinetime::Controllers;
97

@@ -18,7 +16,6 @@ void Battery::Init() {
1816
}
1917

2018
void Battery::Update() {
21-
2219
isCharging = !nrf_gpio_pin_read(chargingPin);
2320
isPowerPresent = !nrf_gpio_pin_read(powerPresentPin);
2421

@@ -33,13 +30,13 @@ void Battery::Update() {
3330
nrfx_saadc_sample();
3431
}
3532

36-
void Battery::adcCallbackStatic(nrfx_saadc_evt_t const* event) {
33+
void Battery::AdcCallbackStatic(nrfx_saadc_evt_t const* event) {
3734
instance->SaadcEventHandler(event);
3835
}
3936

4037
void Battery::SaadcInit() {
4138
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
42-
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic));
39+
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, AdcCallbackStatic));
4340

4441
nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
4542
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
@@ -55,7 +52,6 @@ void Battery::SaadcInit() {
5552
}
5653

5754
void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
58-
5955
const uint16_t battery_max = 4180; // maximum voltage of battery ( max charging voltage is 4.21 )
6056
const uint16_t battery_min = 3200; // minimum voltage of battery before shutdown ( depends on the battery )
6157

@@ -69,13 +65,10 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
6965
// reference_voltage is 0.6V
7066
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
7167
voltage = p_event->data.done.p_buffer[0] * 6000 / 1024;
72-
7368
percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min);
74-
7569
percentRemaining = std::max(percentRemaining, 0);
7670
percentRemaining = std::min(percentRemaining, 100);
77-
78-
percentRemainingBuffer.insert(percentRemaining);
71+
percentRemainingBuffer.Insert(percentRemaining);
7972

8073
samples++;
8174
if (samples > percentRemainingSamples) {

src/components/battery/BatteryController.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ namespace Pinetime {
1919
insert member function overwrites the next data to the current
2020
HEAD and moves the HEAD to the newly inserted value.
2121
*/
22-
void insert(const int num) {
22+
void Insert(const uint8_t num) {
2323
head %= cap;
2424
arr[head++] = num;
2525
if (sz != cap) {
2626
sz++;
2727
}
2828
}
2929

30-
int GetAverage() const {
30+
uint8_t GetAverage() const {
3131
int sum = std::accumulate(arr.begin(), arr.end(), 0);
32-
return (sum / sz);
32+
return static_cast<uint8_t>(sum / sz);
3333
}
3434

3535
private:
36-
std::array<int, N> arr; /**< internal array used to store the values*/
36+
std::array<uint8_t, N> arr; /**< internal array used to store the values*/
3737
uint8_t sz; /**< The current size of the array.*/
3838
uint8_t cap; /**< Total capacity of the CircBuffer.*/
3939
uint8_t head; /**< The current head of the CircBuffer*/
@@ -46,8 +46,11 @@ namespace Pinetime {
4646
void Init();
4747
void Update();
4848

49-
int PercentRemaining() const {
50-
return percentRemainingBuffer.GetAverage();
49+
uint8_t PercentRemaining() const {
50+
auto avg = percentRemainingBuffer.GetAverage();
51+
avg = std::min(avg, static_cast<uint8_t>(100));
52+
avg = std::max(avg, static_cast<uint8_t>(0));
53+
return avg;
5154
}
5255

5356
uint16_t Voltage() const {
@@ -57,6 +60,7 @@ namespace Pinetime {
5760
bool IsCharging() const {
5861
return isCharging;
5962
}
63+
6064
bool IsPowerPresent() const {
6165
return isPowerPresent;
6266
}
@@ -80,7 +84,7 @@ namespace Pinetime {
8084
void SaadcInit();
8185

8286
void SaadcEventHandler(nrfx_saadc_evt_t const* p_event);
83-
static void adcCallbackStatic(nrfx_saadc_evt_t const* event);
87+
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);
8488

8589
bool isReading = false;
8690
uint8_t samples = 0;

src/components/ble/BatteryInformationService.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ BatteryInformationService::BatteryInformationService(Controllers::Battery& batte
1717
characteristicDefinition {{.uuid = (ble_uuid_t*) &batteryLevelUuid,
1818
.access_cb = BatteryInformationServiceCallback,
1919
.arg = this,
20-
.flags = BLE_GATT_CHR_F_READ,
20+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
2121
.val_handle = &batteryLevelHandle},
2222
{0}},
2323
serviceDefinition {
@@ -48,4 +48,8 @@ int BatteryInformationService::OnBatteryServiceRequested(uint16_t connectionHand
4848
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
4949
}
5050
return 0;
51-
}
51+
}
52+
void BatteryInformationService::NotifyBatteryLevel(uint16_t connectionHandle, uint8_t level) {
53+
auto* om = ble_hs_mbuf_from_flat(&level, 1);
54+
ble_gattc_notify_custom(connectionHandle, batteryLevelHandle, om);
55+
}

src/components/ble/BatteryInformationService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Pinetime {
1717
void Init();
1818

1919
int OnBatteryServiceRequested(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context);
20-
20+
void NotifyBatteryLevel(uint16_t connectionHandle, uint8_t level);
2121
private:
2222
Controllers::Battery& batteryController;
2323
static constexpr uint16_t batteryInformationServiceId {0x180F};

src/components/ble/NimbleController.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,9 @@ void NimbleController::StartDiscovery() {
235235
uint16_t NimbleController::connHandle() {
236236
return connectionHandle;
237237
}
238+
239+
void NimbleController::NotifyBatteryLevel(uint8_t level) {
240+
if(connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
241+
batteryInformationService.NotifyBatteryLevel(connectionHandle, level);
242+
}
243+
}

src/components/ble/NimbleController.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ namespace Pinetime {
7070
};
7171

7272
uint16_t connHandle();
73+
void NotifyBatteryLevel(uint8_t level);
7374

7475
private:
7576
static constexpr const char* deviceName = "InfiniTime";
@@ -92,7 +93,7 @@ namespace Pinetime {
9293
HeartRateService heartRateService;
9394

9495
uint8_t addrType; // 1 = Random, 0 = PUBLIC
95-
uint16_t connectionHandle = 0;
96+
uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE;
9697

9798
ble_uuid128_t dfuServiceUuid {
9899
.u {.type = BLE_UUID_TYPE_128},

src/displayapp/screens/BatteryIcon.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
#include <cstdint>
12
#include "BatteryIcon.h"
23
#include "Symbols.h"
34

45
using namespace Pinetime::Applications::Screens;
56

6-
const char* BatteryIcon::GetBatteryIcon(int batteryPercent) {
7+
const char* BatteryIcon::GetBatteryIcon(uint8_t batteryPercent) {
78
if (batteryPercent > 90)
89
return Symbols::batteryFull;
910
if (batteryPercent > 75)

src/displayapp/screens/BatteryIcon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Pinetime {
66
class BatteryIcon {
77
public:
88
static const char* GetUnknownIcon();
9-
static const char* GetBatteryIcon(int batteryPercent);
9+
static const char* GetBatteryIcon(uint8_t batteryPercent);
1010
static const char* GetPlugIcon(bool isCharging);
1111
};
1212
}

src/displayapp/screens/BatteryInfo.cpp

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ BatteryInfo::BatteryInfo(Pinetime::Applications::DisplayApp* app, Pinetime::Cont
3838

3939
percent = lv_label_create(lv_scr_act(), nullptr);
4040
lv_obj_set_style_local_text_font(percent, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76);
41-
if (batteryPercent >= 0) {
42-
lv_label_set_text_fmt(percent, "%02i%%", batteryPercent);
43-
} else {
44-
lv_label_set_text(percent, "--%");
45-
}
41+
lv_label_set_text_fmt(percent, "%02i%%", batteryPercent);
4642
lv_label_set_align(percent, LV_LABEL_ALIGN_LEFT);
4743
lv_obj_align(percent, nullptr, LV_ALIGN_CENTER, 0, -60);
4844

@@ -72,24 +68,22 @@ BatteryInfo::~BatteryInfo() {
7268
void BatteryInfo::UpdateAnim() {
7369
batteryPercent = batteryController.PercentRemaining();
7470

75-
if (batteryPercent >= 0) {
76-
if (batteryController.IsCharging() and batteryPercent < 100) {
77-
animation += 1;
78-
if (animation >= 100) {
79-
animation = 0;
80-
}
81-
82-
} else {
83-
if (animation > batteryPercent) {
84-
animation--;
85-
}
86-
if (animation < batteryPercent) {
87-
animation++;
88-
}
71+
if (batteryController.IsCharging() and batteryPercent < 100) {
72+
animation += 1;
73+
if (animation >= 100) {
74+
animation = 0;
8975
}
9076

91-
lv_bar_set_value(charging_bar, animation, LV_ANIM_OFF);
77+
} else {
78+
if (animation > batteryPercent) {
79+
animation--;
80+
}
81+
if (animation < batteryPercent) {
82+
animation++;
83+
}
9284
}
85+
86+
lv_bar_set_value(charging_bar, animation, LV_ANIM_OFF);
9387
}
9488

9589
void BatteryInfo::UpdateScreen() {
@@ -99,28 +93,22 @@ void BatteryInfo::UpdateScreen() {
9993
batteryPercent = batteryController.PercentRemaining();
10094
batteryVoltage = batteryController.Voltage();
10195

102-
if (batteryPercent >= 0) {
103-
if (batteryController.IsCharging() and batteryPercent < 100) {
104-
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED);
105-
lv_label_set_text_static(status, "Charging");
106-
} else if (batteryPercent == 100) {
107-
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_BLUE);
108-
lv_label_set_text_static(status, "Fully charged");
109-
} else if (batteryPercent < 10) {
110-
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
111-
lv_label_set_text_static(status, "Battery low");
112-
} else {
113-
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_GREEN);
114-
lv_label_set_text_static(status, "Discharging");
115-
}
116-
117-
lv_label_set_text_fmt(percent, "%02i%%", batteryPercent);
118-
96+
if (batteryController.IsCharging() and batteryPercent < 100) {
97+
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED);
98+
lv_label_set_text_static(status, "Charging");
99+
} else if (batteryPercent == 100) {
100+
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_BLUE);
101+
lv_label_set_text_static(status, "Fully charged");
102+
} else if (batteryPercent < 10) {
103+
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_YELLOW);
104+
lv_label_set_text_static(status, "Battery low");
119105
} else {
120-
lv_label_set_text_static(status, "Reading Battery status");
121-
lv_label_set_text(percent, "--%");
106+
lv_obj_set_style_local_bg_color(charging_bar, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_GREEN);
107+
lv_label_set_text_static(status, "Discharging");
122108
}
123109

110+
lv_label_set_text_fmt(percent, "%02i%%", batteryPercent);
111+
124112
lv_obj_align(status, charging_bar, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
125113
lv_label_set_text_fmt(voltage, "%1i.%02i volts", batteryVoltage / 1000, batteryVoltage % 1000 / 10);
126114
}

src/displayapp/screens/BatteryInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace Pinetime {
3636
lv_task_t* taskAnim;
3737

3838
int8_t animation = 0;
39-
int8_t batteryPercent = -1;
39+
uint8_t batteryPercent = 0;
4040
uint16_t batteryVoltage = 0;
4141
};
4242
}

0 commit comments

Comments
 (0)