Skip to content

Commit 1b72a3b

Browse files
committed
timer: Refactor ringing state management
Consolidate timer ringing logic and use Timer component as single source of truth for expired state.
1 parent 1d98a9a commit 1b72a3b

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

src/displayapp/DisplayApp.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ void DisplayApp::Refresh() {
380380
lv_disp_trig_activity(nullptr);
381381
auto* timerScreen = static_cast<Screens::Timer*>(currentScreen.get());
382382
timerScreen->SetTimerRinging();
383-
motorController.StartRinging();
384383
break;
385384
}
386385
case Messages::AlarmTriggered:

src/displayapp/screens/Timer.cpp

Lines changed: 27 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,15 @@ 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+
wakeLock.Release();
87+
timer.ResetExpiredTime();
88+
}
89+
7990
lv_obj_clean(lv_scr_act());
8091
}
8192

@@ -106,20 +117,20 @@ void Timer::UpdateMask() {
106117
}
107118

108119
void Timer::Refresh() {
109-
if (isRinging) {
120+
auto timerStatus = timer.GetTimerState();
121+
122+
if (timerStatus && timerStatus->expired) {
123+
// Timer exists and has expired, so we're in ringing mode
110124
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-
}
125+
126+
if (timerStatus->distanceToExpiry.count() > 10000 && motorController.IsRinging()) {
127+
// Stop buzzing after 10 seconds, but continue the counter
128+
motorController.StopRinging();
129+
wakeLock.Release();
120130
}
131+
121132
// Reset timer after 1 minute
122-
if (displaySeconds.Get().count() > 60) {
133+
if (timerStatus->distanceToExpiry.count() > 60000) {
123134
Reset();
124135
}
125136
} else if (timer.IsRunning()) {
@@ -153,23 +164,24 @@ void Timer::SetTimerRunning() {
153164
}
154165

155166
void Timer::SetTimerStopped() {
156-
isRinging = false;
157167
minuteCounter.ShowControls();
158168
secondCounter.ShowControls();
159169
lv_label_set_text_static(txtPlayPause, "Start");
160170
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GREEN);
161171
}
162172

163173
void Timer::SetTimerRinging() {
164-
isRinging = true;
174+
motorController.StartRinging();
175+
wakeLock.Lock();
165176
minuteCounter.HideControls();
166177
secondCounter.HideControls();
167178
lv_label_set_text_static(txtPlayPause, "Reset");
168179
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
169180
}
170181

171182
void Timer::ToggleRunning() {
172-
if (isRinging) {
183+
auto timerStatus = timer.GetTimerState();
184+
if (timerStatus && timerStatus->expired) {
173185
motorController.StopRinging();
174186
Reset();
175187
} 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)