![]() |
RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
|
A standard rotary encoder. More...
#include <RotaryEncoder.h>
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... | |
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.
| 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.
| [in] | clkPinNumber | The CLK (clock) pin. |
| [in] | dtPinNumber | The DT (data) pin. |
| [in] | swPinNumber | The SW (switch / push-button) pin. |
|
virtual |
Destroys the encoder, stopping and joining its listener thread first.
| 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.
| unsigned int RumPi::RotaryEncoder::GetCLKPinNumber | ( | ) | const |
Gets the CLK (clock) pin number.
| unsigned char RumPi::RotaryEncoder::GetCurrentDTStatus | ( | ) | const |
Gets the DT pin's current reading.
| unsigned int RumPi::RotaryEncoder::GetDTPinNumber | ( | ) | const |
Gets the DT (data) pin number.
| unsigned char RumPi::RotaryEncoder::GetLastDTStatus | ( | ) | const |
Gets the DT pin's previous reading.
| unsigned int RumPi::RotaryEncoder::GetSWPinNumber | ( | ) | const |
Gets the SW (switch / push-button) pin number.
| int RumPi::RotaryEncoder::GetValue | ( | ) | const |
Gets the encoder's running value (increases/decreases as the knob turns).
|
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.
|
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.
Implements RumPi::BaseComponent.
| void RumPi::RotaryEncoder::SetCurrentDTStatus | ( | char | currentDTStatus | ) |
Sets the DT pin's current reading.
| [in] | currentDTStatus | The DT pin's current reading. |
| void RumPi::RotaryEncoder::SetLastDTStatus | ( | char | lastDTStatus | ) |
Sets the DT pin's previous reading.
| [in] | lastDTStatus | The DT pin's previous reading. |
| void RumPi::RotaryEncoder::SetValue | ( | int | val | ) |
Sets the encoder's running value.
| [in] | val | The new running value. |
|
overridevirtual |
Serializes the encoder's pins, value, and pressed state to a string.
Implements RumPi::BaseComponent.
|
overridevirtual |
Turns the encoder off: signals the listener thread to stop and waits for it to finish.
Implements RumPi::BaseComponent.
|
overridevirtual |
Turns the encoder on: starts the background listener thread that watches the pins and updates the value.
Implements RumPi::BaseComponent.
|
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.
|
private |
The CLK (clock) pin.
|
private |
The DT pin's current reading (used to detect rotation direction).
|
private |
The DT (data) pin.
|
private |
Whether the push button has been pressed since the last read (set by the interrupt callback, cleared on read).
|
private |
Whether the listener thread should keep running.
|
private |
The DT pin's previous reading (used to detect rotation direction).
|
private |
The background thread that watches the encoder pins and updates the value.
|
private |
The SW (switch / push-button) pin.
|
private |
The running value, incremented or decremented as the knob turns.
|
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.