use interval as interval, instead of wait time

This commit is contained in:
Patric Gruber 2024-09-22 20:55:45 +02:00
parent 7df39994ab
commit 462ea11bd4
4 changed files with 44 additions and 25 deletions

View file

@ -53,7 +53,7 @@ namespace Pinetime {
enum class HeartRateBackgroundMeasurementInterval : uint8_t {
Off,
Continuous,
TenSeconds,
FifteenSeconds,
ThirtySeconds,
OneMinute,
FiveMinutes,

View file

@ -32,7 +32,7 @@ namespace Pinetime {
static constexpr std::array<Option, 8> options = {{
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Off, " Off"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Continuous, "Cont"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenSeconds, " 10s"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::FifteenSeconds, " 15s"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtySeconds, " 30s"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::OneMinute, " 1m"},
{Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::FiveMinutes, " 5m"},

View file

@ -97,11 +97,6 @@ void HeartRateTask::StopMeasurement() {
vTaskDelay(100);
}
void HeartRateTask::StartWaiting() {
StopMeasurement();
backgroundWaitingStart = xTaskGetTickCount();
}
void HeartRateTask::HandleGoToSleep() {
switch (state) {
case States::ScreenOnAndStopped:
@ -176,8 +171,7 @@ void HeartRateTask::HandleBackgroundWaiting() {
return;
}
TickType_t ticksSinceWaitingStart = xTaskGetTickCount() - backgroundWaitingStart;
if (ticksSinceWaitingStart >= GetHeartRateBackgroundMeasurementIntervalInTicks()) {
if (ShouldStartBackgroundMeasuring()) {
state = States::ScreenOffAndMeasuring;
StartMeasurement();
}
@ -198,34 +192,45 @@ void HeartRateTask::HandleSensorData(int* lastBpm) {
bpm = 0;
}
if (*lastBpm == 0 && bpm == 0) {
bool notEnoughData = *lastBpm == 0 && bpm == 0;
if (notEnoughData) {
controller.Update(Controllers::HeartRateController::States::NotEnoughData, bpm);
}
if (bpm != 0) {
*lastBpm = bpm;
controller.Update(Controllers::HeartRateController::States::Running, bpm);
}
if (state == States::ScreenOnAndMeasuring || IsContinuousModeActivated()) {
return;
}
if (state == States::ScreenOffAndMeasuring) {
// state == States::ScreenOffAndMeasuring
// (because state != ScreenOnAndMeasuring and the only state that enables measuring is ScreenOffAndMeasuring)
// !IsContinuousModeActivated()
if (ShouldStartBackgroundMeasuring()) {
// This doesn't change the state but resets the measurment timer, which basically starts the next measurment without resetting the sensor.
// This is basically a fall back to continuous mode, when measurments take too long.
measurementStart = xTaskGetTickCount();
return;
}
bool noDataWithinTimeLimit = bpm == 0 && ShoudStopTryingToGetData();
bool dataWithinTimeLimit = bpm != 0;
if (dataWithinTimeLimit || noDataWithinTimeLimit) {
state = States::ScreenOffAndWaiting;
StartWaiting();
}
}
TickType_t ticksSinceMeasurementStart = xTaskGetTickCount() - measurementStart;
if (bpm == 0 && state == States::ScreenOffAndMeasuring && !IsContinuousModeActivated() &&
ticksSinceMeasurementStart >= DURATION_UNTIL_BACKGROUND_MEASUREMENT_IS_STOPPED) {
state = States::ScreenOffAndWaiting;
StartWaiting();
StopMeasurement();
}
}
TickType_t HeartRateTask::GetHeartRateBackgroundMeasurementIntervalInTicks() {
int ms;
switch (settings.GetHeartRateBackgroundMeasurementInterval()) {
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::TenSeconds:
ms = 10 * 1000;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::FifteenSeconds:
ms = 15 * 1000;
break;
case Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::ThirtySeconds:
ms = 30 * 1000;
@ -258,3 +263,15 @@ bool HeartRateTask::IsBackgroundMeasurementActivated() {
return settings.GetHeartRateBackgroundMeasurementInterval() !=
Pinetime::Controllers::Settings::HeartRateBackgroundMeasurementInterval::Off;
}
TickType_t HeartRateTask::GetTicksSinceLastMeasurementStarted() {
return xTaskGetTickCount() - measurementStart;
}
bool HeartRateTask::ShoudStopTryingToGetData() {
return GetTicksSinceLastMeasurementStarted() >= DURATION_UNTIL_BACKGROUND_MEASUREMENT_IS_STOPPED;
}
bool HeartRateTask::ShouldStartBackgroundMeasuring() {
return GetTicksSinceLastMeasurementStarted() >= GetHeartRateBackgroundMeasurementIntervalInTicks();
}

View file

@ -45,7 +45,6 @@ namespace Pinetime {
static void Process(void* instance);
void StartMeasurement();
void StopMeasurement();
void StartWaiting();
void HandleGoToSleep();
void HandleWakeUp();
@ -59,6 +58,10 @@ namespace Pinetime {
bool IsContinuousModeActivated();
bool IsBackgroundMeasurementActivated();
TickType_t GetTicksSinceLastMeasurementStarted();
bool ShoudStopTryingToGetData();
bool ShouldStartBackgroundMeasuring();
TaskHandle_t taskHandle;
QueueHandle_t messageQueue;
States state = States::ScreenOnAndStopped;
@ -66,7 +69,6 @@ namespace Pinetime {
Controllers::HeartRateController& controller;
Controllers::Settings& settings;
Controllers::Ppg ppg;
TickType_t backgroundWaitingStart = 0;
TickType_t measurementStart = 0;
};