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