From 330d5e68f3826590ad5dfcfc5ae76b646d3eadc8 Mon Sep 17 00:00:00 2001 From: mark9064 <30447455+mark9064@users.noreply.github.com> Date: Fri, 12 May 2023 01:57:27 +0100 Subject: [PATCH] Heartrate measurements in background #1718 commit d2716701299f9868b22e89643669f99e4dae0af9 Author: Patric Gruber Date: Thu May 11 23:49:39 2023 +0200 remove version change in CMakeLists.txt commit faec69e85210344d5e2498c9fa34f2533ae48891 Author: Patric Gruber Date: Thu May 11 23:47:31 2023 +0200 rebase on main commit c5d2e421b4dcefb2b264327fc89c6fcbc840102d Author: Patric Gruber Date: Mon Apr 3 21:29:17 2023 +0200 remove background start timestamp reset on sleep commit 718064616c37fc925e1c033bb2b8fe928223db1c Author: Patric Gruber Date: Fri Mar 31 12:38:37 2023 +0200 remove version change in CMakeLists.txt commit 9186cd2750eef84450ea4a40a0c41f4397d54fa4 Author: Patric Gruber Date: Fri Mar 31 10:25:36 2023 +0200 increase task delay when waiting in the background to 10s commit a3a30a2294833de5a36b4e4bf7a21d5011fa1fc8 Author: Patric Gruber Date: Fri Mar 31 10:00:56 2023 +0200 add heart rate measurments in the background --- src/heartratetask/HeartRateTask.cpp | 125 +++++++++++++++++----------- src/heartratetask/HeartRateTask.h | 10 ++- 2 files changed, 86 insertions(+), 49 deletions(-) diff --git a/src/heartratetask/HeartRateTask.cpp b/src/heartratetask/HeartRateTask.cpp index 414cdf2c47..11d0018d5c 100644 --- a/src/heartratetask/HeartRateTask.cpp +++ b/src/heartratetask/HeartRateTask.cpp @@ -25,77 +25,57 @@ void HeartRateTask::Process(void* instance) { void HeartRateTask::Work() { int lastBpm = 0; + while (true) { + auto delay = CurrentTaskDelay(); Messages msg; - uint32_t delay; - if (state == States::Running) { - if (measurementStarted) { - delay = ppg.deltaTms; - } else { - delay = 100; - } - } else { - delay = portMAX_DELAY; - } - if (xQueueReceive(messageQueue, &msg, delay)) { + if (xQueueReceive(messageQueue, &msg, delay) == pdTRUE) { switch (msg) { case Messages::GoToSleep: - StopMeasurement(); - state = States::Idle; + if (state == States::Running) { + state = States::Idle; + } else if (state == States::Measuring) { + state = States::BackgroundWaiting; + StopMeasurement(); + } break; case Messages::WakeUp: - state = States::Running; - if (measurementStarted) { - lastBpm = 0; + if (state == States::Idle) { + state = States::Running; + } else if (state == States::BackgroundMeasuring) { + state = States::Measuring; + } else if (state == States::BackgroundWaiting) { + state = States::Measuring; StartMeasurement(); } break; case Messages::StartMeasurement: - if (measurementStarted) { + if (state == States::Measuring || state == States::BackgroundMeasuring) { break; } + state = States::Measuring; lastBpm = 0; StartMeasurement(); - measurementStarted = true; break; case Messages::StopMeasurement: - if (!measurementStarted) { + if (state == States::Running || state == States::Idle) { break; } + if (state == States::Measuring) { + state = States::Running; + } else if (state == States::BackgroundMeasuring) { + state = States::Idle; + } StopMeasurement(); - measurementStarted = false; break; } } - if (measurementStarted) { - int8_t ambient = ppg.Preprocess(heartRateSensor.ReadHrs(), heartRateSensor.ReadAls()); - int bpm = ppg.HeartRate(); - - // If ambient light detected or a reset requested (bpm < 0) - if (ambient > 0) { - // Reset all DAQ buffers - ppg.Reset(true); - // Force state to NotEnoughData (below) - lastBpm = 0; - bpm = 0; - } else if (bpm < 0) { - // Reset all DAQ buffers except HRS buffer - ppg.Reset(false); - // Set HR to zero and update - bpm = 0; - controller.Update(Controllers::HeartRateController::States::Running, bpm); - } - - if (lastBpm == 0 && bpm == 0) { - controller.Update(Controllers::HeartRateController::States::NotEnoughData, bpm); - } - - if (bpm != 0) { - lastBpm = bpm; - controller.Update(Controllers::HeartRateController::States::Running, lastBpm); - } + if (state == States::BackgroundWaiting) { + HandleBackgroundWaiting(); + } else if (state == States::BackgroundMeasuring || state == States::Measuring) { + HandleSensorData(&lastBpm); } } } @@ -120,3 +100,54 @@ void HeartRateTask::StopMeasurement() { ppg.Reset(true); vTaskDelay(100); } + +void HeartRateTask::HandleBackgroundWaiting() { + if (xTaskGetTickCount() - backgroundMeasurementWaitingStart >= DURATION_BETWEEN_BACKGROUND_MEASUREMENTS) { + state = States::BackgroundMeasuring; + StartMeasurement(); + } +} + +void HeartRateTask::HandleSensorData(int* lastBpm) { + int8_t ambient = ppg.Preprocess(heartRateSensor.ReadHrs(), heartRateSensor.ReadAls()); + int bpm = ppg.HeartRate(); + + // If ambient light detected or a reset requested (bpm < 0) + if (ambient > 0) { + // Reset all DAQ buffers + ppg.Reset(true); + } else if (bpm < 0) { + // Reset all DAQ buffers except HRS buffer + ppg.Reset(false); + // Set HR to zero and update + bpm = 0; + } + + if (*lastBpm == 0 && bpm == 0) { + controller.Update(Controllers::HeartRateController::States::NotEnoughData, bpm); + } + + if (bpm != 0) { + *lastBpm = bpm; + controller.Update(Controllers::HeartRateController::States::Running, bpm); + if (state == States::BackgroundMeasuring) { + StopMeasurement(); + state = States::BackgroundWaiting; + backgroundMeasurementWaitingStart = xTaskGetTickCount(); + } + } +} + +int HeartRateTask::CurrentTaskDelay() { + switch (state) { + case States::Measuring: + case States::BackgroundMeasuring: + return ppg.deltaTms; + case States::Running: + return 100; + case States::BackgroundWaiting: + return 10000; + default: + return portMAX_DELAY; + } +} \ No newline at end of file diff --git a/src/heartratetask/HeartRateTask.h b/src/heartratetask/HeartRateTask.h index 5bbfb9fb3e..d27f68e79c 100644 --- a/src/heartratetask/HeartRateTask.h +++ b/src/heartratetask/HeartRateTask.h @@ -4,6 +4,8 @@ #include #include +#define DURATION_BETWEEN_BACKGROUND_MEASUREMENTS 5 * 60 * 1000 // 5 Minutes assuming 1 Hz + namespace Pinetime { namespace Drivers { class Hrs3300; @@ -17,7 +19,7 @@ namespace Pinetime { class HeartRateTask { public: enum class Messages : uint8_t { GoToSleep, WakeUp, StartMeasurement, StopMeasurement }; - enum class States { Idle, Running }; + enum class States { Idle, Running, Measuring, BackgroundWaiting, BackgroundMeasuring }; explicit HeartRateTask(Drivers::Hrs3300& heartRateSensor, Controllers::HeartRateController& controller); void Start(); @@ -29,13 +31,17 @@ namespace Pinetime { void StartMeasurement(); void StopMeasurement(); + void HandleBackgroundWaiting(); + void HandleSensorData(int* lastBpm); + int CurrentTaskDelay(); + TaskHandle_t taskHandle; QueueHandle_t messageQueue; States state = States::Running; Drivers::Hrs3300& heartRateSensor; Controllers::HeartRateController& controller; Controllers::Ppg ppg; - bool measurementStarted = false; + TickType_t backgroundMeasurementWaitingStart = 0; }; }