Control Surface  1.1.0
MIDI Control Surface library for Arduino
IncrementDecrementButtons.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 "Button.hpp"
9 
11 
12 /**
13  * @brief A class for buttons that increment and decrement some counter or
14  * setting.
15  *
16  * It behaves the same way as a computer keyboard: when you press the increment
17  * (decrement) button, it increments (decrements) the counter once.
18  * If you keep on pressing it for longer than a certain threshold, it keeps on
19  * incrementing (decrementing) at a faster rate, until you release it.
20  * If both the increment and the decrement button are pressed at once, it resets
21  * the counter.
22  *
23  * @ingroup AH_HardwareUtils
24  */
26  public:
27  /**
28  * @brief Create a IncrementDecrementButtons object.
29  *
30  * @param incrementButton
31  * The button to increment the counter.
32  * The button is copied.
33  * @param decrementButton
34  * The button to decrement the counter.
35  * The button is copied.
36  */
37  IncrementDecrementButtons(const Button &incrementButton,
38  const Button &decrementButton)
39  : incrementButton(incrementButton), decrementButton(decrementButton) {}
40 
41  /// @see Button::begin
42  void begin() {
43  incrementButton.begin();
44  decrementButton.begin();
45  }
46 
47  /**
48  * @brief An enumeration of the different actions to be performed by the
49  * counter.
50  * @todo Add states for initial press.
51  */
52  enum State {
53  Nothing = 0, ///< The counter should not be incremented.
54  Increment, ///< The counter should be incremented.
55  Decrement, ///< The counter should be decremented.
56  Reset, ///< The counter should be reset to the initial value.
57  };
58 
59  /**
60  * @brief Update and return the state of the increment/decrement button.
61  */
62  State update() { return state = updateImplementation(); }
63 
64  /**
65  * @brief Return the state of the increment/decrement button without
66  * updating it.
67  *
68  * Returns the same value as the last @ref update call.
69  */
70  State getState() const { return state; }
71 
72 #ifdef AH_INDIVIDUAL_BUTTON_INVERT
73  /// @see Button::invert
74  void invert() {
75  incrementButton.invert();
76  decrementButton.invert();
77  }
78 #endif
79 
80  protected:
81  State updateImplementation();
82 
83  private:
86 
87  enum {
91  } longPressState = Initial;
92  unsigned long longPressRepeat;
93  State state = Nothing;
94 };
95 
97 
Warnings.hpp
AH::Button
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:18
AH::IncrementDecrementButtons
A class for buttons that increment and decrement some counter or setting.
Definition: IncrementDecrementButtons.hpp:25
AH::IncrementDecrementButtons::begin
void begin()
Definition: IncrementDecrementButtons.hpp:42
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH::IncrementDecrementButtons::incrementButton
Button incrementButton
Definition: IncrementDecrementButtons.hpp:84
AH::IncrementDecrementButtons::Initial
Definition: IncrementDecrementButtons.hpp:88
AH::IncrementDecrementButtons::AfterReset
Definition: IncrementDecrementButtons.hpp:90
AH::IncrementDecrementButtons::LongPress
Definition: IncrementDecrementButtons.hpp:89
AH::IncrementDecrementButtons::longPressRepeat
unsigned long longPressRepeat
Definition: IncrementDecrementButtons.hpp:92
AH::IncrementDecrementButtons::IncrementDecrementButtons
IncrementDecrementButtons(const Button &incrementButton, const Button &decrementButton)
Create a IncrementDecrementButtons object.
Definition: IncrementDecrementButtons.hpp:37
AH::IncrementDecrementButtons::update
State update()
Update and return the state of the increment/decrement button.
Definition: IncrementDecrementButtons.hpp:62
AH::IncrementDecrementButtons::getState
State getState() const
Return the state of the increment/decrement button without updating it.
Definition: IncrementDecrementButtons.hpp:70
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
AH::IncrementDecrementButtons::Reset
The counter should be reset to the initial value.
Definition: IncrementDecrementButtons.hpp:56
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::IncrementDecrementButtons::Increment
The counter should be incremented.
Definition: IncrementDecrementButtons.hpp:54
AH::IncrementDecrementButtons::State
State
An enumeration of the different actions to be performed by the counter.
Definition: IncrementDecrementButtons.hpp:52
AH::IncrementDecrementButtons::Decrement
The counter should be decremented.
Definition: IncrementDecrementButtons.hpp:55
AH::IncrementDecrementButtons::decrementButton
Button decrementButton
Definition: IncrementDecrementButtons.hpp:85