-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
69 lines (63 loc) · 2.02 KB
/
Copy pathButton.cpp
File metadata and controls
69 lines (63 loc) · 2.02 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
/***** Button.cpp *****/
/* Adapted from an in-class implementation of a debounced button
Debounce state transition formulated by Andrew Mcpherson
*/
#include "Button.h"
Button::Button(int channel) {
this->channel = channel;
debounceCounter = 0;
debounceInterval = 882;
debounceState = kStateOpen;
buttonPreviousValue = 0;
buttonPreviousStateWasClosed = false;
}
void Button::debounce(BelaContext* context, int frame) {
int value = digitalRead(context, frame, channel);
if(debounceState == kStateOpen) {
// Button is not pressed, could be pressed anytime
// Input: look for switch closure
// digitalWrite(context, n, gLEDPin, LOW);
if (value == LOW && buttonPreviousValue == HIGH) {
debounceState = kStateJustClosed;
}
buttonPreviousStateWasClosed = false;
}
else if (debounceState == kStateJustClosed) {
// Button was just pressed, wait for debounce
// Input: run counter, wait for timeout
if (debounceInterval <= ++debounceCounter) {
debounceState = kStateClosed;
debounceCounter = 0;
}
buttonPreviousStateWasClosed = false;
}
else if (debounceState == kStateClosed) {
// Button is pressed, could be released anytime
// Input: look for switch opening
// digitalWrite(context, n, gLEDPin, HIGH);
if (value == HIGH) {
debounceState = kStateJustOpen;
}
buttonPreviousStateWasClosed = true;
}
else if (debounceState == kStateJustOpen) {
// Button was just released, wait for debounce
// Input: run counter, wait for timeout
if (debounceInterval <= ++debounceCounter) {
debounceState = kStateOpen;
debounceCounter = 0;
}
buttonPreviousStateWasClosed = false;
}
// Update the previous button value
buttonPreviousValue = value;
}
bool Button::isOpen() {
return debounceState == kStateOpen;
}
bool Button::isClosed() {
return debounceState == kStateClosed;
}
bool Button::wasClosed() {
return buttonPreviousStateWasClosed;
}