Romi32U4 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 
101  bool getSingleDebouncedPress();
102 
114  bool getSingleDebouncedRelease();
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 };
Main class for interfacing with pushbuttons.
Definition: Pushbutton.h:137
#define DEFAULT_STATE_HIGH
Definition: Pushbutton.h:28
General pushbutton class that handles debouncing.
Definition: Pushbutton.h:70
#define PULL_UP_ENABLED
Definition: Pushbutton.h:20