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