Skip to content

Commit 50656e5

Browse files
committed
Implementation of digitalFASTread() possibility due to split-up of tick() function
1 parent 5667576 commit 50656e5

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/RotaryEncoder.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,36 @@ void RotaryEncoder::setPosition(long newPosition)
109109

110110

111111
void RotaryEncoder::tick(void)
112-
{
112+
{ // Slow, but Simple Variant by directly Read-Out of the Digital State within loop-call
113113
int sig1 = digitalRead(_pin1);
114114
int sig2 = digitalRead(_pin2);
115-
int8_t thisState = sig1 | (sig2 << 1);
115+
_tick(sig1, sig2);
116+
117+
} // tick()
118+
119+
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);
116142

117143
if (_oldState != thisState) {
118144
_position += KNOBDIR[thisState | (_oldState << 2)];
@@ -147,22 +173,7 @@ void RotaryEncoder::tick(void)
147173
break;
148174
} // switch
149175
} // if
150-
} // tick()
151-
152-
153-
unsigned long RotaryEncoder::getMillisBetweenRotations() const
154-
{
155-
return (_positionExtTime - _positionExtTimePrev);
156-
}
157-
158-
unsigned long RotaryEncoder::getRPM()
159-
{
160-
// calculate max of difference in time between last position changes or last change and now.
161-
unsigned long timeBetweenLastPositions = _positionExtTime - _positionExtTimePrev;
162-
unsigned long timeToLastPosition = millis() - _positionExtTime;
163-
unsigned long t = max(timeBetweenLastPositions, timeToLastPosition);
164-
return 60000.0 / ((float)(t * 20));
165-
}
176+
} // RotaryEncoder::_tick()
166177

167178

168179
// End

src/RotaryEncoder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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.
1616
// -----
17+
// 06.06.2024 Implementation of digitalFASTRead() possibility due to splitting up "tick()" function
1718

1819
#ifndef RotaryEncoder_h
1920
#define RotaryEncoder_h
@@ -49,6 +50,7 @@ class RotaryEncoder
4950

5051
// call this function every some milliseconds or by using an interrupt for handling state changes of the rotary encoder.
5152
void tick(void);
53+
void tick(int sig1, int sig2);
5254

5355
// Returns the time in milliseconds between the current observed
5456
unsigned long getMillisBetweenRotations() const;
@@ -57,6 +59,8 @@ class RotaryEncoder
5759
unsigned long getRPM();
5860

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

6266
LatchMode _mode; // Latch mode from initialization

0 commit comments

Comments
 (0)