aboutsummaryrefslogtreecommitdiffstats
path: root/src/displayapp/screens/Timer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/displayapp/screens/Timer.cpp')
-rw-r--r--src/displayapp/screens/Timer.cpp37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp
index df78a5a0..31cde733 100644
--- a/src/displayapp/screens/Timer.cpp
+++ b/src/displayapp/screens/Timer.cpp
@@ -17,7 +17,7 @@ static void btnEventHandler(lv_obj_t* obj, lv_event_t event) {
}
}
-Timer::Timer(Controllers::TimerController& timerController) : timerController {timerController} {
+Timer::Timer(Controllers::Timer& timerController) : timer {timerController} {
lv_obj_t* colonLabel = lv_label_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_text_font(colonLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76);
@@ -59,10 +59,10 @@ Timer::Timer(Controllers::TimerController& timerController) : timerController {t
lv_obj_set_event_cb(btnPlayPause, btnEventHandler);
lv_obj_set_size(btnPlayPause, LV_HOR_RES, 50);
- txtPlayPause = lv_label_create(lv_scr_act(), nullptr);
- lv_obj_align(txtPlayPause, btnPlayPause, LV_ALIGN_CENTER, 0, 0);
+ // Create the label as a child of the button so it stays centered by default
+ txtPlayPause = lv_label_create(btnPlayPause, nullptr);
- if (timerController.IsRunning()) {
+ if (timer.IsRunning()) {
SetTimerRunning();
} else {
SetTimerStopped();
@@ -85,7 +85,7 @@ void Timer::MaskReset() {
buttonPressing = false;
// A click event is processed before a release event,
// so the release event would override the "Pause" text without this check
- if (!timerController.IsRunning()) {
+ if (!timer.IsRunning()) {
lv_label_set_text_static(txtPlayPause, "Start");
}
maskPosition = 0;
@@ -103,10 +103,8 @@ void Timer::UpdateMask() {
}
void Timer::Refresh() {
- if (timerController.IsRunning()) {
- auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timerController.GetTimeRemaining());
- minuteCounter.SetValue(secondsRemaining.count() / 60);
- secondCounter.SetValue(secondsRemaining.count() % 60);
+ if (timer.IsRunning()) {
+ DisplayTime();
} else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) {
lv_label_set_text_static(txtPlayPause, "Reset");
maskPosition += 15;
@@ -119,6 +117,14 @@ void Timer::Refresh() {
}
}
+void Timer::DisplayTime() {
+ displaySeconds = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining());
+ if (displaySeconds.IsUpdated()) {
+ minuteCounter.SetValue(displaySeconds.Get().count() / 60);
+ secondCounter.SetValue(displaySeconds.Get().count() % 60);
+ }
+}
+
void Timer::SetTimerRunning() {
minuteCounter.HideControls();
secondCounter.HideControls();
@@ -132,22 +138,19 @@ void Timer::SetTimerStopped() {
}
void Timer::ToggleRunning() {
- if (timerController.IsRunning()) {
- auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timerController.GetTimeRemaining());
- minuteCounter.SetValue(secondsRemaining.count() / 60);
- secondCounter.SetValue(secondsRemaining.count() % 60);
- timerController.StopTimer();
+ if (timer.IsRunning()) {
+ DisplayTime();
+ timer.StopTimer();
SetTimerStopped();
} else if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) {
auto timerDuration = std::chrono::minutes(minuteCounter.GetValue()) + std::chrono::seconds(secondCounter.GetValue());
- timerController.StartTimer(timerDuration);
+ timer.StartTimer(timerDuration);
Refresh();
SetTimerRunning();
}
}
void Timer::Reset() {
- minuteCounter.SetValue(0);
- secondCounter.SetValue(0);
+ DisplayTime();
SetTimerStopped();
}