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

View file

@ -5,7 +5,7 @@
#include <components/heartrate/Ppg.h> #include <components/heartrate/Ppg.h>
#include "components/settings/Settings.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 Pinetime {
namespace Drivers { namespace Drivers {
@ -19,8 +19,29 @@ namespace Pinetime {
namespace Applications { namespace Applications {
class HeartRateTask { class HeartRateTask {
public: public:
enum class Messages : uint8_t { GoToSleep, WakeUp, StartMeasurement, StopMeasurement }; enum class Messages : uint8_t {
enum class States { Idle, Running, Measuring, BackgroundWaiting, BackgroundMeasuring }; // 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, explicit HeartRateTask(Drivers::Hrs3300& heartRateSensor,
Controllers::HeartRateController& controller, Controllers::HeartRateController& controller,
@ -35,6 +56,11 @@ namespace Pinetime {
void StopMeasurement(); void StopMeasurement();
void StartWaiting(); void StartWaiting();
void HandleGoToSleep();
void HandleWakeUp();
void HandleStartMeasurement();
void HandleStopMeasurement();
void HandleBackgroundWaiting(); void HandleBackgroundWaiting();
void HandleSensorData(int* lastBpm); void HandleSensorData(int* lastBpm);
TickType_t CurrentTaskDelay(); TickType_t CurrentTaskDelay();
@ -52,6 +78,7 @@ namespace Pinetime {
Controllers::Ppg ppg; Controllers::Ppg ppg;
TickType_t backgroundWaitingStart = 0; TickType_t backgroundWaitingStart = 0;
TickType_t measurementStart = 0; TickType_t measurementStart = 0;
int lastBpm = 0;
}; };
} }