add heart rate measurments in the background
This commit is contained in:
parent
f8f8993fac
commit
f7b1111e05
|
@ -24,52 +24,89 @@ void HeartRateTask::Process(void* instance) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void HeartRateTask::Work() {
|
void HeartRateTask::Work() {
|
||||||
int lastBpm = 0;
|
lastBpm = 0;
|
||||||
while (true) {
|
|
||||||
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)) {
|
while (true) {
|
||||||
|
auto delay = CurrentTaskDelay();
|
||||||
|
Messages msg;
|
||||||
|
|
||||||
|
if (xQueueReceive(messageQueue, &msg, delay) == pdTRUE) {
|
||||||
switch (msg) {
|
switch (msg) {
|
||||||
case Messages::GoToSleep:
|
case Messages::GoToSleep:
|
||||||
StopMeasurement();
|
if (state == States::Running) {
|
||||||
state = States::Idle;
|
state = States::Idle;
|
||||||
|
} else if (state == States::Measuring) {
|
||||||
|
state = States::BackgroundWaiting;
|
||||||
|
backgroundMeasurementWaitingStart = xTaskGetTickCount();
|
||||||
|
StopMeasurement();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case Messages::WakeUp:
|
case Messages::WakeUp:
|
||||||
|
if (state == States::Idle) {
|
||||||
state = States::Running;
|
state = States::Running;
|
||||||
if (measurementStarted) {
|
} else if (state == States::BackgroundMeasuring) {
|
||||||
lastBpm = 0;
|
state = States::Measuring;
|
||||||
|
} else if (state == States::BackgroundWaiting) {
|
||||||
|
state = States::Measuring;
|
||||||
StartMeasurement();
|
StartMeasurement();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Messages::StartMeasurement:
|
case Messages::StartMeasurement:
|
||||||
if (measurementStarted) {
|
if (state == States::Measuring || state == States::BackgroundMeasuring) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
state = States::Measuring;
|
||||||
lastBpm = 0;
|
lastBpm = 0;
|
||||||
StartMeasurement();
|
StartMeasurement();
|
||||||
measurementStarted = true;
|
|
||||||
break;
|
break;
|
||||||
case Messages::StopMeasurement:
|
case Messages::StopMeasurement:
|
||||||
if (!measurementStarted) {
|
if (state == States::Running || state == States::Idle) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (state == States::Measuring) {
|
||||||
|
state = States::Running;
|
||||||
|
} else if (state == States::BackgroundMeasuring) {
|
||||||
|
state = States::Idle;
|
||||||
|
}
|
||||||
StopMeasurement();
|
StopMeasurement();
|
||||||
measurementStarted = false;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (measurementStarted) {
|
if (state == States::BackgroundWaiting) {
|
||||||
|
HandleBackgroundWaiting();
|
||||||
|
} else if (state == States::BackgroundMeasuring || state == States::Measuring) {
|
||||||
|
HandleSensorData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void HeartRateTask::PushMessage(HeartRateTask::Messages msg) {
|
||||||
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
|
xQueueSendFromISR(messageQueue, &msg, &xHigherPriorityTaskWoken);
|
||||||
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HeartRateTask::StartMeasurement() {
|
||||||
|
heartRateSensor.Enable();
|
||||||
|
ppg.Reset(true);
|
||||||
|
vTaskDelay(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HeartRateTask::StopMeasurement() {
|
||||||
|
heartRateSensor.Disable();
|
||||||
|
ppg.Reset(true);
|
||||||
|
vTaskDelay(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HeartRateTask::HandleBackgroundWaiting() {
|
||||||
|
if (xTaskGetTickCount() - backgroundMeasurementWaitingStart >= DURATION_BETWEEN_BACKGROUND_MEASUREMENTS) {
|
||||||
|
state = States::BackgroundMeasuring;
|
||||||
|
StartMeasurement();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void HeartRateTask::HandleSensorData() {
|
||||||
int8_t ambient = ppg.Preprocess(heartRateSensor.ReadHrs(), heartRateSensor.ReadAls());
|
int8_t ambient = ppg.Preprocess(heartRateSensor.ReadHrs(), heartRateSensor.ReadAls());
|
||||||
int bpm = ppg.HeartRate();
|
int bpm = ppg.HeartRate();
|
||||||
|
|
||||||
|
@ -95,25 +132,24 @@ void HeartRateTask::Work() {
|
||||||
if (bpm != 0) {
|
if (bpm != 0) {
|
||||||
lastBpm = bpm;
|
lastBpm = bpm;
|
||||||
controller.Update(Controllers::HeartRateController::States::Running, lastBpm);
|
controller.Update(Controllers::HeartRateController::States::Running, lastBpm);
|
||||||
}
|
if (state == States::BackgroundMeasuring) {
|
||||||
|
StopMeasurement();
|
||||||
|
state = States::BackgroundWaiting;
|
||||||
|
backgroundMeasurementWaitingStart = xTaskGetTickCount();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HeartRateTask::PushMessage(HeartRateTask::Messages msg) {
|
int HeartRateTask::CurrentTaskDelay() {
|
||||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
switch (state) {
|
||||||
xQueueSendFromISR(messageQueue, &msg, &xHigherPriorityTaskWoken);
|
case States::Measuring:
|
||||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
case States::BackgroundMeasuring:
|
||||||
}
|
return ppg.deltaTms;
|
||||||
|
case States::Running:
|
||||||
void HeartRateTask::StartMeasurement() {
|
return 100;
|
||||||
heartRateSensor.Enable();
|
case States::BackgroundWaiting:
|
||||||
ppg.Reset(true);
|
return 500;
|
||||||
vTaskDelay(100);
|
default:
|
||||||
}
|
return portMAX_DELAY;
|
||||||
|
}
|
||||||
void HeartRateTask::StopMeasurement() {
|
|
||||||
heartRateSensor.Disable();
|
|
||||||
ppg.Reset(true);
|
|
||||||
vTaskDelay(100);
|
|
||||||
}
|
}
|
|
@ -4,6 +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
|
||||||
|
|
||||||
namespace Pinetime {
|
namespace Pinetime {
|
||||||
namespace Drivers {
|
namespace Drivers {
|
||||||
class Hrs3300;
|
class Hrs3300;
|
||||||
|
@ -17,7 +19,7 @@ namespace Pinetime {
|
||||||
class HeartRateTask {
|
class HeartRateTask {
|
||||||
public:
|
public:
|
||||||
enum class Messages : uint8_t { GoToSleep, WakeUp, StartMeasurement, StopMeasurement };
|
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);
|
explicit HeartRateTask(Drivers::Hrs3300& heartRateSensor, Controllers::HeartRateController& controller);
|
||||||
void Start();
|
void Start();
|
||||||
|
@ -29,13 +31,18 @@ namespace Pinetime {
|
||||||
void StartMeasurement();
|
void StartMeasurement();
|
||||||
void StopMeasurement();
|
void StopMeasurement();
|
||||||
|
|
||||||
|
void HandleBackgroundWaiting();
|
||||||
|
void HandleSensorData();
|
||||||
|
int CurrentTaskDelay();
|
||||||
|
|
||||||
TaskHandle_t taskHandle;
|
TaskHandle_t taskHandle;
|
||||||
QueueHandle_t messageQueue;
|
QueueHandle_t messageQueue;
|
||||||
States state = States::Running;
|
States state = States::Running;
|
||||||
Drivers::Hrs3300& heartRateSensor;
|
Drivers::Hrs3300& heartRateSensor;
|
||||||
Controllers::HeartRateController& controller;
|
Controllers::HeartRateController& controller;
|
||||||
Controllers::Ppg ppg;
|
Controllers::Ppg ppg;
|
||||||
bool measurementStarted = false;
|
int lastBpm = 0;
|
||||||
|
TickType_t backgroundMeasurementWaitingStart;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue