First implementation of the HR sensor using 100% foss code (ported from waspos)

This commit is contained in:
Jean-François Milants 2021-01-10 17:57:26 +01:00
parent 50ae0ae5e0
commit 1a582815ba
23 changed files with 714 additions and 12 deletions

View File

@ -349,6 +349,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/ApplicationList.cpp displayapp/screens/ApplicationList.cpp
displayapp/screens/Notifications.cpp displayapp/screens/Notifications.cpp
displayapp/screens/Twos.cpp displayapp/screens/Twos.cpp
displayapp/screens/HeartRate.cpp
main.cpp main.cpp
drivers/St7789.cpp drivers/St7789.cpp
drivers/SpiNorFlash.cpp drivers/SpiNorFlash.cpp
@ -357,6 +358,7 @@ list(APPEND SOURCE_FILES
drivers/Watchdog.cpp drivers/Watchdog.cpp
drivers/DebugPins.cpp drivers/DebugPins.cpp
drivers/InternalFlash.cpp drivers/InternalFlash.cpp
drivers/Hrs3300.cpp
components/battery/BatteryController.cpp components/battery/BatteryController.cpp
components/ble/BleController.cpp components/ble/BleController.cpp
components/ble/NotificationManager.cpp components/ble/NotificationManager.cpp
@ -385,6 +387,12 @@ list(APPEND SOURCE_FILES
systemtask/SystemTask.cpp systemtask/SystemTask.cpp
drivers/TwiMaster.cpp drivers/TwiMaster.cpp
heartratetask/HeartRateTask.cpp
components/heartrate/Ppg.cpp
components/heartrate/Biquad.cpp
components/heartrate/Ptagc.cpp
components/heartrate/HeartRateController.cpp
) )
list(APPEND GRAPHICS_SOURCE_FILES list(APPEND GRAPHICS_SOURCE_FILES
@ -432,6 +440,7 @@ set(INCLUDE_FILES
displayapp/screens/ApplicationList.h displayapp/screens/ApplicationList.h
displayapp/Apps.h displayapp/Apps.h
displayapp/screens/Notifications.h displayapp/screens/Notifications.h
displayapp/screens/HeartRate.h
drivers/St7789.h drivers/St7789.h
drivers/SpiNorFlash.h drivers/SpiNorFlash.h
drivers/SpiMaster.h drivers/SpiMaster.h
@ -439,6 +448,7 @@ set(INCLUDE_FILES
drivers/Watchdog.h drivers/Watchdog.h
drivers/DebugPins.h drivers/DebugPins.h
drivers/InternalFlash.h drivers/InternalFlash.h
drivers/Hrs3300.h
components/battery/BatteryController.h components/battery/BatteryController.h
components/ble/BleController.h components/ble/BleController.h
components/ble/NotificationManager.h components/ble/NotificationManager.h
@ -470,6 +480,11 @@ set(INCLUDE_FILES
systemtask/SystemMonitor.h systemtask/SystemMonitor.h
displayapp/screens/Symbols.h displayapp/screens/Symbols.h
drivers/TwiMaster.h drivers/TwiMaster.h
heartratetask/HeartRateTask.h
components/heartrate/Ppg.h
components/heartrate/Biquad.h
components/heartrate/Ptagc.h
components/heartrate/HeartRateController.h
) )
include_directories( include_directories(

View File

@ -63,7 +63,7 @@
#define configTICK_RATE_HZ 1024 #define configTICK_RATE_HZ 1024
#define configMAX_PRIORITIES ( 3 ) #define configMAX_PRIORITIES ( 3 )
#define configMINIMAL_STACK_SIZE ( 120 ) #define configMINIMAL_STACK_SIZE ( 120 )
#define configTOTAL_HEAP_SIZE ( 1024*11 ) #define configTOTAL_HEAP_SIZE ( 1024*14 )
#define configMAX_TASK_NAME_LEN ( 4 ) #define configMAX_TASK_NAME_LEN ( 4 )
#define configUSE_16_BIT_TICKS 0 #define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1 #define configIDLE_SHOULD_YIELD 1

View File

@ -0,0 +1,20 @@
#include "Biquad.h"
using namespace Pinetime::Controllers;
Biquad::Biquad(float b0, float b1, float b2, float a1, float a2) : b0{b0}, b1{b1}, b2{b2}, a1{a1}, a2{a2} {
}
float Biquad::Step(float x) {
auto v1 = this->v1;
auto v2 = this->v2;
auto v = x - (a1 * v1) - (a2 * v2);
auto y = (b0 * v) + (b1 * v1) + (b2 * v2);
this->v2 = v1;
this->v1 = v;
return y;
}

View File

@ -0,0 +1,22 @@
#pragma once
namespace Pinetime {
namespace Controllers {
/// Direct Form II Biquad Filter
class Biquad {
public:
Biquad(float b0, float b1, float b2, float a1, float a2);
float Step(float x);
private:
float b0;
float b1;
float b2;
float a1;
float a2;
float v1 = 0.0f;
float v2 = 0.0f;
};
}
}

View File

@ -0,0 +1,23 @@
#include "HeartRateController.h"
#include <heartratetask/HeartRateTask.h>
using namespace Pinetime::Controllers;
void HeartRateController::Update(HeartRateController::States newState, uint8_t heartRate) {
this->state = newState;
this->heartRate = heartRate;
}
void HeartRateController::Start() {
if(task != nullptr)
task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StartMeasurement);
}
void HeartRateController::Stop() {
if(task != nullptr)
task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StopMeasurement);
}
void HeartRateController::SetHeartRateTask(Pinetime::Applications::HeartRateTask *task) {
this->task = task;
}

View File

@ -0,0 +1,28 @@
#pragma once
#include <cstdint>
namespace Pinetime {
namespace Applications {
class HeartRateTask;
}
namespace Controllers {
class HeartRateController {
public:
enum class States { NotEnoughData, NoTouch, Running};
void Start();
void Stop();
void Update(States newState, uint8_t heartRate);
void SetHeartRateTask(Applications::HeartRateTask* task);
States State() const { return state; }
uint8_t HeartRate() const { return heartRate; }
private:
Applications::HeartRateTask* task = nullptr;
States state = States::NotEnoughData;
uint8_t heartRate = 0;
};
}
}

View File

@ -0,0 +1,96 @@
#include <vector>
#include <nrf_log.h>
#include "Ppg.h"
using namespace Pinetime::Controllers;
namespace {
// TODO no vector!
int Compare(int* d1, int* d2, size_t count) {
int e = 0;
for(int i = 0; i < count; i++) {
auto d = d1[i] - d2[i];
e += d * d;
}
return e;
}
int CompareShift(int* d, int shift, size_t count) {
return Compare(d +shift, d, count - shift);
}
int Trough(int* d, size_t size, float mn, float mx) {
auto z2 = CompareShift(d, mn-2, size);
auto z1 = CompareShift(d, mn-1, size);
for(int i = mn; i < mx + 1; i++) {
auto z = CompareShift(d, i, size);
if(z2 > z1 && z1 < z)
return i;
z2 = z1;
z1 = z;
}
return -1;
}
}
Ppg::Ppg(float spl) : offset{spl},
hpf{0.87033078, -1.74066156, 0.87033078,-1.72377617, 0.75754694},
agc{20, 0.971, 2},
lpf{0.11595249, 0.23190498, 0.11595249,-0.72168143, 0.18549138} {
}
int Ppg::Preprocess(float spl) {
spl -= offset;
spl = hpf.Step(spl);
spl = agc.Step(spl);
spl = lpf.Step(spl);
auto spl_int = static_cast<int>(spl);
if(dataIndex < 200)
data[dataIndex++] = spl_int;
return spl_int;
}
float Ppg::HeartRate() {
if(dataIndex < 200)
return 0;
NRF_LOG_INFO("PREPROCESS, offset = %d", offset);
auto hr = ProcessHeartRate();
dataIndex = 0;
return hr;
}
int cccount = 0;
float Ppg::ProcessHeartRate() {
if(cccount > 2)
asm("nop");
cccount ++;
auto t0 = Trough(data.data(), dataIndex, 7, 48);
if(t0 < 0)
return 0;
float t1 = t0 * 2;
t1 = Trough(data.data(), dataIndex, t1-5, t1+5);
if(t1 < 0)
return 0;
float t2 = static_cast<int>(t1 * 3) / 2;
t2 = Trough(data.data(), dataIndex, t2 - 5, t2 + 5);
if(t2 < 0)
return 0;
float t3 = static_cast<int>(t2 * 4) / 3;
t3 = Trough(data.data(), dataIndex, t3 - 4, t3 + 4);
if(t3 < 0)
return static_cast<int>(60 * 24 * 3) / static_cast<int>(t2);
return static_cast<int>(60 * 24 * 4) / static_cast<int>(t3);
}
void Ppg::SetOffset(uint16_t offset) {
this->offset = offset;
dataIndex = 0;
}

View File

@ -0,0 +1,30 @@
#pragma once
#include <array>
#include "Biquad.h"
#include "Ptagc.h"
namespace Pinetime {
namespace Controllers {
class Ppg {
public:
explicit Ppg(float spl);
int Preprocess(float spl);
float HeartRate();
void SetOffset(uint16_t i);
private:
std::array<int, 200> data;
size_t dataIndex = 0;
float offset;
Biquad hpf;
Ptagc agc;
Biquad lpf;
float ProcessHeartRate();
};
}
}

View File

@ -0,0 +1,21 @@
#include <cmath>
#include "Ptagc.h"
using namespace Pinetime::Controllers;
Ptagc::Ptagc(float start, float decay, float threshold) : peak{start}, decay{decay}, boost{1.0f/decay}, threshold{threshold} {
}
float Ptagc::Step(float spl) {
if(std::abs(spl) > peak)
peak *= boost;
else
peak *= decay;
if((spl > (peak * threshold)) || (spl < (peak * -threshold)))
return 0.0f;
spl = 100.0f * spl / (2.0f * peak);
return spl;
}

View File

@ -0,0 +1,18 @@
#pragma once
namespace Pinetime {
namespace Controllers {
class Ptagc {
public:
Ptagc(float start, float decay, float threshold);
float Step(float spl);
private:
float peak;
float decay;
float boost;
float threshold;
};
}
}

View File

@ -2,6 +2,6 @@
namespace Pinetime { namespace Pinetime {
namespace Applications { namespace Applications {
enum class Apps {None, Launcher, Clock, SysInfo, Meter, Gauge, Brightness, Music, FirmwareValidation, Paint, Paddle, Notifications, Twos}; enum class Apps {None, Launcher, Clock, SysInfo, Meter, Gauge, Brightness, Music, FirmwareValidation, Paint, Paddle, Notifications, Twos, HeartRate};
} }
} }

View File

@ -1,5 +1,6 @@
#include "DisplayApp.h" #include "DisplayApp.h"
#include <libraries/log/nrf_log.h> #include <libraries/log/nrf_log.h>
#include <displayapp/screens/HeartRate.h>
#include "components/battery/BatteryController.h" #include "components/battery/BatteryController.h"
#include "components/ble/BleController.h" #include "components/ble/BleController.h"
#include "components/datetime/DateTimeController.h" #include "components/datetime/DateTimeController.h"
@ -29,7 +30,8 @@ DisplayApp::DisplayApp(Drivers::St7789 &lcd, Components::LittleVgl &lvgl, Driver
Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::Battery &batteryController, Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController, Drivers::WatchdogView &watchdog, Controllers::DateTime &dateTimeController, Drivers::WatchdogView &watchdog,
System::SystemTask &systemTask, System::SystemTask &systemTask,
Pinetime::Controllers::NotificationManager& notificationManager) : Pinetime::Controllers::NotificationManager& notificationManager,
Pinetime::Controllers::HeartRateController& heartRateController) :
lcd{lcd}, lcd{lcd},
lvgl{lvgl}, lvgl{lvgl},
batteryController{batteryController}, batteryController{batteryController},
@ -39,7 +41,8 @@ DisplayApp::DisplayApp(Drivers::St7789 &lcd, Components::LittleVgl &lvgl, Driver
touchPanel{touchPanel}, touchPanel{touchPanel},
currentScreen{new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager) }, currentScreen{new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager) },
systemTask{systemTask}, systemTask{systemTask},
notificationManager{notificationManager} { notificationManager{notificationManager},
heartRateController{heartRateController} {
msgQueue = xQueueCreate(queueSize, itemSize); msgQueue = xQueueCreate(queueSize, itemSize);
onClockApp = true; onClockApp = true;
modal.reset(new Screens::Modal(this)); modal.reset(new Screens::Modal(this));
@ -211,6 +214,7 @@ void DisplayApp::RunningState() {
case Apps::Music : currentScreen.reset(new Screens::Music(this, systemTask.nimble().music())); break; case Apps::Music : currentScreen.reset(new Screens::Music(this, systemTask.nimble().music())); break;
case Apps::FirmwareValidation: currentScreen.reset(new Screens::FirmwareValidation(this, validator)); break; case Apps::FirmwareValidation: currentScreen.reset(new Screens::FirmwareValidation(this, validator)); break;
case Apps::Notifications: currentScreen.reset(new Screens::Notifications(this, notificationManager, Screens::Notifications::Modes::Normal)); break; case Apps::Notifications: currentScreen.reset(new Screens::Notifications(this, notificationManager, Screens::Notifications::Modes::Normal)); break;
case Apps::HeartRate: currentScreen.reset(new Screens::HeartRate(this, heartRateController)); break;
} }
nextApp = Apps::None; nextApp = Apps::None;
} }

