Line data Source code
1 : #pragma once 2 : 3 : #include <AH/Containers/BitArray.hpp> 4 : #include <AH/Hardware/Button.hpp> 5 : #include <Banks/BankableAddresses.hpp> 6 : #include <Def/Def.hpp> 7 : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp> 8 : 9 : BEGIN_CS_NAMESPACE 10 : 11 : namespace Bankable { 12 : 13 : /** 14 : * @brief A class for momentary buttons and switches that send MIDI events. 15 : * 16 : * The button is debounced, and the button is latched (press once to enable, 17 : * press again to disable) (toggle). 18 : * 19 : * @see AH::Button 20 : */ 21 : template <uint8_t NumBanks, class BankAddress, class Sender> 22 : class MIDIButtonLatched : public MIDIOutputElement { 23 : protected: 24 : /** 25 : * @brief Create a new bankable MIDIButtonLatched object on the given pin 26 : * and address. 27 : * 28 : * @param bankAddress 29 : * The bankable MIDI address to send to. 30 : * @param pin 31 : * The digital input pin with the button connected. 32 : * The internal pull-up resistor will be enabled. 33 : * @param sender 34 : * The MIDI sender to use. 35 : */ 36 2 : MIDIButtonLatched(BankAddress bankAddress, pin_t pin, const Sender &sender) 37 2 : : address(bankAddress), button(pin), sender(sender) {} 38 : 39 : public: 40 0 : void begin() override { button.begin(); } 41 : 42 0 : void update() override { 43 0 : AH::Button::State state = button.update(); 44 0 : if (state == AH::Button::Falling) 45 0 : toggleState(); 46 0 : } 47 : 48 0 : bool toggleState() { 49 0 : bool newstate = !getState(); 50 0 : setState(newstate); 51 0 : return newstate; 52 : } 53 : 54 0 : bool getState() const { return states.get(address.getSelection()); } 55 : 56 0 : void setState(bool state) { 57 0 : states.set(address.getSelection(), state); 58 0 : if (state) { 59 0 : sender.sendOn(address.getActiveAddress()); 60 : } else { 61 0 : sender.sendOff(address.getActiveAddress()); 62 : } 63 0 : } 64 : 65 : AH::Button::State getButtonState() const { return button.getState(); } 66 : 67 : protected: 68 : BankAddress address; 69 : AH::Button button; 70 : AH::BitArray<NumBanks> states; 71 : 72 : public: 73 : Sender sender; 74 : }; 75 : 76 : } // namespace Bankable 77 : 78 : END_CS_NAMESPACE