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 <Banks/BankableAddresses.hpp> 10 : #include <Def/Def.hpp> 11 : #include <Encoder.h> 12 : #include <MIDI_Outputs/Abstract/MIDIOutputElement.hpp> 13 : 14 : BEGIN_CS_NAMESPACE 15 : 16 : namespace Bankable { 17 : 18 : /** 19 : * @brief An abstract class for rotary encoders that send MIDI events and that 20 : * can be added to a Bank. 21 : */ 22 : template <class BankAddress, class Sender> 23 2 : class MIDIRotaryEncoder : public MIDIOutputElement { 24 : protected: 25 : /** 26 : * @brief Construct a new MIDIRotaryEncoder. 27 : * 28 : * @todo Documentation 29 : */ 30 1 : MIDIRotaryEncoder(BankAddress bankAddress, 31 : const EncoderPinList &pins, 32 : uint8_t speedMultiply, uint8_t pulsesPerStep, 33 : const Sender &sender) 34 1 : : address(bankAddress), encoder{pins.A, pins.B}, 35 1 : speedMultiply(speedMultiply), 36 2 : pulsesPerStep(pulsesPerStep), sender(sender) {} 37 : 38 : // For tests only 39 : #ifndef ARDUINO 40 1 : MIDIRotaryEncoder(BankAddress bankAddress, const Encoder &encoder, 41 : uint8_t speedMultiply, uint8_t pulsesPerStep, 42 : const Sender &sender) 43 1 : : address(bankAddress), encoder{encoder}, 44 1 : speedMultiply(speedMultiply), 45 4 : pulsesPerStep(pulsesPerStep), sender(sender) {} 46 : #endif 47 : 48 : public: 49 0 : void begin() final override {} 50 2 : void update() final override { 51 2 : long currentPosition = encoder.read(); 52 2 : long difference = (currentPosition - previousPosition) / pulsesPerStep; 53 : // I could do the division inside of the if statement for performance 54 2 : if (difference) { 55 2 : sender.send(difference * speedMultiply, address.getActiveAddress()); 56 2 : previousPosition += difference * pulsesPerStep; 57 2 : } 58 2 : } 59 : 60 : private: 61 : BankAddress address; 62 : Encoder encoder; 63 : const uint8_t speedMultiply; 64 : const uint8_t pulsesPerStep; 65 2 : long previousPosition = 0; 66 : 67 : public: 68 : Sender sender; 69 : }; 70 : 71 : } // namespace Bankable 72 : 73 : END_CS_NAMESPACE