RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
RumPi::RotaryEncoder Class Reference

A standard rotary encoder. More...

#include <RotaryEncoder.h>

Inheritance diagram for RumPi::RotaryEncoder:
[legend]
Collaboration diagram for RumPi::RotaryEncoder:
[legend]

Public Member Functions

 RotaryEncoder (unsigned int clkPinNumber, unsigned int dtPinNumber, unsigned int swPinNumber)
 Builds a rotary encoder on the given CLK/DT/SW pins, sets them as inputs, registers this as the active encoder for the interrupt callback, and hooks a falling-edge interrupt on the push-button pin. More...
 
virtual ~RotaryEncoder ()
 Destroys the encoder, stopping and joining its listener thread first. More...
 
virtual void TurnOn () override
 Turns the encoder on: starts the background listener thread that watches the pins and updates the value. More...
 
virtual void TurnOff () override
 Turns the encoder off: signals the listener thread to stop and waits for it to finish. More...
 
virtual void ProcessRawValues () override
 Processes the encoder's raw values. More...
 
virtual std::string ToString () const override
 Serializes the encoder's pins, value, and pressed state to a string. More...
 
unsigned int GetCLKPinNumber () const
 Gets the CLK (clock) pin number. More...
 
unsigned int GetDTPinNumber () const
 Gets the DT (data) pin number. More...
 
unsigned int GetSWPinNumber () const
 Gets the SW (switch / push-button) pin number. More...
 
unsigned char GetCurrentDTStatus () const
 Gets the DT pin's current reading. More...
 
unsigned char GetLastDTStatus () const
 Gets the DT pin's previous reading. More...
 
int GetValue () const
 Gets the encoder's running value (increases/decreases as the knob turns). More...
 
bool ConsumePressed ()
 Gets whether the push button has been pressed since the last call, then clears the pressed state so each press is reported exactly once. More...
 
void SetCurrentDTStatus (char currentDTStatus)
 Sets the DT pin's current reading. More...
 
void SetLastDTStatus (char lastDTStatus)
 Sets the DT pin's previous reading. More...
 
void SetValue (int val)
 Sets the encoder's running value. More...
 
- Public Member Functions inherited from RumPi::BaseComponent
 BaseComponent ()
 Builds a base component with an unknown type and an empty name. More...
 
virtual ~BaseComponent ()
 Virtual destructor so deleting a component through a BaseComponent* also cleans up the concrete subclass. More...
 
