Control Surface new-input
MIDI Control Surface library for Arduino
Button.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
6 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7 
8 #include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp>
10 
12 
18 class Button {
19  public:
27  Button() : pin(NO_PIN) {}
28 
36  Button(pin_t pin);
37 
39  void begin();
40 
51 
53  enum State {
54  Pressed = 0b00,
55  Released = 0b11,
56  Falling = 0b10,
57  Rising = 0b01
58  };
59 
87  State update();
88 
96  State getState() const;
97 
99  static FlashString_t getName(State state);
100 
102  unsigned long previousBounceTime() const;
103 
106  unsigned long stableTime(unsigned long now) const;
107 
109  unsigned long stableTime() const;
110 
119  static void
120  setDebounceTime(unsigned long debounceTime = BUTTON_DEBOUNCE_TIME);
121 
127  static unsigned long getDebounceTime();
128 
129  private:
131 
132  bool prevInput = HIGH;
133  State debouncedState = Released;
134  unsigned long prevBounceTime = 0;
135 
136 #ifdef AH_INDIVIDUAL_BUTTON_INVERT // Edit this in AH/Settings/Settings.hpp
137  bool invertState = false;
138 #else
139  static bool invertState;
140 #endif
141 
144  static unsigned long debounceTime;
145 };
146 
148 
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define AH_INDIVIDUAL_BUTTON_INVERT_STATIC
constexpr PinStatus_t HIGH
std::remove_reference< decltype(*F(""))>::type * FlashString_t
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:18
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:53
pin_t pin
Definition: Button.hpp:130
Button()
Construct a new Button object.
Definition: Button.hpp:27
static unsigned long debounceTime
Edit this in Settings.hpp.
Definition: Button.hpp:144
constexpr pin_t NO_PIN
A special pin number that indicates an unused or invalid pin.
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).