From d376a856b7323217e40349a16750805a18771f5d Mon Sep 17 00:00:00 2001 From: Patric Gruber Date: Sat, 26 Aug 2023 20:35:39 +0200 Subject: [PATCH] use enum instead of uint32_t for heartrater interval setting --- src/components/settings/Settings.h | 18 ++++++-- .../screens/settings/SettingHeartRate.cpp | 3 +- .../screens/settings/SettingHeartRate.h | 18 ++++---- src/heartratetask/HeartRateTask.cpp | 43 ++++++++++++++++--- src/heartratetask/HeartRateTask.h | 4 ++ 5 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 53ab75a2..94fde265 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -50,6 +50,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; @@ -283,11 +294,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; } @@ -320,8 +331,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; diff --git a/src/displayapp/screens/settings/SettingHeartRate.cpp b/src/displayapp/screens/settings/SettingHeartRate.cpp index 99f4a463..d3da19b2 100644 --- a/src/displayapp/screens/settings/SettingHeartRate.cpp +++ b/src/displayapp/screens/settings/SettingHeartRate.cpp @@ -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); @@ -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); diff --git a/src/displayapp/screens/settings/SettingHeartRate.h b/src/displayapp/screens/settings/SettingHeartRate.h index 28605bac..40eff744 100644 --- a/src/displayapp/screens/settings/SettingHeartRate.h +++ b/src/displayapp/screens/settings/SettingHeartRate.h @@ -15,7 +15,7 @@ namespace Pinetime { namespace Screens { struct Option { - const uint32_t interval; + const Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval interval; const char* name; }; @@ -31,14 +31,14 @@ namespace Pinetime { Pinetime::Controllers::Settings& settingsController; static constexpr std::array 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()]; diff --git a/src/heartratetask/HeartRateTask.cpp b/src/heartratetask/HeartRateTask.cpp index 531cdf5e..56439e93 100644 --- a/src/heartratetask/HeartRateTask.cpp +++ b/src/heartratetask/HeartRateTask.cpp @@ -107,12 +107,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(); } @@ -140,13 +140,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(); } @@ -164,4 +168,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; } \ No newline at end of file diff --git a/src/heartratetask/HeartRateTask.h b/src/heartratetask/HeartRateTask.h index 80b6abc8..0361c2ec 100644 --- a/src/heartratetask/HeartRateTask.h +++ b/src/heartratetask/HeartRateTask.h @@ -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;