-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTimer.cpp
86 lines (72 loc) · 1.84 KB
/
CTimer.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
#include "HardwareSerial.h"
#include "CTimer.h"
#include <Arduino.h>
#define DEBUG 1
#if DEBUG == 1
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x)
#define debugln(x)
#endif
CTimer::CTimer(unsigned long duration, U8G2_SSD1309_128X64_NONAME2_F_HW_I2C& displayRef)
: countdownTime(duration), lastUpdateTime(0), lastSetCountdownTime(duration),
initialCountdownTime(duration), state(Stopped), display(&displayRef) {}
void CTimer::start() {
if (state != Running) {
state = Running;
debugln("Timer Started");
}
}
void CTimer::stop() {
if (state == Running) {
state = Stopped;
debugln("Timer Stopped");
}
}
void CTimer::reset() {
countdownTime = 60000;
state = Stopped;
debugln("Timer Reset");
}
void CTimer::update() {
if (state == Running && millis() - lastUpdateTime >= 1000) {
lastUpdateTime = millis();
if (countdownTime > 0) {
countdownTime -= 1000;
} else {
debugln("Timer Finished");
stop();
countdownTime = initialCountdownTime;
// start(); restart timer immediately
}
}
}
void CTimer::addTime(unsigned long additionalTime) {
if (!this->isRunning()) {
this->countdownTime += additionalTime;
debug("Added Time: ");
debugln(additionalTime);
}
}
void CTimer::subtractTime(unsigned long subtractTime) {
if (!this->isRunning() && this->countdownTime > subtractTime) {
this->countdownTime -= subtractTime;
debug("Subtracted Time: ");
debugln(subtractTime);
}
}
bool CTimer::isRunning() {
return state == Running;
}
void CTimer::updateLastSetTime(unsigned long time) {
this->lastSetCountdownTime = time;
debug("Set Time Updated: ");
debugln(time);
}
unsigned long CTimer::getCountdownTime() const {
return countdownTime;
}
unsigned long CTimer::getLastSetTime() const {
return lastSetCountdownTime;
}