-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDelay.cpp
91 lines (79 loc) · 1.87 KB
/
MyDelay.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
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "MyDelay.h"
myDelay::myDelay(void) {
this->_preMills = 0;
this->setDelay(0);
}
myDelay::myDelay(unsigned long dtime) {
this->_preMills = 0;
this->setDelay(dtime);
}
myDelay::myDelay(unsigned long dtime, funTocall funcall) {
this->_preMills = 0;
this->setDelay(dtime);
this->setCallback(funcall);
}
myDelay::myDelay(unsigned long dtime, funTocall funcall, int repeatCount) {
this->_preMills = 0;
this->setDelay(dtime);
this->setCallback(funcall);
this->setRepeat(repeatCount);
}
void myDelay::setDelay(unsigned long dtime) {
this->_delaytime = dtime;
}
void myDelay::setCallback(funTocall funcall) {
if (funcall != 0) {
this->_funcall = funcall;
this->_useFunction = true;
} else {
this->_useFunction = false;
}
}
void myDelay::setRepeat(int repeatCount) {
this->_initialRepeatCount = repeatCount;
this->_currentRepeatCount = repeatCount;
this->_repeating = true;
}
void myDelay::start() {
this->_preMills = millis();
this->_currentRepeatCount = this->_initialRepeatCount;
this->_running = true;
}
void myDelay::stop() {
this->_preMills = 0;
this->_running = false;
}
bool myDelay::update() {
if (this->_running) {
this->_curMills = millis();
if (this->_curMills - this->_preMills >= this->_delaytime) {
if (this->_repeating) {
if (this->_initialRepeatCount != MYDELAY_REPEAT_FOREVER) {
this->_currentRepeatCount--;
if (this->_currentRepeatCount == 0) {
this->stop();
}
}
}
this->_preMills = this->_curMills;
if (this->_useFunction == true) {
this->_funcall();
return true;
} else {
return true;
}
} else {
return false;
}
} else {
return false;
}
}
bool myDelay::isRunning() {
return this->_running;
}