Control Surface  1.1.1
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 Sender>
24  protected:
31  const EncoderPinList &pins,
33  uint8_t speedMultiply, uint8_t pulsesPerStep,
34  const Sender &sender)
35  : BankableMIDIOutput(config), encoder{pins.A, pins.B}, address(address),
38 
39 // For tests only
40 #ifndef ARDUINO
41  MIDIRotaryEncoder(const OutputBankConfig &config, const Encoder &encoder,
43  uint8_t speedMultiply, uint8_t pulsesPerStep,
44  const Sender &sender)
48 #endif
49 
50  public:
51  void begin() final override {}
52  void update() final override {
54  long currentPosition = encoder.read();
55  long difference = (currentPosition - previousPosition) / pulsesPerStep;
56  // I could do the division inside of the if statement for performance
57  if (difference) {
58  sender.send(difference * speedMultiply, sendAddress);
59  previousPosition += difference * pulsesPerStep;
60  }
61  }
62 
63  private:
64  Encoder encoder;
66  const uint8_t speedMultiply;
67  const uint8_t pulsesPerStep;
68  long previousPosition = 0;
69 
70  public:
71  Sender sender;
72 };
73 
74 } // namespace Bankable
75 
Bankable::MIDIRotaryEncoder::MIDIRotaryEncoder
MIDIRotaryEncoder(const OutputBankConfig &config, const EncoderPinList &pins, const MIDICNChannelAddress &address, uint8_t speedMultiply, uint8_t pulsesPerStep, const Sender &sender)
Construct a new MIDIRotaryEncoder.
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:30
AH::Updatable<>
BankableMIDIOutput.hpp
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:66
Bankable::MIDIRotaryEncoder::encoder
Encoder encoder
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:64
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:52
Bankable::MIDIRotaryEncoder::begin
void begin() final override
Initialize this updatable.
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:51
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
EncoderPinList::B
uint8_t B
The pin connected to the B pin of the encoder.
Definition: Def.hpp:46
Bankable::MIDIRotaryEncoder::pulsesPerStep
const uint8_t pulsesPerStep
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:67
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
MIDICNChannelAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDICNChannelAddress.hpp:82
Bankable::MIDIRotaryEncoder::previousPosition
long previousPosition
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:68
EncoderPinList::A
uint8_t A
The pin connected to the A pin of the encoder.
Definition: Def.hpp:45
Bankable::MIDIRotaryEncoder::sender
Sender sender
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:71
EncoderPinList
A struct for the pins of a rotary (quadrature) encoder without a switch.
Definition: Def.hpp:44
Bankable::MIDIRotaryEncoder::address
const MIDICNChannelAddress address
Definition: Bankable/Abstract/MIDIRotaryEncoder.hpp:65
BankableMIDIOutput::getAddressOffset
RelativeMIDICNChannelAddress getAddressOffset() const
Get the offset relative to the base address.
Definition: BankableMIDIOutput.hpp:111
OutputBankConfig
A struct for selecting the bank of BankableMIDIOutputs and the bank type.
Definition: BankConfig.hpp:50
BankableMIDIOutput
A base class for all MIDIOutputElements that can be banked.
Definition: BankableMIDIOutput.hpp:84