This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
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"
10 #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 = 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 
GenericEncoderSelector::update
void update() override
Update this updatable.
Definition: EncoderSelector.hpp:33
INPUT_PULLUP
const uint8_t INPUT_PULLUP
Definition: ExtendedInputOutput.hpp:39
GenericEncoderSelector::pulsesPerStep
int8_t pulsesPerStep
Definition: EncoderSelector.hpp:60
GenericEncoderSelector::GenericEncoderSelector
GenericEncoderSelector(Selectable< N > &selectable, const Callback &callback, const EncoderSwitchPinList &pins, int8_t pulsesPerStep=4, Wrap wrap=Wrap::Wrap)
Definition: EncoderSelector.hpp:20
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
GenericSelector::selectable
Selectable< N > & selectable
Definition: Selector.hpp:137
GenericEncoderSelector::switchPin
pin_t switchPin
Definition: EncoderSelector.hpp:59
ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:34
pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Def.hpp:17
Def.hpp
ExtendedInputOutput.hpp
Selectable
Definition: Selectable.hpp:11
EncoderSelector::EncoderSelector
EncoderSelector(Selectable< N > &selectable, const EncoderSwitchPinList &pins, int8_t pulsesPerStep=4, Wrap wrap=Wrap::Wrap)
Definition: EncoderSelector.hpp:80
GenericEncoderSelector::begin
void begin() override
Initialize this updatable.
Definition: EncoderSelector.hpp:27
HIGH
const uint8_t HIGH
Definition: ExtendedInputOutput.hpp:34
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
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
GenericSelector::update
void update() override
Update this updatable.
Definition: Selector.hpp:78
GenericSelector::callback
Callback callback
Definition: Selector.hpp:140
GenericEncoderSelector::wrap
Wrap wrap
Definition: EncoderSelector.hpp:61
GenericEncoderSelector::encoder
Encoder encoder
Definition: EncoderSelector.hpp:58
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
LOW
const uint8_t LOW
Definition: ExtendedInputOutput.hpp:35
GenericEncoderSelector::previousPosition
long previousPosition
Definition: EncoderSelector.hpp:63
EncoderSelector
Selector that reads from a rotary encoder.
Definition: EncoderSelector.hpp:78
Selector.hpp
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:56
GenericEncoderSelector::previousSwitchState
bool previousSwitchState
Definition: EncoderSelector.hpp:64
Wrap::Wrap
When the maximum (minimum) setting is reached, wrap around to the minimum (maximum) setting.
GenericEncoderSelector
Definition: EncoderSelector.hpp:16
GenericSelector::begin
void begin() override
Initialize this updatable.
Definition: Selector.hpp:73
NO_PIN
constexpr pin_t NO_PIN
A special pin number that indicates an unused or invalid pin.
Definition: Def.hpp:24
GenericSelector::reset
void reset()
Reset the selection to the initial selection.
Definition: Selector.hpp:81
EncoderSwitchPinList
A struct for the pins of a rotary (quadrature) encoder with a switch.
Definition: Def.hpp:43