2021-10-13 22:08:35 +02:00
|
|
|
#include "heartratetask/HeartRateTask.h"
|
2021-01-10 17:57:26 +01:00
|
|
|
#include <drivers/Hrs3300.h>
|
|
|
|
#include <components/heartrate/HeartRateController.h>
|
|
|
|
#include <nrf_log.h>
|
|
|
|
|
|
|
|
using namespace Pinetime::Applications;
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
HeartRateTask::HeartRateTask(Drivers::Hrs3300& heartRateSensor, Controllers::HeartRateController& controller)
|
2022-05-09 17:16:08 +02:00
|
|
|
: heartRateSensor {heartRateSensor}, controller {controller}, ppg {} {
|
2021-01-10 17:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::Start() {
|
2021-06-12 10:58:28 +02:00
|
|
|
messageQueue = xQueueCreate(10, 1);
|
|
|
|
controller.SetHeartRateTask(this);
|
|
|
|
|
2021-02-03 20:28:41 +01:00
|
|
|
if (pdPASS != xTaskCreate(HeartRateTask::Process, "Heartrate", 500, this, 0, &taskHandle))
|
|
|
|
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
|
2021-01-10 17:57:26 +01:00
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
void HeartRateTask::Process(void* instance) {
|
|
|
|
auto* app = static_cast<HeartRateTask*>(instance);
|
2021-01-10 17:57:26 +01:00
|
|
|
app->Work();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::Work() {
|
|
|
|
int lastBpm = 0;
|
|
|
|
while (true) {
|
|
|
|
Messages msg;
|
|
|
|
uint32_t delay;
|
|
|
|
if (state == States::Running) {
|
2021-04-18 19:28:14 +02:00
|
|
|
if (measurementStarted)
|
|
|
|
delay = 40;
|
|
|
|
else
|
|
|
|
delay = 100;
|
2021-01-10 17:57:26 +01:00
|
|
|
} else
|
|
|
|
delay = portMAX_DELAY;
|
|
|
|
|
|
|
|
if (xQueueReceive(messageQueue, &msg, delay)) {
|
|
|
|
switch (msg) {
|
|
|
|
case Messages::GoToSleep:
|
|
|
|
StopMeasurement();
|
|
|
|
state = States::Idle;
|
|
|
|
break;
|
|
|
|
case Messages::WakeUp:
|
|
|
|
state = States::Running;
|
2021-04-18 19:28:14 +02:00
|
|
|
if (measurementStarted) {
|
2021-01-17 10:39:46 +01:00
|
|
|
lastBpm = 0;
|
|
|
|
StartMeasurement();
|
|
|
|
}
|
2021-01-10 17:57:26 +01:00
|
|
|
break;
|
|
|
|
case Messages::StartMeasurement:
|
2021-04-18 19:28:14 +02:00
|
|
|
if (measurementStarted)
|
|
|
|
break;
|
2021-01-10 17:57:26 +01:00
|
|
|
lastBpm = 0;
|
|
|
|
StartMeasurement();
|
2021-01-17 10:39:46 +01:00
|
|
|
measurementStarted = true;
|
2021-01-10 17:57:26 +01:00
|
|
|
break;
|
|
|
|
case Messages::StopMeasurement:
|
2021-04-18 19:28:14 +02:00
|
|
|
if (!measurementStarted)
|
|
|
|
break;
|
2021-01-10 17:57:26 +01:00
|
|
|
StopMeasurement();
|
2021-01-17 10:39:46 +01:00
|
|
|
measurementStarted = false;
|
2021-01-10 17:57:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (measurementStarted) {
|
2021-12-13 11:47:52 +01:00
|
|
|
ppg.Preprocess(static_cast<float>(heartRateSensor.ReadHrs()));
|
2021-01-10 17:57:26 +01:00
|
|
|
auto bpm = ppg.HeartRate();
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
if (lastBpm == 0 && bpm == 0)
|
|
|
|
controller.Update(Controllers::HeartRateController::States::NotEnoughData, 0);
|
|
|
|
if (bpm != 0) {
|
2021-01-10 17:57:26 +01:00
|
|
|
lastBpm = bpm;
|
|
|
|
controller.Update(Controllers::HeartRateController::States::Running, lastBpm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::PushMessage(HeartRateTask::Messages msg) {
|
|
|
|
BaseType_t xHigherPriorityTaskWoken;
|
|
|
|
xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
xQueueSendFromISR(messageQueue, &msg, &xHigherPriorityTaskWoken);
|
|
|
|
if (xHigherPriorityTaskWoken) {
|
|
|
|
/* Actual macro used here is port specific. */
|
|
|
|
// TODO : should I do something here?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::StartMeasurement() {
|
|
|
|
heartRateSensor.Enable();
|
2021-01-17 10:39:46 +01:00
|
|
|
vTaskDelay(100);
|
|
|
|
ppg.SetOffset(static_cast<float>(heartRateSensor.ReadHrs()));
|
2021-01-10 17:57:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::StopMeasurement() {
|
|
|
|
heartRateSensor.Disable();
|
2021-01-17 10:39:46 +01:00
|
|
|
vTaskDelay(100);
|
2021-01-10 17:57:26 +01:00
|
|
|
}
|