@@ -46,14 +46,16 @@ RotaryEncoder::RotaryEncoder(int pin1, int pin2, LatchMode mode)
4646 _mode = mode;
4747
4848 // Setup the input pins and turn on pullup resistor
49- pinMode (pin1, INPUT_PULLUP);
50- pinMode (pin2, INPUT_PULLUP);
51-
52- // when not started in motion, the current state of the encoder should be 3
53- int sig1 = digitalRead (_pin1);
54- int sig2 = digitalRead (_pin2);
49+ if ((pin1 >= 0 ) && (pin2 >= 0 )) {
50+ pinMode (pin1, INPUT_PULLUP);
51+ pinMode (pin2, INPUT_PULLUP);
52+ // when not started in motion, the current state of the encoder should be 3
53+ int sig1 = digitalRead (_pin1);
54+ int sig2 = digitalRead (_pin2);
55+ }
56+
5557 _oldState = sig1 | (sig2 << 1 );
56-
58+
5759 // start with position 0;
5860 _position = 0 ;
5961 _positionExt = 0 ;
@@ -108,37 +110,18 @@ void RotaryEncoder::setPosition(long newPosition)
108110} // setPosition()
109111
110112
113+ // Slow, but Simple Variant by directly Read-Out of the Digital State within loop-call
111114void RotaryEncoder::tick (void )
112- { // Slow, but Simple Variant by directly Read-Out of the Digital State within loop-call
115+ {
113116 int sig1 = digitalRead (_pin1);
114117 int sig2 = digitalRead (_pin2);
115- _tick (sig1, sig2);
116-
118+ tick (sig1, sig2);
117119} // tick()
118120
121+ // When a faster method than digitalRead is available you can _tick with the 2 values directly.
119122void 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 );
123+ {
124+ int8_t thisState = sig1 | (sig2 << 1 );
142125
143126 if (_oldState != thisState) {
144127 _position += KNOBDIR[thisState | (_oldState << 2 )];
@@ -173,7 +156,22 @@ void RotaryEncoder::_tick(int _sig1, int _sig2){
173156 break ;
174157 } // switch
175158 } // if
176- } // RotaryEncoder::_tick()
159+ } // tick()
160+
161+
162+ unsigned long RotaryEncoder::getMillisBetweenRotations () const
163+ {
164+ return (_positionExtTime - _positionExtTimePrev);
165+ }
166+
167+ unsigned long RotaryEncoder::getRPM ()
168+ {
169+ // calculate max of difference in time between last position changes or last change and now.
170+ unsigned long timeBetweenLastPositions = _positionExtTime - _positionExtTimePrev;
171+ unsigned long timeToLastPosition = millis () - _positionExtTime;
172+ unsigned long t = max (timeBetweenLastPositions, timeToLastPosition);
173+ return 60000.0 / ((float )(t * 20 ));
174+ }
177175
178176
179177// End
0 commit comments