Skip to content

Commit

Permalink
use enum instead of uint32_t for heartrater interval setting
Browse files Browse the repository at this point in the history
  • Loading branch information
patricgruber committed Nov 12, 2023
1 parent 4ee6377 commit 6b8effb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 19 deletions.
18 changes: 14 additions & 4 deletions src/components/settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ namespace Pinetime {
int colorIndex = 0;
};

enum class HeartRateBackgroundMeasurementInterval : uint8_t {
Off,
Continuous,
TenSeconds,
ThirtySeconds,
OneMinute,
FiveMinutes,
TenMinutes,
ThirtyMinutes,
};

Settings(Pinetime::Controllers::FS& fs);

Settings(const Settings&) = delete;
Expand Down Expand Up @@ -271,11 +282,11 @@ namespace Pinetime {
return bleRadioEnabled;
};

uint32_t GetHeartRateBackgroundMeasurementInterval() const {
HeartRateBackgroundMeasurementInterval GetHeartRateBackgroundMeasurementInterval() const {
return settings.heartRateBackgroundMeasurementInterval;
}

void SetHeartRateBackgroundMeasurementInterval(uint32_t newHeartRateBackgroundMeasurementInterval) {
void SetHeartRateBackgroundMeasurementInterval(HeartRateBackgroundMeasurementInterval newHeartRateBackgroundMeasurementInterval) {
if (newHeartRateBackgroundMeasurementInterval != settings.heartRateBackgroundMeasurementInterval) {
settingsChanged = true;
}
Expand Down Expand Up @@ -307,8 +318,7 @@ namespace Pinetime {

Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium;

// The interval for measuring the heart rate when the screen is off (in seconds)
uint32_t heartRateBackgroundMeasurementInterval = 0;
HeartRateBackgroundMeasurementInterval heartRateBackgroundMeasurementInterval = HeartRateBackgroundMeasurementInterval::Off;
};

SettingsData settings;
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/settings/SettingHeartRate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SettingHeartRate::SettingHeartRate(Pinetime::Applications::DisplayApp* app, Pine
lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr);

lv_obj_set_style_local_bg_opa(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
lv_obj_set_style_local_pad_all(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
lv_obj_set_style_local_pad_all(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
lv_obj_set_style_local_pad_inner(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
lv_obj_set_style_local_border_width(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);

Expand All @@ -35,6 +35,7 @@ SettingHeartRate::SettingHeartRate(Pinetime::Applications::DisplayApp* app, Pine

lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_static(title, "Backg. Interval");
lv_label_set_text(title, "Backg. Interval");
lv_label_set_align(title, LV_LABEL_ALIGN_CENTER);
lv_obj_align(title, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 10, 15);

Expand Down
18 changes: 9 additions & 9 deletions src/displayapp/screens/settings/SettingHeartRate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Pinetime {
namespace Screens {

struct Option {
const uint32_t interval;
const Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval interval;
const char* name;
};

Expand All @@ -31,14 +31,14 @@ namespace Pinetime {
Pinetime::Controllers::Settings& settingsController;

static constexpr std::array<Option, 8> options = {{
{0, "Off"},
{10, "10s"},
{30, "30s"},
{60, " 1m"},
{5 * 60, " 5m"},
{10 * 60, "10m"},
{30 * 60, "30m"},
{60 * 60, " 1h"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Off, " Off"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Continuous, "Cont"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenSeconds, " 10s"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtySeconds, " 30s"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::OneMinute, " 1m"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::FiveMinutes, " 5m"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenMinutes, " 10m"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtyMinutes, " 30m"},
}};

lv_obj_t* cbOption[options.size()];
Expand Down
43 changes: 38 additions & 5 deletions src/heartratetask/HeartRateTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ void HeartRateTask::StartWaiting() {
}

void HeartRateTask::HandleBackgroundWaiting() {
if (settings.GetHeartRateBackgroundMeasurementInterval() == 0) {
// stay waiting
if (!IsBackgroundMeasurementActivated()) {
return;
}

if (xTaskGetTickCount() - backgroundWaitingStart >= settings.GetHeartRateBackgroundMeasurementInterval() * 1000) {
TickType_t ticksSinceWaitingStart = xTaskGetTickCount() - backgroundWaitingStart;
if (ticksSinceWaitingStart >= GetHeartRateBackgroundMeasurementIntervalInTicks()) {
state = States::BackgroundMeasuring;
StartMeasurement();
}
Expand Down Expand Up @@ -143,13 +143,17 @@ void HeartRateTask::HandleSensorData(int* lastBpm) {
if (bpm != 0) {
*lastBpm = bpm;
controller.Update(Controllers::HeartRateController::States::Running, bpm);
if (state == States::Measuring || IsContinuosModeActivated()) {
return;
}
if (state == States::BackgroundMeasuring) {
state = States::BackgroundWaiting;
StartWaiting();
}
}
if (bpm == 0 && state == States::BackgroundMeasuring &&
xTaskGetTickCount() - measurementStart >= DURATION_UNTIL_BACKGROUND_MEASURMENT_IS_STOPPED) {
TickType_t ticksSinceMeasurementStart = xTaskGetTickCount() - measurementStart;
if (bpm == 0 && state == States::BackgroundMeasuring && !IsContinuosModeActivated() &&
ticksSinceMeasurementStart >= DURATION_UNTIL_BACKGROUND_MEASURMENT_IS_STOPPED) {
state = States::BackgroundWaiting;
StartWaiting();
}
Expand All @@ -167,4 +171,33 @@ int HeartRateTask::CurrentTaskDelay() {
default:
return portMAX_DELAY;
}
}

uint32_t HeartRateTask::GetHeartRateBackgroundMeasurementIntervalInTicks() {
switch (settings.GetHeartRateBackgroundMeasurementInterval()) {
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenSeconds:
return 10 * 1000;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtySeconds:
return 30 * 1000;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::OneMinute:
return 60 * 1000;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::FiveMinutes:
return 5 * 60 * 1000;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenMinutes:
return 10 * 60 * 1000;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtyMinutes:
return 30 * 60 * 1000;
default:
return 0;
}
}

bool HeartRateTask::IsContinuosModeActivated() {
return settings.GetHeartRateBackgroundMeasurementInterval() ==
Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Continuous;
}

bool HeartRateTask::IsBackgroundMeasurementActivated() {
return settings.GetHeartRateBackgroundMeasurementInterval() !=
Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Off;
}
4 changes: 4 additions & 0 deletions src/heartratetask/HeartRateTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ namespace Pinetime {
void HandleSensorData(int* lastBpm);
int CurrentTaskDelay();

uint32_t GetHeartRateBackgroundMeasurementIntervalInTicks();
bool IsContinuosModeActivated();
bool IsBackgroundMeasurementActivated();

TaskHandle_t taskHandle;
QueueHandle_t messageQueue;
States state = States::Running;
Expand Down

0 comments on commit 6b8effb

Please sign in to comment.