Arduino Helpers master
Utility library for Arduino
Classes | Public Member Functions | Private Types | Private Attributes | List of all members
RegisterEncoders< RegisterType, NumEnc, EncoderPositionType, InterruptSafe > Class Template Reference

#include <AH/Hardware/RegisterEncoders.hpp>

Detailed Description

template<class RegisterType, uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
class RegisterEncoders< RegisterType, NumEnc, EncoderPositionType, InterruptSafe >

Class for keeping track of the position of multiple rotary encoders.

Template Parameters
RegisterTypeThe type of the GPIO registers.
NumEncThe number of encoders.
EncoderPositionTypeThe type used for saving the encoder positions. int32_t is the default because this matches the Encoder library. You can use small unsigned types such as uint8_t or uint16_t if you're just interrested in the deltas.
InterruptSafeMake the update method safe to use inside of an interrupt. It makes the necessary variables volatile and disables interrupts while reading the positions from the main program.
Examples
Pin-Change-Interrupt-Encoders.ino, and Timer-Interrupt-Encoders.ino.

Definition at line 52 of file RegisterEncoders.hpp.

+ Collaboration diagram for RegisterEncoders< RegisterType, NumEnc, EncoderPositionType, InterruptSafe >:

Classes

class  Encoder
 Proxy to access a single encoder of the encoders managed by RegisterEncoders. More...
 

Public Member Functions

void reset ()
 Reset the positions to zero and the state to 0xFF...FF. More...
 
void reset (RegisterType resetstate)
 Reset the positions to zero and the state to the given value. More...
 
bool update (RegisterType newstate)
 Update the encoder positions based on the new state. More...
 
EncoderPositionType read (uint8_t idx) const
 Read the position of the given encoder. More...
 
EncoderPositionType readAndReset (uint8_t idx)
 Read the position of the given encoder and reset it to zero. More...
 
void write (uint8_t idx, EncoderPositionType pos)
 Set the position of the given encoder. More...
 
Encoder operator[] (uint8_t index)
 Get a proxy to one of the encoders managed by this object. More...
 

Private Types

using EncoderPositionStorageType = typename std::conditional< InterruptSafe, volatile EncoderPositionType, EncoderPositionType >::type
 
using StateStorageType = typename std::conditional< InterruptSafe, volatile RegisterType, RegisterType >::type
 

Private Attributes

StateStorageType state = std::numeric_limits<RegisterType>::max()
 
Array< EncoderPositionStorageType, NumEnc > positions {{}}
 

Member Typedef Documentation

◆ EncoderPositionStorageType

using EncoderPositionStorageType = typename std::conditional<InterruptSafe, volatile EncoderPositionType, EncoderPositionType>::type
private

Definition at line 57 of file RegisterEncoders.hpp.

◆ StateStorageType

using StateStorageType = typename std::conditional<InterruptSafe, volatile RegisterType, RegisterType>::type
private

Definition at line 61 of file RegisterEncoders.hpp.

Member Function Documentation

◆ reset() [1/2]

void reset ( )
inline

Reset the positions to zero and the state to 0xFF...FF.

Definition at line 70 of file RegisterEncoders.hpp.

◆ reset() [2/2]

void reset ( RegisterType  resetstate)
inline

Reset the positions to zero and the state to the given value.

Definition at line 76 of file RegisterEncoders.hpp.

◆ update()

bool update ( RegisterType  newstate)
inline

Update the encoder positions based on the new state.

Can be called from within an ISR, on the condition that InterruptSafe is set to true.

Don't call this function both from the ISR and from your main program, only call it from one of the two.

Return values
trueThe new state is different from the old state, positions have been updated.
falseThe new state is the same as the old state, nothing updated.
Examples
Pin-Change-Interrupt-Encoders.ino, and Timer-Interrupt-Encoders.ino.

Definition at line 96 of file RegisterEncoders.hpp.

◆ read()

EncoderPositionType read ( uint8_t  idx) const
inline

Read the position of the given encoder.

Does not update the state, just reads it from the buffered position.

Don't call this function from within an ISR.

Parameters
idxThe index of the encoder to read [0, NumEnc - 1].
Examples
Pin-Change-Interrupt-Encoders.ino, and Timer-Interrupt-Encoders.ino.

Definition at line 136 of file RegisterEncoders.hpp.

◆ readAndReset()

EncoderPositionType readAndReset ( uint8_t  idx)
inline

Read the position of the given encoder and reset it to zero.

Does not update the state, just reads it from the buffered position.

Don't call this function from within an ISR.

Parameters
idxThe index of the encoder to read [0, NumEnc - 1].

Definition at line 157 of file RegisterEncoders.hpp.

◆ write()

void write ( uint8_t  idx,
EncoderPositionType  pos 
)
inline

Set the position of the given encoder.

Don't call this function from within an ISR.

Parameters
idxThe index of the encoder to write [0, NumEnc - 1].
posThe position value to write.

Definition at line 181 of file RegisterEncoders.hpp.

◆ operator[]()

Encoder operator[] ( uint8_t  index)
inline

Get a proxy to one of the encoders managed by this object.

Parameters
indexThe index of the encoder to access.

Definition at line 259 of file RegisterEncoders.hpp.

Member Data Documentation

◆ state

StateStorageType state = std::numeric_limits<RegisterType>::max()
private

Definition at line 65 of file RegisterEncoders.hpp.

◆ positions

Array<EncoderPositionStorageType, NumEnc> positions {{}}
private

Definition at line 66 of file RegisterEncoders.hpp.


The documentation for this class was generated from the following file: