Control Surface
main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
src
MIDI_Outputs
Abstract
EncoderState.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <AH/STL/type_traits>
4
#include <Settings/NamespaceSettings.hpp>
5
#include <stdint.h>
6
7
BEGIN_CS_NAMESPACE
8
10
template
<
class
Enc_t>
11
class
EncoderState
{
12
private
:
13
using
SignedEnc_t
=
typename
std::make_signed<Enc_t>::type;
14
int16_t
speedMultiply
;
15
uint8_t
pulsesPerStep
;
16
int16_t
remainder
= 0;
17
Enc_t
deltaOffset
= 0;
18
19
public
:
20
EncoderState
(
int16_t
speedMultiply
,
uint8_t
pulsesPerStep
)
21
:
speedMultiply
(
speedMultiply
),
pulsesPerStep
(
pulsesPerStep
) {}
22
23
int16_t
update
(
Enc_t
encval
) {
24
// If Enc_t is an unsigned type, integer overflow is well-defined, which
25
// is what we want when Enc_t is small and expected to overflow.
26
// However, we need it to be signed because we're interested in the
27
// delta.
28
Enc_t
uDelta
=
encval
-
deltaOffset
;
29
if
(
uDelta
== 0)
30
return
0;
31
int16_t
delta
=
static_cast<
SignedEnc_t
>
(
uDelta
);
32
// Assumption: delta and speedMultiply are relatively small, so
33
// multiplication probably won't overflow.
34
int16_t
multipliedDelta
=
delta
*
speedMultiply
+
remainder
;
35
int16_t
scaledDelta
=
multipliedDelta
/
pulsesPerStep
;
36
remainder
=
multipliedDelta
%
pulsesPerStep
;
37
deltaOffset
+=
uDelta
;
38
return
scaledDelta
;
39
}
40
void
setSpeedMultiply
(
int16_t
speedMultiply
) {
41
// TODO: Is this correct? Is it necessary? What with negative speedMult?
42
remainder
=
remainder
*
speedMultiply
/ this->
speedMultiply
;
43
this->speedMultiply =
speedMultiply
;
44
}
45
int16_t
getSpeedMultiply
()
const
{
return
this->
speedMultiply
; }
46
};
47
48
END_CS_NAMESPACE
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:14
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition
Settings/NamespaceSettings.hpp:11
EncoderState
Class to keep track of relative position changes of rotary encoders.
Definition
EncoderState.hpp:11
EncoderState::getSpeedMultiply
int16_t getSpeedMultiply() const
Definition
EncoderState.hpp:45
EncoderState::setSpeedMultiply
void setSpeedMultiply(int16_t speedMultiply)
Definition
EncoderState.hpp:40
EncoderState::deltaOffset
Enc_t deltaOffset
Definition
EncoderState.hpp:17
EncoderState::EncoderState
EncoderState(int16_t speedMultiply, uint8_t pulsesPerStep)
Definition
EncoderState.hpp:20
EncoderState::update
int16_t update(Enc_t encval)
Definition
EncoderState.hpp:23
EncoderState::speedMultiply
int16_t speedMultiply
Definition
EncoderState.hpp:14
EncoderState::pulsesPerStep
uint8_t pulsesPerStep
Definition
EncoderState.hpp:15
EncoderState::SignedEnc_t
typename std::make_signed< Enc_t >::type SignedEnc_t
Definition
EncoderState.hpp:13
EncoderState::remainder
int16_t remainder
Definition
EncoderState.hpp:16
AH::copyAs
Array< T, N > copyAs(const Array< U, N > &src)
Copy an Array to an Array of a different type.
Definition
ArrayHelpers.hpp:105
Generated by
1.10.0