forked from tigoe/Button
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Button.cpp
executable file
·324 lines (299 loc) · 7.17 KB
/
Button.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* $Id$
||
|| @file Button.cpp
|| @author Alexander Brevig <[email protected]>
|| @url http://alexanderbrevig.com
||
|| @description
|| | This is a Hardware Abstraction Library for Buttons
|| | It provides an easy way of handling buttons
|| #
||
|| @license LICENSE_REPLACE
||
*/
#include <Arduino.h>
#include "Button.h"
// bit positions in the state byte
#define CURRENT 0
#define PREVIOUS 1
#define CHANGED 2
/*
|| @constructor
|| | Set the initial state of this button
|| #
||
|| @parameter buttonPin sets the pin that this switch is connected to
|| @parameter buttonMode indicates BUTTON_PULLUP or BUTTON_PULLDOWN resistor
*/
Button::Button(uint8_t buttonPin, uint8_t buttonMode, bool _debounceMode, int _debounceDuration){
pin=buttonPin;
pinMode(pin,INPUT);
debounceMode = _debounceMode;
debounceDuration = _debounceDuration;
debounceStartTime = millis();
buttonMode==BUTTON_PULLDOWN ? pulldown() : pullup(buttonMode);
state = 0;
bitWrite(state,CURRENT,!mode);
cb_onPress = 0;
cb_onRelease = 0;
cb_onClick = 0;
cb_onHold = 0;
numberOfPresses = 0;
triggeredHoldEvent = true;
}
/*
|| @description
|| | Prepare logic for a pullup button.
|| | If mode is internal set pin HIGH as default
|| #
*/
void Button::pullup(uint8_t buttonMode)
{
mode=BUTTON_PULLUP;
if (buttonMode == BUTTON_PULLUP_INTERNAL)
{
#ifdef ESP8266
pinMode(pin, INPUT_PULLUP);
#else
digitalWrite(pin,HIGH);
#endif
}
}
/*
|| @description
|| | Set pin LOW as default
|| #
*/
void Button::pulldown(void)
{
mode=BUTTON_PULLDOWN;
}
/*
|| @description
|| | Read and write states; issue callbacks
|| #
||
|| @return true if button is pressed
*/
void Button::process(void)
{
//save the previous value
bitWrite(state,PREVIOUS,bitRead(state,CURRENT));
//get the current status of the pin
if (digitalRead(pin) == mode)
{
//currently the button is not pressed
bitWrite(state,CURRENT,false);
}
else
{
//currently the button is pressed
bitWrite(state,CURRENT,true);
}
//handle state changes
if (bitRead(state,CURRENT) != bitRead(state,PREVIOUS))
{
unsigned int interval = millis() - debounceStartTime;
// if(debounceMode){
// Serial.print("debounceStartTime: ");
// Serial.print(debounceStartTime);
// Serial.print("\tdebounceDuration: ");
// Serial.println(debounceDuration);
// Serial.println(interval);
// }
if(debounceMode && interval < debounceDuration){
// not enough time has passed; ignore
return;
}
// Serial.println("state changed");
debounceStartTime = millis();
//the state changed to PRESSED
if (bitRead(state,CURRENT) == true)
{
numberOfPresses++;
if (cb_onPress) { cb_onPress(*this); } //fire the onPress event
pressedStartTime = millis(); //start timing
triggeredHoldEvent = false;
}
else //the state changed to RELEASED
{
if (cb_onRelease) { cb_onRelease(*this); } //fire the onRelease event
if (cb_onClick) { cb_onClick(*this); } //fire the onClick event AFTER the onRelease
//reset states (for timing and for event triggering)
pressedStartTime = -1;
}
//note that the state changed
bitWrite(state,CHANGED,true);
}
else
{
//note that the state did not change
bitWrite(state,CHANGED,false);
//should we trigger an onHold event?
if (pressedStartTime!=-1 && !triggeredHoldEvent)
{
if (millis()-pressedStartTime > holdEventThreshold)
{
if (cb_onHold)
{
cb_onHold(*this);
triggeredHoldEvent = true;
}
}
}
}
}
/*
|| @description
|| | Return the bitRead(state,CURRENT) of the switch
|| #
||
|| @return true if button is pressed
*/
bool Button::isPressed(bool proc)
{
if(proc) process();
return bitRead(state,CURRENT);
}
/*
|| @description
|| | Return true if the button has been pressed
|| #
*/
bool Button::wasPressed(bool proc)
{
if(proc) process();
return bitRead(state,CURRENT);
}
/*
|| @description
|| | Return true if state has been changed
|| #
*/
bool Button::stateChanged(bool proc)
{
if(proc) process();
return bitRead(state,CHANGED);
}
/*
|| @description
|| | Return true if the button is pressed, and was not pressed before
|| #
*/
bool Button::uniquePress()
{
process();
return (isPressed(false) && stateChanged(false));
}
/*
|| @description
|| | onHold polling model
|| | Check to see if the button has been pressed for time ms
|| | This will clear the counter for next iteration and thus return true once
|| #
*/
bool Button::held(unsigned int time /*=0*/)
{
process();
unsigned int threshold = time ? time : holdEventThreshold; //use holdEventThreshold if time == 0
//should we trigger a onHold event?
if (pressedStartTime!=-1 && !triggeredHoldEvent)
{
if (millis()-pressedStartTime > threshold)
{
triggeredHoldEvent = true;
return true;
}
}
return false;
}
/*
|| @description
|| | Polling model for holding, this is true every check after hold time
|| | Check to see if the button has been pressed for time ms
|| #
*/
bool Button::heldFor(unsigned int time)
{
if (isPressed())
{
if (millis()-pressedStartTime > time) { return true; }
}
return false;
}
/*
|| @description
|| | Set the hold event time threshold
|| #
*/
void Button::setHoldThreshold(unsigned int holdTime)
{
holdEventThreshold = holdTime;
}
/*
|| @description
|| | Register a handler for presses on this button
|| #
||
|| @parameter handler The function to call when this button is pressed
*/
void Button::pressHandler(buttonEventHandler handler)
{
cb_onPress = handler;
}
/*
|| @description
|| | Register a handler for releases on this button
|| #
||
|| @parameter handler The function to call when this button is released
*/
void Button::releaseHandler(buttonEventHandler handler)
{
cb_onRelease = handler;
}
/*
|| @description
|| | Register a handler for clicks on this button
|| #
||
|| @parameter handler The function to call when this button is clicked
*/
void Button::clickHandler(buttonEventHandler handler)
{
cb_onClick = handler;
}
/*
|| @description
|| | Register a handler for when this button is held
|| #
||
|| @parameter handler The function to call when this button is held
*/
void Button::holdHandler(buttonEventHandler handler, unsigned int holdTime /*=0*/)
{
if (holdTime>0) { setHoldThreshold(holdTime); }
cb_onHold = handler;
}
/*
|| @description
|| | Get the time this button has been held
|| #
||
|| @return The time this button has been held
*/
unsigned int Button::holdTime() const { if (pressedStartTime!=-1) { return millis()-pressedStartTime; } else return 0; }
/*
|| @description
|| | Compare a button object against this
|| #
||
|| @parameter rhs the Button to compare against this Button
||
|| @return true if they are the same
*/
bool Button::operator==(Button &rhs)
{
return (this==&rhs);
}