diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fde1f586..86cfc1c2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -349,6 +349,7 @@ list(APPEND SOURCE_FILES displayapp/screens/ApplicationList.cpp displayapp/screens/Notifications.cpp displayapp/screens/Twos.cpp + displayapp/screens/HeartRate.cpp main.cpp drivers/St7789.cpp drivers/SpiNorFlash.cpp @@ -357,6 +358,7 @@ list(APPEND SOURCE_FILES drivers/Watchdog.cpp drivers/DebugPins.cpp drivers/InternalFlash.cpp + drivers/Hrs3300.cpp components/battery/BatteryController.cpp components/ble/BleController.cpp components/ble/NotificationManager.cpp @@ -385,6 +387,12 @@ list(APPEND SOURCE_FILES systemtask/SystemTask.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 @@ -432,6 +440,7 @@ set(INCLUDE_FILES displayapp/screens/ApplicationList.h displayapp/Apps.h displayapp/screens/Notifications.h + displayapp/screens/HeartRate.h drivers/St7789.h drivers/SpiNorFlash.h drivers/SpiMaster.h @@ -439,6 +448,7 @@ set(INCLUDE_FILES drivers/Watchdog.h drivers/DebugPins.h drivers/InternalFlash.h + drivers/Hrs3300.h components/battery/BatteryController.h components/ble/BleController.h components/ble/NotificationManager.h @@ -470,6 +480,11 @@ set(INCLUDE_FILES systemtask/SystemMonitor.h displayapp/screens/Symbols.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( diff --git a/src/FreeRTOSConfig.h b/src/FreeRTOSConfig.h index a18a2927..0b6a4b33 100644 --- a/src/FreeRTOSConfig.h +++ b/src/FreeRTOSConfig.h @@ -63,7 +63,7 @@ #define configTICK_RATE_HZ 1024 #define configMAX_PRIORITIES ( 3 ) #define configMINIMAL_STACK_SIZE ( 120 ) -#define configTOTAL_HEAP_SIZE ( 1024*11 ) +#define configTOTAL_HEAP_SIZE ( 1024*14 ) #define configMAX_TASK_NAME_LEN ( 4 ) #define configUSE_16_BIT_TICKS 0 #define configIDLE_SHOULD_YIELD 1 diff --git a/src/components/heartrate/Biquad.cpp b/src/components/heartrate/Biquad.cpp new file mode 100644 index 00000000..37b3ba67 --- /dev/null +++ b/src/components/heartrate/Biquad.cpp @@ -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; +} diff --git a/src/components/heartrate/Biquad.h b/src/components/heartrate/Biquad.h new file mode 100644 index 00000000..dc9b97f6 --- /dev/null +++ b/src/components/heartrate/Biquad.h @@ -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; + }; + } +} diff --git a/src/components/heartrate/HeartRateController.cpp b/src/components/heartrate/HeartRateController.cpp new file mode 100644 index 00000000..f296027f --- /dev/null +++ b/src/components/heartrate/HeartRateController.cpp @@ -0,0 +1,23 @@ +#include "HeartRateController.h" +#include + +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; +} diff --git a/src/components/heartrate/HeartRateController.h b/src/components/heartrate/HeartRateController.h new file mode 100644 index 00000000..b4d6c972 --- /dev/null +++ b/src/components/heartrate/HeartRateController.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +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; + }; + } +} \ No newline at end of file diff --git a/src/components/heartrate/Ppg.cpp b/src/components/heartrate/Ppg.cpp new file mode 100644 index 00000000..c9a11030 --- /dev/null +++ b/src/components/heartrate/Ppg.cpp @@ -0,0 +1,96 @@ +#include +#include +#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(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(t1 * 3) / 2; + t2 = Trough(data.data(), dataIndex, t2 - 5, t2 + 5); + if(t2 < 0) + return 0; + + float t3 = static_cast(t2 * 4) / 3; + t3 = Trough(data.data(), dataIndex, t3 - 4, t3 + 4); + if(t3 < 0) + return static_cast(60 * 24 * 3) / static_cast(t2); + + return static_cast(60 * 24 * 4) / static_cast(t3); +} + +void Ppg::SetOffset(uint16_t offset) { + this->offset = offset; + dataIndex = 0; +} diff --git a/src/components/heartrate/Ppg.h b/src/components/heartrate/Ppg.h new file mode 100644 index 00000000..846ade08 --- /dev/null +++ b/src/components/heartrate/Ppg.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#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 data; + size_t dataIndex = 0; + float offset; + Biquad hpf; + Ptagc agc; + Biquad lpf; + + + float ProcessHeartRate(); + }; + } +} \ No newline at end of file diff --git a/src/components/heartrate/Ptagc.cpp b/src/components/heartrate/Ptagc.cpp new file mode 100644 index 00000000..9302af97 --- /dev/null +++ b/src/components/heartrate/Ptagc.cpp @@ -0,0 +1,21 @@ +#include +#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; +} diff --git a/src/components/heartrate/Ptagc.h b/src/components/heartrate/Ptagc.h new file mode 100644 index 00000000..c20de4c0 --- /dev/null +++ b/src/components/heartrate/Ptagc.h @@ -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; + + }; + } +} diff --git a/src/displayapp/Apps.h b/src/displayapp/Apps.h index f5fb24d0..fcadf2a2 100644 --- a/src/displayapp/Apps.h +++ b/src/displayapp/Apps.h @@ -2,6 +2,6 @@ namespace Pinetime { 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}; } } diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 162d0226..b10b1e1a 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -1,5 +1,6 @@ #include "DisplayApp.h" #include +#include #include "components/battery/BatteryController.h" #include "components/ble/BleController.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::DateTime &dateTimeController, Drivers::WatchdogView &watchdog, System::SystemTask &systemTask, - Pinetime::Controllers::NotificationManager& notificationManager) : + Pinetime::Controllers::NotificationManager& notificationManager, + Pinetime::Controllers::HeartRateController& heartRateController) : lcd{lcd}, lvgl{lvgl}, batteryController{batteryController}, @@ -39,7 +41,8 @@ DisplayApp::DisplayApp(Drivers::St7789 &lcd, Components::LittleVgl &lvgl, Driver touchPanel{touchPanel}, currentScreen{new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager) }, systemTask{systemTask}, - notificationManager{notificationManager} { + notificationManager{notificationManager}, + heartRateController{heartRateController} { msgQueue = xQueueCreate(queueSize, itemSize); onClockApp = true; 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::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::HeartRate: currentScreen.reset(new Screens::HeartRate(this, heartRateController)); break; } nextApp = Apps::None; } diff --git a/src/displayapp/DisplayApp.h b/src/displayapp/DisplayApp.h index 25cd2813..da5a7b22 100644 --- a/src/displayapp/DisplayApp.h +++ b/src/displayapp/DisplayApp.h @@ -23,6 +23,7 @@ namespace Pinetime { class Ble; class DateTime; class NotificationManager; + class HeartRateController; } namespace System { @@ -42,7 +43,8 @@ namespace Pinetime { Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController, Drivers::WatchdogView &watchdog, System::SystemTask &systemTask, - Pinetime::Controllers::NotificationManager& notificationManager); + Pinetime::Controllers::NotificationManager& notificationManager, + Pinetime::Controllers::HeartRateController& heartRateController); void Start(); void PushMessage(Messages msg); @@ -87,6 +89,7 @@ namespace Pinetime { Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::FirmwareValidator validator; TouchModes touchMode = TouchModes::Gestures; + Pinetime::Controllers::HeartRateController& heartRateController; }; } } diff --git a/src/displayapp/screens/ApplicationList.cpp b/src/displayapp/screens/ApplicationList.cpp index 0b8face3..57fbde20 100644 --- a/src/displayapp/screens/ApplicationList.cpp +++ b/src/displayapp/screens/ApplicationList.cpp @@ -45,7 +45,7 @@ std::unique_ptr ApplicationList::CreateScreen1() { {Symbols::sun, Apps::Brightness}, {Symbols::list, Apps::SysInfo}, {Symbols::check, Apps::FirmwareValidation}, - {Symbols::none, Apps::None} + {Symbols::heartBeat, Apps::HeartRate} } diff --git a/src/displayapp/screens/HeartRate.cpp b/src/displayapp/screens/HeartRate.cpp new file mode 100644 index 00000000..2d25f89c --- /dev/null +++ b/src/displayapp/screens/HeartRate.cpp @@ -0,0 +1,76 @@ +#include +#include "HeartRate.h" +#include + +#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_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; +} diff --git a/src/displayapp/screens/HeartRate.h b/src/displayapp/screens/HeartRate.h new file mode 100644 index 00000000..41fd5bb3 --- /dev/null +++ b/src/displayapp/screens/HeartRate.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include "Screen.h" +#include +#include +#include + +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; + + }; + } + } +} diff --git a/src/drivers/Hrs3300.cpp b/src/drivers/Hrs3300.cpp new file mode 100644 index 00000000..31956575 --- /dev/null +++ b/src/drivers/Hrs3300.cpp @@ -0,0 +1,108 @@ +#include +#include +#include "Hrs3300.h" + +#include +#include +#include + +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(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(Registers::PDriver), 0x6E); + + // HRS and ALS both in 16-bit mode + WriteRegister(static_cast(Registers::Res), 0x88); + + // 64x gain + WriteRegister(static_cast(Registers::Hgain), 0x10); +} + +void Hrs3300::Enable() { + NRF_LOG_INFO("ENABLE"); + auto value = ReadRegister(static_cast(Registers::Enable)); + value |= 0x80; + WriteRegister(static_cast(Registers::Enable), value); +} + +void Hrs3300::Disable() { + NRF_LOG_INFO("DISABLE"); + auto value = ReadRegister(static_cast(Registers::Enable)); + value &= ~0x80; + WriteRegister(static_cast(Registers::Enable), value); +} + +uint16_t Hrs3300::ReadHrs() { + auto m = ReadRegister(static_cast(Registers::C0DataM)); + auto h = ReadRegister(static_cast(Registers::C0DataH)); + auto l = ReadRegister(static_cast(Registers::C0dataL)); + return (m << 8) | ((h & 0x0f) << 4) | (l & 0x0f) | ((l & 0x30) << 12); +} + +uint16_t Hrs3300::ReadAls() { + auto m = ReadRegister(static_cast(Registers::C1dataM)); + auto h = ReadRegister(static_cast(Registers::C1dataH)); + auto l = ReadRegister(static_cast(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(Registers::Hgain), hgain << 2); +} + +void Hrs3300::SetDrive(uint8_t drive) { + auto en = ReadRegister(static_cast(Registers::Enable)); + auto pd = ReadRegister(static_cast(Registers::PDriver)); + + en = (en & 0xf7) | ((drive & 2) << 2); + pd = (pd & 0xbf) | ((drive & 1) << 6); + + WriteRegister(static_cast(Registers::Enable), en); + WriteRegister(static_cast(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; +} + + + + + + + + + + + diff --git a/src/drivers/Hrs3300.h b/src/drivers/Hrs3300.h new file mode 100644 index 00000000..c34d55c6 --- /dev/null +++ b/src/drivers/Hrs3300.h @@ -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); + + }; + } +} diff --git a/src/heartratetask/HeartRateTask.cpp b/src/heartratetask/HeartRateTask.cpp new file mode 100644 index 00000000..5ce27452 --- /dev/null +++ b/src/heartratetask/HeartRateTask.cpp @@ -0,0 +1,89 @@ +#include "HeartRateTask.h" +#include +#include +#include + +using namespace Pinetime::Applications; + +HeartRateTask::HeartRateTask(Drivers::Hrs3300 &heartRateSensor, Controllers::HeartRateController& controller) : + heartRateSensor{heartRateSensor}, + controller{controller}, + ppg{static_cast(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(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; +} diff --git a/src/heartratetask/HeartRateTask.h b/src/heartratetask/HeartRateTask.h new file mode 100644 index 00000000..f142af1a --- /dev/null +++ b/src/heartratetask/HeartRateTask.h @@ -0,0 +1,43 @@ +#pragma once +#include +#include +#include +#include + + +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; + + }; + + } +} + diff --git a/src/main.cpp b/src/main.cpp index 35057045..3b993ee9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "components/battery/BatteryController.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 pinTwiSda = 6; static constexpr uint8_t touchPanelTwiAddress = 0x15; +static constexpr uint8_t heartRateSensorTwiAddress = 0x44; Pinetime::Drivers::SpiMaster spi{Pinetime::Drivers::SpiMaster::SpiModule::SPI0, { 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::Components::LittleVgl lvgl {lcd, touchPanel}; +Pinetime::Drivers::Hrs3300 heartRateSensor {twiMaster, heartRateSensorTwiAddress}; + TimerHandle_t debounceTimer; Pinetime::Controllers::Battery batteryController; @@ -237,7 +241,7 @@ int main(void) { debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback); systemTask.reset(new Pinetime::System::SystemTask(spi, lcd, spiNorFlash, twiMaster, touchPanel, lvgl, batteryController, bleController, - dateTimeController, notificationManager)); + dateTimeController, notificationManager, heartRateSensor)); systemTask->Start(); nimble_port_init(); diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 9cd2f5e9..1d405621 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -21,6 +21,7 @@ #include "drivers/SpiMaster.h" #include "drivers/SpiNorFlash.h" #include "drivers/TwiMaster.h" +#include "drivers/Hrs3300.h" #include "main.h" using namespace Pinetime::System; @@ -39,12 +40,14 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController, - Pinetime::Controllers::NotificationManager& notificationManager) : + Pinetime::Controllers::NotificationManager& notificationManager, + Pinetime::Drivers::Hrs3300& heartRateSensor) : spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash}, twiMaster{twiMaster}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, bleController{bleController}, dateTimeController{dateTimeController}, 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); } @@ -77,12 +80,19 @@ void SystemTask::Work() { batteryController.Init(); displayApp.reset(new Pinetime::Applications::DisplayApp(lcd, lvgl, touchPanel, batteryController, bleController, - dateTimeController, watchdogView, *this, notificationManager)); + dateTimeController, watchdogView, *this, notificationManager, heartRateController)); displayApp->Start(); batteryController.Update(); 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_output(15); nrf_gpio_pin_set(15); diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index fe6e7cbe..b93d0bce 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include "SystemMonitor.h" #include "components/battery/BatteryController.h" @@ -20,6 +22,7 @@ namespace Pinetime { class SpiNorFlash; class St7789; class TwiMaster; + class Hrs3300; } namespace System { class SystemTask { @@ -34,7 +37,8 @@ namespace Pinetime { Components::LittleVgl &lvgl, Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController, - Pinetime::Controllers::NotificationManager& manager); + Pinetime::Controllers::NotificationManager& manager, + Pinetime::Drivers::Hrs3300& heartRateSensor); void Start(); @@ -58,6 +62,9 @@ namespace Pinetime { Pinetime::Components::LittleVgl& lvgl; Pinetime::Controllers::Battery& batteryController; std::unique_ptr displayApp; + Pinetime::Controllers::HeartRateController heartRateController; + std::unique_ptr heartRateApp; + Pinetime::Controllers::Ble& bleController; Pinetime::Controllers::DateTime& dateTimeController; QueueHandle_t systemTasksMsgQueue; @@ -68,7 +75,7 @@ namespace Pinetime { Pinetime::Drivers::WatchdogView watchdogView; Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::NimbleController nimbleController; - + Pinetime::Drivers::Hrs3300& heartRateSensor; static constexpr uint8_t pinSpiSck = 2; static constexpr uint8_t pinSpiMosi = 3;