virtual void TurnOn ()=0
 Turns the component on (e.g. More...
 
virtual void TurnOff ()=0
 Turns the component off. More...
 
virtual std::string ToString () const =0
 Serializes the component's current readings to a string (usually JSON). More...
 
virtual void ProcessRawValues ()=0
 Reads and processes the component's latest raw values (called once per poll). More...
 
virtual EComponentType GetComponentType () const
 Gets the component's type. More...
 
const std::string & GetName () const
 Gets the component's unique per-instance name. More...
 
void SetName (const std::string &name)
 Sets the component's unique per-instance name. More...
 

Private Member Functions

void Update ()
 The listener-thread loop: watches the CLK/DT pins and increments or decrements the value based on the rotation direction, until the encoder is turned off. More...
 

Static Private Member Functions

static void OnButtonPressedInterrupt ()
 Interrupt callback for the push-button pin. More...
 

Private Attributes

std::thread myListenerThread
 The background thread that watches the encoder pins and updates the value. More...
 
std::atomic< bool > myIsRunning
 Whether the listener thread should keep running. More...
 
unsigned int myCLKPinNumber
 The CLK (clock) pin. More...
 
unsigned int myDTPinNumber
 The DT (data) pin. More...
 
unsigned int mySWPinNumber
 The SW (switch / push-button) pin. More...
 
std::atomic< unsigned char > myCurrentDTStatus
 The DT pin's current reading (used to detect rotation direction). More...
 
std::atomic< unsigned char > myLastDTStatus
 The DT pin's previous reading (used to detect rotation direction). More...
 
std::atomic< int > myValue
 The running value, incremented or decremented as the knob turns. More...
 
std::atomic< bool > myHasBeenPressed
 Whether the push button has been pressed since the last read (set by the interrupt callback, cleared on read). More...
 

Static Private Attributes

static std::atomic< RotaryEncoder * > theActiveEncoderForISR = nullptr
 The most recently constructed encoder, so the static interrupt callback (wiringPiISR carries no user data) can reach an instance. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from RumPi::BaseComponent
static std::string ComponentTypeToString (const EComponentType theComponentTypeToConvert)
 Maps an EComponentType to its string name. More...
 
static EComponentType StringToComponentType (const std::string &theComponentTypeName)
 Maps a type name back to its EComponentType (the inverse of ComponentTypeToString), so the client can turn the type carried on the wire back into a value it can look an icon up with. More...
 
- Protected Attributes inherited from RumPi::BaseComponent
EComponentType myComponentType
 The kind of component this is (set by each concrete subclass). More...
 
std::string myName
 A unique per-instance name, assigned when the component is added to the manager. More...
 

Detailed Description

A standard rotary encoder.

A background listener thread watches the CLK/DT pins and updates a running value (turned up or down as the knob rotates); the SW pin is a push button.

Author
Eddie O'Hagan
Date
8/13/2026

Constructor & Destructor Documentation

◆ RotaryEncoder()

RumPi::RotaryEncoder::RotaryEncoder ( unsigned int  clkPinNumber,
unsigned int  dtPinNumber,
unsigned int  swPinNumber 
)

Builds a rotary encoder on the given CLK/DT/SW pins, sets them as inputs, registers this as the active encoder for the interrupt callback, and hooks a falling-edge interrupt on the push-button pin.

Author
Eddie O'Hagan
Date
8/13/2026
encoder.TurnOn();
RotaryEncoder(unsigned int clkPinNumber, unsigned int dtPinNumber, unsigned int swPinNumber)
Builds a rotary encoder on the given CLK/DT/SW pins, sets them as inputs, registers this as the activ...
Definition: RotaryEncoder.cpp:25
constexpr unsigned int PIN_GPIO_17
BCM GPIO pin 17.
Definition: Common.h:43
constexpr unsigned int PIN_GPIO_18
BCM GPIO pin 18.
Definition: Common.h:44
constexpr unsigned int PIN_GPIO_27
BCM GPIO pin 27.
Definition: Common.h:53
Parameters
[in]clkPinNumberThe CLK (clock) pin.
[in]dtPinNumberThe DT (data) pin.
[in]swPinNumberThe SW (switch / push-button) pin.

◆ ~RotaryEncoder()

RumPi::RotaryEncoder::~RotaryEncoder ( )
virtual

Destroys the encoder, stopping and joining its listener thread first.

Author
Eddie O'Hagan
Date
8/13/2026
RotaryEncoder* encoder = new RotaryEncoder(17, 18, 27);
delete encoder;

Member Function Documentation

◆ ConsumePressed()

bool RumPi::RotaryEncoder::ConsumePressed ( )

Gets whether the push button has been pressed since the last call, then clears the pressed state so each press is reported exactly once.

Author
Eddie O'Hagan
Date
8/13/2026
if (encoder.ConsumePressed() == true)
{
//button was pressed...
}
Returns
true if the button was pressed since the last call; false otherwise. Reading clears the pressed state.

◆ GetCLKPinNumber()

unsigned int RumPi::RotaryEncoder::GetCLKPinNumber ( ) const

Gets the CLK (clock) pin number.

Author
Eddie O'Hagan
Date
8/13/2026
unsigned int clk = encoder.GetCLKPinNumber();
Returns
The CLK pin number.

◆ GetCurrentDTStatus()

unsigned char RumPi::RotaryEncoder::GetCurrentDTStatus ( ) const

Gets the DT pin's current reading.

Author
Eddie O'Hagan
Date
8/13/2026
unsigned char dtNow = encoder.GetCurrentDTStatus();
Returns
The DT pin's current reading.

◆ GetDTPinNumber()

unsigned int RumPi::RotaryEncoder::GetDTPinNumber ( ) const

Gets the DT (data) pin number.

Author
Eddie O'Hagan
Date
8/13/2026
unsigned int dt = encoder.GetDTPinNumber();
Returns
The DT pin number.

◆ GetLastDTStatus()

unsigned char RumPi::RotaryEncoder::GetLastDTStatus ( ) const

Gets the DT pin's previous reading.

Author
Eddie O'Hagan
Date
8/13/2026
unsigned char dtPrev = encoder.GetLastDTStatus();
Returns
The DT pin's previous reading.

◆ GetSWPinNumber()

unsigned int RumPi::RotaryEncoder::GetSWPinNumber ( ) const

Gets the SW (switch / push-button) pin number.

Author
Eddie O'Hagan
Date
8/13/2026
unsigned int sw = encoder.GetSWPinNumber();
Returns
The SW pin number.

◆ GetValue()

int RumPi::RotaryEncoder::GetValue ( ) const

Gets the encoder's running value (increases/decreases as the knob turns).

Author
Eddie O'Hagan
Date
8/13/2026
int value = encoder.GetValue();
Returns
The current running value.

◆ OnButtonPressedInterrupt()

void RumPi::RotaryEncoder::OnButtonPressedInterrupt ( )
staticprivate

Interrupt callback for the push-button pin.

Reports that a push was detected and latches it on the active encoder so ConsumePressed() reports it.

Author
Eddie O'Hagan
Date
8/13/2026
//Hooked in the constructor:
wiringPiISR(mySWPinNumber, INT_EDGE_FALLING, &OnButtonPressedInterrupt);
static void OnButtonPressedInterrupt()
Interrupt callback for the push-button pin.
Definition: RotaryEncoder.cpp:151
unsigned int mySWPinNumber
The SW (switch / push-button) pin.
Definition: RotaryEncoder.h:35

◆ ProcessRawValues()

void RumPi::RotaryEncoder::ProcessRawValues ( )
overridevirtual

Processes the encoder's raw values.

The listener thread already keeps the value current, so there is nothing to do here each poll - this is a no-op.

Author
Eddie O'Hagan
Date
8/13/2026
encoder.ProcessRawValues(); //no-op; the listener thread does the work.

Implements RumPi::BaseComponent.

◆ SetCurrentDTStatus()

void RumPi::RotaryEncoder::SetCurrentDTStatus ( char  currentDTStatus)

Sets the DT pin's current reading.

Author
Eddie O'Hagan
Date
8/13/2026
encoder.SetCurrentDTStatus((char)digitalRead(dtPin));
Parameters
[in]currentDTStatusThe DT pin's current reading.

◆ SetLastDTStatus()

void RumPi::RotaryEncoder::SetLastDTStatus ( char  lastDTStatus)

Sets the DT pin's previous reading.

Author
Eddie O'Hagan
Date
8/13/2026
encoder.SetLastDTStatus((char)digitalRead(dtPin));
Parameters
[in]lastDTStatusThe DT pin's previous reading.

◆ SetValue()

void RumPi::RotaryEncoder::SetValue ( int  val)

Sets the encoder's running value.

Author
Eddie O'Hagan
Date
8/13/2026
encoder.SetValue(0); //reset the value.
Parameters
[in]valThe new running value.

◆ ToString()

std::string RumPi::RotaryEncoder::ToString ( ) const
overridevirtual

Serializes the encoder's pins, value, and pressed state to a string.

Author
Eddie O'Hagan
Date
8/13/2026
std::string data = encoder.ToString();
Returns
A multi-line string describing the encoder.

Implements RumPi::BaseComponent.

◆ TurnOff()

void RumPi::RotaryEncoder::TurnOff ( )
overridevirtual

Turns the encoder off: signals the listener thread to stop and waits for it to finish.

Author
Eddie O'Hagan
Date
8/13/2026
encoder.TurnOff();

Implements RumPi::BaseComponent.

◆ TurnOn()

void RumPi::RotaryEncoder::TurnOn ( )
overridevirtual

Turns the encoder on: starts the background listener thread that watches the pins and updates the value.

Author
Eddie O'Hagan
Date
8/13/2026
encoder.TurnOn();

Implements RumPi::BaseComponent.

◆ Update()

void RumPi::RotaryEncoder::Update ( )
private

The listener-thread loop: watches the CLK/DT pins and increments or decrements the value based on the rotation direction, until the encoder is turned off.

Runs on its own thread.

Author
Eddie O'Hagan
Date
8/13/2026
//Launched on its own thread by TurnOn():
std::thread myListenerThread
The background thread that watches the encoder pins and updates the value.
Definition: RotaryEncoder.h:23
void Update()
The listener-thread loop: watches the CLK/DT pins and increments or decrements the value based on the...
Definition: RotaryEncoder.cpp:176

Member Data Documentation

◆ myCLKPinNumber

unsigned int RumPi::RotaryEncoder::myCLKPinNumber
private

The CLK (clock) pin.

◆ myCurrentDTStatus

std::atomic<unsigned char> RumPi::RotaryEncoder::myCurrentDTStatus
private

The DT pin's current reading (used to detect rotation direction).

◆ myDTPinNumber

unsigned int RumPi::RotaryEncoder::myDTPinNumber
private

The DT (data) pin.

◆ myHasBeenPressed

std::atomic<bool> RumPi::RotaryEncoder::myHasBeenPressed
private

Whether the push button has been pressed since the last read (set by the interrupt callback, cleared on read).

◆ myIsRunning

std::atomic<bool> RumPi::RotaryEncoder::myIsRunning
private

Whether the listener thread should keep running.

◆ myLastDTStatus

std::atomic<unsigned char> RumPi::RotaryEncoder::myLastDTStatus
private

The DT pin's previous reading (used to detect rotation direction).

◆ myListenerThread

std::thread RumPi::RotaryEncoder::myListenerThread
private

The background thread that watches the encoder pins and updates the value.

◆ mySWPinNumber

unsigned int RumPi::RotaryEncoder::mySWPinNumber
private

The SW (switch / push-button) pin.

◆ myValue

std::atomic<int> RumPi::RotaryEncoder::myValue
private

The running value, incremented or decremented as the knob turns.

◆ theActiveEncoderForISR

std::atomic< RotaryEncoder * > RumPi::RotaryEncoder::theActiveEncoderForISR = nullptr
staticprivate

The most recently constructed encoder, so the static interrupt callback (wiringPiISR carries no user data) can reach an instance.

Single active instance: a newer encoder replaces an older one.


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