Skip to content

Commit b5eabf6

Browse files
committed
Merge branch 'develop' into fix_adc
2 parents 7efe2b7 + a07b638 commit b5eabf6

24 files changed

Lines changed: 78 additions & 102 deletions

src/FreeRTOSConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
#define configENABLE_BACKWARD_COMPATIBILITY 1
7878

7979
/* Hook function related definitions. */
80-
#define configUSE_IDLE_HOOK 1
80+
#define configUSE_IDLE_HOOK 0
8181
#define configUSE_TICK_HOOK 0
8282
#define configCHECK_FOR_STACK_OVERFLOW 0
8383
#define configUSE_MALLOC_FAILED_HOOK 0

src/components/battery/BatteryController.cpp

Lines changed: 9 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

@@ -32,13 +29,13 @@ void Battery::Update() {
3229
nrfx_saadc_sample();
3330
}
3431

35-
void Battery::adcCallbackStatic(nrfx_saadc_evt_t const* event) {
32+
void Battery::AdcCallbackStatic(nrfx_saadc_evt_t const* event) {
3633
instance->SaadcEventHandler(event);
3734
}
3835

3936
void Battery::SaadcInit() {
4037
nrfx_saadc_config_t adcConfig = NRFX_SAADC_DEFAULT_CONFIG;
41-
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, adcCallbackStatic));
38+
APP_ERROR_CHECK(nrfx_saadc_init(&adcConfig, AdcCallbackStatic));
4239

4340
nrf_saadc_channel_config_t adcChannelConfig = {.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
4441
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
@@ -54,7 +51,6 @@ void Battery::SaadcInit() {
5451
}
5552

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

@@ -69,10 +65,13 @@ void Battery::SaadcEventHandler(nrfx_saadc_evt_t const* p_event) {
6965
// p_event->data.done.p_buffer[0] = (adc_voltage / reference_voltage) * 1024
7066
voltage = p_event->data.done.p_buffer[0] * 6000 / 1024;
7167

72-
percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min);
73-
74-
percentRemaining = std::max(percentRemaining, 0);
75-
percentRemaining = std::min(percentRemaining, 100);
68+
if (voltage > battery_max) {
69+
percentRemaining = 100;
70+
} else if (voltage < battery_min) {
71+
percentRemaining = 0;
72+
} else {
73+
percentRemaining = (voltage - battery_min) * 100 / (battery_max - battery_min);
74+
}
7675

7776
nrfx_saadc_uninit();
7877
isReading = false;

src/components/battery/BatteryController.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Pinetime {
1414
void Init();
1515
void Update();
1616

17-
int PercentRemaining() const {
17+
uint8_t PercentRemaining() const {
1818
return percentRemaining;
1919
}
2020

@@ -25,6 +25,7 @@ namespace Pinetime {
2525
bool IsCharging() const {
2626
return isCharging;
2727
}
28+
2829
bool IsPowerPresent() const {
2930
return isPowerPresent;
3031
}
@@ -37,15 +38,15 @@ namespace Pinetime {
3738
static constexpr uint32_t powerPresentPin = 19;
3839
static constexpr nrf_saadc_input_t batteryVoltageAdcInput = NRF_SAADC_INPUT_AIN7;
3940
uint16_t voltage = 0;
40-
int percentRemaining = -1;
41+
uint8_t percentRemaining = 0;
4142

4243
bool isCharging = false;
4344
bool isPowerPresent = false;
4445

4546
void SaadcInit();
4647

4748
void SaadcEventHandler(nrfx_saadc_evt_t const* p_event);
48-
static void adcCallbackStatic(nrfx_saadc_evt_t const* event);
49+
static void AdcCallbackStatic(nrfx_saadc_evt_t const* event);
4950

5051
bool isReading = false;
5152
};

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/DisplayApp.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,29 @@ uint32_t count = 0;
114114
bool toggle = true;
115115
void DisplayApp::Refresh() {
116116
TickType_t queueTimeout;
117+
TickType_t delta;
117118
switch (state) {
118119
case States::Idle:
119120
IdleState();
120121
queueTimeout = portMAX_DELAY;
121122
break;
122123
case States::Running:
123124
RunningState();
124-
queueTimeout = 20;
125+
delta = xTaskGetTickCount() - lastWakeTime;
126+
if (delta > 20) {
127+
delta = 20;
128+
}
129+
queueTimeout = 20 - delta;
125130
break;
126131
default:
127132
queueTimeout = portMAX_DELAY;
128133
break;
129134
}
130135

131136
Messages msg;
132-
if (xQueueReceive(msgQueue, &msg, queueTimeout)) {
137+
bool messageReceived = xQueueReceive(msgQueue, &msg, queueTimeout);
138+
lastWakeTime = xTaskGetTickCount();
139+
if (messageReceived) {
133140
switch (msg) {
134141
case Messages::GoToSleep:
135142
brightnessController.Backup();

src/displayapp/DisplayApp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ namespace Pinetime {
114114

115115
Apps nextApp = Apps::None;
116116
DisplayApp::FullRefreshDirections nextDirection;
117+
TickType_t lastWakeTime;
117118
};
118119
}
119120
}

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)

0 commit comments

Comments
 (0)