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