diff options
Diffstat (limited to 'src/displayapp/screens/Timer.cpp')
| -rw-r--r-- | src/displayapp/screens/Timer.cpp | 99 |
1 files changed, 63 insertions, 36 deletions
diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index 5cd496c..332fcef 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -1,4 +1,5 @@ #include "displayapp/screens/Timer.h" + #include "displayapp/screens/Screen.h" #include "displayapp/screens/Symbols.h" #include <lvgl/lvgl.h> @@ -6,48 +7,62 @@ using namespace Pinetime::Applications::Screens; static void btnEventHandler(lv_obj_t* obj, lv_event_t event) { - auto* screen = static_cast<Timer*>(obj->user_data); + Timer* screen = static_cast<Timer*>(obj->user_data); screen->OnButtonEvent(obj, event); } -void Timer::CreateButtons() { +void Timer::createButtons() { btnMinutesUp = lv_btn_create(lv_scr_act(), nullptr); btnMinutesUp->user_data = this; lv_obj_set_event_cb(btnMinutesUp, btnEventHandler); - lv_obj_align(btnMinutesUp, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, -80); - lv_obj_set_height(btnMinutesUp, 40); - lv_obj_set_width(btnMinutesUp, 60); + lv_obj_set_size(btnMinutesUp, 60, 40); + lv_obj_align(btnMinutesUp, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, -85); txtMUp = lv_label_create(btnMinutesUp, nullptr); lv_label_set_text(txtMUp, "+"); btnMinutesDown = lv_btn_create(lv_scr_act(), nullptr); btnMinutesDown->user_data = this; lv_obj_set_event_cb(btnMinutesDown, btnEventHandler); - lv_obj_align(btnMinutesDown, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, +40); - lv_obj_set_height(btnMinutesDown, 40); - lv_obj_set_width(btnMinutesDown, 60); + lv_obj_set_size(btnMinutesDown, 60, 40); + lv_obj_align(btnMinutesDown, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, +35); txtMDown = lv_label_create(btnMinutesDown, nullptr); lv_label_set_text(txtMDown, "-"); btnSecondsUp = lv_btn_create(lv_scr_act(), nullptr); btnSecondsUp->user_data = this; lv_obj_set_event_cb(btnSecondsUp, btnEventHandler); - lv_obj_align(btnSecondsUp, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, 10, -80); - lv_obj_set_height(btnSecondsUp, 40); - lv_obj_set_width(btnSecondsUp, 60); + lv_obj_set_size(btnSecondsUp, 60, 40); + lv_obj_align(btnSecondsUp, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, -85); txtSUp = lv_label_create(btnSecondsUp, nullptr); lv_label_set_text(txtSUp, "+"); btnSecondsDown = lv_btn_create(lv_scr_act(), nullptr); btnSecondsDown->user_data = this; lv_obj_set_event_cb(btnSecondsDown, btnEventHandler); - lv_obj_align(btnSecondsDown, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, 10, +40); - lv_obj_set_height(btnSecondsDown, 40); - lv_obj_set_width(btnSecondsDown, 60); + lv_obj_set_size(btnSecondsDown, 60, 40); + lv_obj_align(btnSecondsDown, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, +35); txtSDown = lv_label_create(btnSecondsDown, nullptr); lv_label_set_text(txtSDown, "-"); } +void Timer::stop() { + int32_t secondsRemaining = timerController.GetSecondsRemaining(); + if (timerController.IsOvertime()) { + minutesToSet = 0; + secondsToSet = 0; + secondsRemaining = 0; + } else { + minutesToSet = secondsRemaining / 60; + secondsToSet = secondsRemaining % 60; + } + timerController.StopTimer(); + timerController.StopAlerting(); + lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); + lv_label_set_text_fmt(time, "%02lu:%02lu", secondsRemaining / 60, secondsRemaining % 60); + lv_label_set_text(txtPlayPause, Symbols::play); + createButtons(); +} + Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) : Screen(app), running {true}, timerController {timerController} { backgroundLabel = lv_label_create(lv_scr_act(), nullptr); @@ -58,38 +73,58 @@ Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) lv_label_set_text(backgroundLabel, ""); 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); - lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); + lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &neofont3); + + int32_t seconds = timerController.GetSecondsRemaining(); + bool overtime = timerController.IsOvertime(); + + if (overtime) { + seconds = -seconds + 1; // "+ 1" is to not show -00:00 again after +00:00 + lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED); + } else { + lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); + } - uint32_t seconds = timerController.GetTimeRemaining() / 1000; lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60); - lv_obj_align(time, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 0, -20); + lv_obj_align(time, lv_scr_act(), LV_ALIGN_CENTER, 0, -20); btnPlayPause = lv_btn_create(lv_scr_act(), nullptr); btnPlayPause->user_data = this; lv_obj_set_event_cb(btnPlayPause, btnEventHandler); - lv_obj_align(btnPlayPause, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, -10); - lv_obj_set_height(btnPlayPause, 40); + lv_obj_set_size(btnPlayPause, 115, 50); + lv_obj_align(btnPlayPause, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0); txtPlayPause = lv_label_create(btnPlayPause, nullptr); if (timerController.IsRunning()) { - lv_label_set_text(txtPlayPause, Symbols::pause); + lv_label_set_text(txtPlayPause, overtime ? Symbols::stop : Symbols::pause); } else { lv_label_set_text(txtPlayPause, Symbols::play); - CreateButtons(); + createButtons(); } - taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); } Timer::~Timer() { lv_task_del(taskRefresh); lv_obj_clean(lv_scr_act()); + if (timerController.IsRunning() && timerController.IsOvertime()) { + timerController.StopTimer(); + timerController.StopAlerting(); + } } void Timer::Refresh() { if (timerController.IsRunning()) { - uint32_t seconds = timerController.GetTimeRemaining() / 1000; + int32_t seconds = timerController.GetSecondsRemaining(); + if (timerController.IsOvertime()) { + seconds = -seconds + 1; // "+ 1" is to not show -00:00 again after +00:00 + + // safety measures, lets not overflow counter as it will display badly + if (seconds >= 100 * 60) { + stop(); + return; + } + } lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60); } } @@ -98,17 +133,12 @@ void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { if (event == LV_EVENT_CLICKED) { if (obj == btnPlayPause) { if (timerController.IsRunning()) { - lv_label_set_text(txtPlayPause, Symbols::play); - uint32_t seconds = timerController.GetTimeRemaining() / 1000; - minutesToSet = seconds / 60; - secondsToSet = seconds % 60; - timerController.StopTimer(); - CreateButtons(); - + stop(); } else if (secondsToSet + minutesToSet > 0) { lv_label_set_text(txtPlayPause, Symbols::pause); timerController.StartTimer((secondsToSet + minutesToSet * 60) * 1000); + // inlined destroyButtons() lv_obj_del(btnSecondsDown); btnSecondsDown = nullptr; lv_obj_del(btnSecondsUp); @@ -158,9 +188,6 @@ void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { } void Timer::SetDone() { - lv_label_set_text(time, "00:00"); - lv_label_set_text(txtPlayPause, Symbols::play); - secondsToSet = 0; - minutesToSet = 0; - CreateButtons(); + lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED); + lv_label_set_text(txtPlayPause, Symbols::stop); } |
