Control Surface  1.2.0
MIDI Control Surface library for Arduino
Bankable/Abstract/MIDIRotaryEncoder.hpp
Go to the documentation of this file.
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 
10 #include <Def/Def.hpp>
11 #include <Encoder.h>
13 
15 
16 namespace Bankable {
17 
22 template <class BankAddress, class Sender>
24  protected:
30  MIDIRotaryEncoder(BankAddress bankAddress,
31  const EncoderPinList &pins,
32  uint8_t speedMultiply, uint8_t pulsesPerStep,
33  const Sender &sender)
34  : address(bankAddress), encoder{pins.A, pins.B},
37 
38 // For tests only
39 #ifndef ARDUINO
40  MIDIRotaryEncoder(BankAddress bankAddress, const Encoder &encoder,
41  uint8_t speedMultiply, uint8_t pulsesPerStep,
42  const Sender &sender)
43  : address(bankAddress), encoder{encoder},
46 #endif
47 
48  public:
49  void begin() final override {}
50  void update() final override {
51  long currentPosition = encoder.read();
52  long difference = (currentPosition - previousPosition) / pulsesPerStep;
53  // I could do the division inside of the if statement for performance
54  if (difference) {
55  sender.send(difference * speedMultiply, address.getActiveAddress());
56  previousPosition += difference * pulsesPerStep;
57  }
58  }
59 
60  private:
61  BankAddress address;
62  Encoder encoder;
63  const uint8_t speedMultiply;
64  const uint8_t pulsesPerStep;
65  long previousPosition = 0;
66 
67  public:
68  Sender sender;
69 };
70 
71 } // namespace Bankable
72 
Bankable::MIDIRotaryEncoder::MIDIRotaryEncoder
MIDIRotaryEncoder(BankAddress bankAddress, const EncoderPinList &pins, uint8_t speedMultiply, uint8_t pulsesPerStep, const Sender &sender)
Construct a new MIDIRotaryEncoder.
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:30
AH::Updatable<>
Bankable
A namespace for MIDI elements that can be added to a Bank, to change their address or channel.
Definition: BankAddresses.hpp:7
Bankable::MIDIRotaryEncoder::speedMultiply
const uint8_t speedMultiply
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:63
EncoderPinList::A
uint8_t A
The pin connected to the A pin of the encoder.
Definition: Def.hpp:45
Bankable::MIDIRotaryEncoder::encoder
Encoder encoder
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:62
MIDIOutputElement.hpp
Def.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
Bankable::MIDIRotaryEncoder::update
void update() final override
Update this updatable.
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:50
Bankable::MIDIRotaryEncoder::begin
void begin() final override
Initialize this updatable.
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:49
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
Bankable::MIDIRotaryEncoder::pulsesPerStep
const uint8_t pulsesPerStep
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:64
Bankable::MIDIRotaryEncoder
An abstract class for rotary encoders that send MIDI events and that can be added to a Bank.
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:23
Bankable::MIDIRotaryEncoder::previousPosition
long previousPosition
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:65
EncoderPinList
A struct for the pins of a rotary (quadrature) encoder without a switch.
Definition: Def.hpp:44
Bankable::MIDIRotaryEncoder::sender
Sender sender
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:68
Bankable::MIDIRotaryEncoder::address
BankAddress address
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:61
BankableAddresses.hpp
EncoderPinList::B
uint8_t B
The pin connected to the B pin of the encoder.
Definition: Def.hpp:46