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 11 : 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 MIDIAddress &address, 28 : int8_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 10 : MIDIRotaryEncoder(const Encoder &encoder, 37 : const MIDIAddress &address, 38 : int8_t speedMultiply, uint8_t pulsesPerStep, 39 : const Sender &sender) 40 10 : : encoder{encoder}, address(address), speedMultiply(speedMultiply), 41 40 : pulsesPerStep(pulsesPerStep), sender(sender) {} 42 : #endif 43 : 44 : public: 45 0 : void begin() final override {} 46 13 : void update() final override { 47 13 : long currentPosition = encoder.read(); 48 13 : long difference = (currentPosition - previousPosition) / pulsesPerStep; 49 13 : if (difference) { 50 11 : sender.send(difference * speedMultiply, address); 51 11 : previousPosition += difference * pulsesPerStep; 52 11 : } 53 13 : } 54 : 55 : private: 56 : Encoder encoder; 57 : const MIDIAddress address; 58 : const int8_t speedMultiply; 59 : const uint8_t pulsesPerStep; 60 11 : long previousPosition = 0; 61 : 62 : public: 63 : Sender sender; 64 : }; 65 : 66 : END_CS_NAMESPACE