Line | |
---|
1 | /*! \file Pushbutton.h
|
---|
2 | *
|
---|
3 | * See the Pushbutton class reference for more information about this library.
|
---|
4 | *
|
---|
5 | * \class Pushbutton Pushbutton.h
|
---|
6 | * \brief Read button presses and releases with debouncing
|
---|
7 | *
|
---|
8 | */
|
---|
9 |
|
---|
10 | #ifndef Pushbutton_h
|
---|
11 | #define Pushbutton_h
|
---|
12 |
|
---|
13 | #include <Arduino.h>
|
---|
14 |
|
---|
15 | #define ZUMO_BUTTON 12
|
---|
16 |
|
---|
17 | #define PULL_UP_DISABLED 0
|
---|
18 | #define PULL_UP_ENABLED 1
|
---|
19 |
|
---|
20 | #define DEFAULT_STATE_LOW 0
|
---|
21 | #define DEFAULT_STATE_HIGH 1
|
---|
22 |
|
---|
23 | class Pushbutton
|
---|
24 | {
|
---|
25 | public:
|
---|
26 |
|
---|
27 | // constructor; takes arguments specifying whether to enable internal pull-up
|
---|
28 | // and the default state of the pin that the button is connected to
|
---|
29 | Pushbutton(unsigned char pin, unsigned char pullUp = PULL_UP_ENABLED, unsigned char defaultState = DEFAULT_STATE_HIGH);
|
---|
30 |
|
---|
31 | // wait for button to be pressed, released, or pressed and released
|
---|
32 | void waitForPress();
|
---|
33 | void waitForRelease();
|
---|
34 | void waitForButton();
|
---|
35 |
|
---|
36 | // indicates whether button is currently pressed
|
---|
37 | boolean isPressed();
|
---|
38 |
|
---|
39 | // more complex functions that return true once for each button transition
|
---|
40 | // from released to pressed or pressed to released
|
---|
41 | boolean getSingleDebouncedPress();
|
---|
42 | boolean getSingleDebouncedRelease();
|
---|
43 |
|
---|
44 | void init()
|
---|
45 | {
|
---|
46 | if (!initialized)
|
---|
47 | {
|
---|
48 | initialized = true;
|
---|
49 | init2();
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | private:
|
---|
54 |
|
---|
55 | unsigned char _pin;
|
---|
56 | unsigned char _pullUp;
|
---|
57 | unsigned char _defaultState;
|
---|
58 | unsigned char gsdpState;
|
---|
59 | unsigned char gsdrState;
|
---|
60 | unsigned int gsdpPrevTimeMillis;
|
---|
61 | unsigned int gsdrPrevTimeMillis;
|
---|
62 | boolean initialized;
|
---|
63 |
|
---|
64 | // initializes I/O pin for use as button input
|
---|
65 | void init2();
|
---|
66 |
|
---|
67 | boolean _isPressed();
|
---|
68 | };
|
---|
69 |
|
---|
70 | #endif
|
---|
Note:
See
TracBrowser
for help on using the repository browser.