From 7ae790bcdb40c8f849f047f5138b8b091177471d Mon Sep 17 00:00:00 2001 From: Patric Gruber Date: Thu, 25 May 2023 23:41:26 +0200 Subject: [PATCH] stop background after 30s of no data from the heart rate sensor --- src/heartratetask/HeartRateTask.cpp | 17 +++++++++++++---- src/heartratetask/HeartRateTask.h | 7 +++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/heartratetask/HeartRateTask.cpp b/src/heartratetask/HeartRateTask.cpp index a010cd44..1184ba1e 100644 --- a/src/heartratetask/HeartRateTask.cpp +++ b/src/heartratetask/HeartRateTask.cpp @@ -37,7 +37,7 @@ void HeartRateTask::Work() { state = States::Idle; } else if (state == States::Measuring) { state = States::BackgroundWaiting; - StopMeasurement(); + StartWaiting(); } break; case Messages::WakeUp: @@ -90,6 +90,7 @@ void HeartRateTask::StartMeasurement() { heartRateSensor.Enable(); ppg.Reset(true); vTaskDelay(100); + measurementStart = xTaskGetTickCount(); } void HeartRateTask::StopMeasurement() { @@ -98,8 +99,13 @@ void HeartRateTask::StopMeasurement() { vTaskDelay(100); } +void HeartRateTask::StartWaiting() { + StopMeasurement(); + backgroundWaitingStart = xTaskGetTickCount(); +} + void HeartRateTask::HandleBackgroundWaiting() { - if (xTaskGetTickCount() - backgroundMeasurementWaitingStart >= DURATION_BETWEEN_BACKGROUND_MEASUREMENTS) { + if (xTaskGetTickCount() - backgroundWaitingStart >= DURATION_BETWEEN_BACKGROUND_MEASUREMENTS) { state = States::BackgroundMeasuring; StartMeasurement(); } @@ -128,11 +134,14 @@ void HeartRateTask::HandleSensorData(int* lastBpm) { *lastBpm = bpm; controller.Update(Controllers::HeartRateController::States::Running, bpm); if (state == States::BackgroundMeasuring) { - StopMeasurement(); state = States::BackgroundWaiting; - backgroundMeasurementWaitingStart = xTaskGetTickCount(); + StartWaiting(); } } + if (bpm == 0 && state == States::BackgroundMeasuring && xTaskGetTickCount() - measurementStart >= DURATION_UNTIL_BACKGROUND_MEASURMENT_IS_STOPPED) { + state = States::BackgroundWaiting; + StartWaiting(); + } } int HeartRateTask::CurrentTaskDelay() { diff --git a/src/heartratetask/HeartRateTask.h b/src/heartratetask/HeartRateTask.h index d27f68e7..620dcf24 100644 --- a/src/heartratetask/HeartRateTask.h +++ b/src/heartratetask/HeartRateTask.h @@ -4,7 +4,8 @@ #include #include -#define DURATION_BETWEEN_BACKGROUND_MEASUREMENTS 5 * 60 * 1000 // 5 Minutes assuming 1 Hz +#define DURATION_BETWEEN_BACKGROUND_MEASUREMENTS 5 * 60 * 1000 // 5 minutes assuming 1 Hz +#define DURATION_UNTIL_BACKGROUND_MEASURMENT_IS_STOPPED 30 * 1000 // 30 seconds assuming 1 Hz namespace Pinetime { namespace Drivers { @@ -30,6 +31,7 @@ namespace Pinetime { static void Process(void* instance); void StartMeasurement(); void StopMeasurement(); + void StartWaiting(); void HandleBackgroundWaiting(); void HandleSensorData(int* lastBpm); @@ -41,7 +43,8 @@ namespace Pinetime { Drivers::Hrs3300& heartRateSensor; Controllers::HeartRateController& controller; Controllers::Ppg ppg; - TickType_t backgroundMeasurementWaitingStart = 0; + TickType_t backgroundWaitingStart = 0; + TickType_t measurementStart = 0; }; }