2021-10-13 20:08:35 +00:00
|
|
|
#include "heartratetask/HeartRateTask.h"
|
2021-01-10 16:57:26 +00:00
|
|
|
#include <drivers/Hrs3300.h>
|
|
|
|
#include <components/heartrate/HeartRateController.h>
|
|
|
|
#include <nrf_log.h>
|
|
|
|
|
|
|
|
using namespace Pinetime::Applications;
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
HeartRateTask::HeartRateTask(Drivers::Hrs3300& heartRateSensor, Controllers::HeartRateController& controller)
|
2022-12-18 17:14:36 +00:00
|
|
|
: heartRateSensor {heartRateSensor}, controller {controller} {
|
2021-01-10 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::Start() {
|
2021-06-12 08:58:28 +00:00
|
|
|
messageQueue = xQueueCreate(10, 1);
|
|
|
|
controller.SetHeartRateTask(this);
|
|
|
|
|
2022-12-18 17:14:36 +00:00
|
|
|
if (pdPASS != xTaskCreate(HeartRateTask::Process, "Heartrate", 500, this, 0, &taskHandle)) {
|
2021-02-03 19:28:41 +00:00
|
|
|
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
|
2022-12-18 17:14:36 +00:00
|
|
|
}
|
2021-01-10 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
void HeartRateTask::Process(void* instance) {
|
|
|
|
auto* app = static_cast<HeartRateTask*>(instance);
|
2021-01-10 16:57:26 +00:00
|
|
|
app->Work();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::Work() {
|
|
|
|
int lastBpm = 0;
|
|
|
|
while (true) {
|
2023-04-30 13:50:18 +00:00
|
|
|
Messages msg;
|
|
|
|
uint32_t delay;
|
2021-01-10 16:57:26 +00:00
|
|
|
if (state == States::Running) {
|
2022-12-18 17:14:36 +00:00
|
|
|
if (measurementStarted) {
|
2023-04-30 13:50:18 +00:00
|
|
|
delay = ppg.deltaTms;
|
2022-12-18 17:14:36 +00:00
|
|
|
} else {
|
2021-04-18 17:28:14 +00:00
|
|
|
delay = 100;
|
2022-12-18 17:14:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-10 16:57:26 +00:00
|
|
|
delay = portMAX_DELAY;
|
2022-12-18 17:14:36 +00:00
|
|
|
}
|
2021-01-10 16:57:26 +00:00
|
|
|
|
2023-04-30 13:50:18 +00:00
|
|
|
if (xQueueReceive(messageQueue, &msg, delay)) {
|
2021-01-10 16:57:26 +00:00
|
|
|
switch (msg) {
|
|
|
|
case Messages::GoToSleep:
|
|
|
|
StopMeasurement();
|
|
|
|
state = States::Idle;
|
|
|
|
break;
|
|
|
|
case Messages::WakeUp:
|
|
|
|
state = States::Running;
|
2021-04-18 17:28:14 +00:00
|
|
|
if (measurementStarted) {
|
2021-01-17 09:39:46 +00:00
|
|
|
lastBpm = 0;
|
|
|
|
StartMeasurement();
|
|
|
|
}
|
2021-01-10 16:57:26 +00:00
|
|
|
break;
|
|
|
|
case Messages::StartMeasurement:
|
2022-12-18 17:14:36 +00:00
|
|
|
if (measurementStarted) {
|
2021-04-18 17:28:14 +00:00
|
|
|
break;
|
2022-12-18 17:14:36 +00:00
|
|
|
}
|
2021-01-10 16:57:26 +00:00
|
|
|
lastBpm = 0;
|
|
|
|
StartMeasurement();
|
2021-01-17 09:39:46 +00:00
|
|
|
measurementStarted = true;
|
2021-01-10 16:57:26 +00:00
|
|
|
break;
|
|
|
|
case Messages::StopMeasurement:
|
2022-12-18 17:14:36 +00:00
|
|
|
if (!measurementStarted) {
|
2021-04-18 17:28:14 +00:00
|
|
|
break;
|
2022-12-18 17:14:36 +00:00
|
|
|
}
|
2021-01-10 16:57:26 +00:00
|
|
|
StopMeasurement();
|
2021-01-17 09:39:46 +00:00
|
|
|
measurementStarted = false;
|
2021-01-10 16:57:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (measurementStarted) {
|
2023-04-30 13:50:18 +00:00
|
|
|
int8_t ambient = ppg.Preprocess(heartRateSensor.ReadHrs(), heartRateSensor.ReadAls());
|
|
|
|
int bpm = ppg.HeartRate();
|
|
|
|
|
|
|
|
// If ambient light detected or a reset requested (bpm < 0)
|
|
|
|
if (ambient > 0) {
|
|
|
|
// Reset all DAQ buffers
|
|
|
|
ppg.Reset(true);
|
|
|
|
// Force state to NotEnoughData (below)
|
|
|
|
lastBpm = 0;
|
|
|
|
bpm = 0;
|
|
|
|
} else if (bpm < 0) {
|
|
|
|
// Reset all DAQ buffers except HRS buffer
|
|
|
|
ppg.Reset(false);
|
|
|
|
// Set HR to zero and update
|
|
|
|
bpm = 0;
|
|
|
|
controller.Update(Controllers::HeartRateController::States::Running, bpm);
|
|
|
|
}
|
2021-01-10 16:57:26 +00:00
|
|
|
|
2022-12-18 17:14:36 +00:00
|
|
|
if (lastBpm == 0 && bpm == 0) {
|
2023-04-30 13:50:18 +00:00
|
|
|
controller.Update(Controllers::HeartRateController::States::NotEnoughData, bpm);
|
2022-12-18 17:14:36 +00:00
|
|
|
}
|
2023-04-30 13:50:18 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (bpm != 0) {
|
2021-01-10 16:57:26 +00:00
|
|
|
lastBpm = bpm;
|
|
|
|
controller.Update(Controllers::HeartRateController::States::Running, lastBpm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::PushMessage(HeartRateTask::Messages msg) {
|
2022-12-18 17:14:36 +00:00
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
2021-01-10 16:57:26 +00:00
|
|
|
xQueueSendFromISR(messageQueue, &msg, &xHigherPriorityTaskWoken);
|
2024-02-13 00:38:20 +00:00
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
2021-01-10 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::StartMeasurement() {
|
|
|
|
heartRateSensor.Enable();
|
2023-04-30 13:50:18 +00:00
|
|
|
ppg.Reset(true);
|
2021-01-17 09:39:46 +00:00
|
|
|
vTaskDelay(100);
|
2021-01-10 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::StopMeasurement() {
|
|
|
|
heartRateSensor.Disable();
|
2023-04-30 13:50:18 +00:00
|
|
|
ppg.Reset(true);
|
2021-01-17 09:39:46 +00:00
|
|
|
vTaskDelay(100);
|
2021-01-10 16:57:26 +00:00
|
|
|
}
|