From 6238664e53a752b6b20af9445ebc3f95600d7fcc Mon Sep 17 00:00:00 2001 From: "aron.cserkaszky" Date: Mon, 10 Feb 2025 15:57:23 +0100 Subject: [PATCH] added setPositionLimits to handle limited rotators --- examples/LimitedRotator/LimitedRotator.ino | 10 +------ src/RotaryEncoder.cpp | 32 +++++++++++++++++++++- src/RotaryEncoder.h | 10 +++++-- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/examples/LimitedRotator/LimitedRotator.ino b/examples/LimitedRotator/LimitedRotator.ino index 23bfff1..3225872 100644 --- a/examples/LimitedRotator/LimitedRotator.ino +++ b/examples/LimitedRotator/LimitedRotator.ino @@ -55,6 +55,7 @@ void setup() while (! Serial); Serial.println("LimitedRotator example for the RotaryEncoder library."); encoder.setPosition(10 / ROTARYSTEPS); // start with the value of 10. + encoder.setPositionLimits( ROTARYMIN / ROTARYSTEPS, ROTARYMAX / ROTARYSTEPS ); } // setup() @@ -66,15 +67,6 @@ void loop() // get the current physical position and calc the logical position int newPos = encoder.getPosition() * ROTARYSTEPS; - if (newPos < ROTARYMIN) { - encoder.setPosition(ROTARYMIN / ROTARYSTEPS); - newPos = ROTARYMIN; - - } else if (newPos > ROTARYMAX) { - encoder.setPosition(ROTARYMAX / ROTARYSTEPS); - newPos = ROTARYMAX; - } // if - if (lastPos != newPos) { Serial.print(newPos); Serial.println(); diff --git a/src/RotaryEncoder.cpp b/src/RotaryEncoder.cpp index 4985730..2655d37 100644 --- a/src/RotaryEncoder.cpp +++ b/src/RotaryEncoder.cpp @@ -14,6 +14,7 @@ #include "RotaryEncoder.h" #include "Arduino.h" +#include #define LATCH0 0 // input state at position 0 #define LATCH3 3 // input state at position 3 @@ -58,6 +59,10 @@ RotaryEncoder::RotaryEncoder(int pin1, int pin2, LatchMode mode) _position = 0; _positionExt = 0; _positionExtPrev = 0; + + // set absolute min max limits of our storage type + _positionMin = LONG_MIN; + _positionMax = LONG_MAX; } // RotaryEncoder() @@ -146,7 +151,15 @@ void RotaryEncoder::tick(void) } break; } // switch + + // apply limits + if (_positionExt < _positionMin) { + setPosition(_positionMin); + } else if (_positionExt > _positionMax) { + setPosition(_positionMax); + } // if } // if + } // tick() @@ -164,5 +177,22 @@ unsigned long RotaryEncoder::getRPM() return 60000.0 / ((float)(t * 20)); } +void RotaryEncoder::setPositionLimits(long min, long max) +{ + if (max < min) { + long temp = min; + min = max; + max = temp; + } + + _positionMin = min; + _positionMax = max; + + if (_positionExt < _positionMin) { + setPosition(_positionMin); + } else if (_positionExt > _positionMax) { + setPosition(_positionMax); + } // if +} // setPositionLimits() -// End \ No newline at end of file +// End diff --git a/src/RotaryEncoder.h b/src/RotaryEncoder.h index 862f19f..7cc6881 100644 --- a/src/RotaryEncoder.h +++ b/src/RotaryEncoder.h @@ -56,9 +56,12 @@ class RotaryEncoder // Returns the RPM unsigned long getRPM(); + // Set min and max value of position + void setPositionLimits(long min, long max); + private: int _pin1, _pin2; // Arduino pins used for the encoder. - + LatchMode _mode; // Latch mode from initialization volatile int8_t _oldState; @@ -69,8 +72,11 @@ class RotaryEncoder unsigned long _positionExtTime; // The time the last position change was detected. unsigned long _positionExtTimePrev; // The time the previous position change was detected. + + long _positionMin; + long _positionMax; }; #endif -// End \ No newline at end of file +// End