RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
RotaryEncoder.h
Go to the documentation of this file.
1#pragma once
2#include <stdio.h>
3#include <iostream>
4#include <thread>
5#include <atomic>
6#include <wiringPi.h>
7#include "BaseComponent.h"
8
9namespace RumPi
10{
19 {
20
21 private:
23 std::thread myListenerThread;
24
26 std::atomic<bool> myIsRunning;
27
29 unsigned int myCLKPinNumber;
30
32 unsigned int myDTPinNumber;
33
35 unsigned int mySWPinNumber;
36
38 std::atomic<unsigned char> myCurrentDTStatus;
39
41 std::atomic<unsigned char> myLastDTStatus;
42
44 std::atomic<int> myValue;
45
47 std::atomic<bool> myHasBeenPressed;
48
51 static std::atomic<RotaryEncoder*> theActiveEncoderForISR;
52
65 static void OnButtonPressedInterrupt();
66
79 void Update();
80
81 public:
98 RotaryEncoder(unsigned int clkPinNumber, unsigned int dtPinNumber, unsigned int swPinNumber);
99
111 virtual ~RotaryEncoder();
112
123 virtual void TurnOn() override;
124
135 virtual void TurnOff() override;
136
148 virtual void ProcessRawValues() override;
149
162 virtual std::string ToString() const override;
163
176 unsigned int GetCLKPinNumber() const;
177
190 unsigned int GetDTPinNumber() const;
191
204 unsigned int GetSWPinNumber() const;
205
218 unsigned char GetCurrentDTStatus() const;
219
232 unsigned char GetLastDTStatus() const;
233
246 int GetValue() const;
247
264 bool ConsumePressed();
265
278 void SetCurrentDTStatus(char currentDTStatus);
279
292 void SetLastDTStatus(char lastDTStatus);
293
306 void SetValue(int val);
307
308 };
309} // namespace RumPi
Abstract base class for every component (sensor, actuator, etc.) the library manages.
Definition: BaseComponent.h:60
A standard rotary encoder.
Definition: RotaryEncoder.h:19
void SetLastDTStatus(char lastDTStatus)
Sets the DT pin's previous reading.
Definition: RotaryEncoder.cpp:365
unsigned int myDTPinNumber
The DT (data) pin.
Definition: RotaryEncoder.h:32
virtual ~RotaryEncoder()
Destroys the encoder, stopping and joining its listener thread first.
Definition: RotaryEncoder.cpp:67
unsigned int myCLKPinNumber
The CLK (clock) pin.
Definition: RotaryEncoder.h:29
virtual void TurnOff() override
Turns the encoder off: signals the listener thread to stop and waits for it to finish.
Definition: RotaryEncoder.cpp:111
unsigned int GetSWPinNumber() const
Gets the SW (switch / push-button) pin number.
Definition: RotaryEncoder.cpp:259
std::atomic< int > myValue
The running value, incremented or decremented as the knob turns.
Definition: RotaryEncoder.h:44
virtual std::string ToString() const override
Serializes the encoder's pins, value, and pressed state to a string.
Definition: RotaryEncoder.cpp:399
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
std::atomic< bool > myHasBeenPressed
Whether the push button has been pressed since the last read (set by the interrupt callback,...
Definition: RotaryEncoder.h:47
static void OnButtonPressedInterrupt()
Interrupt callback for the push-button pin.
Definition: RotaryEncoder.cpp:151
std::atomic< unsigned char > myCurrentDTStatus
The DT pin's current reading (used to detect rotation direction).
Definition: RotaryEncoder.h:38
std::thread myListenerThread
The background thread that watches the encoder pins and updates the value.
Definition: RotaryEncoder.h:23
unsigned char GetLastDTStatus() const
Gets the DT pin's previous reading.
Definition: RotaryEncoder.cpp:293
virtual void TurnOn() override
Turns the encoder on: starts the background listener thread that watches the pins and updates the val...
Definition: RotaryEncoder.cpp:89
int GetValue() const
Gets the encoder's running value (increases/decreases as the knob turns).
Definition: RotaryEncoder.cpp:310
void Update()
The listener-thread loop: watches the CLK/DT pins and increments or decrements the value based on the...
Definition: RotaryEncoder.cpp:176
void SetCurrentDTStatus(char currentDTStatus)
Sets the DT pin's current reading.
Definition: RotaryEncoder.cpp:348
unsigned int GetDTPinNumber() const
Gets the DT (data) pin number.
Definition: RotaryEncoder.cpp:242
std::atomic< unsigned char > myLastDTStatus
The DT pin's previous reading (used to detect rotation direction).
Definition: RotaryEncoder.h:41
unsigned int mySWPinNumber
The SW (switch / push-button) pin.
Definition: RotaryEncoder.h:35
static std::atomic< RotaryEncoder * > theActiveEncoderForISR
The most recently constructed encoder, so the static interrupt callback (wiringPiISR carries no user ...
Definition: RotaryEncoder.h:51
std::atomic< bool > myIsRunning
Whether the listener thread should keep running.
Definition: RotaryEncoder.h:26
unsigned char GetCurrentDTStatus() const
Gets the DT pin's current reading.
Definition: RotaryEncoder.cpp:276
virtual void ProcessRawValues() override
Processes the encoder's raw values.
Definition: RotaryEncoder.cpp:134
void SetValue(int val)
Sets the encoder's running value.
Definition: RotaryEncoder.cpp:382
unsigned int GetCLKPinNumber() const
Gets the CLK (clock) pin number.
Definition: RotaryEncoder.cpp:225
bool ConsumePressed()
Gets whether the push button has been pressed since the last call, then clears the pressed state so e...
Definition: RotaryEncoder.cpp:331
RumPi is the library's top-level facade.
Definition: AlertManager.h:6