This repository was archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCyclic.cpp
More file actions
112 lines (97 loc) · 2.04 KB
/
Cyclic.cpp
File metadata and controls
112 lines (97 loc) · 2.04 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
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
/*
Cyclic - v1.2 - November 19, 2014.
Arduino library for millis-based cycle control.
Created by William Koch.
Released into the public domain.
*/
#include "Cyclic.h"
// Manual control, no auto-reset.
Cyclic::Cyclic() {
_max = _min = _start = _last = _now = _cycles = 0;
_active = false;
}
// Auto-resets at max value.
// Manual reset always available.
Cyclic::Cyclic(unsigned long max) {
_max = max;
_min = _start = _last = _now = _cycles = 0;
_active = false;
}
// Auto-resets at max value.
// Manual reset only between min and max.
Cyclic::Cyclic(unsigned long max, unsigned long min) {
_max = max;
_min = min;
_start = _last = _now = _cycles = 0;
_active = false;
}
// Starts the cycle.
void Cyclic::start() {
if (!_active) {
_active = true;
_start = millis();
Cyclic::update();
}
}
// Stops the cycle.
void Cyclic::stop() {
if (_active) {
_active = false;
_start = 0;
}
}
// Resets the cycle.
void Cyclic::reset() {
if (_active && _now >= _min) {
_last = _now;
_now = 0;
_cycles++;
_start = millis();
}
}
// Reboots the cycle.
void Cyclic::reboot() {
Cyclic::stop();
_start = _last = _now = _cycles = 0;
}
// Updates the time and auto-resets.
void Cyclic::update() {
if (_active) {
_now = millis() - _start;
if (_max > 0 && _now >= _max) {
Cyclic::reset();
}
}
}
// Returns the time of the cycle from last manual update.
unsigned long Cyclic::time() {
if (_active) {
return _now;
}
}
// Returns the current time of the cycle.
unsigned long Cyclic::now() {
if (_active) {
Cyclic::update();
return _now;
}
}
// Returns the duration of the last cycle.
// Good for implementing median dynamic cycles.
unsigned long Cyclic::last() {
if (_active) {
return _last;
}
}
// Returns the max value.
unsigned long Cyclic::cycle() {
return _max;
}
// Returns the count of cycles completed.
unsigned long Cyclic::cycles() {
return _cycles;
}
// Returns True when Cyclic is running and False otherwise.
boolean Cyclic::status() {
return _active;
}