From 462ea11bd43e87fca39ec76bd90d63d53b469032 Mon Sep 17 00:00:00 2001 From: Patric Gruber Date: Sun, 22 Sep 2024 20:55:45 +0200 Subject: [PATCH] use interval as interval, instead of wait time --- src/components/settings/Settings.h | 2 +- .../screens/settings/SettingHeartRate.h | 2 +- src/heartratetask/HeartRateTask.cpp | 59 ++++++++++++------- src/heartratetask/HeartRateTask.h | 6 +- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 6f7f7369..c86415cd 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -53,7 +53,7 @@ namespace Pinetime { enum class HeartRateBackgroundMeasurementInterval : uint8_t { Off, Continuous, - TenSeconds, + FifteenSeconds, ThirtySeconds, OneMinute, FiveMinutes, diff --git a/src/displayapp/screens/settings/SettingHeartRate.h b/src/displayapp/screens/settings/SettingHeartRate.h index 3cb08907..88b47710 100644 --- a/src/displayapp/screens/settings/SettingHeartRate.h +++ b/src/displayapp/screens/settings/SettingHeartRate.h @@ -32,7 +32,7 @@ namespace Pinetime { static constexpr std::array options = {{ {Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Off, " Off"}, {Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Continuous, "Cont"}, - {Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenSeconds, " 10s"}, + {Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::FifteenSeconds, " 15s"}, {Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtySeconds, " 30s"}, {Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::OneMinute, " 1m"}, {Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::FiveMinutes, " 5m"}, diff --git a/src/heartratetask/HeartRateTask.cpp b/src/heartratetask/HeartRateTask.cpp index 0c082a5d..e82cf0ff 100644 --- a/src/heartratetask/HeartRateTask.cpp +++ b/src/heartratetask/HeartRateTask.cpp @@ -97,11 +97,6 @@ void HeartRateTask::StopMeasurement() { vTaskDelay(100); } -void HeartRateTask::StartWaiting() { - StopMeasurement(); - backgroundWaitingStart = xTaskGetTickCount(); -} - void HeartRateTask::HandleGoToSleep() { switch (state) { case States::ScreenOnAndStopped: @@ -176,8 +171,7 @@ void HeartRateTask::HandleBackgroundWaiting() { return; } - TickType_t ticksSinceWaitingStart = xTaskGetTickCount() - backgroundWaitingStart; - if (ticksSinceWaitingStart >= GetHeartRateBackgroundMeasurementIntervalInTicks()) { + if (ShouldStartBackgroundMeasuring()) { state = States::ScreenOffAndMeasuring; StartMeasurement(); } @@ -198,34 +192,45 @@ void HeartRateTask::HandleSensorData(int* lastBpm) { bpm = 0; } - if (*lastBpm == 0 && bpm == 0) { + bool notEnoughData = *lastBpm == 0 && bpm == 0; + if (notEnoughData) { controller.Update(Controllers::HeartRateController::States::NotEnoughData, bpm); } if (bpm != 0) { *lastBpm = bpm; controller.Update(Controllers::HeartRateController::States::Running, bpm); - if (state == States::ScreenOnAndMeasuring || IsContinuousModeActivated()) { - return; - } - if (state == States::ScreenOffAndMeasuring) { - state = States::ScreenOffAndWaiting; - StartWaiting(); - } } - TickType_t ticksSinceMeasurementStart = xTaskGetTickCount() - measurementStart; - if (bpm == 0 && state == States::ScreenOffAndMeasuring && !IsContinuousModeActivated() && - ticksSinceMeasurementStart >= DURATION_UNTIL_BACKGROUND_MEASUREMENT_IS_STOPPED) { + + if (state == States::ScreenOnAndMeasuring || IsContinuousModeActivated()) { + return; + } + + // state == States::ScreenOffAndMeasuring + // (because state != ScreenOnAndMeasuring and the only state that enables measuring is ScreenOffAndMeasuring) + // !IsContinuousModeActivated() + + if (ShouldStartBackgroundMeasuring()) { + // This doesn't change the state but resets the measurment timer, which basically starts the next measurment without resetting the sensor. + // This is basically a fall back to continuous mode, when measurments take too long. + measurementStart = xTaskGetTickCount(); + return; + } + + bool noDataWithinTimeLimit = bpm == 0 && ShoudStopTryingToGetData(); + bool dataWithinTimeLimit = bpm != 0; + if (dataWithinTimeLimit || noDataWithinTimeLimit) { state = States::ScreenOffAndWaiting; - StartWaiting(); + StopMeasurement(); } + } TickType_t HeartRateTask::GetHeartRateBackgroundMeasurementIntervalInTicks() { int ms; switch (settings.GetHeartRateBackgroundMeasurementInterval()) { - case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenSeconds: - ms = 10 * 1000; + case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::FifteenSeconds: + ms = 15 * 1000; break; case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtySeconds: ms = 30 * 1000; @@ -258,3 +263,15 @@ bool HeartRateTask::IsBackgroundMeasurementActivated() { return settings.GetHeartRateBackgroundMeasurementInterval() != Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Off; } + +TickType_t HeartRateTask::GetTicksSinceLastMeasurementStarted() { + return xTaskGetTickCount() - measurementStart; +} + +bool HeartRateTask::ShoudStopTryingToGetData() { + return GetTicksSinceLastMeasurementStarted() >= DURATION_UNTIL_BACKGROUND_MEASUREMENT_IS_STOPPED; +} + +bool HeartRateTask::ShouldStartBackgroundMeasuring() { + return GetTicksSinceLastMeasurementStarted() >= GetHeartRateBackgroundMeasurementIntervalInTicks(); +} \ No newline at end of file diff --git a/src/heartratetask/HeartRateTask.h b/src/heartratetask/HeartRateTask.h index f7d7e38b..53d4d5bc 100644 --- a/src/heartratetask/HeartRateTask.h +++ b/src/heartratetask/HeartRateTask.h @@ -45,7 +45,6 @@ namespace Pinetime { static void Process(void* instance); void StartMeasurement(); void StopMeasurement(); - void StartWaiting(); void HandleGoToSleep(); void HandleWakeUp(); @@ -59,6 +58,10 @@ namespace Pinetime { bool IsContinuousModeActivated(); bool IsBackgroundMeasurementActivated(); + TickType_t GetTicksSinceLastMeasurementStarted(); + bool ShoudStopTryingToGetData(); + bool ShouldStartBackgroundMeasuring(); + TaskHandle_t taskHandle; QueueHandle_t messageQueue; States state = States::ScreenOnAndStopped; @@ -66,7 +69,6 @@ namespace Pinetime { Controllers::HeartRateController& controller; Controllers::Settings& settings; Controllers::Ppg ppg; - TickType_t backgroundWaitingStart = 0; TickType_t measurementStart = 0; };