Control Surface  1.1.1
MIDI Control Surface library for Arduino
EncoderSelector.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #if !defined(Encoder_h_) && !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 "Selector.hpp"
11 #include <Def/Def.hpp>
12 
14 
15 template <setting_t N, class Callback = EmptySelectorCallback>
16 class GenericEncoderSelector : public GenericSelector<N, Callback> {
18 
19  public:
21  const EncoderSwitchPinList &pins,
22  int8_t pulsesPerStep = 4, Wrap wrap = Wrap::Wrap)
23  : GenericSelector<N, Callback>{selectable, callback}, encoder{pins.A,
24  pins.B},
25  switchPin{pins.switchPin}, pulsesPerStep{pulsesPerStep}, wrap{wrap} {}
26 
27  void begin() override {
28  Parent::begin();
29  if (switchPin != NO_PIN)
31  }
32 
33  void update() override {
35  long currentPosition = encoder.read();
36  long difference = (currentPosition - previousPosition) / pulsesPerStep;
37  if (difference) {
38  previousPosition += difference * pulsesPerStep;
39  if (difference > 0)
40  while (difference-- > 0)
41  this->increment(wrap);
42  else
43  while (difference++ < 0)
44  this->decrement(wrap);
45  }
46 
47  if (switchPin != NO_PIN) {
48  bool currentState = AH::ExtIO::digitalRead(switchPin);
49  if (previousSwitchState == HIGH && currentState == LOW) {
50  // TODO: invert?
51  this->reset();
52  }
53  previousSwitchState = currentState;
54  }
55  }
56 
57  private:
58  Encoder encoder;
60  int8_t pulsesPerStep;
62 
63  long previousPosition = 0;
65 };
66 
67 // -------------------------------------------------------------------------- //
68 
77 template <setting_t N>
79  public:
81  int8_t pulsesPerStep = 4, Wrap wrap = Wrap::Wrap)
83  selectable, {}, pins, pulsesPerStep, wrap,
84  } {}
85 };
86 
AH::ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:36
GenericEncoderSelector::GenericEncoderSelector
GenericEncoderSelector(Selectable< N > &selectable, const Callback &callback, const EncoderSwitchPinList &pins, int8_t pulsesPerStep=4, Wrap wrap=Wrap::Wrap)
Definition: EncoderSelector.hpp:20
INPUT_PULLUP
const uint8_t INPUT_PULLUP
Definition: ExtendedInputOutput.hpp:51
GenericSelector::begin
void begin() override
Initialize this updatable.
Definition: Selector.hpp:73
GenericEncoderSelector::pulsesPerStep
int8_t pulsesPerStep
Definition: EncoderSelector.hpp:60
AH::ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:60
GenericEncoderSelector::switchPin
pin_t switchPin
Definition: EncoderSelector.hpp:59
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
Def.hpp
GenericEncoderSelector::wrap
Wrap wrap
Definition: EncoderSelector.hpp:61
GenericSelector::update
void update() override
Update this updatable.
Definition: Selector.hpp:78
ExtendedInputOutput.hpp
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
Selectable
Definition: Selectable.hpp:11
GenericSelector::callback
Callback callback
Definition: Selector.hpp:140
HIGH
const uint8_t HIGH
Definition: ExtendedInputOutput.hpp:46
GenericSelector::increment
void increment(Wrap wrap)
Add one to the setting, wrap around or clamp, depending on the parameter, if the new setting would be...
Definition: Selector.hpp:105
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
GenericEncoderSelector::begin
void begin() override
Initialize this updatable.
Definition: EncoderSelector.hpp:27
Wrap
Wrap
An enumeration to set the behavior of selectors that are incremented (decremented) beyond their maxim...
Definition: Selector.hpp:14
GenericSelector
Definition: Selector.hpp:57
GenericEncoderSelector::previousSwitchState
bool previousSwitchState
Definition: EncoderSelector.hpp:64
LOW
const uint8_t LOW
Definition: ExtendedInputOutput.hpp:47
AH::NO_PIN
constexpr pin_t NO_PIN
A special pin number that indicates an unused or invalid pin.
Definition: Hardware-Types.hpp:24
EncoderSelector
Selector that reads from a rotary encoder.
Definition: EncoderSelector.hpp:78
Selector.hpp
GenericSelector::reset
void reset()
Reset the selection to the initial selection.
Definition: Selector.hpp:81
GenericEncoderSelector::previousPosition
long previousPosition
Definition: EncoderSelector.hpp:63
Wrap::Wrap
When the maximum (minimum) setting is reached, wrap around to the minimum (maximum) setting.
GenericSelector::decrement
void decrement(Wrap wrap)
Subtract one from the setting, wrap around or clamp, depending on the parameter, if the new setting w...
Definition: Selector.hpp:124
GenericEncoderSelector
Definition: EncoderSelector.hpp:16
GenericEncoderSelector::update
void update() override
Update this updatable.
Definition: EncoderSelector.hpp:33
GenericSelector::selectable
Selectable< N > & selectable
Definition: Selector.hpp:137
GenericEncoderSelector::encoder
Encoder encoder
Definition: EncoderSelector.hpp:58
EncoderSelector::EncoderSelector
EncoderSelector(Selectable< N > &selectable, const EncoderSwitchPinList &pins, int8_t pulsesPerStep=4, Wrap wrap=Wrap::Wrap)
Definition: EncoderSelector.hpp:80
EncoderSwitchPinList
A struct for the pins of a rotary (quadrature) encoder with a switch.
Definition: Def.hpp:30