View File

@ -23,6 +23,7 @@ namespace Pinetime {
class Ble; class Ble;
class DateTime; class DateTime;
class NotificationManager; class NotificationManager;
class HeartRateController;
} }
namespace System { namespace System {
@ -42,7 +43,8 @@ namespace Pinetime {
Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::Battery &batteryController, Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController, Drivers::WatchdogView &watchdog, Controllers::DateTime &dateTimeController, Drivers::WatchdogView &watchdog,
System::SystemTask &systemTask, System::SystemTask &systemTask,
Pinetime::Controllers::NotificationManager& notificationManager); Pinetime::Controllers::NotificationManager& notificationManager,
Pinetime::Controllers::HeartRateController& heartRateController);
void Start(); void Start();
void PushMessage(Messages msg); void PushMessage(Messages msg);
@ -87,6 +89,7 @@ namespace Pinetime {
Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::NotificationManager& notificationManager;
Pinetime::Controllers::FirmwareValidator validator; Pinetime::Controllers::FirmwareValidator validator;
TouchModes touchMode = TouchModes::Gestures; TouchModes touchMode = TouchModes::Gestures;
Pinetime::Controllers::HeartRateController& heartRateController;
}; };
} }
} }

View File

@ -45,7 +45,7 @@ std::unique_ptr<Screen> ApplicationList::CreateScreen1() {
{Symbols::sun, Apps::Brightness}, {Symbols::sun, Apps::Brightness},
{Symbols::list, Apps::SysInfo}, {Symbols::list, Apps::SysInfo},
{Symbols::check, Apps::FirmwareValidation}, {Symbols::check, Apps::FirmwareValidation},
{Symbols::none, Apps::None} {Symbols::heartBeat, Apps::HeartRate}
} }

