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 2 : 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, 37 : const Sender &sender) 38 4 : : address{bankAddress}, button{pin}, sender{sender} {} 39 : 40 : public: 41 0 : void begin() override { button.begin(); } 42 : 43 0 : void update() override { 44 0 : AH::Button::State state = button.update(); 45 0 : if (state == AH::Button::Falling) 46 0 : toggleState(); 47 0 : } 48 : 49 0 : bool toggleState() { 50 0 : bool newstate = !getState(); 51 0 : setState(newstate); 52 0 : return newstate; 53 0 : } 54 : 55 0 : bool getState() const { return states.get(address.getSelection()); } 56 : 57 0 : void setState(bool state) { 58 0 : states.set(address.getSelection(), state); 59 0 : if (state) { 60 0 : sender.sendOn(address.getActiveAddress()); 61 0 : } else { 62 0 : sender.sendOff(address.getActiveAddress()); 63 : } 64 0 : } 65 : 66 : AH::Button::State getButtonState() const { return button.getState(); } 67 : 68 : private: 69 : BankAddress address; 70 : AH::Button button; 71 : AH::BitArray<NumBanks> states; 72 : 73 : public: 74 : Sender sender; 75 : }; 76 : 77 : } // namespace Bankable 78 : 79 : END_CS_NAMESPACE