-
Notifications
You must be signed in to change notification settings - Fork 2
/
debounce.h
executable file
·144 lines (125 loc) · 3.2 KB
/
debounce.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
#ifndef _DEBOUNCE_H
#define _DEBOUNCE_H
#define STM32_SPECIFIC
typedef enum {
DEBOUNCE_NONE = 0,
DEBOUNCE_PRESSED = 1,
DEBOUNCE_RELEASED = 2
} DebounceEvent;
class Debounce {
protected:
uint32_t lastToggleTime;
uint8_t activeValue;
bool curState;
bool highState;
bool lowState;
bool eventInitialized;
uint32_t debounceTime;
bool releaseCanceled = false;
#ifdef STM32_SPECIFIC
volatile uint32_t* port;
uint32_t mask;
#endif
Debounce() {}
public:
int pin;
virtual inline bool getRawState(void) {
#ifdef STM32_SPECIFIC
if (*port & mask)
return highState;
else
return lowState;
#else
return activeValue==digitalRead(pin);
#endif
}
private:
bool update(void) {
bool v = getRawState();
uint32_t t = millis();
if (v != curState && t - lastToggleTime >= debounceTime) {
lastToggleTime = t;
curState = v;
return true;
}
return false;
}
public:
Debounce(int p, uint8_t active=HIGH, uint32_t time=20) {
activeValue = active;
debounceTime = time;
highState = active==HIGH;
lowState = active==LOW;
pin = p;
#ifdef STM32_SPECIFIC
port = &(PIN_MAP[p].gpio_device->regs->IDR);
mask = 1u << PIN_MAP[p].gpio_bit;
#endif
}
void begin(void) {
curState = getRawState();
lastToggleTime = millis();
}
// Code using Debounce should choose exactly one of three polling methods:
// wasToggled(), getState() and wasPressed()
// and should not switch between them, with the exception that a single initial call to
// getState() is acceptable.
// for toggle monitoring
inline bool wasToggled(void) {
return update();
}
DebounceEvent getEvent(void) {
if (update() || ! eventInitialized) {
eventInitialized = true;
return curState ? DEBOUNCE_PRESSED : DEBOUNCE_RELEASED;
}
else
return DEBOUNCE_NONE;
}
// for state monitoring
bool getState(void) {
update();
return curState;
}
// for press monitoring
bool wasPressed(void) {
return wasToggled() && curState; // curState value is a side-effect of wasToggled()
}
void cancelRelease(void) {
releaseCanceled = true;
}
// for release monitoring
bool wasReleased(void) {
if (wasToggled() && ! curState) { // curState value is a side-effect of wasToggled()
if (releaseCanceled) {
releaseCanceled = false;
return false;
}
else {
return true;
}
}
return false;
}
};
class DebounceAnalog : public Debounce {
private:
uint16_t threshold;
public:
bool getRawState(void) {
// TODO: go analog
if (analogRead(pin) >= threshold)
return highState;
else
return lowState;
}
DebounceAnalog(int p, uint8_t active=HIGH, uint16_t _threshold=512, uint32_t time=20) : Debounce() {
activeValue = active;
debounceTime = time;
threshold = _threshold;
highState = active==HIGH;
lowState = active==LOW;
pin = p;
}
};
#endif