Skip to content

Commit 3d1ee6d

Browse files
committed
Use std::optional for heart rate
If no value is available, use null. Use this distinction to differentiate in the UI between missing data and zero value. Still sends value 0 over BLE. I'm unsure whether this should change or not
1 parent 9afc23c commit 3d1ee6d

File tree

12 files changed

+39
-23
lines changed

12 files changed

+39
-23
lines changed

src/components/ble/HeartRateService.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ void HeartRateService::Init() {
4848
int HeartRateService::OnHeartRateRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
4949
if (attributeHandle == heartRateMeasurementHandle) {
5050
NRF_LOG_INFO("HEARTRATE : handle = %d", heartRateMeasurementHandle);
51-
uint8_t buffer[2] = {0, heartRateController.HeartRate()}; // [0] = flags, [1] = hr value
51+
uint8_t buffer[2] = {0, heartRateController.HeartRate().value_or(0)}; // [0] = flags, [1] = hr value
5252

5353
int res = os_mbuf_append(context->om, buffer, 2);
5454
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
5555
}
5656
return 0;
5757
}
5858

59-
void HeartRateService::OnNewHeartRateValue(uint8_t heartRateValue) {
59+
void HeartRateService::OnNewHeartRateValue(std::optional<uint8_t> heartRateValue) {
6060
if (!heartRateMeasurementNotificationEnable)
6161
return;
6262

63-
uint8_t buffer[2] = {0, heartRateValue}; // [0] = flags, [1] = hr value
63+
uint8_t buffer[2] = {0, heartRateValue.value_or(0)}; // [0] = flags, [1] = hr value
6464
auto* om = ble_hs_mbuf_from_flat(buffer, 2);
6565

6666
uint16_t connectionHandle = nimble.connHandle();

src/components/ble/HeartRateService.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#undef max
66
#undef min
77
#include <atomic>
8+
#include <optional>
89

910
namespace Pinetime {
1011
namespace Controllers {
@@ -16,7 +17,7 @@ namespace Pinetime {
1617
HeartRateService(NimbleController& nimble, Controllers::HeartRateController& heartRateController);
1718
void Init();
1819
int OnHeartRateRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context);
19-
void OnNewHeartRateValue(uint8_t hearRateValue);
20+
void OnNewHeartRateValue(std::optional<uint8_t> hearRateValue);
2021

2122
void SubscribeNotification(uint16_t attributeHandle);
2223
void UnsubscribeNotification(uint16_t attributeHandle);

src/components/heartrate/HeartRateController.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include "components/heartrate/HeartRateController.h"
22
#include <heartratetask/HeartRateTask.h>
33
#include <systemtask/SystemTask.h>
4+
#include <optional>
45

56
using namespace Pinetime::Controllers;
67

7-
void HeartRateController::Update(HeartRateController::States newState, uint8_t heartRate) {
8+
void HeartRateController::Update(HeartRateController::States newState, std::optional<uint8_t> heartRate) {
89
this->state = newState;
910
if (this->heartRate != heartRate) {
1011
this->heartRate = heartRate;

src/components/heartrate/HeartRateController.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstdint>
44
#include <components/ble/HeartRateService.h>
5+
#include <optional>
56

67
namespace Pinetime {
78
namespace Applications {
@@ -20,15 +21,15 @@ namespace Pinetime {
2021
HeartRateController() = default;
2122
void Start();
2223
void Stop();
23-
void Update(States newState, uint8_t heartRate);
24+
void Update(States newState, std::optional<uint8_t> heartRate);
2425

2526
void SetHeartRateTask(Applications::HeartRateTask* task);
2627

2728
States State() const {
2829
return state;
2930
}
3031

31-
uint8_t HeartRate() const {
32+
std::optional<uint8_t> HeartRate() const {
3233
return heartRate;
3334
}
3435

@@ -37,8 +38,8 @@ namespace Pinetime {
3738
private:
3839
Applications::HeartRateTask* task = nullptr;
3940
States state = States::Stopped;
40-
uint8_t heartRate = 0;
41+
std::optional<uint8_t> heartRate = std::nullopt;
4142
Pinetime::Controllers::HeartRateService* service = nullptr;
4243
};
4344
}
44-
}
45+
}

src/displayapp/screens/HeartRate.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@ void HeartRate::Refresh() {
8484
lv_label_set_text_static(label_hr, "---");
8585
break;
8686
default:
87-
if (heartRateController.HeartRate() == 0) {
87+
auto value = heartRateController.HeartRate();
88+
if (!value) {
8889
lv_label_set_text_static(label_hr, "---");
8990
} else {
90-
lv_label_set_text_fmt(label_hr, "%03d", heartRateController.HeartRate());
91+
lv_label_set_text_fmt(label_hr, "%03d", value.value());
9192
}
9293
}
9394

src/displayapp/screens/WatchFaceCasioStyleG7710.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,12 @@ void WatchFaceCasioStyleG7710::Refresh() {
294294
if (heartbeat.IsUpdated() || heartbeatRunning.IsUpdated()) {
295295
if (heartbeatRunning.Get()) {
296296
lv_obj_set_style_local_text_color(heartbeatIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, color_text);
297-
lv_label_set_text_fmt(heartbeatValue, "%d", heartbeat.Get());
297+
auto hb = heartbeat.Get();
298+
if (hb.has_value()) {
299+
lv_label_set_text_fmt(heartbeatValue, "%d", hb.value());
300+
} else {
301+
lv_label_set_text_static(heartbeatValue, "-");
302+
}
298303
} else {
299304
lv_obj_set_style_local_text_color(heartbeatIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x1B1B1B));
300305
lv_label_set_text_static(heartbeatValue, "");

src/displayapp/screens/WatchFaceCasioStyleG7710.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace Pinetime {
4848
Utility::DirtyValue<bool> bleRadioEnabled {};
4949
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>> currentDateTime {};
5050
Utility::DirtyValue<uint32_t> stepCount {};
51-
Utility::DirtyValue<uint8_t> heartbeat {};
51+
Utility::DirtyValue<std::optional<uint8_t>> heartbeat {};
5252
Utility::DirtyValue<bool> heartbeatRunning {};
5353
Utility::DirtyValue<bool> notificationState {};
5454
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::days>> currentDate;

src/displayapp/screens/WatchFaceDigital.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ void WatchFaceDigital::Refresh() {
155155
if (heartbeat.IsUpdated() || heartbeatRunning.IsUpdated()) {
156156
if (heartbeatRunning.Get()) {
157157
lv_obj_set_style_local_text_color(heartbeatIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0xCE1B1B));
158-
lv_label_set_text_fmt(heartbeatValue, "%d", heartbeat.Get());
158+
auto hb = heartbeat.Get();
159+
if (hb.has_value()) {
160+
lv_label_set_text_fmt(heartbeatValue, "%d", hb.value());
161+
} else {
162+
lv_label_set_text_static(heartbeatValue, "-");
163+
}
159164
} else {
160165
lv_obj_set_style_local_text_color(heartbeatIcon, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x1B1B1B));
161166
lv_label_set_text_static(heartbeatValue, "");

src/displayapp/screens/WatchFaceDigital.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace Pinetime {
4747

4848
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>> currentDateTime {};
4949
Utility::DirtyValue<uint32_t> stepCount {};
50-
Utility::DirtyValue<uint8_t> heartbeat {};
50+
Utility::DirtyValue<std::optional<uint8_t>> heartbeat {};
5151
Utility::DirtyValue<bool> heartbeatRunning {};
5252
Utility::DirtyValue<bool> notificationState {};
5353
Utility::DirtyValue<std::optional<Pinetime::Controllers::SimpleWeatherService::CurrentWeather>> currentWeather {};

src/displayapp/screens/WatchFaceTerminal.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ void WatchFaceTerminal::Refresh() {
137137
heartbeat = heartRateController.HeartRate();
138138
heartbeatRunning = heartRateController.State() != Controllers::HeartRateController::States::Stopped;
139139
if (heartbeat.IsUpdated() || heartbeatRunning.IsUpdated()) {
140-
if (heartbeatRunning.Get()) {
141-
lv_label_set_text_fmt(heartbeatValue, "[L_HR]#ee3311 %d bpm#", heartbeat.Get());
140+
auto hb = heartbeat.Get();
141+
if (hb.has_value()) {
142+
lv_label_set_text_fmt(heartbeatValue, "[L_HR]#ee3311 %d bpm#", hb.value());
142143
} else {
143144
lv_label_set_text_static(heartbeatValue, "[L_HR]#ee3311 ---#");
144145
}

0 commit comments

Comments
 (0)