Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
EncoderSelector.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Selector.hpp"
5#include <Def/Def.hpp>
6#include <Def/TypeTraits.hpp>
7
8#ifdef ARDUINO
10#else
11#include <Encoder.h> // Mock
12#endif
13
15
16template <setting_t N, class Callback = EmptySelectorCallback>
17class GenericEncoderSelector : public GenericSelector<N, Callback> {
19
20 public:
22 const EncoderSwitchPinList &pins,
23 int8_t pulsesPerStep = 4, Wrap wrap = Wrap::Wrap)
24 : GenericSelector<N, Callback> {selectable, callback},
25 encoder {pins.A, pins.B}, switchPin(pins.switchPin),
27
28 void begin() override {
30 if (switchPin != NO_PIN)
33 }
34
35 void update() override {
37 // TODO: use EncoderState
38 long currentPosition = encoder.read();
39 long difference = (currentPosition - previousPosition) / pulsesPerStep;
40 if (difference) {
41 previousPosition += difference * pulsesPerStep;
42 if (difference > 0)
43 while (difference-- > 0)
44 this->increment(wrap);
45 else
46 while (difference++ < 0)
47 this->decrement(wrap);
48 }
49
50 if (switchPin != NO_PIN) {
51 bool currentState = AH::ExtIO::digitalRead(switchPin);
52 if (previousSwitchState == HIGH && currentState == LOW) {
53 // TODO: invert?
54 this->reset();
55 }
56 previousSwitchState = currentState;
57 }
58 }
59
60 private:
65
68};
69
70// -------------------------------------------------------------------------- //
71
80template <setting_t N>
89
constexpr PinStatus_t LOW
constexpr PinStatus_t HIGH
constexpr PinMode_t INPUT_PULLUP
Wrap
An enumeration to set the behavior of selectors that are incremented (decremented) beyond their maxim...
Definition Selector.hpp:14
@ Wrap
When the maximum (minimum) setting is reached, wrap around to the minimum (maximum) setting.
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
std::enable_if< has_method_begin< T >::value >::type begin_if_possible(T &t)
Calls the begin() method of t if that method exists.
Class for reading quadrature encoders, heavily influenced by http://www.pjrc.com/teensy/td_libs_Encod...
Definition AHEncoder.hpp:24
int32_t read()
Read the current absolute position of the encoder.
Selector that reads from a rotary encoder.
EncoderSelector(Selectable< N > &selectable, const EncoderSwitchPinList &pins, int8_t pulsesPerStep=4, Wrap wrap=Wrap::Wrap)
void update() override
Update this updatable.
void begin() override
Initialize this updatable.
GenericEncoderSelector(Selectable< N > &selectable, const Callback &callback, const EncoderSwitchPinList &pins, int8_t pulsesPerStep=4, Wrap wrap=Wrap::Wrap)
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:109
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:128
Callback callback
Definition Selector.hpp:144
void update() override
Update this updatable.
Definition Selector.hpp:77
Selectable< N > & selectable
Definition Selector.hpp:141
void begin() override
Initialize this updatable.
Definition Selector.hpp:72
void reset()
Reset the selection to the initial selection.
Definition Selector.hpp:80
void pinMode(pin_t pin, PinMode_t mode)
An ExtIO version of the Arduino function.
PinStatus_t digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Type for storing pin numbers of Extended Input/Output elements.
A struct for the pins of a rotary (quadrature) encoder with a switch.
Definition Def.hpp:33