Skip to content

Commit e7da909

Browse files
committed
Refactor tick() function to accept input values directly for improved performance; update documentation accordingly.
1 parent 50656e5 commit e7da909

2 files changed

Lines changed: 36 additions & 36 deletions

File tree

src/RotaryEncoder.cpp

Lines changed: 31 additions & 33 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,37 +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)
112-
{ // Slow, but Simple Variant by directly Read-Out of the Digital State within loop-call
115+
{
113116
int sig1 = digitalRead(_pin1);
114117
int sig2 = digitalRead(_pin2);
115-
_tick(sig1, sig2);
116-
118+
tick(sig1, sig2);
117119
} // tick()
118120

121+
// When a faster method than digitalRead is available you can _tick with the 2 values directly.
119122
void RotaryEncoder::tick(int sig1, int sig2)
120-
{ // Possibility to make a fast call with digitalFASTRead() option and directly Register Readout
121-
_tick(sig1, sig2);
122-
123-
} // tick()
124-
125-
126-
unsigned long RotaryEncoder::getMillisBetweenRotations() const
127-
{
128-
return (_positionExtTime - _positionExtTimePrev);
129-
}
130-
131-
unsigned long RotaryEncoder::getRPM()
132-
{
133-
// calculate max of difference in time between last position changes or last change and now.
134-
unsigned long timeBetweenLastPositions = _positionExtTime - _positionExtTimePrev;
135-
unsigned long timeToLastPosition = millis() - _positionExtTime;
136-
unsigned long t = max(timeBetweenLastPositions, timeToLastPosition);
137-
return 60000.0 / ((float)(t * 20));
138-
}
139-
140-
void RotaryEncoder::_tick(int _sig1, int _sig2){
141-
int8_t thisState = _sig1 | (_sig2 << 1);
123+
{
124+
int8_t thisState = sig1 | (sig2 << 1);
142125

143126
if (_oldState != thisState) {
144127
_position += KNOBDIR[thisState | (_oldState << 2)];
@@ -173,7 +156,22 @@ void RotaryEncoder::_tick(int _sig1, int _sig2){
173156
break;
174157
} // switch
175158
} // if
176-
} // RotaryEncoder::_tick()
159+
} // tick()
160+
161+
162+
unsigned long RotaryEncoder::getMillisBetweenRotations() const
163+
{
164+
return (_positionExtTime - _positionExtTimePrev);
165+
}
166+
167+
unsigned long RotaryEncoder::getRPM()
168+
{
169+
// calculate max of difference in time between last position changes or last change and now.
170+
unsigned long timeBetweenLastPositions = _positionExtTime - _positionExtTimePrev;
171+
unsigned long timeToLastPosition = millis() - _positionExtTime;
172+
unsigned long t = max(timeBetweenLastPositions, timeToLastPosition);
173+
return 60000.0 / ((float)(t * 20));
174+
}
177175

178176

179177
// End

src/RotaryEncoder.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
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
// -----
17-
// 06.06.2024 Implementation of digitalFASTRead() possibility due to splitting up "tick()" function
1818

1919
#ifndef RotaryEncoder_h
2020
#define RotaryEncoder_h
@@ -49,7 +49,11 @@ class RotaryEncoder
4949
void setPosition(long newPosition);
5050

5151
// 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.
5253
void tick(void);
54+
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.
5357
void tick(int sig1, int sig2);
5458

5559
// Returns the time in milliseconds between the current observed
@@ -59,8 +63,6 @@ class RotaryEncoder
5963
unsigned long getRPM();
6064

6165
private:
62-
void _tick(int _sig1, int _sig2); // Private internal tick function
63-
6466
int _pin1, _pin2; // Arduino pins used for the encoder.
6567

6668
LatchMode _mode; // Latch mode from initialization

0 commit comments

Comments
 (0)