This repository was archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterval.cpp
More file actions
77 lines (63 loc) · 1.97 KB
/
Interval.cpp
File metadata and controls
77 lines (63 loc) · 1.97 KB
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
/*
* Copyright 2016 Patrick Pedersen (ctx.xda@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Lightweight Arduino Interval Library by Patrick Pedersen
* Please see Interval.h for more details */
#include "Interval.h"
/* --- Time Stamp --- */
/* Set stamp on Initialization */
timestamp::timestamp(time (*f)()) : getTime(f) {
stamp = getTime();
}
/* Set stamp */
void timestamp::setStamp() {
stamp = getTime();
}
/* Setting stamp in future (or even past) */
void timestamp::setStamp(time t) {
stamp = getTime() + t;
}
/* Check wether stamp has expired a specific time */
bool timestamp::olderThan(time t) {
return (getTime() >= stamp + t);
}
/* Check wether future stamp has expired (awlays true on setStamp() */
bool timestamp::expired() {
return (getTime() >= stamp);
}
/* Get passed time since stamp (in ms) */
time_ms timestamp::timeSinceStamp() {
return getTime() - stamp;
}
/* --- Interval --- */
/* For Inheritance */
definable_interval::definable_interval(){}
/* Without function reference (Must be defined later */
definable_interval::definable_interval(time_ms ms) : interval_len(ms) {
interval_time.setStamp(ms);
}
/* With function reference */
definable_interval::definable_interval(time_ms ms, void(*f)()) : interval_len(ms), onInterval(f) {
interval_time.setStamp(ms);
}
/* Run interval */
bool definable_interval::interval() {
bool ret;
if(ret = interval_time.expired()) {
onInterval();
interval_time.setStamp(interval_len);
}
return ret;
}