Skip to content

Commit 66b5977

Browse files
vkarehJF002
authored andcommitted
timer: Refactor ringing state management
Consolidate timer ringing logic and use Timer component as single source of truth for expired state.
1 parent a4918c0 commit 66b5977

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

src/displayapp/DisplayApp.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,15 @@ void DisplayApp::Refresh() {
372372
if (state != States::Running) {
373373
PushMessageToSystemTask(System::Messages::GoToRunning);
374374
}
375+
lv_disp_trig_activity(nullptr);
375376
// Load timer app if not loaded
376377
if (currentApp != Apps::Timer) {
377378
LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::Up);
379+
} else {
380+
// Set the timer to ringing mode if already loaded
381+
auto* timerScreen = static_cast<Screens::Timer*>(currentScreen.get());
382+
timerScreen->SetTimerRinging();
378383
}
379-
// Set the timer to ringing mode
380-
lv_disp_trig_activity(nullptr);
381-
auto* timerScreen = static_cast<Screens::Timer*>(currentScreen.get());
382-
timerScreen->SetTimerRinging();
383-
motorController.StartRinging();
384384
break;
385385
}
386386
case Messages::AlarmTriggered:

src/displayapp/screens/Timer.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ Timer::Timer(Controllers::Timer& timerController, Controllers::MotorController&
6363
// Create the label as a child of the button so it stays centered by default
6464
txtPlayPause = lv_label_create(btnPlayPause, nullptr);
6565

66-
if (motorController.IsRinging()) {
66+
auto timerStatus = timer.GetTimerState();
67+
68+
if (timerStatus && timerStatus->expired) {
6769
SetTimerRinging();
6870
} else if (timer.IsRunning()) {
6971
SetTimerRunning();
@@ -76,6 +78,14 @@ Timer::Timer(Controllers::Timer& timerController, Controllers::MotorController&
7678

7779
Timer::~Timer() {
7880
lv_task_del(taskRefresh);
81+
82+
// If timer has expired, reset it when leaving the screen
83+
auto timerStatus = timer.GetTimerState();
84+
if (timerStatus && timerStatus->expired) {
85+
motorController.StopRinging();
86+
timer.ResetExpiredTime();
87+
}
88+
7989
lv_obj_clean(lv_scr_act());
8090
}
8191

@@ -106,20 +116,20 @@ void Timer::UpdateMask() {
106116
}
107117

108118
void Timer::Refresh() {
109-
if (isRinging) {
119+
auto timerStatus = timer.GetTimerState();
120+
121+
if (timerStatus && timerStatus->expired) {
122+
// Timer exists and has expired, so we're in ringing mode
110123
DisplayTime();
111-
if (motorController.IsRinging()) {
112-
if (displaySeconds.Get().count() > 10) {
113-
// Stop buzzing after 10 seconds, but continue the counter
114-
motorController.StopRinging();
115-
wakeLock.Release();
116-
} else {
117-
// Keep the screen awake during the first 10 seconds
118-
wakeLock.Lock();
119-
}
124+
125+
if (timerStatus->distanceToExpiry.count() > 10000 && motorController.IsRinging()) {
126+
// Stop buzzing after 10 seconds, but continue the counter
127+
motorController.StopRinging();
128+
wakeLock.Release();
120129
}
130+
121131
// Reset timer after 1 minute
122-
if (displaySeconds.Get().count() > 60) {
132+
if (timerStatus->distanceToExpiry.count() > 60000) {
123133
Reset();
124134
}
125135
} else if (timer.IsRunning()) {
@@ -153,23 +163,24 @@ void Timer::SetTimerRunning() {
153163
}
154164

155165
void Timer::SetTimerStopped() {
156-
isRinging = false;
157166
minuteCounter.ShowControls();
158167
secondCounter.ShowControls();
159168
lv_label_set_text_static(txtPlayPause, "Start");
160169
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
161170
}
162171

163172
void Timer::SetTimerRinging() {
164-
isRinging = true;
173+
motorController.StartRinging();
174+
wakeLock.Lock();
165175
minuteCounter.HideControls();
166176
secondCounter.HideControls();
167177
lv_label_set_text_static(txtPlayPause, "Reset");
168178
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
169179
}
170180

171181
void Timer::ToggleRunning() {
172-
if (isRinging) {
182+
auto timerStatus = timer.GetTimerState();
183+
if (timerStatus && timerStatus->expired) {
173184
motorController.StopRinging();
174185
Reset();
175186
} else if (timer.IsRunning()) {

src/displayapp/screens/Timer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ namespace Pinetime::Applications {
4848
Widgets::Counter secondCounter = Widgets::Counter(0, 59, jetbrains_mono_76);
4949

5050
bool buttonPressing = false;
51-
bool isRinging = false;
5251
lv_coord_t maskPosition = 0;
5352
TickType_t pressTime = 0;
5453
Utility::DirtyValue<std::chrono::seconds> displaySeconds;

0 commit comments

Comments
 (0)