7#include <AH/STL/limits>
8#include <AH/STL/type_traits>
50template <
class RegisterType, uint8_t NumEnc,
51 class EncoderPositionType = int32_t,
bool InterruptSafe =
false>
54 static_assert(std::is_unsigned<RegisterType>::value,
55 "RegisterType should be an unsigned integer type");
58 typename std::conditional<InterruptSafe,
volatile EncoderPositionType,
59 EncoderPositionType>::type;
62 typename std::conditional<InterruptSafe,
volatile RegisterType,
76 void reset(RegisterType resetstate) {
97 RegisterType oldstate =
state;
100 if (newstate == oldstate)
110 for (uint8_t i = 0; i < NumEnc; ++i) {
112 uint8_t change =
static_cast<uint8_t
>(newstate) & 0b11;
115 change |=
static_cast<uint8_t
>(oldstate) & 0b11;
136 EncoderPositionType
read(uint8_t idx)
const {
139 EncoderPositionType ret =
positions[idx];
160 EncoderPositionType ret =
positions[idx];
165 EncoderPositionType ret =
positions[idx];
181 void write(uint8_t idx, EncoderPositionType pos) {
207 EncoderPositionType
read()
const {
210 EncoderPositionType ret = *
position;
223 EncoderPositionType ret = *
position;
228 EncoderPositionType ret = *
position;
236 void write(EncoderPositionType pos) {
246#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
#define BEGIN_AH_NAMESPACE
static constexpr int8_t RegisterEncodersLUT[16]
Lookup table that maps rotary encoder (2-bit gray code) state changes to position deltas.
Proxy to access a single encoder of the encoders managed by RegisterEncoders.
EncoderPositionStorageType * position
A pointer to the position value inside of the RegisterEncoders class.
void write(EncoderPositionType pos)
Set the position of the encoder.
EncoderPositionType read() const
Read the position of the encoder.
EncoderPositionType readAndReset()
Read the position of the encoder and reset it to zero.
Encoder(EncoderPositionStorageType *position)
Class for keeping track of the position of multiple rotary encoders.
Encoder operator[](uint8_t index)
Get a proxy to one of the encoders managed by this object.
typename std::conditional< InterruptSafe, volatile EncoderPositionType, EncoderPositionType >::type EncoderPositionStorageType
typename std::conditional< InterruptSafe, volatile RegisterType, RegisterType >::type StateStorageType
void reset(RegisterType resetstate)
Reset the positions to zero and the state to the given value.
EncoderPositionType read(uint8_t idx) const
Read the position of the given encoder.
Array< EncoderPositionStorageType, NumEnc > positions
bool update(RegisterType newstate)
Update the encoder positions based on the new state.
EncoderPositionType readAndReset(uint8_t idx)
Read the position of the given encoder and reset it to zero.
void reset()
Reset the positions to zero and the state to 0xFF...FF.
void write(uint8_t idx, EncoderPositionType pos)
Set the position of the given encoder.
constexpr auto max(const T &a, const U &b) -> decltype(a< b ? b :a)
Return the larger of two numbers/objects.