Line data Source code
1 : #pragma once
2 :
3 : #include <AH/Hardware/Button.hpp>
4 : #include <Def/Def.hpp>
5 : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp>
6 :
7 : BEGIN_CS_NAMESPACE
8 :
9 : /**
10 : * @brief A class for latching buttons and switches that send MIDI events.
11 : *
12 : * The button is debounced.
13 : *
14 : * @see Button
15 : */
16 : template <class Sender>
17 : class MIDIButtonLatching : public MIDIOutputElement {
18 : protected:
19 : /**
20 : * @brief Construct a new MIDIButtonLatching.
21 : *
22 : * @param pin
23 : * The digital input pin with the button connected.
24 : * The internal pull-up resistor will be enabled.
25 : * @param address
26 : * The MIDI address to send to.
27 : * @param sender
28 : * The MIDI sender to use.
29 : */
30 3 : MIDIButtonLatching(pin_t pin, MIDIAddress address, const Sender &sender)
31 3 : : button(pin), address(address), sender(sender) {}
32 :
33 : public:
34 1 : void begin() override { button.begin(); }
35 4 : void update() override {
36 4 : AH::Button::State state = button.update();
37 4 : if (state == AH::Button::Falling || state == AH::Button::Rising) {
38 2 : sender.sendOn(address);
39 2 : sender.sendOff(address);
40 : }
41 4 : }
42 :
43 : AH::Button::State getButtonState() const { return button.getState(); }
44 :
45 : /// Get the MIDI address.
46 : MIDIAddress getAddress() const { return this->address; }
47 : /// Set the MIDI address.
48 : void setAddress(MIDIAddress address) { this->address = address; }
49 :
50 : private:
51 : AH::Button button;
52 : MIDIAddress address;
53 :
54 : public:
55 : Sender sender;
56 : };
57 :
58 : END_CS_NAMESPACE
|