IoAbstraction
Loading...
Searching...
No Matches
StateRotaryEncoderBuilder Class Reference

#include <StateMachineEncoder.h>

Public Member Functions

StateRotaryEncoderBuilder & withEncoderPins (const pinid_t l_pinA, const pinid_t l_pinB)
StateRotaryEncoderBuilder & withEncoderNum (const uint8_t l_encoderNum)
StateRotaryEncoderBuilder & withPullUpInput (const bool l_needPullup)
StateRotaryEncoderBuilder & withCallback (const EncoderCallbackFn l_callback)
StateRotaryEncoderBuilder & withListener (EncoderListener *l_listener)
StateRotaryEncoderBuilder & interruptOnSinglePin (const pinid_t l_interruptPin)
StateRotaryEncoderBuilder & interruptOnBothPins ()
StateRotaryEncoderBuilder & interruptOnTimer ()
StateRotaryEncoderBuilder & withAccelerationMode (const HWAccelerationMode l_accelerationMode)
StateRotaryEncoderBuilder & withEncoderType (const EncoderType l_encoderType)
bool configurationValid () const
bool build () const

Detailed Description

Builder class for configuring a state machine based rotary encoder with various options such as pins, callbacks, listeners, interrupt handling, acceleration modes, and encoder types. Note that unlike other IoAbstraction based rotary encoders, this encoder requires either a dedicated interrupt or a hardware timer to be used. The configuration will not be considered valid unless one of the interrupt/timer modes is set.

Interrupt modes that are supported:

  1. Interrupt on both pins - both A and B are interrupt capable.
  2. Interrupt on single pin - must not be A or B, must be wired such that a change in A or B will trigger the interrupt.
  3. Timer mode - uses a hardware timer to generate interrupts. An example would be tie to SYS_TICK (1ms on ARM).

If you define the timer mode, it is your responsibility to ensure that the timer is configured to generate interrupts at a rate that is appropriate for your application. For example, on ARM, SYS_TICK is configured to generate interrupts at a rate of 1ms. You could attach to this and it should be fast enough to catch 99.9% of movements.

Member Function Documentation

◆ withEncoderPins()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::withEncoderPins ( const pinid_t l_pinA,
const pinid_t l_pinB )
inline

Configure the A and B pins of the encoder.

Parameters
l_pinAthe A pin
l_pinBthe B pin

◆ withEncoderNum()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::withEncoderNum ( const uint8_t l_encoderNum)
inline

IoAbstraction's Switches allows more than one rotary encoder, this defaults to 0 and for most will not need to be changed. Important note: this class can only provide one encoder, as it is a singleton.

Parameters
l_encoderNumthe encoder number, defaults to 0

◆ withPullUpInput()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::withPullUpInput ( const bool l_needPullup)
inline

You can configure whether the encoder pins need pullup input enabled or not. If you do not enable INPUT_PULLUP then you must ensure that the pins are pulled up externally and probably add some capacitance to reduce noise.

Parameters
l_needPulluptrue if the pins need pullup input enabled

◆ withCallback()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::withCallback ( const EncoderCallbackFn l_callback)
inline

The encoder will use the provided callback function to notify of changes.

Parameters
l_callbacka callback function matching EncoderCallbackFn

◆ withListener()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::withListener ( EncoderListener * l_listener)
inline

The encoder will use the provided listener to notify of changes.

Parameters
l_listenera listener matching EncoderListener

◆ interruptOnSinglePin()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::interruptOnSinglePin ( const pinid_t l_interruptPin)
inline

The encoder will use the provided interrupt pin to update its state. This single interrupt pin must be the combination of the A and B pins. I.E. most likely a hardware or of both pins. Only use this when interrupt capable pins are in short supply.

Parameters
l_interruptPinthe interrupt pin to use

◆ interruptOnBothPins()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::interruptOnBothPins ( )
inline

Enable interrupts on both the A and B pins. This requires that both are interrupt capable pins.

◆ interruptOnTimer()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::interruptOnTimer ( )
inline

Indicate to the builder that you are going to use either a hardware timer or manually configure interrupts. If you use a timer, it must be very frequent, I would recommend at least 1000Hz. If you manually configure interrupts it must capture both the A and B pins. In either case you must call encoderInterruptHandler() on every event.

◆ withAccelerationMode()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::withAccelerationMode ( const HWAccelerationMode l_accelerationMode)
inline

Set the acceleration mode for the encoder, it defaults to regular acceleration.

Parameters
l_accelerationModethe acceleration mode to use

◆ withEncoderType()

StateRotaryEncoderBuilder & StateRotaryEncoderBuilder::withEncoderType ( const EncoderType l_encoderType)
inline

Sets the encoder type, it defaults to FULL_CYCLE. At the moment only full and half encoders are supported.

Parameters
l_encoderTypethe encoder type that you have.

◆ configurationValid()

bool StateRotaryEncoderBuilder::configurationValid ( ) const
inlinenodiscard

Checks if the configuration is valid by making sure at least the basic configuration is sensible. It is not guaranteed that this returning true will provide a working encoder.

Returns
true if valid, otherwise false.

◆ build()

bool StateRotaryEncoderBuilder::build ( ) const

A somewhat internal method used by the setupStateMachineRotaryEncoder function to build the encoder and register it with switches and taskmanager.

Returns
true if the encoder was successfully built, false otherwise

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