forked from inflop/Countimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Countimer.cpp
177 lines (147 loc) · 2.91 KB
/
Countimer.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
#include "Countimer.h"
#include "Arduino.h"
Countimer::Countimer()
{
_previousMillis = 0;
_currentCountTime = 0;
_countTime = 0;
_isCounterCompleted = false;
_isStopped = true;
_countType = COUNT_NONE;
_startCountTime = 0;
}
Countimer::~Countimer()
{
}
void Countimer::setCounter(uint8_t hours, uint8_t minutes, uint8_t seconds, CountType countType, timer_callback onComplete)
{
_onComplete = onComplete;
_countType = countType;
setCounter(hours, minutes, seconds);
}
void Countimer::setCounter(uint8_t hours, uint8_t minutes, uint8_t seconds)
{
if (hours > COUNTIMER_MAX_HOURS) {
hours = COUNTIMER_MAX_HOURS;
}
if (minutes > COUNTIMER_MAX_MINUTES_SECONDS) {
minutes = COUNTIMER_MAX_MINUTES_SECONDS;
}
if (seconds > COUNTIMER_MAX_MINUTES_SECONDS) {
seconds = COUNTIMER_MAX_MINUTES_SECONDS;
}
_currentCountTime = ((hours * 3600L) + (minutes * 60L) + seconds) * 1000L;
_countTime = _currentCountTime;
if (_countType == COUNT_UP)
{
// if is count up mode, we have to start from 00:00:00;
_currentCountTime = 0;
}
_startCountTime = _currentCountTime;
}
void Countimer::setInterval(timer_callback callback, uint32_t interval)
{
_interval = interval;
_callback = callback;
}
uint8_t Countimer::getCurrentHours()
{
return _currentCountTime / 1000 / 3600;
}
uint8_t Countimer::getCurrentMinutes()
{
return _currentCountTime / 1000 % 3600 / 60;
}
uint8_t Countimer::getCurrentSeconds()
{
return _currentCountTime / 1000 % 3600 % 60 % 60;
}
char* Countimer::getCurrentTime()
{
sprintf(_formatted_time, "%02d:%02d:%02d", getCurrentHours(), getCurrentMinutes(), getCurrentSeconds());
return _formatted_time;
}
bool Countimer::isCounterCompleted()
{
return _isCounterCompleted;
}
bool Countimer::isStopped()
{
return _isStopped;
}
void Countimer::start()
{
_isStopped = false;
if(_isCounterCompleted)
_isCounterCompleted = false;
}
void Countimer::pause()
{
_isStopped = true;
}
void Countimer::stop()
{
_isStopped = true;
_isCounterCompleted = true;
_currentCountTime = _countTime;
if(_countType == COUNT_UP)
{
_currentCountTime = 0;
}
}
void Countimer::restart()
{
_currentCountTime = _startCountTime;
_isCounterCompleted = false;
_isStopped = false;
start();
}
void Countimer::run()
{
// timer is running only if is not completed or not stopped.
if (_isCounterCompleted || _isStopped)
return;
if (millis() - _previousMillis >= _interval) {
if (_countType == COUNT_DOWN)
{
countDown();
}
else if (_countType == COUNT_UP)
{
countUp();
}
else
{
_callback();
}
_previousMillis = millis();
}
}
void Countimer::countDown()
{
if (_currentCountTime > 0)
{
_callback();
_currentCountTime -= _interval;
}
else
{
stop();
if (_onComplete != NULL)
_onComplete();
}
}
void Countimer::countUp()
{
if (_currentCountTime < _countTime)
{
_callback();
_currentCountTime += _interval;
}
else
{
stop();
if (_onComplete != NULL)
_onComplete();
}
}