diff options
Diffstat (limited to 'src/displayapp/screens/Timer.cpp')
| -rw-r--r-- | src/displayapp/screens/Timer.cpp | 64 |
1 files changed, 47 insertions, 17 deletions
diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index 9ef620a..002c9e3 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -44,15 +44,42 @@ void Timer::CreateButtons() { lv_label_set_text_static(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} { 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_MAKE(0xb0, 0xb0, 0xb0)); - uint32_t seconds = timerController.GetTimeRemaining() / 1000; + 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); + } + lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60); + lv_obj_align(time, lv_scr_act(), LV_ALIGN_CENTER, 0, -25); btnPlayPause = lv_btn_create(lv_scr_act(), nullptr); @@ -62,23 +89,34 @@ Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) lv_obj_align(btnPlayPause, nullptr, LV_ALIGN_IN_BOTTOM_MID, 0, 0); txtPlayPause = lv_label_create(btnPlayPause, nullptr); if (timerController.IsRunning()) { - lv_label_set_text_static(txtPlayPause, Symbols::pause); + lv_label_set_text(txtPlayPause, overtime ? Symbols::stop : Symbols::pause); } else { lv_label_set_text_static(txtPlayPause, Symbols::play); 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(); + } } 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); } } @@ -87,17 +125,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_static(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_static(txtPlayPause, Symbols::pause); timerController.StartTimer((secondsToSet + minutesToSet * 60) * 1000); + // inlined destroyButtons() lv_obj_del(btnSecondsDown); btnSecondsDown = nullptr; lv_obj_del(btnSecondsUp); @@ -147,9 +180,6 @@ void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { } void Timer::SetDone() { - lv_label_set_text_static(time, "00:00"); - lv_label_set_text_static(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_static(txtPlayPause, Symbols::stop); } |
