Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved alarm alert handling #945

Merged
merged 1 commit into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
currentScreen = std::make_unique<Screens::Timer>(this, timerController);
break;
case Apps::Alarm:
currentScreen = std::make_unique<Screens::Alarm>(this, alarmController, settingsController);
currentScreen = std::make_unique<Screens::Alarm>(this, alarmController, settingsController, *systemTask);
break;

// Settings
Expand Down
39 changes: 32 additions & 7 deletions src/displayapp/screens/Alarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Alarm*>(obj->user_data);
auto* screen = static_cast<Alarm*>(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<Alarm*>(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);
Expand Down Expand Up @@ -122,17 +130,16 @@ Alarm::Alarm(DisplayApp* app, Controllers::AlarmController& alarmController, Pin
}

Alarm::~Alarm() {
if (alarmController.State() == AlarmController::AlarmState::Alerting) {
StopAlerting();
}
lv_obj_clean(lv_scr_act());
}

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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 8 additions & 2 deletions src/displayapp/screens/Alarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down