|
0
|
1 #ifndef Button_h
|
|
|
2 #define Button_h
|
|
|
3
|
|
1
|
4
|
|
0
|
5 /**
|
|
|
6 utility class for debouncing a button based on pin state and time.
|
|
|
7
|
|
|
8 notes time of state change. ignores further state changes for
|
|
|
9 Button::BOUNCE_THRESHOLD miliseconds.
|
|
|
10
|
|
|
11 (c) 2017 arkadiusz@wobblyhead.net
|
|
|
12 */
|
|
|
13 class Button {
|
|
|
14 public:
|
|
|
15 // stabilised button state
|
|
1
|
16 bool isOn;
|
|
0
|
17 // millis() time of last state change
|
|
|
18 unsigned long lastChangeMs;
|
|
1
|
19 // flag, true if state changed to pressed
|
|
|
20 bool isPressed;
|
|
|
21 // flag, true if state changed to off
|
|
|
22 bool isReleased;
|
|
|
23
|
|
0
|
24 /**
|
|
|
25 create an uninitialised Button.
|
|
|
26 setup() must be called after this to make it usable.
|
|
|
27 */
|
|
|
28 Button();
|
|
|
29
|
|
|
30 /**
|
|
|
31 setup button instance.
|
|
|
32 assumes internal pullup.
|
|
|
33 @param {byte} pin button input pin
|
|
|
34 */
|
|
|
35 void setup(byte pin);
|
|
|
36
|
|
|
37 /**
|
|
|
38 setup button instance.
|
|
|
39 @param {byte} pin button input pin
|
|
|
40 @param {bool} pu if true, button uses internal pullup
|
|
|
41 */
|
|
|
42 void setup(byte pin, bool pu);
|
|
|
43
|
|
|
44 /**
|
|
|
45 check pin and update button state.
|
|
|
46 calls millis() before calling update(ulong). if a recent millis()
|
|
|
47 value is known, call update(ulong) directly to avoid the additional
|
|
|
48 millis() call.
|
|
|
49 */
|
|
|
50 void update();
|
|
|
51
|
|
|
52 /**
|
|
|
53 check pin and update button state.
|
|
|
54 @param {unsigned long} ms current millis() value
|
|
|
55 */
|
|
|
56 void update(unsigned long ms);
|
|
|
57
|
|
|
58 /** bounce threshold in miliseconds. state changes are ignored for that
|
|
|
59 * long after a change.
|
|
|
60 */
|
|
|
61 const unsigned long BOUNCE_THRESHOLD = 100;
|
|
|
62
|
|
|
63 private:
|
|
|
64 byte pin;
|
|
|
65 bool isPullup;
|
|
|
66
|
|
|
67 /**
|
|
|
68 read pin state, translate to pressed state.
|
|
|
69 @return {bool} true if buton is pressed
|
|
|
70 */
|
|
|
71 bool readState();
|
|
|
72
|
|
|
73 };
|
|
|
74
|
|
|
75 #endif
|