Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of digitalFASTread() possibility #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions src/RotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/RotaryEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down