c22e30a4a6
New implementation of the heart rate sensor data processing using a frequency based PPG algorithm. The HRS3300 settings are fine-tuned for better signal to noise at 10Hz. The measurement delay is now set to 100ms. Enable and use the ambient light sensor. FFT implementation based on ArduinoFFT (https://github.com/kosme/arduinoFFT, GPLv3.0).
123 lines
3.2 KiB
C++
123 lines
3.2 KiB
C++
#include "heartratetask/HeartRateTask.h"
|
|
#include <drivers/Hrs3300.h>
|
|
#include <components/heartrate/HeartRateController.h>
|
|
#include <nrf_log.h>
|
|
|
|
using namespace Pinetime::Applications;
|
|
|
|
HeartRateTask::HeartRateTask(Drivers::Hrs3300& heartRateSensor, Controllers::HeartRateController& controller)
|
|
: heartRateSensor {heartRateSensor}, controller {controller} {
|
|
}
|
|
|
|
void HeartRateTask::Start() {
|
|
messageQueue = xQueueCreate(10, 1);
|
|
controller.SetHeartRateTask(this);
|
|
|
|
if (pdPASS != xTaskCreate(HeartRateTask::Process, "Heartrate", 500, this, 0, &taskHandle)) {
|
|
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
|
|
}
|
|
}
|
|
|
|
void HeartRateTask::Process(void* instance) {
|
|
auto* app = static_cast<HeartRateTask*>(instance);
|
|
app->Work();
|
|
}
|
|
|
|
void HeartRateTask::Work() {
|
|
int lastBpm = 0;
|
|
while (true) {
|
|
Messages msg;
|
|
uint32_t delay;
|
|
if (state == States::Running) {
|
|
if (measurementStarted) {
|
|
delay = ppg.deltaTms;
|
|
} else {
|
|
delay = 100;
|
|
}
|
|
} 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;
|
|
if (measurementStarted) {
|
|
lastBpm = 0;
|
|
StartMeasurement();
|
|
}
|
|
break;
|
|
case Messages::StartMeasurement:
|
|
if (measurementStarted) {
|
|
break;
|
|
}
|
|
lastBpm = 0;
|
|
StartMeasurement();
|
|
measurementStarted = true;
|
|
break;
|
|
case Messages::StopMeasurement:
|
|
if (!measurementStarted) {
|
|
break;
|
|
}
|
|
StopMeasurement();
|
|
measurementStarted = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (measurementStarted) {
|
|
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);
|
|
}
|
|
|
|
if (lastBpm == 0 && bpm == 0) {
|
|
controller.Update(Controllers::HeartRateController::States::NotEnoughData, bpm);
|
|
}
|
|
|
|
if (bpm != 0) {
|
|
lastBpm = bpm;
|
|
controller.Update(Controllers::HeartRateController::States::Running, lastBpm);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void HeartRateTask::PushMessage(HeartRateTask::Messages msg) {
|
|
BaseType_t 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();
|
|
ppg.Reset(true);
|
|
vTaskDelay(100);
|
|
}
|
|
|
|
void HeartRateTask::StopMeasurement() {
|
|
heartRateSensor.Disable();
|
|
ppg.Reset(true);
|
|
vTaskDelay(100);
|
|
}
|