View File

@ -0,0 +1,76 @@
#include <libs/lvgl/lvgl.h>
#include "HeartRate.h"
#include <components/heartrate/HeartRateController.h>
#include "../DisplayApp.h"
using namespace Pinetime::Applications::Screens;
extern lv_font_t jetbrains_mono_extrabold_compressed;
extern lv_font_t jetbrains_mono_bold_20;
const char* ToString(Pinetime::Controllers::HeartRateController::States s) {
switch(s) {
case Pinetime::Controllers::HeartRateController::States::NotEnoughData: return "Not enough data,\nplease wait...";
case Pinetime::Controllers::HeartRateController::States::NoTouch: return "No touch detected";
case Pinetime::Controllers::HeartRateController::States::Running: return "Measuring...";
}
return "";
}
HeartRate::HeartRate(Pinetime::Applications::DisplayApp *app, Controllers::HeartRateController& heartRateController) : Screen(app), heartRateController{heartRateController} {
label_bpm = lv_label_create(lv_scr_act(), NULL);
labelStyle = const_cast<lv_style_t *>(lv_label_get_style(label_bpm, LV_LABEL_STYLE_MAIN));
labelStyle->text.font = &jetbrains_mono_bold_20;
lv_style_copy(&labelBigStyle, labelStyle);
labelBigStyle.text.font = &jetbrains_mono_extrabold_compressed;
lv_label_set_style(label_bpm, LV_LABEL_STYLE_MAIN, labelStyle);
label_hr = lv_label_create(lv_scr_act(), NULL);
lv_label_set_style(label_hr, LV_LABEL_STYLE_MAIN, &labelBigStyle);
lv_obj_align(label_hr, lv_scr_act(), LV_ALIGN_CENTER, -70, 0);
lv_label_set_text(label_hr, "000");
lv_label_set_text(label_bpm, "Heart rate BPM");
lv_obj_align(label_bpm, label_hr, LV_ALIGN_OUT_TOP_MID, 0, -20);
label_status = lv_label_create(lv_scr_act(), NULL);
lv_label_set_text(label_status, ToString(Pinetime::Controllers::HeartRateController::States::NotEnoughData));
lv_label_set_style(label_status, LV_LABEL_STYLE_MAIN, labelStyle);
lv_obj_align(label_status, label_hr, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
heartRateController.Start();
}
HeartRate::~HeartRate() {
heartRateController.Stop();
lv_obj_clean(lv_scr_act());
}
bool HeartRate::Refresh() {
char hr[4];
auto state = heartRateController.State();
switch(state) {
case Controllers::HeartRateController::States::NoTouch:
case Controllers::HeartRateController::States::NotEnoughData:
lv_label_set_text(label_hr, "000");
break;
default:
sprintf(hr, "%03d", heartRateController.HeartRate());
lv_label_set_text(label_hr, hr);
}
lv_label_set_text(label_status, ToString(state));
lv_obj_align(label_status, label_hr, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
return running;
}
bool HeartRate::OnButtonPushed() {
running = false;
return true;
}

View File

@ -0,0 +1,38 @@
#pragma once
#include <cstdint>
#include <chrono>
#include "Screen.h"
#include <bits/unique_ptr.h>
#include <libs/lvgl/src/lv_core/lv_style.h>
#include <libs/lvgl/src/lv_core/lv_obj.h>
namespace Pinetime {
namespace Controllers {
class HeartRateController;
}
namespace Applications {
namespace Screens {
class HeartRate : public Screen{
public:
HeartRate(DisplayApp* app, Controllers::HeartRateController& HeartRateController);
~HeartRate() override;
bool Refresh() override;
bool OnButtonPushed() override;
private:
Controllers::HeartRateController& heartRateController;
lv_obj_t* label_hr;
lv_obj_t* label_bpm;
lv_obj_t* label_status;
lv_style_t labelBigStyle;
lv_style_t* labelStyle;
bool running = true;
};
}
}
}

108
src/drivers/Hrs3300.cpp Normal file
View File

@ -0,0 +1,108 @@
#include <algorithm>
#include <nrf_gpio.h>
#include "Hrs3300.h"
#include <FreeRTOS.h>
#include <task.h>
#include <nrf_log.h>
using namespace Pinetime::Drivers;
Hrs3300::Hrs3300(TwiMaster &twiMaster, uint8_t twiAddress) : twiMaster{twiMaster}, twiAddress{twiAddress} {
}
void Hrs3300::Init() {
nrf_gpio_cfg_input(30, NRF_GPIO_PIN_NOPULL);
Disable();
vTaskDelay(100);
// HRS disabled, 12.5 ms wait time between cycles, (partly) 20mA drive
WriteRegister(static_cast<uint8_t>(Registers::Enable), 0x60);
// (partly) 20mA drive, power on, "magic" (datasheet says both
// "reserved" and "set low nibble to 8" but 0xe gives better results
// and is used by at least two other HRS3300 drivers
WriteRegister(static_cast<uint8_t>(Registers::PDriver), 0x6E);
// HRS and ALS both in 16-bit mode
WriteRegister(static_cast<uint8_t>(Registers::Res), 0x88);
// 64x gain
WriteRegister(static_cast<uint8_t>(Registers::Hgain), 0x10);
}
void Hrs3300::Enable() {
NRF_LOG_INFO("ENABLE");
auto value = ReadRegister(static_cast<uint8_t>(Registers::Enable));
value |= 0x80;
WriteRegister(static_cast<uint8_t>(Registers::Enable), value);
}
void Hrs3300::Disable() {
NRF_LOG_INFO("DISABLE");
auto value = ReadRegister(static_cast<uint8_t>(Registers::Enable));
value &= ~0x80;
WriteRegister(static_cast<uint8_t>(Registers::Enable), value);
}
uint16_t Hrs3300::ReadHrs() {
auto m = ReadRegister(static_cast<uint8_t>(Registers::C0DataM));
auto h = ReadRegister(static_cast<uint8_t>(Registers::C0DataH));
auto l = ReadRegister(static_cast<uint8_t>(Registers::C0dataL));
return (m << 8) | ((h & 0x0f) << 4) | (l & 0x0f) | ((l & 0x30) << 12);
}
uint16_t Hrs3300::ReadAls() {
auto m = ReadRegister(static_cast<uint8_t>(Registers::C1dataM));
auto h = ReadRegister(static_cast<uint8_t>(Registers::C1dataH));
auto l = ReadRegister(static_cast<uint8_t>(Registers::C1dataL));
return (m << 3) | ((h & 0x3f) << 11) | (l & 0x07);
}
void Hrs3300::SetGain(uint8_t gain) {
static constexpr uint8_t maxGain = 64;
gain = std::min(gain, maxGain);
uint8_t hgain = 0;
while((1 << hgain) < gain)
hgain++;
WriteRegister(static_cast<uint8_t>(Registers::Hgain), hgain << 2);
}
void Hrs3300::SetDrive(uint8_t drive) {
auto en = ReadRegister(static_cast<uint8_t>(Registers::Enable));
auto pd = ReadRegister(static_cast<uint8_t>(Registers::PDriver));
en = (en & 0xf7) | ((drive & 2) << 2);
pd = (pd & 0xbf) | ((drive & 1) << 6);
WriteRegister(static_cast<uint8_t>(Registers::Enable), en);
WriteRegister(static_cast<uint8_t>(Registers::PDriver), pd);
}
void Hrs3300::WriteRegister(uint8_t reg, uint8_t data) {
auto ret = twiMaster.Write(twiAddress, reg, &data, 1);
if(ret != TwiMaster::ErrorCodes::NoError)
NRF_LOG_INFO("WRITE ERROR");
}
uint8_t Hrs3300::ReadRegister(uint8_t reg) {
uint8_t value;
auto ret = twiMaster.Read(twiAddress, reg, &value, 1);
if(ret != TwiMaster::ErrorCodes::NoError)
NRF_LOG_INFO("READ ERROR");
return value;
}

47
src/drivers/Hrs3300.h Normal file
View File

@ -0,0 +1,47 @@
#pragma once
#include "TwiMaster.h"
namespace Pinetime {
namespace Drivers {
class Hrs3300 {
public:
enum class Registers : uint8_t {
Id = 0x00,
Enable = 0x01,
EnableHen = 0x80,
C1dataM = 0x08,
C0DataM = 0x09,
C0DataH = 0x0a,
PDriver = 0x0c,
C1dataH = 0x0d,
C1dataL = 0x0e,
C0dataL = 0x0f,
Res = 0x16,
Hgain = 0x17
};
Hrs3300(TwiMaster& twiMaster, uint8_t twiAddress);
Hrs3300(const Hrs3300&) = delete;
Hrs3300& operator=(const Hrs3300&) = delete;
Hrs3300(Hrs3300&&) = delete;
Hrs3300& operator=(Hrs3300&&) = delete;
void Init();
void Enable();
void Disable();
uint16_t ReadHrs();
uint16_t ReadAls();
void SetGain(uint8_t gain);
void SetDrive(uint8_t drive);
private:
TwiMaster& twiMaster;
uint8_t twiAddress;
void WriteRegister(uint8_t reg, uint8_t data);
uint8_t ReadRegister(uint8_t reg);
};
}
}

View File

@ -0,0 +1,89 @@
#include "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},
ppg{static_cast<float>(heartRateSensor.ReadHrs())} {
messageQueue = xQueueCreate(10, 1);
controller.SetHeartRateTask(this);
}
void HeartRateTask::Start() {
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 = 40;
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;
break;
case Messages::StartMeasurement:
lastBpm = 0;
StartMeasurement();
ppg.SetOffset(heartRateSensor.ReadHrs());
break;
case Messages::StopMeasurement:
StopMeasurement();
break;
}
}
if (measurementStarted) {
auto hrs = heartRateSensor.ReadHrs();
ppg.Preprocess(hrs);
auto bpm = ppg.HeartRate();
if (lastBpm == 0 && bpm == 0) controller.Update(Controllers::HeartRateController::States::NoTouch, 0);
if(bpm != 0) {
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();
measurementStarted = true;
}
void HeartRateTask::StopMeasurement() {
heartRateSensor.Disable();
measurementStarted = false;
}

View File

@ -0,0 +1,43 @@
#pragma once
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <components/heartrate/Ppg.h>
namespace Pinetime {
namespace Drivers {
class Hrs3300;
}
namespace Controllers{
class HeartRateController;
}
namespace Applications {
class HeartRateTask {
public:
enum class Messages : uint8_t {GoToSleep, WakeUp, StartMeasurement, StopMeasurement };
enum class States {Idle, Running};
explicit HeartRateTask(Drivers::Hrs3300& heartRateSensor, Controllers::HeartRateController& controller);
void Start();
void Work();
void PushMessage(Messages msg);
private:
static void Process(void* instance);
void StartMeasurement();
void StopMeasurement();
TaskHandle_t taskHandle;
QueueHandle_t messageQueue;
States state = States::Running;
Drivers::Hrs3300 &heartRateSensor;
Controllers::HeartRateController& controller;
Controllers::Ppg ppg;
bool measurementStarted = false;
};
}
}

View File

@ -25,6 +25,7 @@
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include <task.h> #include <task.h>
#include <timers.h> #include <timers.h>
#include <drivers/Hrs3300.h>
#include "components/battery/BatteryController.h" #include "components/battery/BatteryController.h"
#include "components/ble/BleController.h" #include "components/ble/BleController.h"
@ -57,6 +58,7 @@ static constexpr uint8_t pinLcdDataCommand = 18;
static constexpr uint8_t pinTwiScl = 7; static constexpr uint8_t pinTwiScl = 7;
static constexpr uint8_t pinTwiSda = 6; static constexpr uint8_t pinTwiSda = 6;
static constexpr uint8_t touchPanelTwiAddress = 0x15; static constexpr uint8_t touchPanelTwiAddress = 0x15;
static constexpr uint8_t heartRateSensorTwiAddress = 0x44;
Pinetime::Drivers::SpiMaster spi{Pinetime::Drivers::SpiMaster::SpiModule::SPI0, { Pinetime::Drivers::SpiMaster spi{Pinetime::Drivers::SpiMaster::SpiModule::SPI0, {
Pinetime::Drivers::SpiMaster::BitOrder::Msb_Lsb, Pinetime::Drivers::SpiMaster::BitOrder::Msb_Lsb,
@ -84,6 +86,8 @@ Pinetime::Drivers::TwiMaster twiMaster{Pinetime::Drivers::TwiMaster::Modules::TW
Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress}; Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress};
Pinetime::Components::LittleVgl lvgl {lcd, touchPanel}; Pinetime::Components::LittleVgl lvgl {lcd, touchPanel};
Pinetime::Drivers::Hrs3300 heartRateSensor {twiMaster, heartRateSensorTwiAddress};
TimerHandle_t debounceTimer; TimerHandle_t debounceTimer;
Pinetime::Controllers::Battery batteryController; Pinetime::Controllers::Battery batteryController;
@ -237,7 +241,7 @@ int main(void) {
debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback); debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback);
systemTask.reset(new Pinetime::System::SystemTask(spi, lcd, spiNorFlash, twiMaster, touchPanel, lvgl, batteryController, bleController, systemTask.reset(new Pinetime::System::SystemTask(spi, lcd, spiNorFlash, twiMaster, touchPanel, lvgl, batteryController, bleController,
dateTimeController, notificationManager)); dateTimeController, notificationManager, heartRateSensor));
systemTask->Start(); systemTask->Start();
nimble_port_init(); nimble_port_init();

View File

@ -21,6 +21,7 @@
#include "drivers/SpiMaster.h" #include "drivers/SpiMaster.h"
#include "drivers/SpiNorFlash.h" #include "drivers/SpiNorFlash.h"
#include "drivers/TwiMaster.h" #include "drivers/TwiMaster.h"
#include "drivers/Hrs3300.h"
#include "main.h" #include "main.h"
using namespace Pinetime::System; using namespace Pinetime::System;
@ -39,12 +40,14 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,
Components::LittleVgl &lvgl, Components::LittleVgl &lvgl,
Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::Battery &batteryController, Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController, Controllers::DateTime &dateTimeController,
Pinetime::Controllers::NotificationManager& notificationManager) : Pinetime::Controllers::NotificationManager& notificationManager,
Pinetime::Drivers::Hrs3300& heartRateSensor) :
spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash}, spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash},
twiMaster{twiMaster}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, twiMaster{twiMaster}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController},
bleController{bleController}, dateTimeController{dateTimeController}, bleController{bleController}, dateTimeController{dateTimeController},
watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager},
nimbleController(*this, bleController,dateTimeController, notificationManager, batteryController, spiNorFlash) { nimbleController(*this, bleController,dateTimeController, notificationManager, batteryController, spiNorFlash),
heartRateSensor{heartRateSensor}{
systemTasksMsgQueue = xQueueCreate(10, 1); systemTasksMsgQueue = xQueueCreate(10, 1);
} }
@ -77,12 +80,19 @@ void SystemTask::Work() {
batteryController.Init(); batteryController.Init();
displayApp.reset(new Pinetime::Applications::DisplayApp(lcd, lvgl, touchPanel, batteryController, bleController, displayApp.reset(new Pinetime::Applications::DisplayApp(lcd, lvgl, touchPanel, batteryController, bleController,
dateTimeController, watchdogView, *this, notificationManager)); dateTimeController, watchdogView, *this, notificationManager, heartRateController));
displayApp->Start(); displayApp->Start();
batteryController.Update(); batteryController.Update();
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel);
heartRateSensor.Init();
heartRateSensor.Disable();
heartRateApp.reset(new Pinetime::Applications::HeartRateTask(heartRateSensor, heartRateController));
heartRateApp->Start();
nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t)GPIO_PIN_CNF_SENSE_High); nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t)GPIO_PIN_CNF_SENSE_High);
nrf_gpio_cfg_output(15); nrf_gpio_cfg_output(15);
nrf_gpio_pin_set(15); nrf_gpio_pin_set(15);

