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 5 : 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, const MIDIAddress &address, 31 : const Sender &sender) 32 10 : : button{pin}, address{address}, sender(sender) {} 33 : 34 2 : void begin() override { button.begin(); } 35 8 : void update() override { 36 8 : AH::Button::State state = button.update(); 37 8 : if (state == AH::Button::Falling) { 38 2 : sender.sendOn(address); 39 8 : } else if (state == AH::Button::Rising) { 40 2 : sender.sendOff(address); 41 2 : } 42 8 : } 43 : 44 : #ifdef AH_INDIVIDUAL_BUTTON_INVERT 45 : void invert() { button.invert(); } 46 : #endif 47 : 48 : AH::Button::State getButtonState() const { return button.getState(); } 49 : 50 : private: 51 : AH::Button button; 52 : const MIDIAddress address; 53 : 54 : public: 55 : Sender sender; 56 : }; 57 : 58 : END_CS_NAMESPACE