stop background after 30s of no data from the heart rate sensor

This commit is contained in:
Patric Gruber 2023-05-25 23:41:26 +02:00
parent a5db54af27
commit 7ae790bcdb
2 changed files with 18 additions and 6 deletions

View file

@ -37,7 +37,7 @@ void HeartRateTask::Work() {
state = States::Idle; state = States::Idle;
} else if (state == States::Measuring) { } else if (state == States::Measuring) {
state = States::BackgroundWaiting; state = States::BackgroundWaiting;
StopMeasurement(); StartWaiting();
} }
break; break;
case Messages::WakeUp: case Messages::WakeUp:
@ -90,6 +90,7 @@ void HeartRateTask::StartMeasurement() {
heartRateSensor.Enable(); heartRateSensor.Enable();
ppg.Reset(true); ppg.Reset(true);
vTaskDelay(100); vTaskDelay(100);
measurementStart = xTaskGetTickCount();
} }
void HeartRateTask::StopMeasurement() { void HeartRateTask::StopMeasurement() {
@ -98,8 +99,13 @@ void HeartRateTask::StopMeasurement() {
vTaskDelay(100); vTaskDelay(100);
} }
void HeartRateTask::StartWaiting() {
StopMeasurement();
backgroundWaitingStart = xTaskGetTickCount();
}
void HeartRateTask::HandleBackgroundWaiting() { void HeartRateTask::HandleBackgroundWaiting() {
if (xTaskGetTickCount() - backgroundMeasurementWaitingStart >= DURATION_BETWEEN_BACKGROUND_MEASUREMENTS) { if (xTaskGetTickCount() - backgroundWaitingStart >= DURATION_BETWEEN_BACKGROUND_MEASUREMENTS) {
state = States::BackgroundMeasuring; state = States::BackgroundMeasuring;
StartMeasurement(); StartMeasurement();
} }
@ -128,11 +134,14 @@ void HeartRateTask::HandleSensorData(int* lastBpm) {
*lastBpm = bpm; *lastBpm = bpm;
controller.Update(Controllers::HeartRateController::States::Running, bpm); controller.Update(Controllers::HeartRateController::States::Running, bpm);
if (state == States::BackgroundMeasuring) { if (state == States::BackgroundMeasuring) {
StopMeasurement();
state = States::BackgroundWaiting; 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() { int HeartRateTask::CurrentTaskDelay() {

View file

@ -4,7 +4,8 @@
#include <queue.h> #include <queue.h>
#include <components/heartrate/Ppg.h> #include <components/heartrate/Ppg.h>
#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 Pinetime {
namespace Drivers { namespace Drivers {
@ -30,6 +31,7 @@ namespace Pinetime {
static void Process(void* instance); static void Process(void* instance);
void StartMeasurement(); void StartMeasurement();
void StopMeasurement(); void StopMeasurement();
void StartWaiting();
void HandleBackgroundWaiting(); void HandleBackgroundWaiting();
void HandleSensorData(int* lastBpm); void HandleSensorData(int* lastBpm);
@ -41,7 +43,8 @@ namespace Pinetime {
Drivers::Hrs3300& heartRateSensor; Drivers::Hrs3300& heartRateSensor;
Controllers::HeartRateController& controller; Controllers::HeartRateController& controller;
Controllers::Ppg ppg; Controllers::Ppg ppg;
TickType_t backgroundMeasurementWaitingStart = 0; TickType_t backgroundWaitingStart = 0;
TickType_t measurementStart = 0;
}; };
} }