Control Surface  1.1.0
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 
13 /**
14  * @brief A class for reading and debouncing buttons and switches.
15  *
16  * @ingroup AH_HardwareUtils
17  */
18 class Button {
19  public:
20  /**
21  * @brief Construct a new Button object.
22  *
23  * **This constructor should not be used.**
24  * It is just a way to easily create arrays of buttons, and initializing
25  * them later.
26  */
27  Button() : pin(NO_PIN) {}
28 
29  /**
30  * @brief Construct a new Button object.
31  *
32  * @param pin
33  * The digital pin to read from. The internal pull-up resistor
34  * will be enabled when `begin` is called.
35  */
36  Button(pin_t pin);
37 
38  /// @brief Initialize (enable the internal pull-up resistor).
39  void begin();
40 
41  /**
42  * @brief Invert the state of all buttons, or of this specific button
43  * (button pressed is `HIGH` instead of `LOW`).
44  *
45  * @note This affects **all** Button objects if
46  * `AH_INDIVIDUAL_BUTTON_INVERT` is not defined.
47  *
48  * @see AH_INDIVIDUAL_BUTTON_INVERT
49  */
51 
52  /// @brief An enumeration of the different states a button can be in.
53  enum State {
54  Pressed = 0b00, /// < Input went from low to low (0,0)
55  Released = 0b11, /// < Input went from high to high (1,1)
56  Falling = 0b10, /// < Input went from high to low (1,0)
57  Rising = 0b01 /// < Input went from low to high (0,1)
58  };
59 
60  /**
61  * @brief Read the button and return its new state.
62  *
63  * The button is debounced, the debounce time can be set in
64  * Settings.hpp: #BUTTON_DEBOUNCE_TIME.
65  *
66  * ```
67  * Debounce time: ├────┤
68  *
69  * Raw input:
70  * HIGH ──────┐ ┌──────┐ ┌─┐ ┌─┐ ┌──────┐ ┌────────
71  * LOW └──────┘ └─┘ └──────┘ └─┘ └─┘
72  * ├────┤ ├────┤ ├─┼─┼────┤ ├─┼─┼────┤ ├─┼────┤
73  *
74  * Debounced output:
75  * HIGH ──────┐ ┌──────┐ ┌──────────┐ ┌───
76  * LOW └──────┘ └──────────┘ └──────┘
77  *
78  * States:
79  * HIGH ────────────────┐ ┌─────────────────
80  * LOW └──────────────────┘
81  * RELEASED FALLING PRESSED RISING
82  * ```
83  *
84  * @return The state of the button, either Button::PRESSED,
85  * Button::RELEASED, Button::FALLING or Button::RISING.
86  */
87  State update();
88 
89  /**
90  * @brief Get the state of the button, without updating it.
91  * Returns the same value as the last call to @ref update.
92  *
93  * @return The state of the button, either Button::PRESSED,
94  * Button::RELEASED, Button::FALLING or Button::RISING.
95  */
96  State getState() const;
97 
98  /// @brief Return the name of the state as a string.
99  static const __FlashStringHelper *getName(State state);
100 
101  /// Return the time point (in milliseconds) when the button last bounced.
102  unsigned long previousBounceTime() const;
103 
104  /// Return the time (in milliseconds) that the button has been stable for,
105  /// compared to the given time point.
106  unsigned long stableTime(unsigned long now) const;
107 
108  /// Return the time (in milliseconds) that the button has been stable for.
109  unsigned long stableTime() const;
110 
111  private:
113 
114  bool prevInput = HIGH;
115  State debouncedState = Released;
116  unsigned long prevBounceTime = 0;
117 
118 #ifdef AH_INDIVIDUAL_BUTTON_INVERT // Edit this in AH/Settings/Settings.hpp
119  bool invertState = false;
120 #else
121  static bool invertState;
122 #endif
123 
124  /// Edit this in Settings.hpp
125  /// @see BUTTON_DEBOUNCE_TIME
126  constexpr static unsigned long debounceTime = BUTTON_DEBOUNCE_TIME;
127 };
128 
130 
AH::NO_PIN
constexpr pin_t NO_PIN
A special pin number that indicates an unused or invalid pin.
Definition: Hardware-Types.hpp:24
Warnings.hpp
AH::Button
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:18
SettingsWrapper.hpp
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH::Button::Button
Button()
Construct a new Button object.
Definition: Button.hpp:27
AH_INDIVIDUAL_BUTTON_INVERT_STATIC
#define AH_INDIVIDUAL_BUTTON_INVERT_STATIC
Definition: AH/Settings/SettingsWrapper.hpp:29
HIGH
const uint8_t HIGH
Definition: ExtendedInputOutput.hpp:46
AH::Button::pin
pin_t pin
Definition: Button.hpp:112
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::Button::State
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:53
AH::Button::invertState
static bool invertState
Definition: Button.hpp:121
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
AH::BUTTON_DEBOUNCE_TIME
constexpr unsigned long BUTTON_DEBOUNCE_TIME
The debounce time for momentary push buttons in milliseconds.
Definition: AH/Settings/Settings.hpp:74