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 An abstract class for momentary push buttons that send MIDI events. 11 : * 12 : * The buttons are debounced. 13 : * 14 : * @see Button 15 : */ 16 : template <class Sender, uint8_t NUMBER_OF_BUTTONS> 17 3 : class MIDIButtons : public MIDIOutputElement { 18 : protected: 19 : /** 20 : * @brief Construct a new MIDIButtons. 21 : * 22 : * @todo Documentation 23 : */ 24 3 : MIDIButtons(const Array<AH::Button, NUMBER_OF_BUTTONS> &buttons, 25 : const MIDICNChannelAddress &baseAddress, 26 : const RelativeMIDICNChannelAddress &incrementAddress, 27 : const Sender &sender) 28 3 : : buttons{buttons}, baseAddress(baseAddress), 29 9 : incrementAddress(incrementAddress), sender{sender} {} 30 : 31 : public: 32 1 : void begin() final override { 33 3 : for (auto &button : buttons) 34 2 : button.begin(); 35 1 : } 36 7 : void update() final override { 37 7 : MIDICNChannelAddress address = baseAddress; 38 21 : for (auto &button : buttons) { 39 14 : AH::Button::State state = button.update(); 40 14 : if (state == AH::Button::Falling) { 41 2 : sender.sendOn(address); 42 14 : } else if (state == AH::Button::Rising) { 43 2 : sender.sendOff(address); 44 2 : } 45 14 : address += incrementAddress; 46 : } 47 7 : } 48 : 49 : AH::Button::State getButtonState(size_t index) const { 50 : return buttons[index].getState(); 51 : } 52 : 53 : #ifdef INDIVIDUAL_BUTTON_INVERT 54 : void invert() { 55 : for (auto &button : buttons) 56 : button.invert(); 57 : } 58 : #endif 59 : 60 : private: 61 : Array<AH::Button, NUMBER_OF_BUTTONS> buttons; 62 : const MIDICNChannelAddress baseAddress; 63 : const RelativeMIDICNChannelAddress incrementAddress; 64 : 65 : public: 66 : Sender sender; 67 : }; 68 : 69 : END_CS_NAMESPACE