Control Surface  1.1.1
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 
19 template <class Sender>
21  protected:
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 
48  bool toggleState() {
49  setState(!getState());
50  return getState();
51  }
52 
54  bool getState() const { return state; }
55 
58  void setState(bool state) {
59  this->state = state;
60  state ? sender.sendOn(address) : sender.sendOff(address);
61  }
62 
63 #ifdef AH_INDIVIDUAL_BUTTON_INVERT
64  void invert() { button.invert(); }
65 #endif
66 
69 
70  private:
73  bool state = false;
74 
75  public:
76  Sender sender;
77 };
78 
MIDIButtonLatched::update
void update() final override
Update this updatable.
Definition: Abstract/MIDIButtonLatched.hpp:40
MIDIButtonLatched::button
AH::Button button
Definition: Abstract/MIDIButtonLatched.hpp:71
AH::Updatable<>
MIDIButtonLatched::invert
void invert()
Definition: Abstract/MIDIButtonLatched.hpp:64
MIDIButtonLatched::getState
bool getState() const
Get the current state.
Definition: Abstract/MIDIButtonLatched.hpp:54
Button.hpp
AH::Button::Falling
Input went from high to low (1,0)
Definition: Button.hpp:56
AH::Button::State
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:53
AH::Button::getState
State getState() const
Get the state of the button, without updating it.
Definition: Button.cpp:36
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::Button
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:18
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
MIDIOutputElement.hpp
MIDIButtonLatched::toggleState
bool toggleState()
Flip the state (on → off or off → on).
Definition: Abstract/MIDIButtonLatched.hpp:48
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
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
MIDIButtonLatched::setState
void setState(bool state)
Set the state to the given value.
Definition: Abstract/MIDIButtonLatched.hpp:58
MIDIButtonLatched::begin
void begin() final override
Initialize this updatable.
Definition: Abstract/MIDIButtonLatched.hpp:39
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::sender
Sender sender
Definition: Abstract/MIDIButtonLatched.hpp:76
MIDIButtonLatched::state
bool state
Definition: Abstract/MIDIButtonLatched.hpp:73
MIDIButtonLatched::getButtonState
AH::Button::State getButtonState() const
Get the state of the underlying button.
Definition: Abstract/MIDIButtonLatched.hpp:68
AH::Button::begin
void begin()
Initialize (enable the internal pull-up resistor).
Definition: Button.cpp:11
AH::Button::update
State update()
Read the button and return its new state.
Definition: Button.cpp:19
MIDIButtonLatched
A class for momentary buttons and switches that send MIDI events.
Definition: Abstract/MIDIButtonLatched.hpp:20