comparison Button.h @ 0:968a3e3eb8ef

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