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 An abstract class for momentary push buttons that send MIDI events.
11 : *
12 : * The button is debounced.
13 : *
14 : * @see Button
15 : */
16 : template <class Sender>
17 : class MIDIButton : public MIDIOutputElement {
18 : public:
19 : /**
20 : * @brief Construct a new MIDIButton.
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 5 : MIDIButton(pin_t pin, MIDIAddress address, const Sender &sender)
31 5 : : button(pin), address(address), sender(sender) {}
32 :
33 2 : void begin() override { button.begin(); }
34 8 : void update() override {
35 8 : AH::Button::State state = button.update();
36 8 : if (state == AH::Button::Falling) {
37 2 : sender.sendOn(address);
38 6 : } else if (state == AH::Button::Rising) {
39 2 : sender.sendOff(address);
40 : }
41 8 : }
42 :
43 : /// @see @ref AH::Button::invert()
44 : void invert() { button.invert(); }
45 :
46 : AH::Button::State getButtonState() const { return button.getState(); }
47 :
48 : /// Get the MIDI address.
49 : MIDIAddress getAddress() const { return this->address; }
50 : /// Set the MIDI address. Has unexpected consequences if used while the
51 : /// push button is pressed. Use banks if you need to support that.
52 : void setAddressUnsafe(MIDIAddress address) { this->address = address; }
53 :
54 : private:
55 : AH::Button button;
56 : MIDIAddress address;
57 :
58 : public:
59 : Sender sender;
60 : };
61 :
62 : END_CS_NAMESPACE
|