Control Surface  1.1.0
MIDI Control Surface library for Arduino
Abstract/MIDIChordButton.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include <AH/Hardware/Button.hpp>
5 #include <Def/Def.hpp>
8 
10 
11 /**
12  * @brief An abstract class for momentary push buttons that send multiple MIDI
13  * events.
14  *
15  * The button is debounced.
16  *
17  * @see AH::Button
18  */
19 template <class Sender>
21  public:
22  /**
23  * @brief Construct a new MIDIChordButton.
24  *
25  * @param pin
26  * The digital input pin with the button connected.
27  * The internal pull-up resistor will be enabled.
28  * @param address
29  * The MIDI address of the base note, containing the note
30  * number [0, 127], channel [CHANNEL_1, CHANNEL_16], and optional
31  * cable number [0, 15].
32  * @param chord
33  * The chord containing the intervals of the other notes to play.
34  * @param sender
35  * The MIDI Note sender to use.
36  *
37  * @tparam N
38  * The number of notes in the chord.
39  */
40  template <uint8_t N>
42  const Chord<N> &chord, const Sender &sender)
43  : button{pin}, address(address),
45  // TODO: can I somehow get rid of the dynamic memory allocation here?
46 
47  void begin() final override { button.begin(); }
48  void update() final override {
50  MIDICNChannelAddress sendAddress = address;
51  if (state == AH::Button::Falling) {
52  if (newChord)
53  chord = std::move(newChord);
54  sender.sendOn(sendAddress);
55  for (int8_t offset : *chord)
56  sender.sendOn(sendAddress + offset);
57  } else if (state == AH::Button::Rising) {
58  sender.sendOff(sendAddress);
59  for (int8_t offset : *chord)
60  sender.sendOff(sendAddress + offset);
61  }
62  }
63 
64 #ifdef INDIVIDUAL_BUTTON_INVERT
65  void invert() { button.invert(); }
66 #endif
67 
69 
70  template <uint8_t N>
71  void setChord(const Chord<N> &chord) {
72  newChord = new Chord<N>(chord);
73  }
74 
75  private:
80 
81  public:
82  Sender sender;
83 };
84 
AH::Button::Rising
< Input went from high to low (1,0)
Definition: Button.hpp:57
MIDIChordButton::begin
void begin() final override
Initialize this updatable.
Definition: Abstract/MIDIChordButton.hpp:47
AH::Updatable<>
Button.hpp
Chord
Definition: Chords.hpp:16
AH::Button
A class for reading and debouncing buttons and switches.
Definition: Button.hpp:18
MIDIOutputElement.hpp
MIDIChordButton::chord
AH::UniquePtr< const IChord > chord
Definition: Abstract/MIDIChordButton.hpp:78
Def.hpp
Chords.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
MIDIChordButton::newChord
AH::UniquePtr< const IChord > newChord
Definition: Abstract/MIDIChordButton.hpp:79
MIDIChordButton::update
void update() final override
Update this updatable.
Definition: Abstract/MIDIChordButton.hpp:48
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
UniquePtr.hpp
AH::Button::begin
void begin()
Initialize (enable the internal pull-up resistor).
Definition: Button.cpp:11
MIDICNChannelAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDICNChannelAddress.hpp:82
MIDIChordButton::button
AH::Button button
Definition: Abstract/MIDIChordButton.hpp:76
AH::Button::update
State update()
Read the button and return its new state.
Definition: Button.cpp:19
MIDIChordButton::MIDIChordButton
MIDIChordButton(pin_t pin, const MIDICNChannelAddress &address, const Chord< N > &chord, const Sender &sender)
Construct a new MIDIChordButton.
Definition: Abstract/MIDIChordButton.hpp:41
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
AH::Button::State
State
An enumeration of the different states a button can be in.
Definition: Button.hpp:53
MIDIChordButton::getButtonState
AH::Button::State getButtonState() const
Definition: Abstract/MIDIChordButton.hpp:68
AH::UniquePtr< const IChord >
MIDIChordButton
An abstract class for momentary push buttons that send multiple MIDI events.
Definition: Abstract/MIDIChordButton.hpp:20
AH::MakeUnique
UniquePtr< T > MakeUnique(Args &&... args)
Definition: UniquePtr.hpp:66
MIDIChordButton::address
const MIDICNChannelAddress address
Definition: Abstract/MIDIChordButton.hpp:77
MIDIChordButton::sender
Sender sender
Definition: Abstract/MIDIChordButton.hpp:82
MIDIChordButton::setChord
void setChord(const Chord< N > &chord)
Definition: Abstract/MIDIChordButton.hpp:71
AH::Button::Falling
< Input went from high to high (1,1)
Definition: Button.hpp:56