1
0
Fork 0

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() {
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);
}
}

View file

@ -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;
};
}

View file

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

View file

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

View file

@ -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;

View file

@ -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);

View file

@ -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<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime;
DirtyValue<uint32_t> stepCount {0};
DirtyValue<uint8_t> heartbeat {0};
DirtyValue<bool> heartbeatRunning {false};
DirtyValue<bool> 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;

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_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<HeartRate *>(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");
}

View file

@ -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;

View file

@ -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<float>(heartRateSensor.ReadHrs()));
}
void HeartRateTask::StopMeasurement() {
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::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;
}
}

View file

@ -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,