-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cpp
191 lines (178 loc) · 3.5 KB
/
Timer.cpp
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
/*
* Timer.cpp
* created: Dec 2020
*/
#include "Timer.h"
#include "Arduino.h"
#include "PT6961.h"
void Timer::countdown(Fidelity fidelity, uint16_t digits) {
_fidelity = fidelity;
_type = COUNTDOWN;
setTime(digits);
init();
}
void Timer::countup(Fidelity fidelity) {
_fidelity = fidelity;
_type = COUNTUP;
_digitOne = 0;
_digitTwo = 0;
_digitThree = 0;
_digitFour = 0;
init();
}
void Timer::update() {
if (millis() >= _nextTime && !_isPaused) {
if (_type == COUNTDOWN) {
decrement();
} else {
increment();
}
_display.sendDigits(_digitOne, _digitTwo, _digitThree, _digitFour, 1);
if (_fidelity == MINUTES) {
_nextTime += MINUTE_INCREMENT;
} else {
_nextTime += SECOND_INCREMENT;
}
}
}
void Timer::pause() {
if (!_isPaused) {
_isPaused = true;
_pauseTime = _nextTime - millis();
}
}
void Timer::restart() {
if (_isPaused) {
_isPaused = false;
_nextTime = millis() + _pauseTime;
}
}
/*
* A common initialization method.
*/
void Timer::init() {
#if (TIMER_DEBUG == 1)
Serial.begin(9600);
delay(1000);
Serial.println("Timer initialization");
#endif
_done = false;
_isPaused = false;
_display.initDisplay();
_display.sendDigits(_digitOne, _digitTwo, _digitThree, _digitFour, 1);
if (_fidelity == MINUTES) {
_nextTime = millis() + MINUTE_INCREMENT;
} else {
_nextTime = millis() + SECOND_INCREMENT;
}
}
/*
* Sets the time to be displayed by the timer for a countdown.
*/
void Timer::setTime(uint16_t digits) {
if (digits > 9999 || digits <= 0) {
#if (TIMER_DEBUG == 1)
Serial.println("Given time outside of range.");
#endif
} else {
_digitFour = digits % 10;
digits = digits / 10;
_digitThree = digits % 10;
digits = digits / 10;
_digitTwo = digits % 10;
digits = digits / 10;
_digitOne = digits % 10;
#if (TIMER_DEBUG == 1)
Serial.print("Time Set: ");
Serial.print(_digitOne);
Serial.print(_digitTwo);
Serial.print(":");
Serial.print(_digitThree);
Serial.println(_digitFour);
#endif
}
}
/*
* Decrements the third and fourth digits.
*/
void Timer::decrement() {
if (_digitFour == 0) {
if (_digitThree == 0) {
decrementLarge();
} else {
_digitThree--;
_digitFour = 9;
}
} else {
_digitFour--;
}
}
/*
* Decrements the first and second digits.
*/
void Timer::decrementLarge() {
if (_digitTwo == 0) {
if (_digitOne == 0) {
alarm();
} else {
_digitOne--;
_digitTwo = 9;
_digitThree = 5;
_digitFour = 9;
}
} else {
_digitTwo--;
_digitThree = 5;
_digitFour = 9;
}
}
/*
* Increments the third and fourth digits.
*/
void Timer::increment() {
if (_digitFour == 9) {
if (_digitThree == 5) {
incrementLarge();
} else {
_digitThree++;
_digitFour = 0;
}
} else {
_digitFour++;
}
}
/*
* Increments the first and second digits.
*/
void Timer::incrementLarge() {
if (_digitTwo == 9) {
if (_digitOne == 9) {
_digitOne = 0;
_digitTwo = 0;
_digitThree = 0;
_digitFour = 0;
alarm();
} else {
_digitOne++;
_digitTwo = 0;
_digitThree = 0;
_digitFour = 0;
}
} else {
_digitTwo++;
_digitThree = 0;
_digitFour = 0;
}
}
/*
* An alarm that is called when a countdown reaches 00:00 or a countup timer
* reaches 99:99.
*/
void Timer::alarm() {
if (!_done) {
digitalWrite(_alarmPin, HIGH);
delay(ALARM_LENGTH);
digitalWrite(_alarmPin, LOW);
_done = true;
}
}