-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timemark.h
130 lines (119 loc) · 2.7 KB
/
Timemark.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
/**
* @file Timemark.h
* @version 1.0
*
* @section License
* Copyright (C) 2016, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef TIMEMARK_H
#define TIMEMARK_H
/**
* The Timemark class is an abstraction of the classical
* "Blink-without-delay" pattern. Used in the format:
* @code
* Timemark mark(100);
* ...
* mark.start();
* ...
* if (mark.expired()) {
* ...
* mark.stop();
* }
* @endcode
* The maximum timemark limit is approx 49 days.
*
* @section Reference
* 1. https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
class Timemark {
public:
/**
* Construct timemark with the given time limit in milli-seconds.
* @param[in] ms time limit in milli-seconds (Default 0).
*/
Timemark(uint32_t ms = 0UL) :
m_limitMillis(ms),
m_previousMillis(0UL),
m_running(false)
{
}
/**
* Get current timemark limit.
* @return time limit.
*/
uint32_t limitMillis()
{
return (m_limitMillis);
}
/**
* Set timemark limit.
* @param[in] ms time limit in milli-seconds.
*/
void limitMillis(uint32_t ms)
{
m_limitMillis = ms;
}
/**
* Get current timemark status. Return true if running otherwise
* false.
* @return bool.
*/
bool running()
{
return (m_running);
}
/**
* Returns true if the timemark has expired otherwise false. A new
* time interval is started if the timemark was running and expired
* was detected.
* @return bool.
*/
bool expired()
{
if (!m_running) return (false);
uint32_t currentMillis = millis();
if ((currentMillis - m_previousMillis) < m_limitMillis) return (false);
m_previousMillis += m_limitMillis;
return (true);
}
/**
* Returns the number of milli-seconds that have elapsed since the
* timemark was started. Zero is returned if the timemark is not running.
* @return ms.
*/
uint32_t elapsed()
{
if (!m_running) return (0UL);
return (millis() - m_previousMillis);
}
/**
* Start the timemark.
*/
void start()
{
m_previousMillis = millis();
m_running = true;
}
/**
* Stop the timemark.
*/
void stop()
{
m_running = false;
}
protected:
uint32_t m_limitMillis;
uint32_t m_previousMillis;
bool m_running;
};
#endif