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 An abstract class for momentary push buttons that send MIDI events. 14 : * 15 : * The button is debounced. 16 : * 17 : * @see Button 18 : */ 19 : template <class BankAddress, class Sender> 20 12 : class MIDIButton : public MIDIOutputElement { 21 : public: 22 : /** 23 : * @brief Construct a new bankable MIDIButton. 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 12 : MIDIButton(BankAddress bankAddress, pin_t pin, const Sender &sender) 34 24 : : address{bankAddress}, button{pin}, sender(sender) {} 35 : 36 6 : void begin() override { button.begin(); } 37 26 : void update() override { 38 26 : AH::Button::State state = button.update(); 39 26 : if (state == AH::Button::Falling) { 40 7 : address.lock(); 41 7 : sender.sendOn(address.getActiveAddress()); 42 26 : } else if (state == AH::Button::Rising) { 43 7 : sender.sendOff(address.getActiveAddress()); 44 7 : address.unlock(); 45 7 : } 46 26 : } 47 : 48 : #ifdef AH_INDIVIDUAL_BUTTON_INVERT 49 : void invert() { button.invert(); } 50 : #endif 51 : 52 : AH::Button::State getButtonState() const { return button.getState(); } 53 : 54 : private: 55 : BankAddress address; 56 : AH::Button button; 57 : 58 : public: 59 : Sender sender; 60 : }; 61 : 62 : } // namespace Bankable 63 : 64 : END_CS_NAMESPACE