forked from AndrewMascolo/CountUpDownTimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountUpDownTimer.h
233 lines (194 loc) · 5.31 KB
/
CountUpDownTimer.h
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
/* Count Up/Down Timer */
/*
Helpful Corrections by:
Github user Mannelito - Correction to ResumeTimer function
*/
/*
The MIT License (MIT)
Copyright (c) 2016 Andrew Mascolo Jr
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef CountUpDownTimer_h
#define CountUpDownTimer_h
#include<Arduino.h>
#define UP 1
#define DOWN 0
class CountUpDownTimer
{
public:
CountUpDownTimer(bool type, bool precision = HIGH) : _type(type), _precision(precision)
{
SetStopTime((type? 0xFFFF : 0)); // 18h 12m 15s
time = precision ? micros() : millis();
Clock = 0;
Reset = false, Stop = true, Paused = true;
timeFlag = false;
duration = precision ? 1000000 : 1000;
}
unsigned long _InternalClock()
{
return _precision ? micros() : millis();
}
boolean Timer()
{
timeFlag = false;
if (!Stop && !Paused) // if not Stopped or Paused, run timer
{
if(Paused)
time = _InternalClock();
if ((_intTime = _InternalClock() ) - time > duration ) // check the time difference and see if 1 second has elapsed
{
_type == UP? Clock++ : Clock--;
timeFlag = true;
if ((_type == DOWN && Clock == 0) || TimeCheck()) // check to see if the clock is 0
Stop = true; // If so, stop the timer
time = _intTime;
if(_intTime < time)
time = 0; // check to see if micros() has rolled over, if not, then increment "time" by duration
}
}
return !Stop; // return the state of the timer
}
void ResetTimer()
{
if(_type)
Clock = 0;
else
SetTimer(R_clock);
Stop = false;
}
void StartTimer()
{
Watch = _InternalClock();
Stop = false;
Paused = false;
if(_type == UP)
Clock = 0;
}
void StopTimer()
{
Stop = true;
}
void StopTimerAt(unsigned long hours, unsigned long minutes, unsigned long seconds)
{
if (TimeCheck(hours, minutes, seconds) )
Stop = true;
}
void PauseTimer()
{
time = _InternalClock();
Paused = true;
}
void ResumeTimer() // You can resume the timer if you ever stop it.
{
Paused = false;
time = _InternalClock();
}
void SetTimer(unsigned long hours, unsigned long minutes, unsigned long seconds)
{
// This handles invalid time overflow ie 1(H), 0(M), 120(S) -> 1h, 2m, 0s
unsigned int Secs = (seconds / 60), Mins = (minutes / 60);
if(Secs) minutes += Secs;
if(Mins) hours += Mins;
Clock = (hours * 3600) + (minutes * 60) + (seconds % 60);
R_clock = Clock;
Stop = false;
}
void SetTimer(unsigned long seconds)
{
// StartTimer(seconds / 3600, (seconds / 3600) / 60, seconds % 60);
Clock = seconds;
R_clock = Clock;
Stop = false;
}
void SetStopTime(unsigned long seconds)
{
STh = seconds / 3600;
STm = (seconds / 60) % 60;
STs = seconds % 60;
STotaltime = seconds;
}
void SetStopTime(unsigned long hours, unsigned long minutes, unsigned long seconds)
{
STh = hours;
STm = minutes;
STs = seconds;
STotaltime = (hours * 3600) + (minutes * 60) + (seconds % 60);
}
unsigned long ShowHours()
{
return Clock / 3600;
}
unsigned long ShowMinutes()
{
return (Clock / 60) % 60;
}
unsigned long ShowSeconds()
{
return Clock % 60;
}
unsigned long ShowMilliSeconds()
{
if (_precision == HIGH)
return ((_intTime - Watch)/ 1000.0) + 1;
else
return (_intTime - Watch) + 1;
}
unsigned long ShowMicroSeconds()
{
if (_precision == LOW)
return ((_intTime - Watch)/ 1000.0) + 1;
else
return (_intTime - Watch) + 1;
}
unsigned long ShowTotalSeconds()
{
return Clock;
}
unsigned long ShowStopTime()
{
return STotaltime;
}
boolean TimeHasChanged()
{
return timeFlag;
}
boolean TimeCheck(unsigned int hours, unsigned int minutes, unsigned int seconds) // output true if timer equals requested time or has passed it.
{
unsigned long TC = ((hours * 3600) + (minutes * 60) + (seconds % 60));
if(_type)
return (Clock >= TC);
else
return (Clock <= TC);
}
boolean TimeCheck() // output true if timer equals requested time or has passed it.
{
if(_type)
return Clock >= STotaltime;
else
return Clock <= STotaltime;
}
private:
unsigned long duration;
unsigned long STh, STm, STs, STotaltime;
unsigned long Watch, _intTime, time;
unsigned long Clock, R_clock;
boolean Reset, Stop, Paused;
volatile boolean timeFlag;
bool _type, _precision;
};
#endif