Line data Source code
1 : #pragma once 2 : 3 : #if not defined(Encoder_h_) && not defined(IDE) 4 : #error \ 5 : "The PJRC Encoder library should be included before the Control-Surface " \ 6 : "library. (#include <Encoder.h>)" 7 : #endif 8 : 9 : #include <Def/Def.hpp> 10 : #include <Encoder.h> 11 : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp> 12 : 13 : BEGIN_CS_NAMESPACE 14 : 15 : /** 16 : * @brief An abstract class for rotary encoders that send MIDI events. 17 : */ 18 : template <class Sender> 19 8 : class MIDIRotaryEncoder : public MIDIOutputElement { 20 : protected: 21 : /** 22 : * @brief Construct a new MIDIRotaryEncoder. 23 : * 24 : * @todo Documentation 25 : */ 26 1 : MIDIRotaryEncoder(const EncoderPinList &pins, 27 : const MIDICNChannelAddress &address, 28 : uint8_t speedMultiply, uint8_t pulsesPerStep, 29 : const Sender &sender) 30 1 : : encoder{pins.A, pins.B}, address(address), 31 1 : speedMultiply(speedMultiply), 32 2 : pulsesPerStep(pulsesPerStep), sender{sender} {} 33 : 34 : // For tests only 35 : #ifndef ARDUINO 36 7 : MIDIRotaryEncoder(const Encoder &encoder, 37 : const MIDICNChannelAddress &address, 38 : uint8_t speedMultiply, uint8_t pulsesPerStep, 39 : const Sender &sender) 40 7 : : encoder{encoder}, address(address), speedMultiply(speedMultiply), 41 28 : pulsesPerStep(pulsesPerStep), sender{sender} {} 42 : #endif 43 : 44 : public: 45 0 : void begin() final override {} 46 10 : void update() final override { 47 10 : long currentPosition = encoder.read(); 48 10 : long difference = (currentPosition - previousPosition) / pulsesPerStep; 49 10 : if (difference) { 50 8 : sender.send(difference * speedMultiply, address); 51 8 : previousPosition += difference * pulsesPerStep; 52 8 : } 53 10 : } 54 : 55 : private: 56 : Encoder encoder; 57 : const MIDICNChannelAddress address; 58 : const uint8_t speedMultiply; 59 : const uint8_t pulsesPerStep; 60 8 : long previousPosition = 0; 61 : 62 : public: 63 : Sender sender; 64 : }; 65 : 66 : END_CS_NAMESPACE