From 66b5977f39bf22e0641be43766439a3ed025d604 Mon Sep 17 00:00:00 2001 From: Victor Kareh Date: Tue, 18 Nov 2025 10:33:54 -0500 Subject: timer: Refactor ringing state management Consolidate timer ringing logic and use Timer component as single source of truth for expired state. --- src/displayapp/screens/Timer.cpp | 41 +++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'src/displayapp/screens/Timer.cpp') diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index 02c84160..749d9859 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -63,7 +63,9 @@ Timer::Timer(Controllers::Timer& timerController, Controllers::MotorController& // Create the label as a child of the button so it stays centered by default txtPlayPause = lv_label_create(btnPlayPause, nullptr); - if (motorController.IsRinging()) { + auto timerStatus = timer.GetTimerState(); + + if (timerStatus && timerStatus->expired) { SetTimerRinging(); } else if (timer.IsRunning()) { SetTimerRunning(); @@ -76,6 +78,14 @@ Timer::Timer(Controllers::Timer& timerController, Controllers::MotorController& Timer::~Timer() { lv_task_del(taskRefresh); + + // If timer has expired, reset it when leaving the screen + auto timerStatus = timer.GetTimerState(); + if (timerStatus && timerStatus->expired) { + motorController.StopRinging(); + timer.ResetExpiredTime(); + } + lv_obj_clean(lv_scr_act()); } @@ -106,20 +116,20 @@ void Timer::UpdateMask() { } void Timer::Refresh() { - if (isRinging) { + auto timerStatus = timer.GetTimerState(); + + if (timerStatus && timerStatus->expired) { + // Timer exists and has expired, so we're in ringing mode DisplayTime(); - if (motorController.IsRinging()) { - if (displaySeconds.Get().count() > 10) { - // Stop buzzing after 10 seconds, but continue the counter - motorController.StopRinging(); - wakeLock.Release(); - } else { - // Keep the screen awake during the first 10 seconds - wakeLock.Lock(); - } + + if (timerStatus->distanceToExpiry.count() > 10000 && motorController.IsRinging()) { + // Stop buzzing after 10 seconds, but continue the counter + motorController.StopRinging(); + wakeLock.Release(); } + // Reset timer after 1 minute - if (displaySeconds.Get().count() > 60) { + if (timerStatus->distanceToExpiry.count() > 60000) { Reset(); } } else if (timer.IsRunning()) { @@ -153,7 +163,6 @@ void Timer::SetTimerRunning() { } void Timer::SetTimerStopped() { - isRinging = false; minuteCounter.ShowControls(); secondCounter.ShowControls(); lv_label_set_text_static(txtPlayPause, "Start"); @@ -161,7 +170,8 @@ void Timer::SetTimerStopped() { } void Timer::SetTimerRinging() { - isRinging = true; + motorController.StartRinging(); + wakeLock.Lock(); minuteCounter.HideControls(); secondCounter.HideControls(); lv_label_set_text_static(txtPlayPause, "Reset"); @@ -169,7 +179,8 @@ void Timer::SetTimerRinging() { } void Timer::ToggleRunning() { - if (isRinging) { + auto timerStatus = timer.GetTimerState(); + if (timerStatus && timerStatus->expired) { motorController.StopRinging(); Reset(); } else if (timer.IsRunning()) { -- cgit v1.2.3-70-g09d2