Repository navigation
Expand file tree
/
Copy pathCountdownModule.cpp
More file actions
390 lines (314 loc) · 11.1 KB
/
Copy pathCountdownModule.cpp
File metadata and controls
390 lines (314 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "CountdownModule.hpp"
#include "webconfig.hpp"
#include "MultiLogger.hpp"
#include <Arduino.h>
CountdownModule::CountdownModule(U8G2_FOR_ADAFRUIT_GFX& u8g2_ref, GFXcanvas16& canvas_ref,
const GeneralTimeConverter& timeConverter_ref, DeviceConfig* config)
: u8g2(u8g2_ref), canvas(canvas_ref), _currentCanvas(&canvas_ref),
timeConverter(timeConverter_ref), config(config) {
}
CountdownModule::~CountdownModule() {
stopCountdown();
}
void CountdownModule::onUpdate(std::function<void()> callback) {
updateCallback = callback;
}
void CountdownModule::setDuration(uint32_t durationMinutes) {
this->_durationMinutes = durationMinutes > 0 ? durationMinutes : 15; // Default to 15 minutes
Log.printf("[Countdown] Duration set to %d minutes (non-persistent)\n", _durationMinutes);
}
bool CountdownModule::isEnabled() {
// Countdown is always available as a utility function
return true;
}
unsigned long CountdownModule::getDisplayDuration() {
// If countdown is running, return 0 for continuous display
if (_isRunning) {
return 0; // Continuous display - module controls its own lifetime
}
// When not running, don't show in rotation
return 0;
}
void CountdownModule::resetPaging() {
_isFinished = false;
_blinkPhase = 0;
// Release interrupt if active
if (_hasActiveInterrupt && _interruptUID > 0) {
releasePriorityEx(_interruptUID);
_hasActiveInterrupt = false;
Log.println("[Countdown] Released interrupt on reset");
}
}
bool CountdownModule::startCountdown(uint32_t durationMinutes) {
if (_isRunning && !_isPaused) {
Log.println("[Countdown] Already running, ignoring start request");
return false;
}
// Override duration if specified
if (durationMinutes > 0) {
_durationMinutes = durationMinutes;
}
_isRunning = true;
_isPaused = false;
_startTimeMillis = millis();
_totalPausedMs = 0;
_pausedTimeMillis = 0;
_targetDurationMs = (unsigned long)_durationMinutes * 60000UL;
_isFinished = false;
_blinkPhase = 0;
Log.printf("[Countdown] Started: duration=%d minutes (%lu ms)\n", _durationMinutes, _targetDurationMs);
// Request high priority interrupt to show countdown immediately
_interruptUID = COUNTDOWN_INTERRUPT_UID_BASE + 1;
_hasActiveInterrupt = true;
// Use total duration + 1 second buffer for interrupt timeout
unsigned long interruptDuration = _targetDurationMs + 1000;
if (requestPriorityEx(Priority::High, _interruptUID, interruptDuration)) {
Log.printf("[Countdown] Interrupt requested with UID %u for %lu ms\n", _interruptUID, interruptDuration);
} else {
Log.println("[Countdown] Interrupt request failed");
_hasActiveInterrupt = false;
}
if (updateCallback) {
updateCallback();
}
return true;
}
void CountdownModule::stopCountdown() {
if (!_isRunning) {
return;
}
_isRunning = false;
_isPaused = false;
_isFinished = true;
Log.println("[Countdown] Stopped");
// Release interrupt if active
if (_hasActiveInterrupt && _interruptUID > 0) {
releasePriorityEx(_interruptUID);
_hasActiveInterrupt = false;
Log.println("[Countdown] Released interrupt on stop");
}
if (updateCallback) {
updateCallback();
}
}
bool CountdownModule::pauseCountdown() {
if (!_isRunning || _isPaused) {
Log.println("[Countdown] Cannot pause - not running or already paused");
return false;
}
_isPaused = true;
_pausedTimeMillis = millis();
Log.println("[Countdown] Paused");
if (updateCallback) {
updateCallback();
}
return true;
}
bool CountdownModule::resumeCountdown() {
if (!_isRunning || !_isPaused) {
Log.println("[Countdown] Cannot resume - not running or not paused");
return false;
}
// Add the paused duration to total paused time
_totalPausedMs += (millis() - _pausedTimeMillis);
_isPaused = false;
_pausedTimeMillis = 0;
Log.printf("[Countdown] Resumed (total paused: %lu ms)\n", _totalPausedMs);
if (updateCallback) {
updateCallback();
}
return true;
}
void CountdownModule::resetCountdown() {
if (!_isRunning) {
Log.println("[Countdown] Cannot reset - not running");
return;
}
// Reset to start with same duration
_startTimeMillis = millis();
_totalPausedMs = 0;
_pausedTimeMillis = 0;
_isPaused = false;
_isFinished = false;
_blinkPhase = 0;
Log.println("[Countdown] Reset to start");
if (updateCallback) {
updateCallback();
}
}
void CountdownModule::tick() {
// Animation tick for blinking effects
_blinkPhase = (_blinkPhase + 1) % 20; // Blink cycle every 20 ticks
if (_isRunning && !_isPaused && updateCallback) {
updateCallback(); // Update display for millisecond precision
}
}
void CountdownModule::logicTick() {
// Check if countdown has finished (only if not paused)
if (_isRunning && !_isPaused) {
unsigned long elapsed = millis() - _startTimeMillis - _totalPausedMs;
if (elapsed >= _targetDurationMs) {
Log.println("[Countdown] Finished!");
stopCountdown();
}
}
}
void CountdownModule::periodicTick() {
// Nothing to do in periodic tick
}
void CountdownModule::calculateRemainingTime(unsigned long& minutes, unsigned long& seconds, unsigned long& millis) const {
if (!_isRunning) {
minutes = 0;
seconds = 0;
millis = 0;
return;
}
// Calculate elapsed time, accounting for paused time
unsigned long elapsed;
if (_isPaused) {
// When paused, use the time at pause point
elapsed = _pausedTimeMillis - _startTimeMillis - _totalPausedMs;
} else {
elapsed = ::millis() - _startTimeMillis - _totalPausedMs;
}
if (elapsed >= _targetDurationMs) {
minutes = 0;
seconds = 0;
millis = 0;
return;
}
unsigned long remaining = _targetDurationMs - elapsed;
minutes = remaining / 60000;
seconds = (remaining % 60000) / 1000;
millis = remaining % 1000;
}
float CountdownModule::calculatePercentComplete() const {
if (!_isRunning || _targetDurationMs == 0) {
return 0.0f;
}
// Calculate elapsed time, accounting for paused time
unsigned long elapsed;
if (_isPaused) {
elapsed = _pausedTimeMillis - _startTimeMillis - _totalPausedMs;
} else {
elapsed = ::millis() - _startTimeMillis - _totalPausedMs;
}
if (elapsed >= _targetDurationMs) {
return 100.0f;
}
return (float)elapsed / (float)_targetDurationMs * 100.0f;
}
float CountdownModule::calculateCaloriesBurned() const {
if (!_isRunning) {
return 0.0f;
}
// Calculate elapsed time, accounting for paused time
unsigned long elapsed;
if (_isPaused) {
elapsed = _pausedTimeMillis - _startTimeMillis - _totalPausedMs;
} else {
elapsed = ::millis() - _startTimeMillis - _totalPausedMs;
}
if (elapsed >= _targetDurationMs) {
elapsed = _targetDurationMs;
}
// Convert to minutes and multiply by calories per minute
float minutes = (float)elapsed / 60000.0f;
return minutes * CALORIES_PER_MINUTE;
}
void CountdownModule::draw() {
// Choose the correct canvas based on fullscreen mode
_currentCanvas = wantsFullscreen() ? _fullscreenCanvas : &canvas;
if (!_currentCanvas) {
Log.println("[Countdown] draw() - No valid canvas!");
return;
}
_currentCanvas->fillScreen(0);
u8g2.begin(*_currentCanvas);
if (!_isRunning) {
// Show "Countdown stopped" message
u8g2.setFont(u8g2_font_profont12_tf);
u8g2.setForegroundColor(0xFFFF);
const char* msg = "Countdown stopped";
int msgWidth = u8g2.getUTF8Width(msg);
u8g2.setCursor((_currentCanvas->width() - msgWidth) / 2, _currentCanvas->height() / 2);
u8g2.print(msg);
return;
}
drawCountdown();
}
void CountdownModule::drawCountdown() {
// Calculate values
unsigned long mins, secs, ms;
calculateRemainingTime(mins, secs, ms);
float percent = calculatePercentComplete();
float calories = calculateCaloriesBurned();
int y = 20; // Starting Y position
// Line 1: Title "COUNTDOWN" or "PAUSED"
u8g2.setFont(u8g2_font_profont12_tf);
const char* title;
if (_isPaused) {
// Show PAUSED in yellow
u8g2.setForegroundColor(0xFFE0); // Yellow
title = "PAUSED";
} else if (mins == 0 && secs < 10) {
// Blink red/white when less than 10 seconds
if (_blinkPhase < 10) {
u8g2.setForegroundColor(0xF800); // Red
} else {
u8g2.setForegroundColor(0xFFFF); // White
}
title = "COUNTDOWN";
} else {
u8g2.setForegroundColor(0x07FF); // Cyan
title = "COUNTDOWN";
}
int titleWidth = u8g2.getUTF8Width(title);
u8g2.setCursor((_currentCanvas->width() - titleWidth) / 2, y);
u8g2.print(title);
y += 18;
// Line 2: Time remaining (MM:SS.mmm)
u8g2.setFont(u8g2_font_profont15_tf);
u8g2.setForegroundColor(0xFFFF); // White
char timeStr[16];
snprintf(timeStr, sizeof(timeStr), "%02lu:%02lu.%03lu", mins, secs, ms);
int timeWidth = u8g2.getUTF8Width(timeStr);
u8g2.setCursor((_currentCanvas->width() - timeWidth) / 2, y);
u8g2.print(timeStr);
y += 14;
// Line 3: Percentage
u8g2.setFont(u8g2_font_profont10_tf);
u8g2.setForegroundColor(0xFFE0); // Yellow
char percentStr[16];
snprintf(percentStr, sizeof(percentStr), "%.1f%%", percent);
int percentWidth = u8g2.getUTF8Width(percentStr);
u8g2.setCursor((_currentCanvas->width() - percentWidth) / 2, y);
u8g2.print(percentStr);
y += 12;
// Line 4: Percentage bar
drawPercentageBar(percent);
y += 12;
// Line 5: Calories burned
u8g2.setFont(u8g2_font_profont10_tf);
u8g2.setForegroundColor(0x07E0); // Green
char caloriesStr[32];
snprintf(caloriesStr, sizeof(caloriesStr), "%.1f kcal", calories);
int caloriesWidth = u8g2.getUTF8Width(caloriesStr);
u8g2.setCursor((_currentCanvas->width() - caloriesWidth) / 2, y);
u8g2.print(caloriesStr);
}
void CountdownModule::drawPercentageBar(float percent) {
if (!_currentCanvas) return;
const int BAR_WIDTH = _currentCanvas->width() - 20; // 10px margin on each side
const int BAR_HEIGHT = 8;
const int BAR_X = 10;
const int BAR_Y = _currentCanvas->height() - 20;
// Draw border
_currentCanvas->drawRect(BAR_X, BAR_Y, BAR_WIDTH, BAR_HEIGHT, 0xFFFF); // White border
// Draw filled portion
int fillWidth = (int)((float)BAR_WIDTH * percent / 100.0f);
if (fillWidth > BAR_WIDTH - 2) fillWidth = BAR_WIDTH - 2; // Don't overflow border
if (fillWidth > 0) {
_currentCanvas->fillRect(BAR_X + 1, BAR_Y + 1, fillWidth, BAR_HEIGHT - 2, 0x07E0); // Green fill
}
}