5
5
ButtonEvents::ButtonEvents () {
6
6
debouncedButton.interval (DEFAULT_DEBOUNCE_MS);
7
7
doubleTapTime_ms = DEFAULT_DOUBLETAP_MS;
8
- holdTime_ms = DEFAULT_HOLD_MS;
8
+ holdTime_ms = DEFAULT_HOLD_MS;
9
9
isActiveLow = DEFAULT_ACTIVE_LOW;
10
- pressTime_ms = 0 ; // initialize button timestamps and states...
11
- releaseTime_ms = 0 ;
12
- buttonState = idle;
13
- buttonEvent = none;
10
+ pressTime_ms = 0 ; // initialize button timestamps and states...
11
+ releaseTime_ms = 0 ;
12
+ buttonState = idle;
13
+ buttonEvent = none;
14
14
}
15
15
16
16
// passthru to Bounce2 attach() method
17
17
void ButtonEvents::attach (int pin) {
18
- debouncedButton.attach (pin);
18
+ debouncedButton.attach (pin);
19
19
}
20
20
21
21
// passthru to Bounce2 attach() overload
22
22
void ButtonEvents::attach (int pin, int mode) {
23
- debouncedButton.attach (pin, mode);
23
+ debouncedButton.attach (pin, mode);
24
24
}
25
25
26
26
// set button mode to active high
@@ -69,66 +69,62 @@ bool ButtonEvents::buttonReleased() {
69
69
70
70
// calls the Bounce2 update() method, then runs button event detection logic
71
71
bool ButtonEvents::update () {
72
- bool passthruState = debouncedButton.update (); // update debounced button state
73
-
74
- if (buttonPressed ()) {
75
-
76
- // if the button was previously idle, store the press time and update the button state
77
- if (buttonState == idle) {
78
- pressTime_ms = millis ();
79
- buttonState = pressed;
80
- }
81
-
82
- // if the button was in a released state (waiting for a double tap), update the button
83
- // state and indicate that a double tap event occurred
84
- else if (buttonState == released) {
85
- buttonState = idle;
86
- buttonEvent = doubleTap;
87
- return true ;
88
- }
89
- }
90
-
91
- else if (buttonReleased ()) {
92
-
93
- // if the button was in a pressed state, store the release time and update the button state
94
- if (buttonState == pressed) {
95
- releaseTime_ms = millis ();
96
- buttonState = released;
97
- }
98
- }
99
-
100
- // if the button is currently in a pressed state...
101
- if (buttonState == pressed) {
102
-
103
- // if the specified hold time has been reached or passed, update the button state and
104
- // indicate that a hold event occurred
105
- if ((millis () - pressTime_ms) >= holdTime_ms) {
106
- buttonState = idle;
107
- buttonEvent = hold;
108
- return true ;
109
- }
110
- }
111
-
112
- // if the button is currently in a released state...
113
- else if (buttonState == released) {
114
-
115
- // if the specified double tap time has been reached or passed, update the button state
116
- // and indicate that a (single) tap event occurred
117
- if ((millis () - releaseTime_ms) >= doubleTapTime_ms) {
118
- buttonState = idle;
119
- buttonEvent = tap;
120
- return true ;
121
- }
122
- }
123
-
124
- // if we get to this point, indicate that no button event occurred in this cycle
125
- buttonEvent = none;
126
- return passthruState;
72
+ bool passthruState = debouncedButton.update (); // update debounced button state
73
+
74
+ if (buttonPressed ()) {
75
+ // if the button was previously idle, store the press time and update the button state
76
+ if (buttonState == idle) {
77
+ pressTime_ms = millis ();
78
+ buttonState = pressed;
79
+ }
80
+
81
+ // if the button was in a released state (waiting for a double tap), update the button
82
+ // state and indicate that a double tap event occurred
83
+ else if (buttonState == released) {
84
+ buttonState = idle;
85
+ buttonEvent = doubleTap;
86
+ return true ;
87
+ }
88
+ }
89
+
90
+ else if (buttonReleased ()) {
91
+ // if the button was in a pressed state, store the release time and update the button state
92
+ if (buttonState == pressed) {
93
+ releaseTime_ms = millis ();
94
+ buttonState = released;
95
+ }
96
+ }
97
+
98
+ // if the button is currently in a pressed state...
99
+ if (buttonState == pressed) {
100
+ // if the specified hold time has been reached or passed, update the button state and
101
+ // indicate that a hold event occurred
102
+ if ((millis () - pressTime_ms) >= holdTime_ms) {
103
+ buttonState = idle;
104
+ buttonEvent = hold;
105
+ return true ;
106
+ }
107
+ }
108
+
109
+ // if the button is currently in a released state...
110
+ else if (buttonState == released) {
111
+ // if the specified double tap time has been reached or passed, update the button state
112
+ // and indicate that a (single) tap event occurred
113
+ if ((millis () - releaseTime_ms) >= doubleTapTime_ms) {
114
+ buttonState = idle;
115
+ buttonEvent = tap;
116
+ return true ;
117
+ }
118
+ }
119
+
120
+ // if we get to this point, indicate that no button event occurred in this cycle
121
+ buttonEvent = none;
122
+ return passthruState;
127
123
}
128
124
129
125
// returns the last triggered event
130
126
ButtonEvent ButtonEvents::event () {
131
- return buttonEvent;
127
+ return buttonEvent;
132
128
}
133
129
134
130
// returns true if button was tapped
@@ -148,15 +144,15 @@ bool ButtonEvents::held() {
148
144
149
145
// passthru to Bounce2 read() method
150
146
bool ButtonEvents::read () {
151
- return debouncedButton.read ();
147
+ return debouncedButton.read ();
152
148
}
153
149
154
150
// passthru to Bounce2 fell() method
155
151
bool ButtonEvents::fell () {
156
- return debouncedButton.fell ();
152
+ return debouncedButton.fell ();
157
153
}
158
154
159
155
// passthru to Bounce2 rose() method
160
156
bool ButtonEvents::rose () {
161
- return debouncedButton.rose ();
162
- }
157
+ return debouncedButton.rose ();
158
+ }
0 commit comments