2021-10-13 16:08:35 -04:00
|
|
|
#include "heartratetask/HeartRateTask.h"
|
2021-01-10 11:57:26 -05:00
|
|
|
#include <drivers/Hrs3300.h>
|
|
|
|
#include <components/heartrate/HeartRateController.h>
|
|
|
|
#include <nrf_log.h>
|
|
|
|
|
|
|
|
using namespace Pinetime::Applications;
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
HeartRateTask::HeartRateTask(Drivers::Hrs3300& heartRateSensor, Controllers::HeartRateController& controller)
|
2022-12-18 12:14:36 -05:00
|
|
|
: heartRateSensor {heartRateSensor}, controller {controller} {
|
2021-01-10 11:57:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::Start() {
|
2021-06-12 04:58:28 -04:00
|
|
|
messageQueue = xQueueCreate(10, 1);
|
|
|
|
controller.SetHeartRateTask(this);
|
|
|
|
|
2022-12-18 12:14:36 -05:00
|
|
|
if (pdPASS != xTaskCreate(HeartRateTask::Process, "Heartrate", 500, this, 0, &taskHandle)) {
|
2021-02-03 14:28:41 -05:00
|
|
|
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
|
2022-12-18 12:14:36 -05:00
|
|
|
}
|
2021-01-10 11:57:26 -05:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void HeartRateTask::Process(void* instance) {
|
|
|
|
auto* app = static_cast<HeartRateTask*>(instance);
|
2021-01-10 11:57:26 -05:00
|
|
|
app->Work();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::Work() {
|
|
|
|
int lastBpm = 0;
|
|
|
|
while (true) {
|
2023-04-30 09:50:18 -04:00
|
|
|
Messages msg;
|
|
|
|
uint32_t delay;
|
2021-01-10 11:57:26 -05:00
|
|
|
if (state == States::Running) {
|
2022-12-18 12:14:36 -05:00
|
|
|
if (measurementStarted) {
|
2023-04-30 09:50:18 -04:00
|
|
|
delay = ppg.deltaTms;
|
2022-12-18 12:14:36 -05:00
|
|
|
} else {
|
2021-04-18 13:28:14 -04:00
|
|
|
delay = 100;
|
2022-12-18 12:14:36 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-10 11:57:26 -05:00
|
|
|
delay = portMAX_DELAY;
|
2022-12-18 12:14:36 -05:00
|
|
|
}
|
2021-01-10 11:57:26 -05:00
|
|
|
|
2023-04-30 09:50:18 -04:00
|
|
|
if (xQueueReceive(messageQueue, &msg, delay)) {
|
2021-01-10 11:57:26 -05:00
|
|
|
switch (msg) {
|
|
|
|
case Messages::GoToSleep:
|
|
|
|
StopMeasurement();
|
|
|
|
state = States::Idle;
|
|
|
|
break;
|
|
|
|
case Messages::WakeUp:
|
|
|
|
state = States::Running;
|
2021-04-18 13:28:14 -04:00
|
|
|
if (measurementStarted) {
|
2021-01-17 04:39:46 -05:00
|
|
|
lastBpm = 0;
|
|
|
|
StartMeasurement();
|
|
|
|
}
|
2021-01-10 11:57:26 -05:00
|
|
|
break;
|
|
|
|
case Messages::StartMeasurement:
|
2022-12-18 12:14:36 -05:00
|
|
|
if (measurementStarted) {
|
2021-04-18 13:28:14 -04:00
|
|
|
break;
|
2022-12-18 12:14:36 -05:00
|
|
|
}
|
2021-01-10 11:57:26 -05:00
|
|
|
lastBpm = 0;
|
|
|
|
StartMeasurement();
|
2021-01-17 04:39:46 -05:00
|
|
|
measurementStarted = true;
|
2021-01-10 11:57:26 -05:00
|
|
|
break;
|
|
|
|
case Messages::StopMeasurement:
|
2022-12-18 12:14:36 -05:00
|
|
|
if (!measurementStarted) {
|
2021-04-18 13:28:14 -04:00
|
|
|
break;
|
2022-12-18 12:14:36 -05:00
|
|
|
}
|
2021-01-10 11:57:26 -05:00
|
|
|
StopMeasurement();
|
2021-01-17 04:39:46 -05:00
|
|
|
measurementStarted = false;
|
2021-01-10 11:57:26 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (measurementStarted) {
|
2023-04-30 09:50:18 -04: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 11:57:26 -05:00
|
|
|
|
2022-12-18 12:14:36 -05:00
|
|
|
if (lastBpm == 0 && bpm == 0) {
|
2023-04-30 09:50:18 -04:00
|
|
|
controller.Update(Controllers::HeartRateController::States::NotEnoughData, bpm);
|
2022-12-18 12:14:36 -05:00
|
|
|
}
|
2023-04-30 09:50:18 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
if (bpm != 0) {
|
2021-01-10 11:57:26 -05:00
|
|
|
lastBpm = bpm;
|
|
|
|
controller.Update(Controllers::HeartRateController::States::Running, lastBpm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::PushMessage(HeartRateTask::Messages msg) {
|
2022-12-18 12:14:36 -05:00
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
2021-01-10 11:57:26 -05:00
|
|
|
xQueueSendFromISR(messageQueue, &msg, &xHigherPriorityTaskWoken);
|
2024-02-12 19:38:20 -05:00
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
2021-01-10 11:57:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::StartMeasurement() {
|
|
|
|
heartRateSensor.Enable();
|
2023-04-30 09:50:18 -04:00
|
|
|
ppg.Reset(true);
|
2021-01-17 04:39:46 -05:00
|
|
|
vTaskDelay(100);
|
2021-01-10 11:57:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::StopMeasurement() {
|
|
|
|
heartRateSensor.Disable();
|
2023-04-30 09:50:18 -04:00
|
|
|
ppg.Reset(true);
|
2021-01-17 04:39:46 -05:00
|
|
|
vTaskDelay(100);
|
2021-01-10 11:57:26 -05:00
|
|
|
}
|