From 04063cf0af29e09946bf3d1fa77ae91235cd8b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Sun, 17 Jan 2021 10:39:46 +0100 Subject: [PATCH] HR Sensor : Add start/stop button to the HeartRate app (the HR sensors stays ON when the app is closed), display the HR value on the Clock app. --- .../heartrate/HeartRateController.cpp | 4 +- .../heartrate/HeartRateController.h | 4 +- src/components/heartrate/Ppg.cpp | 4 ++ src/components/heartrate/Ppg.h | 1 + src/displayapp/DisplayApp.cpp | 4 +- src/displayapp/screens/Clock.cpp | 18 ++++-- src/displayapp/screens/Clock.h | 6 +- src/displayapp/screens/HeartRate.cpp | 61 +++++++++++++++---- src/displayapp/screens/HeartRate.h | 4 ++ src/heartratetask/HeartRateTask.cpp | 16 +++-- src/systemtask/SystemTask.cpp | 8 +-- src/systemtask/SystemTask.h | 3 +- 12 files changed, 98 insertions(+), 35 deletions(-) diff --git a/src/components/heartrate/HeartRateController.cpp b/src/components/heartrate/HeartRateController.cpp index 53b34e6c..62ce8aef 100644 --- a/src/components/heartrate/HeartRateController.cpp +++ b/src/components/heartrate/HeartRateController.cpp @@ -16,15 +16,15 @@ void HeartRateController::Update(HeartRateController::States newState, uint8_t h void HeartRateController::Start() { if(task != nullptr) { + state = States::NotEnoughData; task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StartMeasurement); - systemTask.PushMessage(System::SystemTask::Messages::HeartRateRunning); } } void HeartRateController::Stop() { if(task != nullptr) { + state = States::Stopped; task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StopMeasurement); - systemTask.PushMessage(System::SystemTask::Messages::HeartRateStopped); } } diff --git a/src/components/heartrate/HeartRateController.h b/src/components/heartrate/HeartRateController.h index b401f76a..bbb1b88b 100644 --- a/src/components/heartrate/HeartRateController.h +++ b/src/components/heartrate/HeartRateController.h @@ -12,7 +12,7 @@ namespace Pinetime { namespace Controllers { class HeartRateController { public: - enum class States { NotEnoughData, NoTouch, Running}; + enum class States { Stopped, NotEnoughData, NoTouch, Running}; explicit HeartRateController(System::SystemTask& systemTask); @@ -27,7 +27,7 @@ namespace Pinetime { private: System::SystemTask& systemTask; Applications::HeartRateTask* task = nullptr; - States state = States::NotEnoughData; + States state = States::Stopped; uint8_t heartRate = 0; }; } diff --git a/src/components/heartrate/Ppg.cpp b/src/components/heartrate/Ppg.cpp index c9a11030..27617c15 100644 --- a/src/components/heartrate/Ppg.cpp +++ b/src/components/heartrate/Ppg.cpp @@ -94,3 +94,7 @@ void Ppg::SetOffset(uint16_t offset) { this->offset = offset; dataIndex = 0; } + +void Ppg::Reset() { + dataIndex = 0; +} diff --git a/src/components/heartrate/Ppg.h b/src/components/heartrate/Ppg.h index 846ade08..747ae019 100644 --- a/src/components/heartrate/Ppg.h +++ b/src/components/heartrate/Ppg.h @@ -14,6 +14,7 @@ namespace Pinetime { float HeartRate(); void SetOffset(uint16_t i); + void Reset(); private: std::array data; diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index b10b1e1a..7ae9f819 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -39,7 +39,7 @@ DisplayApp::DisplayApp(Drivers::St7789 &lcd, Components::LittleVgl &lvgl, Driver dateTimeController{dateTimeController}, watchdog{watchdog}, touchPanel{touchPanel}, - currentScreen{new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager) }, + currentScreen{new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager, heartRateController) }, systemTask{systemTask}, notificationManager{notificationManager}, heartRateController{heartRateController} { @@ -200,7 +200,7 @@ void DisplayApp::RunningState() { case Apps::None: case Apps::Launcher: currentScreen.reset(new Screens::ApplicationList(this)); break; case Apps::Clock: - currentScreen.reset(new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager)); + currentScreen.reset(new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager, heartRateController)); onClockApp = true; break; // case Apps::Test: currentScreen.reset(new Screens::Message(this)); break; diff --git a/src/displayapp/screens/Clock.cpp b/src/displayapp/screens/Clock.cpp index 57659141..2988922e 100644 --- a/src/displayapp/screens/Clock.cpp +++ b/src/displayapp/screens/Clock.cpp @@ -10,6 +10,7 @@ #include "components/battery/BatteryController.h" #include "components/ble/BleController.h" #include "components/ble/NotificationManager.h" +#include "components/heartrate/HeartRateController.h" #include "../DisplayApp.h" using namespace Pinetime::Applications::Screens; @@ -26,9 +27,11 @@ Clock::Clock(DisplayApp* app, Controllers::DateTime& dateTimeController, Controllers::Battery& batteryController, Controllers::Ble& bleController, - Controllers::NotificationManager& notificatioManager) : Screen(app), currentDateTime{{}}, + Controllers::NotificationManager& notificatioManager, + Controllers::HeartRateController& heartRateController): Screen(app), currentDateTime{{}}, dateTimeController{dateTimeController}, batteryController{batteryController}, - bleController{bleController}, notificatioManager{notificatioManager} { + bleController{bleController}, notificatioManager{notificatioManager}, + heartRateController{heartRateController} { displayedChar[0] = 0; displayedChar[1] = 0; displayedChar[2] = 0; @@ -171,10 +174,15 @@ bool Clock::Refresh() { } } - // TODO heartbeat = heartBeatController.GetValue(); - if(heartbeat.IsUpdated()) { + heartbeat = heartRateController.HeartRate(); + heartbeatRunning = heartRateController.State() != Controllers::HeartRateController::States::Stopped; + if(heartbeat.IsUpdated() || heartbeatRunning.IsUpdated()) { char heartbeatBuffer[4]; - sprintf(heartbeatBuffer, "%d", heartbeat.Get()); + if(heartbeatRunning.Get()) + sprintf(heartbeatBuffer, "%d", heartbeat.Get()); + else + sprintf(heartbeatBuffer, "---"); + lv_label_set_text(heartbeatValue, heartbeatBuffer); lv_obj_align(heartbeatIcon, lv_scr_act(), LV_ALIGN_IN_BOTTOM_LEFT, 5, -2); lv_obj_align(heartbeatValue, heartbeatIcon, LV_ALIGN_OUT_RIGHT_MID, 5, 0); diff --git a/src/displayapp/screens/Clock.h b/src/displayapp/screens/Clock.h index 4c5f60a0..4d5be282 100644 --- a/src/displayapp/screens/Clock.h +++ b/src/displayapp/screens/Clock.h @@ -12,6 +12,7 @@ namespace Pinetime { class Battery; class Ble; class NotificationManager; + class HeartRateController; } namespace Applications { @@ -42,7 +43,8 @@ namespace Pinetime { Controllers::DateTime& dateTimeController, Controllers::Battery& batteryController, Controllers::Ble& bleController, - Controllers::NotificationManager& notificatioManager); + Controllers::NotificationManager& notificatioManager, + Controllers::HeartRateController& heartRateController); ~Clock() override; bool Refresh() override; @@ -67,6 +69,7 @@ namespace Pinetime { DirtyValue> currentDateTime; DirtyValue stepCount {0}; DirtyValue heartbeat {0}; + DirtyValue heartbeatRunning {false}; DirtyValue notificationState {false}; lv_obj_t* label_time; @@ -86,6 +89,7 @@ namespace Pinetime { Controllers::Battery& batteryController; Controllers::Ble& bleController; Controllers::NotificationManager& notificatioManager; + Controllers::HeartRateController& heartRateController; bool running = true; diff --git a/src/displayapp/screens/HeartRate.cpp b/src/displayapp/screens/HeartRate.cpp index 2d25f89c..d55ed019 100644 --- a/src/displayapp/screens/HeartRate.cpp +++ b/src/displayapp/screens/HeartRate.cpp @@ -8,13 +8,25 @@ 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..."; +namespace { + 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..."; + case Pinetime::Controllers::HeartRateController::States::Stopped: + return "Stopped"; + } + return ""; + } + + static void btnStartStopEventHandler(lv_obj_t *obj, lv_event_t event) { + HeartRate *screen = static_cast(obj->user_data); + screen->OnStartStopEvent(event); } - return ""; } HeartRate::HeartRate(Pinetime::Applications::DisplayApp *app, Controllers::HeartRateController& heartRateController) : Screen(app), heartRateController{heartRateController} { @@ -30,7 +42,7 @@ HeartRate::HeartRate(Pinetime::Applications::DisplayApp *app, Controllers::Heart 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_obj_align(label_hr, lv_scr_act(), LV_ALIGN_CENTER, -70, -40); lv_label_set_text(label_hr, "000"); lv_label_set_text(label_bpm, "Heart rate BPM"); @@ -40,13 +52,19 @@ HeartRate::HeartRate(Pinetime::Applications::DisplayApp *app, Controllers::Heart 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); + lv_obj_align(label_status, label_hr, LV_ALIGN_OUT_BOTTOM_MID, 0, 10); - heartRateController.Start(); + btn_startStop = lv_btn_create(lv_scr_act(), NULL); + btn_startStop->user_data = this; + lv_obj_set_height(btn_startStop, 50); + lv_obj_set_event_cb(btn_startStop, btnStartStopEventHandler); + lv_obj_align(btn_startStop, nullptr, LV_ALIGN_IN_BOTTOM_MID, 0, 0); + + label_startStop = lv_label_create(btn_startStop, nullptr); + UpdateStartStopButton(heartRateController.State() != Controllers::HeartRateController::States::Stopped); } HeartRate::~HeartRate() { - heartRateController.Stop(); lv_obj_clean(lv_scr_act()); } @@ -57,6 +75,7 @@ bool HeartRate::Refresh() { switch(state) { case Controllers::HeartRateController::States::NoTouch: case Controllers::HeartRateController::States::NotEnoughData: + case Controllers::HeartRateController::States::Stopped: lv_label_set_text(label_hr, "000"); break; default: @@ -65,7 +84,7 @@ bool HeartRate::Refresh() { } lv_label_set_text(label_status, ToString(state)); - lv_obj_align(label_status, label_hr, LV_ALIGN_OUT_BOTTOM_MID, 0, 20); + lv_obj_align(label_status, label_hr, LV_ALIGN_OUT_BOTTOM_MID, 0, 10); return running; } @@ -74,3 +93,23 @@ bool HeartRate::OnButtonPushed() { running = false; return true; } + +void HeartRate::OnStartStopEvent(lv_event_t event) { + if (event == LV_EVENT_CLICKED) { + if(heartRateController.State() == Controllers::HeartRateController::States::Stopped) { + heartRateController.Start(); + UpdateStartStopButton(heartRateController.State() != Controllers::HeartRateController::States::Stopped); + } + else { + heartRateController.Stop(); + UpdateStartStopButton(heartRateController.State() != Controllers::HeartRateController::States::Stopped); + } + } +} + +void HeartRate::UpdateStartStopButton(bool isRunning) { + if(isRunning) + lv_label_set_text(label_startStop, "Stop"); + else + lv_label_set_text(label_startStop, "Start"); +} diff --git a/src/displayapp/screens/HeartRate.h b/src/displayapp/screens/HeartRate.h index 41fd5bb3..b9424998 100644 --- a/src/displayapp/screens/HeartRate.h +++ b/src/displayapp/screens/HeartRate.h @@ -21,14 +21,18 @@ namespace Pinetime { bool Refresh() override; bool OnButtonPushed() override; + void OnStartStopEvent(lv_event_t event); private: Controllers::HeartRateController& heartRateController; + void UpdateStartStopButton(bool isRunning); lv_obj_t* label_hr; lv_obj_t* label_bpm; lv_obj_t* label_status; lv_style_t labelBigStyle; lv_style_t* labelStyle; + lv_obj_t* btn_startStop; + lv_obj_t* label_startStop; bool running = true; diff --git a/src/heartratetask/HeartRateTask.cpp b/src/heartratetask/HeartRateTask.cpp index 5ce27452..5a6d2a5e 100644 --- a/src/heartratetask/HeartRateTask.cpp +++ b/src/heartratetask/HeartRateTask.cpp @@ -42,14 +42,21 @@ void HeartRateTask::Work() { break; case Messages::WakeUp: state = States::Running; + if(measurementStarted) { + lastBpm = 0; + StartMeasurement(); + } break; case Messages::StartMeasurement: + if(measurementStarted) break; lastBpm = 0; StartMeasurement(); - ppg.SetOffset(heartRateSensor.ReadHrs()); + measurementStarted = true; break; case Messages::StopMeasurement: + if(!measurementStarted) break; StopMeasurement(); + measurementStarted = false; break; } } @@ -59,7 +66,7 @@ void HeartRateTask::Work() { ppg.Preprocess(hrs); auto bpm = ppg.HeartRate(); - if (lastBpm == 0 && bpm == 0) controller.Update(Controllers::HeartRateController::States::NoTouch, 0); + if (lastBpm == 0 && bpm == 0) controller.Update(Controllers::HeartRateController::States::NotEnoughData, 0); if(bpm != 0) { lastBpm = bpm; controller.Update(Controllers::HeartRateController::States::Running, lastBpm); @@ -80,10 +87,11 @@ void HeartRateTask::PushMessage(HeartRateTask::Messages msg) { void HeartRateTask::StartMeasurement() { heartRateSensor.Enable(); - measurementStarted = true; + vTaskDelay(100); + ppg.SetOffset(static_cast(heartRateSensor.ReadHrs())); } void HeartRateTask::StopMeasurement() { heartRateSensor.Disable(); - measurementStarted = false; + vTaskDelay(100); } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index e7551235..eedbcf59 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -141,6 +141,7 @@ void SystemTask::Work() { displayApp->PushMessage(Applications::DisplayApp::Messages::GoToRunning); displayApp->PushMessage(Applications::DisplayApp::Messages::UpdateBatteryLevel); + heartRateApp->PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp); isSleeping = false; isWakingUp = false; @@ -150,6 +151,7 @@ void SystemTask::Work() { NRF_LOG_INFO("[systemtask] Going to sleep"); xTimerStop(idleTimer, 0); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep); + heartRateApp->PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep); break; case Messages::OnNewTime: ReloadIdleTimer(); @@ -195,12 +197,6 @@ void SystemTask::Work() { isSleeping = true; isGoingToSleep = false; break; - case Messages::HeartRateRunning: - doNotGoToSleep = true; - break; - case Messages::HeartRateStopped: - doNotGoToSleep = false; - break; default: break; } } diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index e4eb8d47..b93d0bce 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -28,8 +28,7 @@ namespace Pinetime { class SystemTask { public: enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected, - BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping, - HeartRateRunning, HeartRateStopped + BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping }; SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,