View File

@ -5,6 +5,8 @@
#include <FreeRTOS.h> #include <FreeRTOS.h>
#include <task.h> #include <task.h>
#include <timers.h> #include <timers.h>
#include <heartratetask/HeartRateTask.h>
#include <components/heartrate/HeartRateController.h>
#include "SystemMonitor.h" #include "SystemMonitor.h"
#include "components/battery/BatteryController.h" #include "components/battery/BatteryController.h"
@ -20,6 +22,7 @@ namespace Pinetime {
class SpiNorFlash; class SpiNorFlash;
class St7789; class St7789;
class TwiMaster; class TwiMaster;
class Hrs3300;
} }
namespace System { namespace System {
class SystemTask { class SystemTask {
@ -34,7 +37,8 @@ namespace Pinetime {
Components::LittleVgl &lvgl, Components::LittleVgl &lvgl,
Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::Battery &batteryController, Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController, Controllers::DateTime &dateTimeController,
Pinetime::Controllers::NotificationManager& manager); Pinetime::Controllers::NotificationManager& manager,
Pinetime::Drivers::Hrs3300& heartRateSensor);
void Start(); void Start();
@ -58,6 +62,9 @@ namespace Pinetime {
Pinetime::Components::LittleVgl& lvgl; Pinetime::Components::LittleVgl& lvgl;
Pinetime::Controllers::Battery& batteryController; Pinetime::Controllers::Battery& batteryController;
std::unique_ptr<Pinetime::Applications::DisplayApp> displayApp; std::unique_ptr<Pinetime::Applications::DisplayApp> displayApp;
Pinetime::Controllers::HeartRateController heartRateController;
std::unique_ptr<Pinetime::Applications::HeartRateTask> heartRateApp;
Pinetime::Controllers::Ble& bleController; Pinetime::Controllers::Ble& bleController;
Pinetime::Controllers::DateTime& dateTimeController; Pinetime::Controllers::DateTime& dateTimeController;
QueueHandle_t systemTasksMsgQueue; QueueHandle_t systemTasksMsgQueue;
@ -68,7 +75,7 @@ namespace Pinetime {
Pinetime::Drivers::WatchdogView watchdogView; Pinetime::Drivers::WatchdogView watchdogView;
Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::NotificationManager& notificationManager;
Pinetime::Controllers::NimbleController nimbleController; Pinetime::Controllers::NimbleController nimbleController;
Pinetime::Drivers::Hrs3300& heartRateSensor;
static constexpr uint8_t pinSpiSck = 2; static constexpr uint8_t pinSpiSck = 2;
static constexpr uint8_t pinSpiMosi = 3; static constexpr uint8_t pinSpiMosi = 3;