Line data Source code
1 : #pragma once 2 : 3 : #include <AH/STL/utility> // std::forward 4 : #include <Def/Def.hpp> 5 : #include <Def/TypeTraits.hpp> 6 : #include <MIDI_Outputs/Abstract/EncoderState.hpp> 7 : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp> 8 : 9 : #ifdef ARDUINO 10 : #include <Submodules/Encoder/AHEncoder.hpp> 11 : #else 12 : #include <Encoder.h> // Mock 13 : #endif 14 : 15 : AH_DIAGNOSTIC_WERROR() 16 : 17 : BEGIN_CS_NAMESPACE 18 : 19 : /** 20 : * @brief An abstract class for rotary encoders that send MIDI events. 21 : */ 22 : template <class Enc, class Sender> 23 : class GenericMIDIRotaryEncoder : public MIDIOutputElement { 24 : public: 25 : /** 26 : * @brief Construct a new MIDIRotaryEncoder. 27 : * 28 : * @todo Documentation 29 : */ 30 13 : GenericMIDIRotaryEncoder(Enc &&encoder, MIDIAddress address, 31 : int16_t speedMultiply, uint8_t pulsesPerStep, 32 : const Sender &sender) 33 13 : : encoder(std::forward<Enc>(encoder)), address(address), 34 26 : encstate(speedMultiply, pulsesPerStep), sender(sender) {} 35 : 36 0 : void begin() override { begin_if_possible(encoder); } 37 : 38 32 : void update() override { 39 32 : auto encval = encoder.read(); 40 32 : if (int16_t delta = encstate.update(encval)) { 41 29 : sender.send(delta, address); 42 : } 43 32 : } 44 : 45 : void setSpeedMultiply(int16_t speedMultiply) { 46 : encstate.setSpeedMultiply(speedMultiply); 47 : } 48 : int16_t getSpeedMultiply() const { return encstate.getSpeedMultiply(); } 49 : 50 : /// Get the MIDI address. 51 : MIDIAddress getAddress() const { return this->address; } 52 : /// Set the MIDI address. 53 : void setAddress(MIDIAddress address) { this->address = address; } 54 : 55 : private: 56 : Enc encoder; 57 : MIDIAddress address; 58 : EncoderState<decltype(encoder.read())> encstate; 59 : 60 : public: 61 : Sender sender; 62 : }; 63 : 64 : template <class Sender> 65 : using MIDIRotaryEncoder = GenericMIDIRotaryEncoder<AHEncoder, Sender>; 66 : 67 : template <class Sender> 68 : using BorrowedMIDIRotaryEncoder = GenericMIDIRotaryEncoder<AHEncoder &, Sender>; 69 : 70 : END_CS_NAMESPACE 71 : 72 : AH_DIAGNOSTIC_POP()