Control Surface  1.1.0
MIDI Control Surface library for Arduino
Abstract/MIDIButtonLatched.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
5 #include <AH/Hardware/Button.hpp>
6 #include <Def/Def.hpp>
8 
10 
11 /**
12  * @brief A class for momentary buttons and switches that send MIDI events.
13  *
14  * The button is debounced, and the button is latched (press once to enable,
15  * press again to disable) (toggle).
16  *
17  * @see Button
18  */
19 template <class Sender>
21  protected:
22  /**
23  * @brief Create a new MIDIButtonLatched object on the given pin and
24  * address.
25  *
26  * @param pin
27  * The digital input pin to read from.
28  * The internal pull-up resistor will be enabled.
29  * @param address
30  * The MIDI address to send to.
31  * @param sender
32  * The MIDI sender to use.
33  */
35  const Sender &sender)
36  : button{pin}, address{address}, sender{sender} {}
37 
38  public:
39  void begin() final override { button.begin(); }
40  void update() final override {
43  toggleState();
44  }
45 
46  /// Flip the state (on → off or off → on).
47  /// Sends the appropriate MIDI event.
48  bool toggleState() {
49  setState(!getState());
50  return getState();
51  }
52 
53  /// Get the current state.
54  bool getState() const { return state; }
55 
56  /// Set the state to the given value.
57  /// Sends the appropriate MIDI event.
58  void setState(bool state) {
59  this->state = state;
60  state ? sender.sendOn(address) : sender.sendOff(address);
61  }
62 
63 #ifdef INDIVIDUAL_BUTTON_INVERT
64  void invert() { button.invert(); }
65 #endif
66 
67  /// Get the state of the underlying button.
69 
70  private:
73  bool state = false;
74 
75  public:
76  Sender sender;
77 };
78 
MIDIButtonLatched::sender
Sender sender
Definition: Abstract/MIDIButtonLatched.hpp:76
AH::Updatable<>
Button.hpp
MIDIButtonLatched::button
AH::Button button
Definition: Abstract/MIDIButtonLatched.hpp:71
MIDIButtonLatched::getState
bool getState() const
Get the current state.
Definition: Abstract/MIDIButtonLatched.hpp:54
AH::Button
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:18
MIDIOutputElement.hpp
Def.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
AH::Button::getState
State getState() const
Get the state of the button, without updating it.
Definition: Button.cpp:36
AH::Button::begin
void begin()
Initialize (enable the internal pull-up resistor).
Definition: Button.cpp:11
MIDIButtonLatched::setState
void setState(bool state)
Set the state to the given value.
Definition: Abstract/MIDIButtonLatched.hpp:58
MIDIButtonLatched::update
void update() final override
Update this updatable.
Definition: Abstract/MIDIButtonLatched.hpp:40
MIDICNChannelAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDICNChannelAddress.hpp:82
MIDIButtonLatched::address
const MIDICNChannelAddress address
Definition: Abstract/MIDIButtonLatched.hpp:72
AH::Button::update
State update()
Read the button and return its new state.
Definition: Button.cpp:19
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::Button::invert
AH_INDIVIDUAL_BUTTON_INVERT_STATIC void invert()
Invert the state of all buttons, or of this specific button (button pressed is HIGH instead of LOW).
Definition: Button.cpp:13
MIDIButtonLatched::toggleState
bool toggleState()
Flip the state (on → off or off → on).
Definition: Abstract/MIDIButtonLatched.hpp:48
AH::Button::State
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:53
MIDIButtonLatched::MIDIButtonLatched
MIDIButtonLatched(pin_t pin, const MIDICNChannelAddress &address, const Sender &sender)
Create a new MIDIButtonLatched object on the given pin and address.
Definition: Abstract/MIDIButtonLatched.hpp:34
MIDIButtonLatched::getButtonState
AH::Button::State getButtonState() const
Get the state of the underlying button.
Definition: Abstract/MIDIButtonLatched.hpp:68
MIDIButtonLatched::state
bool state
Definition: Abstract/MIDIButtonLatched.hpp:73
MIDIButtonLatched
A class for momentary buttons and switches that send MIDI events.
Definition: Abstract/MIDIButtonLatched.hpp:20
AH::Button::Falling
< Input went from high to high (1,1)
Definition: Button.hpp:56
MIDIButtonLatched::begin
void begin() final override
Initialize this updatable.
Definition: Abstract/MIDIButtonLatched.hpp:39