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

alarm: Long press to stop alarm #2222

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
49 changes: 45 additions & 4 deletions src/displayapp/screens/Alarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ Alarm::Alarm(Controllers::AlarmController& alarmController,
lv_label_set_text_static(colonLabel, ":");
lv_obj_align(colonLabel, lv_scr_act(), LV_ALIGN_CENTER, 0, -29);

progressStop = lv_bar_create(lv_scr_act(), nullptr);
lv_bar_set_range(progressStop, 0, 240);
lv_bar_set_value(progressStop, 0, LV_ANIM_OFF);
lv_obj_set_size(progressStop, 240, 50);
lv_obj_align(progressStop, nullptr, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_style_local_bg_color(progressStop, LV_BAR_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_ORANGE);

btnStop = lv_btn_create(lv_scr_act(), nullptr);
btnStop->user_data = this;
lv_obj_set_event_cb(btnStop, btnEventHandler);
Expand Down Expand Up @@ -122,12 +129,27 @@ Alarm::Alarm(Controllers::AlarmController& alarmController,
} else {
SetSwitchState(LV_ANIM_OFF);
}

taskRefresh = lv_task_create(RefreshTaskCallback, 50, LV_TASK_PRIO_MID, this);
}

void Alarm::Refresh() {
if (stopBtnPressing && xTaskGetTickCount() > stopBtnPressTime + pdMS_TO_TICKS(150)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xTaskGetTickCount() - stopBtnPressTime > pdMS_TO_TICKS(150) is tolerant to tick count wraparound
Should we start showing the bar earlier than 150ms, as you need to hold the button to dismiss it. In fact, would it be better with no delay at all? A long press isn't super intuitive, so giving immediate feedback will make it clearer that holding it is needed IMO

stopPosition += 15;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we calculate elapsed ticks with xTaskGetTickCount() - stopBtnPressTime, I think it'd be better to calculate the bar position using this. So if we want to hold for say 1 second, we can do something like elapsedTicks * 1000 / configTICK_RATE to get the number of milliseconds the button has been held, and then draw the progress appropriately / stop alerting when more than 1000 milliseconds has passed.

if (stopPosition > 240) {
ResetStopProgress();
StopAlerting();
} else {
UpdateStopProgress();
}
}
}

Alarm::~Alarm() {
if (alarmController.IsAlerting()) {
StopAlerting();
}
lv_task_del(taskRefresh);
lv_obj_clean(lv_scr_act());
alarmController.SaveAlarm();
}
Expand All @@ -139,12 +161,31 @@ void Alarm::DisableAlarm() {
}
}

void Alarm::StopButtonPressed() {
stopBtnPressTime = xTaskGetTickCount();
stopBtnPressing = true;
}

void Alarm::ResetStopProgress() {
stopBtnPressing = false;
stopPosition = 0;
UpdateStopProgress();
}

void Alarm::UpdateStopProgress() {
lv_bar_set_value(progressStop, stopPosition, LV_ANIM_OFF);
}

void Alarm::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
if (event == LV_EVENT_CLICKED) {
if (obj == btnStop) {
StopAlerting();
return;
if (obj == btnStop) {
if (event == LV_EVENT_PRESSED) {
StopButtonPressed();
} else if (event == LV_EVENT_RELEASED || event == LV_EVENT_PRESS_LOST) {
ResetStopProgress();
}
return;
}
if (event == LV_EVENT_CLICKED) {
if (obj == btnInfo) {
ShowInfo();
return;
Expand Down
10 changes: 10 additions & 0 deletions src/displayapp/screens/Alarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ namespace Pinetime {
System::SystemTask& systemTask,
Controllers::MotorController& motorController);
~Alarm() override;
void Refresh() override;
void SetAlerting();
void OnButtonEvent(lv_obj_t* obj, lv_event_t event);
bool OnButtonPushed() override;
bool OnTouchEvent(TouchEvents event) override;
void OnValueChanged();
void StopAlerting();
void StopButtonPressed();
void ResetStopProgress();

private:
Controllers::AlarmController& alarmController;
Expand All @@ -51,6 +54,7 @@ namespace Pinetime {
lv_obj_t* lblampm = nullptr;
lv_obj_t* txtMessage = nullptr;
lv_obj_t* btnMessage = nullptr;
lv_task_t* taskRefresh = nullptr;
lv_task_t* taskStopAlarm = nullptr;

enum class EnableButtonState { On, Off, Alerting };
Expand All @@ -62,8 +66,14 @@ namespace Pinetime {
void HideInfo();
void ToggleRecurrence();
void UpdateAlarmTime();
void UpdateStopProgress();
Widgets::Counter hourCounter = Widgets::Counter(0, 23, jetbrains_mono_76);
Widgets::Counter minuteCounter = Widgets::Counter(0, 59, jetbrains_mono_76);

lv_obj_t* progressStop;
bool stopBtnPressing = false;
TickType_t stopBtnPressTime = 0;
lv_coord_t stopPosition = 0;
};
}

Expand Down