@@ -60,12 +60,20 @@ Timer::Timer(Controllers::Timer& timerController) : timer {timerController} {
60
60
lv_obj_set_size (btnPlayPause, LV_HOR_RES, 50 );
61
61
62
62
txtPlayPause = lv_label_create (lv_scr_act (), nullptr );
63
- lv_obj_align (txtPlayPause, btnPlayPause, LV_ALIGN_CENTER, 0 , 0 );
64
63
65
- if (timer.IsRunning ()) {
66
- SetTimerRunning ();
67
- } else {
68
- SetTimerStopped ();
64
+ switch (timer.GetState ()) {
65
+ case Controllers::Timer::TimerState::Stopped:
66
+ SetCounters (timer.GetLast ());
67
+ SetInterfaceStopped ();
68
+ break ;
69
+ case Controllers::Timer::TimerState::Running:
70
+ SetCounters (timer.GetTimeRemaining ());
71
+ SetInterfaceRunning ();
72
+ break ;
73
+ case Controllers::Timer::TimerState::Paused:
74
+ SetCounters (timer.GetTimeRemaining ());
75
+ SetInterfacePaused ();
76
+ break ;
69
77
}
70
78
71
79
taskRefresh = lv_task_create (RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this );
@@ -76,22 +84,52 @@ Timer::~Timer() {
76
84
lv_obj_clean (lv_scr_act ());
77
85
}
78
86
87
+ void Timer::SetButtonText (const char * text) {
88
+ lv_label_set_text_static (txtPlayPause, text);
89
+ lv_obj_align (txtPlayPause, btnPlayPause, LV_ALIGN_CENTER, 0 , 0 );
90
+ }
91
+
79
92
void Timer::ButtonPressed () {
80
93
pressTime = xTaskGetTickCount ();
81
94
buttonPressing = true ;
82
95
}
83
96
84
97
void Timer::MaskReset () {
85
98
buttonPressing = false ;
99
+
86
100
// A click event is processed before a release event,
87
101
// so the release event would override the "Pause" text without this check
88
- if (!timer.IsRunning ()) {
89
- lv_label_set_text_static (txtPlayPause, " Start" );
102
+ switch (timer.GetState ()) {
103
+ case Controllers::Timer::TimerState::Stopped:
104
+ SetButtonText (" Start" );
105
+ break ;
106
+ case Controllers::Timer::TimerState::Running:
107
+ SetButtonText (" Pause" );
108
+ break ;
109
+ case Controllers::Timer::TimerState::Paused:
110
+ SetButtonText (" Resume" );
111
+ break ;
90
112
}
113
+
91
114
maskPosition = 0 ;
92
115
UpdateMask ();
93
116
}
94
117
118
+ void Timer::HandleHold () {
119
+ if (timer.GetState () == Controllers::Timer::TimerState::Stopped) {
120
+ SetButtonText (" Reset" );
121
+ } else {
122
+ SetButtonText (" Stop" );
123
+ }
124
+
125
+ maskPosition += 15 ;
126
+ if (maskPosition > 240 ) {
127
+ HandleLongPress ();
128
+ } else {
129
+ UpdateMask ();
130
+ }
131
+ }
132
+
95
133
void Timer::UpdateMask () {
96
134
lv_draw_mask_line_param_t maskLine;
97
135
@@ -102,52 +140,78 @@ void Timer::UpdateMask() {
102
140
lv_objmask_update_mask (btnObjectMask, btnMask, &maskLine);
103
141
}
104
142
143
+ void Timer::HandleLongPress () {
144
+ if (timer.GetState () == Controllers::Timer::TimerState::Stopped) {
145
+ Reset ();
146
+ } else {
147
+ Stop ();
148
+ }
149
+
150
+ MaskReset ();
151
+ }
152
+
105
153
void Timer::Refresh () {
106
- if (timer.IsRunning () ) {
154
+ if (timer.GetState () == Controllers::Timer::TimerState::Running ) {
107
155
auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining ());
108
- minuteCounter.SetValue (secondsRemaining.count () / 60 );
109
- secondCounter.SetValue (secondsRemaining.count () % 60 );
110
- } else if (buttonPressing && xTaskGetTickCount () > pressTime + pdMS_TO_TICKS (150 )) {
111
- lv_label_set_text_static (txtPlayPause, " Reset" );
112
- maskPosition += 15 ;
113
- if (maskPosition > 240 ) {
114
- MaskReset ();
115
- Reset ();
116
- } else {
117
- UpdateMask ();
156
+ SetCounters (secondsRemaining);
157
+ } else {
158
+ if (buttonPressing && xTaskGetTickCount () > pressTime + pdMS_TO_TICKS (150 )) {
159
+ HandleHold ();
118
160
}
119
161
}
120
162
}
121
163
122
- void Timer::SetTimerRunning () {
164
+ void Timer::SetInterfaceRunning () {
165
+ minuteCounter.HideControls ();
166
+ secondCounter.HideControls ();
167
+ SetButtonText (" Pause" );
168
+ }
169
+
170
+ void Timer::SetInterfacePaused () {
123
171
minuteCounter.HideControls ();
124
172
secondCounter.HideControls ();
125
- lv_label_set_text_static (txtPlayPause, " Pause " );
173
+ SetButtonText ( " Resume " );
126
174
}
127
175
128
- void Timer::SetTimerStopped () {
176
+ void Timer::SetInterfaceStopped () {
129
177
minuteCounter.ShowControls ();
130
178
secondCounter.ShowControls ();
131
- lv_label_set_text_static (txtPlayPause, " Start" );
179
+ SetButtonText (" Start" );
180
+ }
181
+
182
+ void Timer::SetCounters (const std::chrono::milliseconds& duration) {
183
+ SetCounters (std::chrono::duration_cast<std::chrono::seconds>(duration));
184
+ }
185
+
186
+ void Timer::SetCounters (const std::chrono::seconds& duration) {
187
+ minuteCounter.SetValue (duration.count () / 60 );
188
+ secondCounter.SetValue (duration.count () % 60 );
132
189
}
133
190
134
191
void Timer::ToggleRunning () {
135
- if (timer.IsRunning ()) {
136
- auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timer.GetTimeRemaining ());
137
- minuteCounter.SetValue (secondsRemaining.count () / 60 );
138
- secondCounter.SetValue (secondsRemaining.count () % 60 );
139
- timer.StopTimer ();
140
- SetTimerStopped ();
141
- } else if (secondCounter.GetValue () + minuteCounter.GetValue () > 0 ) {
142
- auto timerDuration = std::chrono::minutes (minuteCounter.GetValue ()) + std::chrono::seconds (secondCounter.GetValue ());
143
- timer.StartTimer (timerDuration);
144
- Refresh ();
145
- SetTimerRunning ();
192
+ if (timer.GetState () == Controllers::Timer::TimerState::Stopped) {
193
+ if (secondCounter.GetValue () + minuteCounter.GetValue () > 0 ) {
194
+ auto timerDuration = std::chrono::minutes (minuteCounter.GetValue ()) + std::chrono::seconds (secondCounter.GetValue ());
195
+ timer.Start (timerDuration);
196
+ Refresh ();
197
+ SetInterfaceRunning ();
198
+ }
199
+ } else if (timer.GetState () == Controllers::Timer::TimerState::Running) {
200
+ timer.Pause ();
201
+ SetInterfacePaused ();
202
+ } else { // Paused
203
+ timer.Resume ();
204
+ SetInterfaceRunning ();
146
205
}
147
206
}
148
207
149
208
void Timer::Reset () {
150
- minuteCounter.SetValue (0 );
151
- secondCounter.SetValue (0 );
152
- SetTimerStopped ();
209
+ timer.ResetLast ();
210
+ Stop ();
211
+ }
212
+
213
+ void Timer::Stop () {
214
+ timer.Stop ();
215
+ SetCounters (timer.GetLast ());
216
+ SetInterfaceStopped ();
153
217
}
0 commit comments