2021-01-10 16:57:26 +00:00
|
|
|
#include "HeartRateTask.h"
|
|
|
|
#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)
|
2021-06-06 13:56:03 +00:00
|
|
|
: heartRateSensor {heartRateSensor}, controller {controller}, ppg{} {
|
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);
|
|
|
|
|
2021-02-03 19:28:41 +00:00
|
|
|
if (pdPASS != xTaskCreate(HeartRateTask::Process, "Heartrate", 500, this, 0, &taskHandle))
|
|
|
|
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
|
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) {
|
|
|
|
Messages msg;
|
|
|
|
uint32_t delay;
|
|
|
|
if (state == States::Running) {
|
2021-04-18 17:28:14 +00:00
|
|
|
if (measurementStarted)
|
|
|
|
delay = 40;
|
|
|
|
else
|
|
|
|
delay = 100;
|
2021-01-10 16:57:26 +00: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 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:
|
2021-04-18 17:28:14 +00:00
|
|
|
if (measurementStarted)
|
|
|
|
break;
|
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:
|
2021-04-18 17:28:14 +00:00
|
|
|
if (!measurementStarted)
|
|
|
|
break;
|
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) {
|
|
|
|
auto hrs = heartRateSensor.ReadHrs();
|
|
|
|
ppg.Preprocess(hrs);
|
|
|
|
auto bpm = ppg.HeartRate();
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
if (lastBpm == 0 && bpm == 0)
|
|
|
|
controller.Update(Controllers::HeartRateController::States::NotEnoughData, 0);
|
|
|
|
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) {
|
|
|
|
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 09:39:46 +00:00
|
|
|
vTaskDelay(100);
|
|
|
|
ppg.SetOffset(static_cast<float>(heartRateSensor.ReadHrs()));
|
2021-01-10 16:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HeartRateTask::StopMeasurement() {
|
|
|
|
heartRateSensor.Disable();
|
2021-01-17 09:39:46 +00:00
|
|
|
vTaskDelay(100);
|
2021-01-10 16:57:26 +00:00
|
|
|
}
|