refactor heartrate task (switch cases, comments with explanation)

This commit is contained in:
Patric Gruber 2024-07-11 14:56:06 +02:00
parent 6a0276f164
commit 616926345e
2 changed files with 122 additions and 46 deletions

View file

@ -26,7 +26,7 @@ void HeartRateTask::Process(void* instance) {
}
void HeartRateTask::Work() {
int lastBpm = 0;
lastBpm = 0;
while (true) {
TickType_t delay = CurrentTaskDelay();
@ -35,41 +35,16 @@ void HeartRateTask::Work() {
if (xQueueReceive(messageQueue, &msg, delay) == pdTRUE) {
switch (msg) {
case Messages::GoToSleep:
if (state == States::Running) {
state = States::Idle;
} else if (state == States::Measuring) {
state = States::BackgroundWaiting;
StartWaiting();
}
HandleGoToSleep();
break;
case Messages::WakeUp:
if (state == States::Idle) {
state = States::Running;
} else if (state == States::BackgroundMeasuring) {
state = States::Measuring;
} else if (state == States::BackgroundWaiting) {
state = States::Measuring;
StartMeasurement();
}
HandleWakeUp();
break;
case Messages::StartMeasurement:
if (state == States::Measuring || state == States::BackgroundMeasuring) {
break;
}
state = States::Measuring;
lastBpm = 0;
StartMeasurement();
HandleStartMeasurement();
break;
case Messages::StopMeasurement:
if (state == States::Running || state == States::Idle) {
break;
}
if (state == States::Measuring) {
state = States::Running;
} else if (state == States::BackgroundMeasuring) {
state = States::Idle;
}
StopMeasurement();
HandleStopMeasurement();
break;
}
}
@ -106,6 +81,76 @@ void HeartRateTask::StartWaiting() {
backgroundWaitingStart = xTaskGetTickCount();
}
void HeartRateTask::HandleGoToSleep() {
switch (state) {
case States::Running:
state = States::Idle;
break;
case States::Measuring:
state = States::BackgroundWaiting;
StartWaiting();
break;
case States::Idle:
case States::BackgroundWaiting:
case States::BackgroundMeasuring:
// shouldn't happen -> ignore
break;
}
}
void HeartRateTask::HandleWakeUp() {
switch (state) {
case States::Idle:
state = States::Running;
break;
case States::BackgroundMeasuring:
state = States::Measuring;
break;
case States::BackgroundWaiting:
state = States::Measuring;
StartMeasurement();
break;
case States::Running:
case States::Measuring:
// shouldn't happen -> ignore
break;
}
}
void HeartRateTask::HandleStartMeasurement() {
switch (state) {
case States::Idle:
case States::Running:
state = States::Measuring;
lastBpm = 0;
StartMeasurement();
break;
case States::Measuring:
case States::BackgroundMeasuring:
case States::BackgroundWaiting:
// shouldn't happen -> ignore
break;
}
}
void HeartRateTask::HandleStopMeasurement() {
switch (state) {
case States::Measuring:
state = States::Running;
StopMeasurement();
break;
case States::BackgroundMeasuring:
case States::BackgroundWaiting:
state = States::Idle;
StopMeasurement();
break;
case States::Running:
case States::Idle:
// shouldn't happen -> ignore
break;
}
}
void HeartRateTask::HandleBackgroundWaiting() {
if (!IsBackgroundMeasurementActivated()) {
return;
@ -150,12 +195,7 @@ void HeartRateTask::HandleSensorData(int* lastBpm) {
}
TickType_t ticksSinceMeasurementStart = xTaskGetTickCount() - measurementStart;
if (bpm == 0 && state == States::BackgroundMeasuring && !IsContinuosModeActivated() &&
ticksSinceMeasurementStart >= DURATION_UNTIL_BACKGROUND_MEASURMENT_IS_STOPPED) {
state = States::BackgroundWaiting;
StartWaiting();
}
if (bpm == 0 && state == States::BackgroundMeasuring &&
xTaskGetTickCount() - measurementStart >= DURATION_UNTIL_BACKGROUND_MEASURMENT_IS_STOPPED) {
ticksSinceMeasurementStart >= DURATION_UNTIL_BACKGROUND_MEASUREMENT_IS_STOPPED) {
state = States::BackgroundWaiting;
StartWaiting();
}
@ -176,22 +216,31 @@ TickType_t HeartRateTask::CurrentTaskDelay() {
}
TickType_t HeartRateTask::GetHeartRateBackgroundMeasurementIntervalInTicks() {
int ms;
switch (settings.GetHeartRateBackgroundMeasurementInterval()) {
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenSeconds:
return pdMS_TO_TICKS(10 * 1000);
ms = 10 * 1000;
break;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtySeconds:
return pdMS_TO_TICKS(30 * 1000);
ms = 30 * 1000;
break;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::OneMinute:
return pdMS_TO_TICKS(60 * 1000);
ms = 60 * 1000;
break;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::FiveMinutes:
return pdMS_TO_TICKS(5 * 60 * 1000);
ms = 5 * 60 * 1000;
break;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenMinutes:
return pdMS_TO_TICKS(10 * 60 * 1000);
ms = 10 * 60 * 1000;
break;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtyMinutes:
return pdMS_TO_TICKS(30 * 60 * 1000);
ms = 30 * 60 * 1000;
break;
default:
return 0;
ms = 0;
break;
}
return pdMS_TO_TICKS(ms);
}
bool HeartRateTask::IsContinuosModeActivated() {

View file

@ -5,7 +5,7 @@
#include <components/heartrate/Ppg.h>
#include "components/settings/Settings.h"
#define DURATION_UNTIL_BACKGROUND_MEASURMENT_IS_STOPPED pdMS_TO_TICKS(30 * 1000)
#define DURATION_UNTIL_BACKGROUND_MEASUREMENT_IS_STOPPED pdMS_TO_TICKS(30 * 1000)
namespace Pinetime {
namespace Drivers {
@ -19,8 +19,29 @@ namespace Pinetime {
namespace Applications {
class HeartRateTask {
public:
enum class Messages : uint8_t { GoToSleep, WakeUp, StartMeasurement, StopMeasurement };
enum class States { Idle, Running, Measuring, BackgroundWaiting, BackgroundMeasuring };
enum class Messages : uint8_t {
// Screen gets turned off
GoToSleep,
// Screen gets turned on
WakeUp,
// Start button pressed
StartMeasurement,
// Stop button pressed
StopMeasurement
};
enum class States {
// Screen turned off, heartrate not measured
Idle,
// Screen turned on, heartrate app open, heartrate not measured
Running,
// Screen turned on, heartrate app open, heartrate actively measured
Measuring,
// Screen turned off, heartrate task is waiting until the next measurement should be started
BackgroundWaiting,
// Screen turned off, heartrate actively measured
BackgroundMeasuring
};
explicit HeartRateTask(Drivers::Hrs3300& heartRateSensor,
Controllers::HeartRateController& controller,
@ -35,6 +56,11 @@ namespace Pinetime {
void StopMeasurement();
void StartWaiting();
void HandleGoToSleep();
void HandleWakeUp();
void HandleStartMeasurement();
void HandleStopMeasurement();
void HandleBackgroundWaiting();
void HandleSensorData(int* lastBpm);
TickType_t CurrentTaskDelay();
@ -52,6 +78,7 @@ namespace Pinetime {
Controllers::Ppg ppg;
TickType_t backgroundWaitingStart = 0;
TickType_t measurementStart = 0;
int lastBpm = 0;
};
}