Control Surface  1.1.1
MIDI Control Surface library for Arduino
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 
9 #include <Def/Def.hpp>
10 #include <Encoder.h>
12 
14 
18 template <class Sender>
20  protected:
28  int8_t speedMultiply, uint8_t pulsesPerStep,
29  const Sender &sender)
30  : encoder{pins.A, pins.B}, address(address),
33 
34 // For tests only
35 #ifndef ARDUINO
36  MIDIRotaryEncoder(const Encoder &encoder,
38  int8_t speedMultiply, uint8_t pulsesPerStep,
39  const Sender &sender)
42 #endif
43 
44  public:
45  void begin() final override {}
46  void update() final override {
47  long currentPosition = encoder.read();
48  long difference = (currentPosition - previousPosition) / pulsesPerStep;
49  if (difference) {
50  sender.send(difference * speedMultiply, address);
51  previousPosition += difference * pulsesPerStep;
52  }
53  }
54 
55  private:
56  Encoder encoder;
58  const int8_t speedMultiply;
59  const uint8_t pulsesPerStep;
60  long previousPosition = 0;
61 
62  public:
63  Sender sender;
64 };
65 
AH::Updatable
A super class for object that have to be updated regularly.
Definition: Updatable.hpp:25
MIDIRotaryEncoder
An abstract class for rotary encoders that send MIDI events.
Definition: Abstract/MIDIRotaryEncoder.hpp:19
MIDIRotaryEncoder::encoder
Encoder encoder
Definition: Abstract/MIDIRotaryEncoder.hpp:56
MIDIRotaryEncoder::MIDIRotaryEncoder
MIDIRotaryEncoder(const EncoderPinList &pins, const MIDICNChannelAddress &address, int8_t speedMultiply, uint8_t pulsesPerStep, const Sender &sender)
Construct a new MIDIRotaryEncoder.
Definition: Abstract/MIDIRotaryEncoder.hpp:26
MIDIOutputElement.hpp
MIDIRotaryEncoder::speedMultiply
const int8_t speedMultiply
Definition: Abstract/MIDIRotaryEncoder.hpp:58
Def.hpp
MIDIRotaryEncoder::begin
void begin() final override
Initialize this updatable.
Definition: Abstract/MIDIRotaryEncoder.hpp:45
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
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
MIDICNChannelAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDICNChannelAddress.hpp:82
MIDIRotaryEncoder::previousPosition
long previousPosition
Definition: Abstract/MIDIRotaryEncoder.hpp:60
MIDIRotaryEncoder::update
void update() final override
Update this updatable.
Definition: Abstract/MIDIRotaryEncoder.hpp:46
MIDIRotaryEncoder::sender
Sender sender
Definition: Abstract/MIDIRotaryEncoder.hpp:63
MIDIRotaryEncoder::pulsesPerStep
const uint8_t pulsesPerStep
Definition: Abstract/MIDIRotaryEncoder.hpp:59
EncoderPinList::A
uint8_t A
The pin connected to the A pin of the encoder.
Definition: Def.hpp:45
MIDIRotaryEncoder::address
const MIDICNChannelAddress address
Definition: Abstract/MIDIRotaryEncoder.hpp:57
EncoderPinList
A struct for the pins of a rotary (quadrature) encoder without a switch.
Definition: Def.hpp:44