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/DisplayApp.cpp | 10 +++++----- src/displayapp/screens/Timer.cpp | 41 +++++++++++++++++++++++++--------------- src/displayapp/screens/Timer.h | 1 - 3 files changed, 31 insertions(+), 21 deletions(-) (limited to 'src/displayapp') diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 85e0ad56..35330fb7 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -372,15 +372,15 @@ void DisplayApp::Refresh() { if (state != States::Running) { PushMessageToSystemTask(System::Messages::GoToRunning); } + lv_disp_trig_activity(nullptr); // Load timer app if not loaded if (currentApp != Apps::Timer) { LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::Up); + } else { + // Set the timer to ringing mode if already loaded + auto* timerScreen = static_cast(currentScreen.get()); + timerScreen->SetTimerRinging(); } - // Set the timer to ringing mode - lv_disp_trig_activity(nullptr); - auto* timerScreen = static_cast(currentScreen.get()); - timerScreen->SetTimerRinging(); - motorController.StartRinging(); break; } case Messages::AlarmTriggered: 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()) { diff --git a/src/displayapp/screens/Timer.h b/src/displayapp/screens/Timer.h index 63ca2456..651c7f0d 100644 --- a/src/displayapp/screens/Timer.h +++ b/src/displayapp/screens/Timer.h @@ -48,7 +48,6 @@ namespace Pinetime::Applications { Widgets::Counter secondCounter = Widgets::Counter(0, 59, jetbrains_mono_76); bool buttonPressing = false; - bool isRinging = false; lv_coord_t maskPosition = 0; TickType_t pressTime = 0; Utility::DirtyValue displaySeconds; -- cgit v1.2.3-70-g09d2