From 4f649a85448fc79b55202d9db0e8f4c8828975e3 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Tue, 18 Jan 2022 19:08:03 +0200 Subject: [PATCH] Improved alarm alert handling --- src/displayapp/DisplayApp.cpp | 2 +- src/displayapp/screens/Alarm.cpp | 39 ++++++++++++++++++++++++++------ src/displayapp/screens/Alarm.h | 10 ++++++-- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 672bcb34..495969b3 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -379,7 +379,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) currentScreen = std::make_unique(this, timerController); break; case Apps::Alarm: - currentScreen = std::make_unique(this, alarmController, settingsController); + currentScreen = std::make_unique(this, alarmController, settingsController, *systemTask); break; // Settings diff --git a/src/displayapp/screens/Alarm.cpp b/src/displayapp/screens/Alarm.cpp index a2f534df..879e50d8 100644 --- a/src/displayapp/screens/Alarm.cpp +++ b/src/displayapp/screens/Alarm.cpp @@ -23,12 +23,20 @@ using namespace Pinetime::Applications::Screens; using Pinetime::Controllers::AlarmController; static void btnEventHandler(lv_obj_t* obj, lv_event_t event) { - Alarm* screen = static_cast(obj->user_data); + auto* screen = static_cast(obj->user_data); screen->OnButtonEvent(obj, event); } -Alarm::Alarm(DisplayApp* app, Controllers::AlarmController& alarmController, Pinetime::Controllers::Settings& settingsController) - : Screen(app), alarmController {alarmController}, settingsController {settingsController} { +static void StopAlarmTaskCallback(lv_task_t* task) { + auto* screen = static_cast(task->user_data); + screen->StopAlerting(); +} + +Alarm::Alarm(DisplayApp* app, + Controllers::AlarmController& alarmController, + Pinetime::Controllers::Settings& settingsController, + System::SystemTask& systemTask) + : Screen(app), alarmController {alarmController}, settingsController {settingsController}, systemTask {systemTask} { time = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76); @@ -122,6 +130,9 @@ Alarm::Alarm(DisplayApp* app, Controllers::AlarmController& alarmController, Pin } Alarm::~Alarm() { + if (alarmController.State() == AlarmController::AlarmState::Alerting) { + StopAlerting(); + } lv_obj_clean(lv_scr_act()); } @@ -129,10 +140,6 @@ void Alarm::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { using Pinetime::Controllers::AlarmController; if (event == LV_EVENT_CLICKED) { if (obj == btnStop) { - if (alarmController.State() == AlarmController::AlarmState::Alerting) { - alarmController.StopAlerting(); - } - SetSwitchState(LV_ANIM_OFF); StopAlerting(); return; } @@ -205,9 +212,18 @@ bool Alarm::OnButtonPushed() { HideInfo(); return true; } + if (alarmController.State() == AlarmController::AlarmState::Alerting) { + StopAlerting(); + return true; + } return false; } +bool Alarm::OnTouchEvent(Pinetime::Applications::TouchEvents event) { + // Don't allow closing the screen by swiping while the alarm is alerting + return alarmController.State() == AlarmController::AlarmState::Alerting && event == TouchEvents::SwipeDown; +} + void Alarm::UpdateAlarmTime() { if (settingsController.GetClockType() == Controllers::Settings::ClockType::H12) { switch (alarmHours) { @@ -237,9 +253,18 @@ void Alarm::UpdateAlarmTime() { void Alarm::SetAlerting() { lv_obj_set_hidden(enableSwitch, true); lv_obj_set_hidden(btnStop, false); + taskStopAlarm = lv_task_create(StopAlarmTaskCallback, pdMS_TO_TICKS(60 * 1000), LV_TASK_PRIO_MID, this); + systemTask.PushMessage(System::Messages::DisableSleeping); } void Alarm::StopAlerting() { + alarmController.StopAlerting(); + SetSwitchState(LV_ANIM_OFF); + if (taskStopAlarm != nullptr) { + lv_task_del(taskStopAlarm); + taskStopAlarm = nullptr; + } + systemTask.PushMessage(System::Messages::EnableSleeping); lv_obj_set_hidden(enableSwitch, false); lv_obj_set_hidden(btnStop, true); } diff --git a/src/displayapp/screens/Alarm.h b/src/displayapp/screens/Alarm.h index a7f35232..f74dd68e 100644 --- a/src/displayapp/screens/Alarm.h +++ b/src/displayapp/screens/Alarm.h @@ -27,28 +27,34 @@ namespace Pinetime { namespace Screens { class Alarm : public Screen { public: - Alarm(DisplayApp* app, Controllers::AlarmController& alarmController, Pinetime::Controllers::Settings& settingsController); + Alarm(DisplayApp* app, + Controllers::AlarmController& alarmController, + Pinetime::Controllers::Settings& settingsController, + System::SystemTask& systemTask); ~Alarm() override; void SetAlerting(); void OnButtonEvent(lv_obj_t* obj, lv_event_t event); bool OnButtonPushed() override; + bool OnTouchEvent(TouchEvents event) override; + void StopAlerting(); private: uint8_t alarmHours; uint8_t alarmMinutes; Controllers::AlarmController& alarmController; Controllers::Settings& settingsController; + System::SystemTask& systemTask; lv_obj_t *time, *lblampm, *btnStop, *txtStop, *btnMinutesUp, *btnMinutesDown, *btnHoursUp, *btnHoursDown, *txtMinUp, *txtMinDown, *txtHrUp, *txtHrDown, *btnRecur, *txtRecur, *btnInfo, *txtInfo, *enableSwitch; lv_obj_t* txtMessage = nullptr; lv_obj_t* btnMessage = nullptr; + lv_task_t* taskStopAlarm = nullptr; enum class EnableButtonState { On, Off, Alerting }; void SetRecurButtonState(); void SetSwitchState(lv_anim_enable_t anim); void SetAlarm(); - void StopAlerting(); void ShowInfo(); void HideInfo(); void ToggleRecurrence();