-
Notifications
You must be signed in to change notification settings - Fork 1
/
DetectEdge.h
155 lines (135 loc) · 3.43 KB
/
DetectEdge.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
#ifndef DETECTEDGE_H
#define DETECTEDGE_H
#include <WProgram.h>
class DetectEdge {
public:
DetectEdge(unsigned char pin = 0, bool activeLow = true, unsigned short deBounce_ms = 0)
{
pPin = pin;
pActiveLow = activeLow;
pPreviousState = activeLow;
pPreviousStateBounce = activeLow;
pRisingFlag = false;
pFallingFlag = false;
pTime = 0;
pPreviousTime = 0;
pDebounceTime_ms = deBounce_ms;
pRisingWait = false;
pFallingWait = false;
}
void scan()
{
bool input = digitalRead(pPin);
bool stateChanged = false;
if(pActiveLow) {
input ^= 1;
}
if(input == 0 && pPreviousState != 0) {
pFallingWait = true;
pChangeTime = millis();
stateChanged = true;
}
else if(input == 1 && pPreviousState != 1) {
pRisingWait = true;
pChangeTime = millis();
stateChanged = true;
}
if(pRisingWait || pFallingWait )
{
if(CompareTime(pChangeTime, millis()) >= pDebounceTime_ms)
{
if(pFallingWait && pPreviousStateBounce && input == 0)
{
pRisingFlag = false;
pFallingFlag = true;
pPreviousStateBounce = false;
}
else if(pRisingWait && !pPreviousStateBounce && input == 1)
{
pRisingFlag = true;
pFallingFlag = false;
pPreviousStateBounce = true;
}
pRisingWait = false;
pFallingWait = false;
}
}
if(stateChanged) {
pPreviousState = input;
pPreviousTime = pTime;
pTime = millis();
}
}
void clear()
{
scan();
pRisingFlag = false;
pFallingFlag = false;
}
bool rising()
{
bool state = pRisingFlag;
pRisingFlag = false;
return state;
}
bool falling()
{
bool state = pFallingFlag;
pFallingFlag = false;
return state;
}
bool isRisen()
{
return pActiveLow ? !digitalRead(pPin) : digitalRead(pPin);
}
bool isFallen()
{
return pActiveLow ? digitalRead(pPin) : !digitalRead(pPin);
}
void setRising()
{
pRisingFlag = true;
pFallingFlag = false;
}
void setFalling()
{
pRisingFlag = false;
pFallingFlag = true;
}
unsigned long time()
{
return pTime;
}
unsigned long previousTime()
{
return pPreviousTime;
}
private:
unsigned char pPin;
bool pActiveLow;
bool pPreviousState;
bool pPreviousStateBounce;
bool pRisingFlag;
bool pFallingFlag;
unsigned long pTime;
unsigned long pPreviousTime;
unsigned long pChangeTime;
unsigned short pDebounceTime_ms;
bool pRisingWait;
bool pFallingWait;
unsigned long CompareTime(unsigned long start, unsigned long stop)
{
if(start > stop) //accout for rollover
{
// Serial.println((0xFFFFFFFF - start) + stop,DEC);
// Serial.println("boundry");
return (0xFFFFFFFF - start) + stop;
}
else
{
// Serial.println(stop - start,DEC);
return stop - start;
}
}
};
#endif // DETECTEDGE_H