Rotary get rid of duplicate methods.

This commit is contained in:
Balazs Kelemen 2022-01-12 22:50:37 +01:00
parent 3fa00f603b
commit 0f1c424731
2 changed files with 46 additions and 40 deletions

View File

@ -76,15 +76,6 @@ void RotaryEncoderInterruptBase::intPressHandler()
setInterval(20); setInterval(20);
} }
/**
* @brief Rotary action implementation.
* We assume, the following pin setup:
* A --||
* GND --||]========
* B --||
*
* @return The new level of the actual pin (that is actualPinCurrentLevel).
*/
void RotaryEncoderInterruptBase::intAHandler() void RotaryEncoderInterruptBase::intAHandler()
{ {
// CW rotation (at least on most common rotary encoders) // CW rotation (at least on most common rotary encoders)
@ -94,27 +85,11 @@ void RotaryEncoderInterruptBase::intAHandler()
return; return;
} }
this->rotaryLevelA = currentLevelA; this->rotaryLevelA = currentLevelA;
bool pinARaising = currentLevelA == HIGH; intHandler(
if (pinARaising && (this->rotaryLevelB == LOW)) currentLevelA == HIGH,
{ this->rotaryLevelB,
if (this->rotaryStateCCW == ROTARY_EVENT_CLEARED) ROTARY_ACTION_CCW,
{ this->rotaryStateCCW);
this->rotaryStateCCW = ROTARY_EVENT_OCCURRED;
if ((this->action == ROTARY_ACTION_NONE)
|| (this->action == ROTARY_ACTION_CW))
{
this->action = ROTARY_ACTION_CCW;
DEBUG_MSG("Rotary action CCW\n");
}
}
}
else if (!pinARaising && (this->rotaryLevelB == HIGH))
{
// Logic to prevent bouncing.
this->rotaryStateCCW = ROTARY_EVENT_CLEARED;
}
runned(millis());
setInterval(50);
} }
void RotaryEncoderInterruptBase::intBHandler() void RotaryEncoderInterruptBase::intBHandler()
@ -126,25 +101,50 @@ void RotaryEncoderInterruptBase::intBHandler()
return; return;
} }
this->rotaryLevelB = currentLevelB; this->rotaryLevelB = currentLevelB;
bool pinBRaising = currentLevelB == HIGH; this->rotaryStateCW = intHandler(
if (pinBRaising && (this->rotaryLevelA == LOW)) currentLevelB == HIGH,
this->rotaryLevelA,
ROTARY_ACTION_CW,
this->rotaryStateCW);
}
/**
* @brief Rotary action implementation.
* We assume, the following pin setup:
* A --||
* GND --||]========
* B --||
*
* @return The new state for rotary pin.
*/
RotaryEncoderInterruptBaseStateType RotaryEncoderInterruptBase::intHandler(
bool actualPinRaising,
int otherPinLevel,
RotaryEncoderInterruptBaseActionType action,
RotaryEncoderInterruptBaseStateType state)
{
RotaryEncoderInterruptBaseStateType newState =
state;
if (actualPinRaising && (otherPinLevel == LOW))
{ {
if (this->rotaryStateCW == ROTARY_EVENT_CLEARED) if (state == ROTARY_EVENT_CLEARED)
{ {
this->rotaryStateCW = ROTARY_EVENT_OCCURRED; newState = ROTARY_EVENT_OCCURRED;
if ((this->action == ROTARY_ACTION_NONE) if ((this->action != ROTARY_ACTION_PRESSED)
|| (this->action == ROTARY_ACTION_CCW)) && (this->action != action))
{ {
this->action = ROTARY_ACTION_CW; this->action = action;
DEBUG_MSG("Rotary action CW\n"); DEBUG_MSG("Rotary action\n");
} }
} }
} }
else if (!pinBRaising && (this->rotaryLevelA == HIGH)) else if (!actualPinRaising && (otherPinLevel == HIGH))
{ {
// Logic to prevent bouncing. // Logic to prevent bouncing.
this->rotaryStateCW = ROTARY_EVENT_CLEARED; newState = ROTARY_EVENT_CLEARED;
} }
runned(millis()); runned(millis());
setInterval(50); setInterval(50);
return newState;
} }

View File

@ -35,6 +35,12 @@ class RotaryEncoderInterruptBase :
protected: protected:
virtual int32_t runOnce(); virtual int32_t runOnce();
RotaryEncoderInterruptBaseStateType intHandler(
bool actualPinRaising,
int otherPinLevel,
RotaryEncoderInterruptBaseActionType action,
RotaryEncoderInterruptBaseStateType state);
volatile RotaryEncoderInterruptBaseStateType rotaryStateCW = ROTARY_EVENT_CLEARED; volatile RotaryEncoderInterruptBaseStateType rotaryStateCW = ROTARY_EVENT_CLEARED;
volatile RotaryEncoderInterruptBaseStateType rotaryStateCCW = ROTARY_EVENT_CLEARED; volatile RotaryEncoderInterruptBaseStateType rotaryStateCCW = ROTARY_EVENT_CLEARED;
volatile int rotaryLevelA = LOW; volatile int rotaryLevelA = LOW;