Skip to content

Commit 1fb1452

Browse files
authored
Merge pull request #45 from JoJos1220:digitalFASTread()---Integration
Implementation of digitalFASTread() possibility
2 parents d523210 + e7da909 commit 1fb1452

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/RotaryEncoder.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,16 @@ RotaryEncoder::RotaryEncoder(int pin1, int pin2, LatchMode mode)
4646
_mode = mode;
4747

4848
// Setup the input pins and turn on pullup resistor
49-
pinMode(pin1, INPUT_PULLUP);
50-
pinMode(pin2, INPUT_PULLUP);
51-
52-
// when not started in motion, the current state of the encoder should be 3
53-
int sig1 = digitalRead(_pin1);
54-
int sig2 = digitalRead(_pin2);
49+
if ((pin1 >= 0) && (pin2 >= 0)) {
50+
pinMode(pin1, INPUT_PULLUP);
51+
pinMode(pin2, INPUT_PULLUP);
52+
// when not started in motion, the current state of the encoder should be 3
53+
int sig1 = digitalRead(_pin1);
54+
int sig2 = digitalRead(_pin2);
55+
}
56+
5557
_oldState = sig1 | (sig2 << 1);
56-
58+
5759
// start with position 0;
5860
_position = 0;
5961
_positionExt = 0;
@@ -108,11 +110,18 @@ void RotaryEncoder::setPosition(long newPosition)
108110
} // setPosition()
109111

110112

113+
// Slow, but Simple Variant by directly Read-Out of the Digital State within loop-call
111114
void RotaryEncoder::tick(void)
112115
{
113116
unsigned long now = millis();
114117
int sig1 = digitalRead(_pin1);
115118
int sig2 = digitalRead(_pin2);
119+
tick(sig1, sig2);
120+
} // tick()
121+
122+
// When a faster method than digitalRead is available you can _tick with the 2 values directly.
123+
void RotaryEncoder::tick(int sig1, int sig2)
124+
{
116125
int8_t thisState = sig1 | (sig2 << 1);
117126

118127
if (_oldState != thisState) {

src/RotaryEncoder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// 16.06.2019 pin initialization using INPUT_PULLUP
1414
// 10.11.2020 Added the ability to obtain the encoder RPM
1515
// 29.01.2021 Options for using rotary encoders with 2 state changes per latch.
16+
// 06.06.2024 Implementation of tick() with passing the input values for more performant implementations.
1617
// -----
1718

1819
#ifndef RotaryEncoder_h
@@ -48,8 +49,13 @@ class RotaryEncoder
4849
void setPosition(long newPosition);
4950

5051
// call this function every some milliseconds or by using an interrupt for handling state changes of the rotary encoder.
52+
// This method uses the standard Arduino digitalRead() function with the 2 pins provided in the class creation.
5153
void tick(void);
5254

55+
// Use this tick variant when a faster method than digitalRead is available and provide the values directly.
56+
// The 2 pins provided in the class creation are ignored.
57+
void tick(int sig1, int sig2);
58+
5359
// Returns the time in milliseconds between the current observed
5460
unsigned long getMillisBetweenRotations() const;
5561

0 commit comments

Comments
 (0)