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.

This commit is contained in:
Jean-François Milants 2021-01-17 10:39:46 +01:00
parent c82c22650c
commit 04063cf0af
12 changed files with 98 additions and 35 deletions

View File

@ -16,15 +16,15 @@ void HeartRateController::Update(HeartRateController::States newState, uint8_t h
void HeartRateController::Start() { void HeartRateController::Start() {
if(task != nullptr) { if(task != nullptr) {
state = States::NotEnoughData;
task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StartMeasurement); task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StartMeasurement);
systemTask.PushMessage(System::SystemTask::Messages::HeartRateRunning);
} }
} }
void HeartRateController::Stop() { void HeartRateController::Stop() {
if(task != nullptr) { if(task != nullptr) {
state = States::Stopped;
task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StopMeasurement); task->PushMessage(Pinetime::Applications::HeartRateTask::Messages::StopMeasurement);
systemTask.PushMessage(System::SystemTask::Messages::HeartRateStopped);
} }
} }

View File

@ -12,7 +12,7 @@ namespace Pinetime {
namespace Controllers { namespace Controllers {
class HeartRateController { class HeartRateController {
public: public:
enum class States { NotEnoughData, NoTouch, Running}; enum class States { Stopped, NotEnoughData, NoTouch, Running};
explicit HeartRateController(System::SystemTask& systemTask); explicit HeartRateController(System::SystemTask& systemTask);
@ -27,7 +27,7 @@ namespace Pinetime {
private: private:
System::SystemTask& systemTask; System::SystemTask& systemTask;
Applications::HeartRateTask* task = nullptr; Applications::HeartRateTask* task = nullptr;
States state = States::NotEnoughData; States state = States::Stopped;
uint8_t heartRate = 0; uint8_t heartRate = 0;
}; };
} }

View File

@ -94,3 +94,7 @@ void Ppg::SetOffset(uint16_t offset) {
this->offset = offset; this->offset = offset;
dataIndex = 0; dataIndex = 0;
} }
void Ppg::Reset() {
dataIndex = 0;
}

View File

@ -14,6 +14,7 @@ namespace Pinetime {
float HeartRate(); float HeartRate();
void SetOffset(uint16_t i); void SetOffset(uint16_t i);
void Reset();
private: private:
std::array<int, 200> data; std::array<int, 200> data;

View File

@ -39,7 +39,7 @@ DisplayApp::DisplayApp(Drivers::St7789 &lcd, Components::LittleVgl &lvgl, Driver
dateTimeController{dateTimeController}, dateTimeController{dateTimeController},
watchdog{watchdog}, watchdog{watchdog},
touchPanel{touchPanel}, touchPanel{touchPanel},
currentScreen{new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager) }, currentScreen{new Screens::Clock(this, dateTimeController, batteryController, bleController, notificationManager, heartRateController) },
systemTask{systemTask}, systemTask{systemTask},
notificationManager{notificationManager}, notificationManager{notificationManager},
heartRateController{heartRateController} { heartRateController{heartRateController} {
@ -200,7 +200,7 @@ void DisplayApp::RunningState() {
case Apps::None: case Apps::None:
case Apps::Launcher: currentScreen.reset(new Screens::ApplicationList(this)); break; case Apps::Launcher: currentScreen.reset(new Screens::ApplicationList(this)); break;
case Apps::Clock: 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; onClockApp = true;
break; break;
// case Apps::Test: currentScreen.reset(new Screens::Message(this)); break; // case Apps::Test: currentScreen.reset(new Screens::Message(this)); break;

View File

@ -10,6 +10,7 @@
#include "components/battery/BatteryController.h" #include "components/battery/BatteryController.h"
#include "components/ble/BleController.h" #include "components/ble/BleController.h"
#include "components/ble/NotificationManager.h" #include "components/ble/NotificationManager.h"
#include "components/heartrate/HeartRateController.h"
#include "../DisplayApp.h" #include "../DisplayApp.h"
using namespace Pinetime::Applications::Screens; using namespace Pinetime::Applications::Screens;
@ -26,9 +27,11 @@ Clock::Clock(DisplayApp* app,
Controllers::DateTime& dateTimeController, Controllers::DateTime& dateTimeController,
Controllers::Battery& batteryController, Controllers::Battery& batteryController,
Controllers::Ble& bleController, Controllers::Ble& bleController,
Controllers::NotificationManager& notificatioManager) : Screen(app), currentDateTime{{}}, Controllers::NotificationManager& notificatioManager,
Controllers::HeartRateController& heartRateController): Screen(app), currentDateTime{{}},
dateTimeController{dateTimeController}, batteryController{batteryController}, dateTimeController{dateTimeController}, batteryController{batteryController},
bleController{bleController}, notificatioManager{notificatioManager} { bleController{bleController}, notificatioManager{notificatioManager},
heartRateController{heartRateController} {
displayedChar[0] = 0; displayedChar[0] = 0;
displayedChar[1] = 0; displayedChar[1] = 0;
displayedChar[2] = 0; displayedChar[2] = 0;
@ -171,10 +174,15 @@ bool Clock::Refresh() {
} }
} }
// TODO heartbeat = heartBeatController.GetValue(); heartbeat = heartRateController.HeartRate();
if(heartbeat.IsUpdated()) { heartbeatRunning = heartRateController.State() != Controllers::HeartRateController::States::Stopped;
if(heartbeat.IsUpdated() || heartbeatRunning.IsUpdated()) {
char heartbeatBuffer[4]; 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_label_set_text(heartbeatValue, heartbeatBuffer);
lv_obj_align(heartbeatIcon, lv_scr_act(), LV_ALIGN_IN_BOTTOM_LEFT, 5, -2); 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); lv_obj_align(heartbeatValue, heartbeatIcon, LV_ALIGN_OUT_RIGHT_MID, 5, 0);

View File

@ -12,6 +12,7 @@ namespace Pinetime {
class Battery; class Battery;
class Ble; class Ble;
class NotificationManager; class NotificationManager;
class HeartRateController;
} }
namespace Applications { namespace Applications {
@ -42,7 +43,8 @@ namespace Pinetime {
Controllers::DateTime& dateTimeController, Controllers::DateTime& dateTimeController,
Controllers::Battery& batteryController, Controllers::Battery& batteryController,
Controllers::Ble& bleController, Controllers::Ble& bleController,
Controllers::NotificationManager& notificatioManager); Controllers::NotificationManager& notificatioManager,
Controllers::HeartRateController& heartRateController);
~Clock() override; ~Clock() override;
bool Refresh() override; bool Refresh() override;
@ -67,6 +69,7 @@ namespace Pinetime {
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime; DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime;
DirtyValue<uint32_t> stepCount {0}; DirtyValue<uint32_t> stepCount {0};
DirtyValue<uint8_t> heartbeat {0}; DirtyValue<uint8_t> heartbeat {0};
DirtyValue<bool> heartbeatRunning {false};
DirtyValue<bool> notificationState {false}; DirtyValue<bool> notificationState {false};
lv_obj_t* label_time; lv_obj_t* label_time;
@ -86,6 +89,7 @@ namespace Pinetime {
Controllers::Battery& batteryController; Controllers::Battery& batteryController;
Controllers::Ble& bleController; Controllers::Ble& bleController;
Controllers::NotificationManager& notificatioManager; Controllers::NotificationManager& notificatioManager;
Controllers::HeartRateController& heartRateController;
bool running = true; bool running = true;

View File

@ -8,13 +8,25 @@ using namespace Pinetime::Applications::Screens;
extern lv_font_t jetbrains_mono_extrabold_compressed; extern lv_font_t jetbrains_mono_extrabold_compressed;
extern lv_font_t jetbrains_mono_bold_20; extern lv_font_t jetbrains_mono_bold_20;
const char* ToString(Pinetime::Controllers::HeartRateController::States s) { namespace {
switch(s) { const char *ToString(Pinetime::Controllers::HeartRateController::States s) {
case Pinetime::Controllers::HeartRateController::States::NotEnoughData: return "Not enough data,\nplease wait..."; switch (s) {
case Pinetime::Controllers::HeartRateController::States::NoTouch: return "No touch detected"; case Pinetime::Controllers::HeartRateController::States::NotEnoughData:
case Pinetime::Controllers::HeartRateController::States::Running: return "Measuring..."; 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<HeartRate *>(obj->user_data);
screen->OnStartStopEvent(event);
} }
return "";
} }
HeartRate::HeartRate(Pinetime::Applications::DisplayApp *app, Controllers::HeartRateController& heartRateController) : Screen(app), heartRateController{heartRateController} { 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); label_hr = lv_label_create(lv_scr_act(), NULL);
lv_label_set_style(label_hr, LV_LABEL_STYLE_MAIN, &labelBigStyle); 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_hr, "000");
lv_label_set_text(label_bpm, "Heart rate BPM"); 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); label_status = lv_label_create(lv_scr_act(), NULL);
lv_label_set_text(label_status, ToString(Pinetime::Controllers::HeartRateController::States::NotEnoughData)); lv_label_set_text(label_status, ToString(Pinetime::Controllers::HeartRateController::States::NotEnoughData));
lv_label_set_style(label_status, LV_LABEL_STYLE_MAIN, labelStyle); 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() { HeartRate::~HeartRate() {
heartRateController.Stop();
lv_obj_clean(lv_scr_act()); lv_obj_clean(lv_scr_act());
} }
@ -57,6 +75,7 @@ bool HeartRate::Refresh() {
switch(state) { switch(state) {
case Controllers::HeartRateController::States::NoTouch: case Controllers::HeartRateController::States::NoTouch:
case Controllers::HeartRateController::States::NotEnoughData: case Controllers::HeartRateController::States::NotEnoughData:
case Controllers::HeartRateController::States::Stopped:
lv_label_set_text(label_hr, "000"); lv_label_set_text(label_hr, "000");
break; break;
default: default:
@ -65,7 +84,7 @@ bool HeartRate::Refresh() {
} }
lv_label_set_text(label_status, ToString(state)); 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; return running;
} }
@ -74,3 +93,23 @@ bool HeartRate::OnButtonPushed() {
running = false; running = false;
return true; 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");
}

View File

@ -21,14 +21,18 @@ namespace Pinetime {
bool Refresh() override; bool Refresh() override;
bool OnButtonPushed() override; bool OnButtonPushed() override;
void OnStartStopEvent(lv_event_t event);
private: private:
Controllers::HeartRateController& heartRateController; Controllers::HeartRateController& heartRateController;
void UpdateStartStopButton(bool isRunning);
lv_obj_t* label_hr; lv_obj_t* label_hr;
lv_obj_t* label_bpm; lv_obj_t* label_bpm;
lv_obj_t* label_status; lv_obj_t* label_status;
lv_style_t labelBigStyle; lv_style_t labelBigStyle;
lv_style_t* labelStyle; lv_style_t* labelStyle;
lv_obj_t* btn_startStop;
lv_obj_t* label_startStop;
bool running = true; bool running = true;

View File

@ -42,14 +42,21 @@ void HeartRateTask::Work() {
break; break;
case Messages::WakeUp: case Messages::WakeUp:
state = States::Running; state = States::Running;
if(measurementStarted) {
lastBpm = 0;
StartMeasurement();
}
break; break;
case Messages::StartMeasurement: case Messages::StartMeasurement:
if(measurementStarted) break;
lastBpm = 0; lastBpm = 0;
StartMeasurement(); StartMeasurement();
ppg.SetOffset(heartRateSensor.ReadHrs()); measurementStarted = true;
break; break;
case Messages::StopMeasurement: case Messages::StopMeasurement:
if(!measurementStarted) break;
StopMeasurement(); StopMeasurement();
measurementStarted = false;
break; break;
} }
} }
@ -59,7 +66,7 @@ void HeartRateTask::Work() {
ppg.Preprocess(hrs); ppg.Preprocess(hrs);
auto bpm = ppg.HeartRate(); 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) { if(bpm != 0) {
lastBpm = bpm; lastBpm = bpm;
controller.Update(Controllers::HeartRateController::States::Running, lastBpm); controller.Update(Controllers::HeartRateController::States::Running, lastBpm);
@ -80,10 +87,11 @@ void HeartRateTask::PushMessage(HeartRateTask::Messages msg) {
void HeartRateTask::StartMeasurement() { void HeartRateTask::StartMeasurement() {
heartRateSensor.Enable(); heartRateSensor.Enable();
measurementStarted = true; vTaskDelay(100);
ppg.SetOffset(static_cast<float>(heartRateSensor.ReadHrs()));
} }
void HeartRateTask::StopMeasurement() { void HeartRateTask::StopMeasurement() {
heartRateSensor.Disable(); heartRateSensor.Disable();
measurementStarted = false; vTaskDelay(100);
} }

View File

@ -141,6 +141,7 @@ void SystemTask::Work() {
displayApp->PushMessage(Applications::DisplayApp::Messages::GoToRunning); displayApp->PushMessage(Applications::DisplayApp::Messages::GoToRunning);
displayApp->PushMessage(Applications::DisplayApp::Messages::UpdateBatteryLevel); displayApp->PushMessage(Applications::DisplayApp::Messages::UpdateBatteryLevel);
heartRateApp->PushMessage(Pinetime::Applications::HeartRateTask::Messages::WakeUp);
isSleeping = false; isSleeping = false;
isWakingUp = false; isWakingUp = false;
@ -150,6 +151,7 @@ void SystemTask::Work() {
NRF_LOG_INFO("[systemtask] Going to sleep"); NRF_LOG_INFO("[systemtask] Going to sleep");
xTimerStop(idleTimer, 0); xTimerStop(idleTimer, 0);
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep); displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep);
heartRateApp->PushMessage(Pinetime::Applications::HeartRateTask::Messages::GoToSleep);
break; break;
case Messages::OnNewTime: case Messages::OnNewTime:
ReloadIdleTimer(); ReloadIdleTimer();
@ -195,12 +197,6 @@ void SystemTask::Work() {
isSleeping = true; isSleeping = true;
isGoingToSleep = false; isGoingToSleep = false;
break; break;
case Messages::HeartRateRunning:
doNotGoToSleep = true;
break;
case Messages::HeartRateStopped:
doNotGoToSleep = false;
break;
default: break; default: break;
} }
} }

View File

@ -28,8 +28,7 @@ namespace Pinetime {
class SystemTask { class SystemTask {
public: public:
enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected, enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected,
BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping, BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping
HeartRateRunning, HeartRateStopped
}; };
SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,