1 #ifndef RotaryEncoder_h
2 #define RotaryEncoder_h
40 fUseInterrupt(useInterrupt ? 2 : 0)
42 pinMode(fPin1, INPUT_PULLUP);
43 pinMode(fPin2, INPUT_PULLUP);
47 int sig1 = digitalRead(fPin1);
48 int sig2 = digitalRead(fPin2);
49 fOldState = sig1 | (sig2 << 1);
65 int32_t curval = fValue;
78 if (fUseInterrupt == 2)
84 if (fUseInterrupt == 1)
122 if (fPositionExtPrev > fPositionExt)
125 fPositionExtPrev = fPositionExt;
127 else if (fPositionExtPrev < fPositionExt)
130 fPositionExtPrev = fPositionExt;
135 fPositionExtPrev = fPositionExt;
150 fPosition = ((newValue << 2) | (fPosition & 0x03L));
151 fPositionExt = newValue;
152 fPositionExtPrev = newValue;
157 fPosition = ((newValue << 1) | (fPosition & 0x01L));
158 fPositionExt = newValue;
159 fPositionExtPrev = newValue;
169 return (fPositionExtTime - fPositionExtTimePrev);
178 uint32_t timeBetweenLastPositions = fPositionExtTime - fPositionExtTimePrev;
179 uint32_t timeToLastPosition = millis() - fPositionExtTime;
180 uint32_t t = max(timeBetweenLastPositions, timeToLastPosition);
181 return 60000.0 / ((float)(t * 20));
191 static const int8_t KNOBDIR[] = {
197 int sig1 = digitalRead(fPin1);
198 int sig2 = digitalRead(fPin2);
199 int8_t thisState = sig1 | (sig2 << 1);
200 if (fOldState != thisState)
202 fPosition += KNOBDIR[thisState | (fOldState << 2)];
203 fOldState = thisState;
208 if (thisState == kLatch3)
211 fPositionExt = fPosition >> 2;
212 fPositionExtTimePrev = fPositionExtTime;
213 fPositionExtTime = millis();
218 if (thisState == kLatch0)
221 fPositionExt = fPosition >> 2;
222 fPositionExtTimePrev = fPositionExtTime;
223 fPositionExtTime = millis();
228 if ((thisState == kLatch0) || (thisState == kLatch3))
231 fPositionExt = fPosition >> 1;
232 fPositionExtTimePrev = fPositionExtTime;
233 fPositionExtTime = millis();
244 uint8_t fUseInterrupt;
246 volatile int8_t fOldState;
249 volatile long fPosition = 0;
250 volatile long fPositionExt = 0;
251 volatile long fPositionExtPrev = 0;
253 uint32_t fPositionExtTime = 0;
254 uint32_t fPositionExtTimePrev = 0;