-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimerLogic.cpp
More file actions
205 lines (173 loc) · 5.27 KB
/
Copy pathtimerLogic.cpp
File metadata and controls
205 lines (173 loc) · 5.27 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
#include "timerLogic.h"
#include <cstdio>
namespace timerlogic
{
const char *timerPhaseName(TimerPhase phase)
{
switch (phase) {
case TIMER_RUNNING: return "RUNNING";
case TIMER_PAUSED: return "PAUSED";
case TIMER_FINISHED: return "FINISHED";
default: return "READY";
}
}
uint64_t clampCountdownMs(int64_t ms)
{
if (ms < (int64_t)COUNTDOWN_MIN_MS) return COUNTDOWN_MIN_MS;
if (ms > (int64_t)COUNTDOWN_MAX_MS) return COUNTDOWN_MAX_MS;
return (uint64_t)ms;
}
bool reminderIntervalValid(int minutes)
{
return minutes >= REMINDER_MIN_MINUTES && minutes <= REMINDER_MAX_MINUTES;
}
/*-------- Stopwatch ----------*/
bool Stopwatch::start(uint64_t nowMs)
{
if (phase != TIMER_READY) return false;
accumMs = 0;
startMs = nowMs;
firedCount = 0;
phase = TIMER_RUNNING;
return true;
}
bool Stopwatch::pause(uint64_t nowMs)
{
if (phase != TIMER_RUNNING) return false;
accumMs += nowMs - startMs;
phase = TIMER_PAUSED;
return true;
}
bool Stopwatch::resume(uint64_t nowMs)
{
if (phase != TIMER_PAUSED) return false;
startMs = nowMs;
phase = TIMER_RUNNING;
return true;
}
void Stopwatch::reset()
{
phase = TIMER_READY;
accumMs = 0;
startMs = 0;
firedCount = 0;
}
uint64_t Stopwatch::elapsedMs(uint64_t nowMs) const
{
if (phase == TIMER_RUNNING) return accumMs + (nowMs - startMs);
return accumMs; // READY: 0, PAUSED: frozen
}
uint32_t Stopwatch::pollMilestoneMinutes(uint64_t nowMs, int intervalMinutes)
{
if (phase == TIMER_READY || !reminderIntervalValid(intervalMinutes)) return 0;
uint64_t intervalMs = (uint64_t)intervalMinutes * 60000ULL;
uint64_t k = elapsedMs(nowMs) / intervalMs;
// Interval changed mid-run (web settings): rebase so old boundaries are
// not replayed against the new spacing; the next NEW boundary fires.
if (intervalMinutes != firedIntervalMin) {
firedIntervalMin = intervalMinutes;
firedCount = k;
return 0;
}
if (k <= firedCount) return 0;
firedCount = k; // several boundaries skipped -> report only the latest
return (uint32_t)(k * (uint64_t)intervalMinutes);
}
/*-------- Countdown ----------*/
bool Countdown::setDurationMs(int64_t ms)
{
if (phase == TIMER_RUNNING || phase == TIMER_PAUSED) return false;
durationMs = clampCountdownMs(ms);
remainMs = durationMs;
phase = TIMER_READY; // an edit after FINISHED re-arms the timer
return true;
}
bool Countdown::adjustDurationMs(int64_t deltaMs)
{
return setDurationMs((int64_t)durationMs + deltaMs);
}
bool Countdown::start(uint64_t nowMs)
{
if (phase == TIMER_RUNNING || phase == TIMER_PAUSED) return false;
endMs = nowMs + durationMs;
remainMs = durationMs;
prevRemainMs = durationMs; // boundaries strictly below the start fire
phase = TIMER_RUNNING;
return true;
}
bool Countdown::pause(uint64_t nowMs)
{
if (phase != TIMER_RUNNING) return false;
remainMs = remainingMs(nowMs);
phase = TIMER_PAUSED;
return true;
}
bool Countdown::resume(uint64_t nowMs)
{
if (phase != TIMER_PAUSED) return false;
endMs = nowMs + remainMs;
phase = TIMER_RUNNING;
return true;
}
void Countdown::reset()
{
phase = TIMER_READY;
remainMs = durationMs;
endMs = 0;
prevRemainMs = 0;
}
uint64_t Countdown::remainingMs(uint64_t nowMs) const
{
if (phase == TIMER_RUNNING) return endMs > nowMs ? endMs - nowMs : 0;
return remainMs; // READY: full duration, PAUSED: frozen, FINISHED: 0
}
bool Countdown::pollFinished(uint64_t nowMs)
{
if (phase != TIMER_RUNNING) return false;
if (remainingMs(nowMs) > 0) return false;
phase = TIMER_FINISHED;
remainMs = 0;
prevRemainMs = 0;
return true;
}
uint32_t Countdown::pollMilestoneMinutes(uint64_t nowMs, int intervalMinutes)
{
if (phase != TIMER_RUNNING || !reminderIntervalValid(intervalMinutes)) return 0;
uint64_t intervalMs = (uint64_t)intervalMinutes * 60000ULL;
uint64_t rem = remainingMs(nowMs);
uint64_t prev = prevRemainMs;
prevRemainMs = rem;
if (rem == 0) return 0; // the final alarm owns zero
// Smallest boundary at/above the current remaining; it fired iff it was
// still strictly below the previously seen remaining. Boundaries skipped
// by a delayed update all sit between rem and prev, so only this most
// recent one is reported and the older ones can never replay.
uint64_t m = (rem + intervalMs - 1) / intervalMs;
if (m >= 1 && m * intervalMs < prev) {
return (uint32_t)(m * (uint64_t)intervalMinutes);
}
return 0;
}
/*-------- Formatting ----------*/
void formatHMS(uint64_t totalSeconds, char *buf, size_t bufLen)
{
unsigned long long h = totalSeconds / 3600ULL;
unsigned m = (unsigned)((totalSeconds / 60ULL) % 60ULL);
unsigned s = (unsigned)(totalSeconds % 60ULL);
snprintf(buf, bufLen, "%02llu:%02u:%02u", h, m, s);
}
void formatHM(uint64_t totalMinutes, char *buf, size_t bufLen)
{
unsigned long long h = totalMinutes / 60ULL;
unsigned m = (unsigned)(totalMinutes % 60ULL);
snprintf(buf, bufLen, "%02llu:%02u", h, m);
}
uint64_t displayMinutesElapsed(uint64_t elapsedMs)
{
return elapsedMs / 60000ULL;
}
uint64_t displayMinutesRemaining(uint64_t remainingMs)
{
return (remainingMs + 59999ULL) / 60000ULL;
}
} // namespace timerlogic