From 50656e551397e2fc5858288dbdfa6e1dff58294c Mon Sep 17 00:00:00 2001 From: Jojo1220 Date: Thu, 6 Jun 2024 13:32:27 +0200 Subject: [PATCH] Implementation of digitalFASTread() possibility due to split-up of tick() function --- src/RotaryEncoder.cpp | 47 ++++++++++++++++++++++++++----------------- src/RotaryEncoder.h | 4 ++++ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/RotaryEncoder.cpp b/src/RotaryEncoder.cpp index 4985730..97df6ce 100644 --- a/src/RotaryEncoder.cpp +++ b/src/RotaryEncoder.cpp @@ -109,10 +109,36 @@ void RotaryEncoder::setPosition(long newPosition) void RotaryEncoder::tick(void) -{ +{ // Slow, but Simple Variant by directly Read-Out of the Digital State within loop-call int sig1 = digitalRead(_pin1); int sig2 = digitalRead(_pin2); - int8_t thisState = sig1 | (sig2 << 1); + _tick(sig1, sig2); + +} // tick() + +void RotaryEncoder::tick(int sig1, int sig2) +{ // Possibility to make a fast call with digitalFASTRead() option and directly Register Readout + _tick(sig1, sig2); + +} // tick() + + +unsigned long RotaryEncoder::getMillisBetweenRotations() const +{ + return (_positionExtTime - _positionExtTimePrev); +} + +unsigned long RotaryEncoder::getRPM() +{ + // calculate max of difference in time between last position changes or last change and now. + unsigned long timeBetweenLastPositions = _positionExtTime - _positionExtTimePrev; + unsigned long timeToLastPosition = millis() - _positionExtTime; + unsigned long t = max(timeBetweenLastPositions, timeToLastPosition); + return 60000.0 / ((float)(t * 20)); +} + +void RotaryEncoder::_tick(int _sig1, int _sig2){ + int8_t thisState = _sig1 | (_sig2 << 1); if (_oldState != thisState) { _position += KNOBDIR[thisState | (_oldState << 2)]; @@ -147,22 +173,7 @@ void RotaryEncoder::tick(void) break; } // switch } // if -} // tick() - - -unsigned long RotaryEncoder::getMillisBetweenRotations() const -{ - return (_positionExtTime - _positionExtTimePrev); -} - -unsigned long RotaryEncoder::getRPM() -{ - // calculate max of difference in time between last position changes or last change and now. - unsigned long timeBetweenLastPositions = _positionExtTime - _positionExtTimePrev; - unsigned long timeToLastPosition = millis() - _positionExtTime; - unsigned long t = max(timeBetweenLastPositions, timeToLastPosition); - return 60000.0 / ((float)(t * 20)); -} +} // RotaryEncoder::_tick() // End \ No newline at end of file diff --git a/src/RotaryEncoder.h b/src/RotaryEncoder.h index 862f19f..6eb31f0 100644 --- a/src/RotaryEncoder.h +++ b/src/RotaryEncoder.h @@ -14,6 +14,7 @@ // 10.11.2020 Added the ability to obtain the encoder RPM // 29.01.2021 Options for using rotary encoders with 2 state changes per latch. // ----- +// 06.06.2024 Implementation of digitalFASTRead() possibility due to splitting up "tick()" function #ifndef RotaryEncoder_h #define RotaryEncoder_h @@ -49,6 +50,7 @@ class RotaryEncoder // call this function every some milliseconds or by using an interrupt for handling state changes of the rotary encoder. void tick(void); + void tick(int sig1, int sig2); // Returns the time in milliseconds between the current observed unsigned long getMillisBetweenRotations() const; @@ -57,6 +59,8 @@ class RotaryEncoder unsigned long getRPM(); private: + void _tick(int _sig1, int _sig2); // Private internal tick function + int _pin1, _pin2; // Arduino pins used for the encoder. LatchMode _mode; // Latch mode from initialization