Control Surface
main
MIDI Control Surface library for Arduino
Toggle main menu visibility
Loading...
Searching...
No Matches
src
Selectors
EncoderSelector.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include "
Selector.hpp
"
4
#include <
AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp
>
5
#include <
Def/Def.hpp
>
6
#include <
Def/TypeTraits.hpp
>
7
8
#ifdef ARDUINO
9
#include <
Submodules/Encoder/AHEncoder.hpp
>
10
#else
11
#include <Encoder.h>
// Mock
12
#endif
13
14
BEGIN_CS_NAMESPACE
15
16
template
<setting_t N,
class
Callback = EmptySelectorCallback>
17
class
GenericEncoderSelector
:
public
GenericSelector
<N, Callback> {
18
using
Parent
=
GenericSelector<N, Callback>
;
19
20
public
:
21
GenericEncoderSelector
(
Selectable<N>
&
selectable
,
const
Callback &
callback
,
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),
26
pulsesPerStep
(
pulsesPerStep
),
wrap
(
wrap
) {}
27
28
void
begin
()
override
{
29
Parent::begin
();
30
if
(
switchPin
!=
NO_PIN
)
31
AH::ExtIO::pinMode
(
switchPin
,
INPUT_PULLUP
);
32
begin_if_possible
(
encoder
);
33
}
34
35
void
update
()
override
{
36
Parent::update
();
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
:
61
AHEncoder
encoder
;
62
pin_t
switchPin
;
63
int8_t
pulsesPerStep
;
64
Wrap
wrap
;
65
66
long
previousPosition
= 0;
67
bool
previousSwitchState
=
HIGH
;
68
};
69
70
// -------------------------------------------------------------------------- //
71
80
template
<setting_t N>
81
class
EncoderSelector
:
public
GenericEncoderSelector
<N> {
82
public
:
83
EncoderSelector
(
Selectable<N>
&
selectable
,
const
EncoderSwitchPinList
&pins,
84
int8_t
pulsesPerStep
= 4,
Wrap
wrap
=
Wrap::Wrap
)
85
:
GenericEncoderSelector
<N> {
86
selectable
, {}, pins,
pulsesPerStep
,
wrap
,
87
} {}
88
};
89
90
END_CS_NAMESPACE
AHEncoder.hpp
LOW
constexpr PinStatus_t LOW
Definition
Arduino-Hardware-Types.hpp:63
HIGH
constexpr PinStatus_t HIGH
Definition
Arduino-Hardware-Types.hpp:62
INPUT_PULLUP
constexpr PinMode_t INPUT_PULLUP
Definition
Arduino-Hardware-Types.hpp:66
Def.hpp
ExtendedInputOutput.hpp
Selector.hpp
Wrap
Wrap
An enumeration to set the behavior of selectors that are incremented (decremented) beyond their maxim...
Definition
Selector.hpp:14
Wrap::Wrap
@ Wrap
When the maximum (minimum) setting is reached, wrap around to the minimum (maximum) setting.
Definition
Selector.hpp:17
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:14
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:11
TypeTraits.hpp
begin_if_possible
std::enable_if< has_method_begin< T >::value >::type begin_if_possible(T &t)
Calls the begin() method of t if that method exists.
Definition
TypeTraits.hpp:23
AHEncoder
Class for reading quadrature encoders, heavily influenced by http://www.pjrc.com/teensy/td_libs_Encod...
Definition
AHEncoder.hpp:24
EncoderSelector::EncoderSelector
EncoderSelector(Selectable< N > &selectable, const EncoderSwitchPinList &pins, int8_t pulsesPerStep=4, Wrap wrap=Wrap::Wrap)
Definition
EncoderSelector.hpp:83
GenericEncoderSelector::switchPin
pin_t switchPin
Definition
EncoderSelector.hpp:62
GenericEncoderSelector::previousPosition
long previousPosition
Definition
EncoderSelector.hpp:66
GenericEncoderSelector::encoder
AHEncoder encoder
Definition
EncoderSelector.hpp:61
GenericEncoderSelector::previousSwitchState
bool previousSwitchState
Definition
EncoderSelector.hpp:67
GenericEncoderSelector::update
void update() override
Update this updatable.
Definition
EncoderSelector.hpp:35
GenericEncoderSelector::wrap
Wrap wrap
Definition
EncoderSelector.hpp:64
GenericEncoderSelector::begin
void begin() override
Initialize this updatable.
Definition
EncoderSelector.hpp:28
GenericEncoderSelector::GenericEncoderSelector
GenericEncoderSelector(Selectable< N > &selectable, const Callback &callback, const EncoderSwitchPinList &pins, int8_t pulsesPerStep=4, Wrap wrap=Wrap::Wrap)
Definition
EncoderSelector.hpp:21
GenericEncoderSelector::pulsesPerStep
int8_t pulsesPerStep
Definition
EncoderSelector.hpp:63
GenericEncoderSelector::Parent
GenericSelector< N, Callback > Parent
Definition
EncoderSelector.hpp:18
GenericSelector< N, EmptySelectorCallback >::increment
void increment(Wrap wrap)
Definition
Selector.hpp:109
GenericSelector< N, EmptySelectorCallback >::decrement
void decrement(Wrap wrap)
Definition
Selector.hpp:128
GenericSelector< N, EmptySelectorCallback >::callback
EmptySelectorCallback callback
Definition
Selector.hpp:144
GenericSelector< N, Callback >::update
void update() override
Definition
Selector.hpp:77
GenericSelector< N, EmptySelectorCallback >::selectable
Selectable< N > & selectable
Definition
Selector.hpp:141
GenericSelector< N, Callback >::begin
void begin() override
Definition
Selector.hpp:72
GenericSelector< N, EmptySelectorCallback >::reset
void reset()
Definition
Selector.hpp:80
GenericSelector< N, EmptySelectorCallback >::GenericSelector
GenericSelector(Selectable< N > &selectable, const EmptySelectorCallback &callback)
Definition
Selector.hpp:69
Selectable
Definition
Selectable.hpp:11
AH::ExtIO::pinMode
void pinMode(pin_t pin, PinMode_t mode)
An ExtIO version of the Arduino function.
Definition
ExtendedInputOutput.cpp:32
AH::ExtIO::digitalRead
PinStatus_t digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition
ExtendedInputOutput.cpp:54
AH::NO_PIN
constexpr pin_t NO_PIN
A special pin number that indicates an unused or invalid pin.
Definition
Hardware-Types.hpp:96
AH::ExtIO::pin_t
Type for storing pin numbers of Extended Input/Output elements.
Definition
Hardware-Types.hpp:25
EncoderSwitchPinList
A struct for the pins of a rotary (quadrature) encoder with a switch.
Definition
Def.hpp:33
Generated by
1.17.0