@@ -37,6 +37,9 @@ void IRAM_ATTR AiEsp32RotaryEncoder::readEncoder_ISR()
3737
3838 if (currentDirection != 0 )
3939 {
40+ // bool ignoreCorrection = false;
41+ // if (this->encoder0Pos > this->_maxEncoderValue) ignoreCorrection = true;
42+ // if (this->encoder0Pos < this->_minEncoderValue) ignoreCorrection = true;
4043 long prevRotaryPosition = this ->encoder0Pos / this ->encoderSteps ;
4144 this ->encoder0Pos += currentDirection;
4245 long newRotaryPosition = this ->encoder0Pos / this ->encoderSteps ;
@@ -78,11 +81,55 @@ void IRAM_ATTR AiEsp32RotaryEncoder::readEncoder_ISR()
7881 this ->lastMovementDirection = currentDirection;
7982 }
8083
84+ // https://github.com/igorantolic/ai-esp32-rotary-encoder/issues/40
85+ /*
86+ when circling there is an issue since encoderSteps is tipically 4
87+ that means 4 changes for a single roary movement (step)
88+ so if maximum is 4 that means _maxEncoderValue is 4*4=16
89+ when we detact 18 we cannot go to zero since next 2 will make it wild
90+ Here we changed to 18 set not to 0 but to -2; 17 to -3...
91+ Now it seems better however that -3 divided with 4 will give -1 which is not regular -> also readEncoder() is changed to give allowed values
92+ It is not yet perfect for cycling options but it is much better than before
93+
94+ optimistic view was that most of the time encoder0Pos values will be near to N*encodersteps
95+ */
8196 // respect limits
82- if (this ->encoder0Pos > (this ->_maxEncoderValue ))
83- this ->encoder0Pos = this ->_circleValues ? this ->_minEncoderValue : this ->_maxEncoderValue ;
84- if (this ->encoder0Pos < (this ->_minEncoderValue ))
97+ if ((this ->encoder0Pos / this ->encoderSteps ) > (this ->_maxEncoderValue / this ->encoderSteps )){
98+ // Serial.print("circle values limit HIGH");
99+ // Serial.print(this->encoder0Pos);
100+ // this->encoder0Pos = this->_circleValues ? this->_minEncoderValue : this->_maxEncoderValue;
101+ if (_circleValues){
102+ // if (!ignoreCorrection){
103+ int delta =this ->_maxEncoderValue + this ->encoderSteps -this ->encoder0Pos ;
104+ this ->encoder0Pos = this ->_minEncoderValue -delta;
105+ // }
106+ } else {
107+ this ->encoder0Pos = this ->_maxEncoderValue ;
108+ }
109+ // this->encoder0Pos = this->_circleValues ? (this->_minEncoderValue this->encoder0Pos-this->encoderSteps) : this->_maxEncoderValue;
110+ // Serial.print(" -> ");
111+ // Serial.println(this->encoder0Pos);
112+ } else if ((this ->encoder0Pos / this ->encoderSteps ) < (this ->_minEncoderValue / this ->encoderSteps )){
113+ // Serial.print("circle values limit LOW");
114+ // Serial.print(this->encoder0Pos);
115+ // this->encoder0Pos = this->_circleValues ? this->_maxEncoderValue : this->_minEncoderValue;
85116 this ->encoder0Pos = this ->_circleValues ? this ->_maxEncoderValue : this ->_minEncoderValue ;
117+ if (_circleValues){
118+ // if (!ignoreCorrection){
119+ int delta =this ->_minEncoderValue +this ->encoderSteps +this ->encoder0Pos ;
120+ this ->encoder0Pos = this ->_maxEncoderValue +delta;
121+ // }
122+ } else {
123+ this ->encoder0Pos = this ->_minEncoderValue ;
124+ }
125+
126+ // Serial.print(" -> ");
127+ // Serial.println(this->encoder0Pos);
128+ }else {
129+ // Serial.print("no circle values limit ");
130+ // Serial.println(this->encoder0Pos);
131+ }
132+ // Serial.println(this->encoder0Pos);
86133 }
87134 }
88135#if defined(ESP8266)
@@ -146,8 +193,8 @@ AiEsp32RotaryEncoder::AiEsp32RotaryEncoder(uint8_t encoder_APin, uint8_t encoder
146193 pinMode (this ->encoderAPin , INPUT_PULLUP);
147194 pinMode (this ->encoderBPin , INPUT_PULLUP);
148195#else
149- pinMode (this ->encoderAPin , INPUT_PULLDOWN);
150- pinMode (this ->encoderBPin , INPUT_PULLDOWN);
196+ pinMode (this ->encoderAPin , (areEncoderPinsPulldownforEsp32? INPUT_PULLDOWN:INPUT_PULLUP) );
197+ pinMode (this ->encoderBPin , (areEncoderPinsPulldownforEsp32? INPUT_PULLDOWN:INPUT_PULLUP) );
151198#endif
152199}
153200
@@ -161,6 +208,11 @@ void AiEsp32RotaryEncoder::setBoundaries(long minEncoderValue, long maxEncoderVa
161208
162209long AiEsp32RotaryEncoder::readEncoder ()
163210{
211+ // return (this->encoder0Pos / this->encoderSteps);
212+ if ((this ->encoder0Pos / this ->encoderSteps ) > (this ->_maxEncoderValue / this ->encoderSteps ))
213+ return this ->_maxEncoderValue / this ->encoderSteps ;
214+ if ((this ->encoder0Pos / this ->encoderSteps ) < (this ->_minEncoderValue / this ->encoderSteps ))
215+ return this ->_minEncoderValue / this ->encoderSteps ;
164216 return (this ->encoder0Pos / this ->encoderSteps );
165217}
166218
@@ -207,7 +259,13 @@ void AiEsp32RotaryEncoder::begin()
207259 this ->previous_butt_state = 0 ;
208260 if (this ->encoderButtonPin >= 0 )
209261 {
262+
263+ #if defined(ESP8266)
210264 pinMode (this ->encoderButtonPin , INPUT_PULLUP);
265+ #else
266+ pinMode (this ->encoderButtonPin ,isButtonPulldown?INPUT_PULLDOWN, INPUT_PULLUP);
267+ #endif
268+
211269 }
212270}
213271
@@ -226,12 +284,14 @@ ButtonState AiEsp32RotaryEncoder::readButtonState()
226284void AiEsp32RotaryEncoder::reset (long newValue_)
227285{
228286 newValue_ = newValue_ * this ->encoderSteps ;
229- this ->encoder0Pos = newValue_;
287+ this ->encoder0Pos = newValue_ + this -> correctionOffset ;
230288 this ->lastReadEncoder0Pos = this ->encoder0Pos ;
231289 if (this ->encoder0Pos > this ->_maxEncoderValue )
232290 this ->encoder0Pos = this ->_circleValues ? this ->_minEncoderValue : this ->_maxEncoderValue ;
233291 if (this ->encoder0Pos < this ->_minEncoderValue )
234292 this ->encoder0Pos = this ->_circleValues ? this ->_maxEncoderValue : this ->_minEncoderValue ;
293+
294+ this ->lastReadEncoder0Pos = this ->readEncoder ();
235295}
236296
237297void AiEsp32RotaryEncoder::enable ()
0 commit comments