Control Surface stm32
MIDI Control Surface library for Arduino
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
8
10template <class Enc_t>
12 private:
13 using SignedEnc_t = typename std::make_signed<Enc_t>::type;
16 int16_t remainder = 0;
17 Enc_t deltaOffset = 0;
18
19 public:
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 }
41 // TODO: Is this correct? Is it necessary? What with negative speedMult?
43 this->speedMultiply = speedMultiply;
44 }
45 int16_t getSpeedMultiply() const { return this->speedMultiply; }
46};
47
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Class to keep track of relative position changes of rotary encoders.
int16_t getSpeedMultiply() const
void setSpeedMultiply(int16_t speedMultiply)
EncoderState(int16_t speedMultiply, uint8_t pulsesPerStep)
int16_t update(Enc_t encval)
int16_t speedMultiply
uint8_t pulsesPerStep
typename std::make_signed< Enc_t >::type SignedEnc_t
int16_t remainder