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