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/BankableMIDIOutput.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 Sender> 23 2 : class MIDIRotaryEncoder : public BankableMIDIOutput, public MIDIOutputElement { 24 : protected: 25 : /** 26 : * @brief Construct a new MIDIRotaryEncoder. 27 : * 28 : * @todo Documentation 29 : */ 30 1 : MIDIRotaryEncoder(const OutputBankConfig &config, 31 : const EncoderPinList &pins, 32 : const MIDICNChannelAddress &address, 33 : uint8_t speedMultiply, uint8_t pulsesPerStep, 34 : const Sender &sender) 35 2 : : BankableMIDIOutput(config), encoder{pins.A, pins.B}, address(address), 36 1 : speedMultiply(speedMultiply), 37 3 : pulsesPerStep(pulsesPerStep), sender{sender} {} 38 : 39 : // For tests only 40 : #ifndef ARDUINO 41 1 : MIDIRotaryEncoder(const OutputBankConfig &config, const Encoder &encoder, 42 : const MIDICNChannelAddress &address, 43 : uint8_t speedMultiply, uint8_t pulsesPerStep, 44 : const Sender &sender) 45 2 : : BankableMIDIOutput(config), encoder{encoder}, address(address), 46 1 : speedMultiply(speedMultiply), 47 5 : pulsesPerStep(pulsesPerStep), sender{sender} {} 48 : #endif 49 : 50 : public: 51 0 : void begin() final override {} 52 2 : void update() final override { 53 2 : MIDICNChannelAddress sendAddress = address + getAddressOffset(); 54 2 : long currentPosition = encoder.read(); 55 2 : long difference = (currentPosition - previousPosition) / pulsesPerStep; 56 : // I could do the division inside of the if statement for performance 57 2 : if (difference) { 58 2 : sender.send(difference * speedMultiply, sendAddress); 59 2 : previousPosition += difference * pulsesPerStep; 60 2 : } 61 2 : } 62 : 63 : private: 64 : Encoder encoder; 65 : const MIDICNChannelAddress address; 66 : const uint8_t speedMultiply; 67 : const uint8_t pulsesPerStep; 68 2 : long previousPosition = 0; 69 : 70 : public: 71 : Sender sender; 72 : }; 73 : 74 : } // namespace Bankable 75 : 76 : END_CS_NAMESPACE