Control Surface main
MIDI Control Surface library for Arduino
Loading...
Searching...
No Matches
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 AH::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.

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

Private Types

using EncoderPositionStorageType
 
using StateStorageType
 

Private Attributes

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

Member Typedef Documentation

◆ EncoderPositionStorageType

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
using EncoderPositionStorageType
private
Initial value:
typename std::conditional<InterruptSafe, volatile EncoderPositionType,
EncoderPositionType>::type

Definition at line 57 of file RegisterEncoders.hpp.

◆ StateStorageType

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
using StateStorageType
private
Initial value:
typename std::conditional<InterruptSafe, volatile RegisterType,
RegisterType>::type

Definition at line 61 of file RegisterEncoders.hpp.

Member Function Documentation

◆ reset() [1/2]

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
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]

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
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()

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
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.

Definition at line 96 of file RegisterEncoders.hpp.

◆ read()

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
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].

Definition at line 136 of file RegisterEncoders.hpp.

◆ readAndReset()

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
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()

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
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[]()

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
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

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
StateStorageType state = std::numeric_limits<RegisterType>::max()
private

Definition at line 65 of file RegisterEncoders.hpp.

◆ positions

template<class RegisterType , uint8_t NumEnc, class EncoderPositionType = int32_t, bool InterruptSafe = false>
Array<EncoderPositionStorageType, NumEnc> positions {{}}
private

Definition at line 66 of file RegisterEncoders.hpp.


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