Pushbutton library
Pushbutton.h
Go to the documentation of this file.
1 // Copyright Pololu Corporation. For more information, see http://www.pololu.com/
2 
12 #pragma once
13 
14 #include <Arduino.h>
15 
17 #define PULL_UP_DISABLED 0
18 
20 #define PULL_UP_ENABLED 1
21 
24 #define DEFAULT_STATE_LOW 0
25 
28 #define DEFAULT_STATE_HIGH 1
29 
35 #define ZUMO_BUTTON 12
36 
37 // \cond
43 class PushbuttonStateMachine
44 {
45 public:
46 
47  PushbuttonStateMachine();
48 
51  bool getSingleDebouncedRisingEdge(bool value);
52 
53 private:
54 
55  uint8_t state;
56  uint16_t prevTimeMillis;
57 };
58 // \endcond
59 
71 {
72 public:
73 
79  void waitForPress();
80 
86  void waitForRelease();
87 
92  void waitForButton();
93 
102 
115 
122  virtual bool isPressed() = 0;
123 
124 private:
125 
126  PushbuttonStateMachine pressState;
127  PushbuttonStateMachine releaseState;
128 };
129 
138 {
139 public:
140 
154  Pushbutton(uint8_t pin, uint8_t pullUp = PULL_UP_ENABLED,
155  uint8_t defaultState = DEFAULT_STATE_HIGH);
156 
157  virtual bool isPressed();
158 
159 private:
160 
161  void init()
162  {
163  if (!initialized)
164  {
165  initialized = true;
166  init2();
167  }
168  }
169 
170  void init2();
171 
172  bool initialized;
173  uint8_t _pin;
174  bool _pullUp;
175  bool _defaultState;
176 };
void waitForRelease()
Waits until the button is released and takes care of debouncing.
Definition: Pushbutton.cpp:94
virtual bool isPressed()
indicates whether button is currently pressed without any debouncing.
Definition: Pushbutton.cpp:142
bool getSingleDebouncedPress()
Uses a state machine to return true once after each time it detects the button moving from the releas...
Definition: Pushbutton.cpp:110
void waitForPress()
Waits until the button is pressed and takes care of debouncing.
Definition: Pushbutton.cpp:84
Main class for interfacing with pushbuttons.
Definition: Pushbutton.h:137
Pushbutton(uint8_t pin, uint8_t pullUp=PULL_UP_ENABLED, uint8_t defaultState=DEFAULT_STATE_HIGH)
Definition: Pushbutton.cpp:120
#define DEFAULT_STATE_HIGH
Definition: Pushbutton.h:28
void waitForButton()
Waits until the button is pressed and then waits until the button is released, taking care of debounc...
Definition: Pushbutton.cpp:104
bool getSingleDebouncedRelease()
Uses a state machine to return true once after each time it detects the button moving from the presse...
Definition: Pushbutton.cpp:115
virtual bool isPressed()=0
indicates whether button is currently pressed without any debouncing.
General pushbutton class that handles debouncing.
Definition: Pushbutton.h:70
#define PULL_UP_ENABLED
Definition: Pushbutton.h:20