firmware/src/plugins/input/RotaryEncoderInterruptBase.cpp

151 lines
3.9 KiB
C++
Raw Normal View History

2022-01-08 12:13:29 +00:00
#include "configuration.h"
#include "RotaryEncoderInterruptBase.h"
RotaryEncoderInterruptBase::RotaryEncoderInterruptBase(
2022-01-11 12:12:04 +00:00
const char *name) :
concurrency::OSThread(name)
{
2022-01-11 15:02:55 +00:00
this->_originName = name;
2022-01-11 12:12:04 +00:00
}
void RotaryEncoderInterruptBase::init(
2022-01-08 12:13:29 +00:00
uint8_t pinA, uint8_t pinB, uint8_t pinPress,
char eventCw, char eventCcw, char eventPressed,
2022-01-11 12:12:04 +00:00
// std::function<void(void)> onIntA, std::function<void(void)> onIntB, std::function<void(void)> onIntPress) :
void (*onIntA)(), void (*onIntB)(), void (*onIntPress)())
2022-01-08 12:13:29 +00:00
{
this->_pinA = pinA;
this->_pinB = pinB;
this->_eventCw = eventCw;
this->_eventCcw = eventCcw;
this->_eventPressed = eventPressed;
pinMode(pinPress, INPUT_PULLUP);
pinMode(this->_pinA, INPUT_PULLUP);
pinMode(this->_pinB, INPUT_PULLUP);
2022-01-11 12:12:04 +00:00
2022-01-08 12:13:29 +00:00
// attachInterrupt(pinPress, onIntPress, RISING);
attachInterrupt(pinPress, onIntPress, RISING);
attachInterrupt(this->_pinA, onIntA, CHANGE);
attachInterrupt(this->_pinB, onIntB, CHANGE);
2022-01-11 12:12:04 +00:00
2022-01-08 12:13:29 +00:00
this->rotaryLevelA = digitalRead(this->_pinA);
this->rotaryLevelB = digitalRead(this->_pinB);
2022-01-09 20:14:23 +00:00
DEBUG_MSG("Rotary initialized (%d, %d, %d)\n",
this->_pinA, this->_pinB, pinPress);
2022-01-08 12:13:29 +00:00
}
2022-01-11 12:12:04 +00:00
2022-01-08 12:13:29 +00:00
int32_t RotaryEncoderInterruptBase::runOnce()
{
2022-01-11 15:02:55 +00:00
InputEvent e;
2022-01-12 08:26:42 +00:00
e.inputEvent = InputEventChar_NULL;
2022-01-11 15:02:55 +00:00
e.origin = this->_originName;
2022-01-09 09:08:31 +00:00
if (this->action == ROTARY_ACTION_PRESSED)
{
2022-01-11 15:02:55 +00:00
DEBUG_MSG("Rotary event Press\n");
2022-01-09 09:08:31 +00:00
e.inputEvent = this->_eventPressed;
}
else if (this->action == ROTARY_ACTION_CW)
2022-01-08 12:13:29 +00:00
{
2022-01-09 20:14:23 +00:00
DEBUG_MSG("Rotary event CW\n");
2022-01-09 09:08:31 +00:00
e.inputEvent = this->_eventCw;
2022-01-08 12:13:29 +00:00
}
2022-01-09 09:08:31 +00:00
else if (this->action == ROTARY_ACTION_CCW)
{
2022-01-09 20:14:23 +00:00
DEBUG_MSG("Rotary event CW\n");
2022-01-09 09:08:31 +00:00
e.inputEvent = this->_eventCcw;
2022-01-11 15:02:55 +00:00
}
2022-01-12 08:26:42 +00:00
if (e.inputEvent != InputEventChar_NULL)
2022-01-11 15:02:55 +00:00
{
2022-01-09 09:08:31 +00:00
this->notifyObservers(&e);
}
2022-01-11 15:02:55 +00:00
2022-01-09 09:08:31 +00:00
this->action = ROTARY_ACTION_NONE;
2022-01-08 12:13:29 +00:00
return 30000;
}
void RotaryEncoderInterruptBase::intPressHandler()
{
2022-01-09 09:08:31 +00:00
this->action = ROTARY_ACTION_PRESSED;
2022-01-08 12:13:29 +00:00
runned(millis());
setInterval(20);
}
void RotaryEncoderInterruptBase::intAHandler()
{
// CW rotation (at least on most common rotary encoders)
int currentLevelA = digitalRead(this->_pinA);
if (this->rotaryLevelA == currentLevelA)
{
return;
}
this->rotaryLevelA = currentLevelA;
2022-01-12 21:50:37 +00:00
intHandler(
currentLevelA == HIGH,
this->rotaryLevelB,
ROTARY_ACTION_CCW,
this->rotaryStateCCW);
2022-01-08 12:13:29 +00:00
}
void RotaryEncoderInterruptBase::intBHandler()
{
// CW rotation (at least on most common rotary encoders)
int currentLevelB = digitalRead(this->_pinB);
if (this->rotaryLevelB == currentLevelB)
{
return;
}
this->rotaryLevelB = currentLevelB;
2022-01-12 21:50:37 +00:00
this->rotaryStateCW = intHandler(
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))
2022-01-08 12:13:29 +00:00
{
2022-01-12 21:50:37 +00:00
if (state == ROTARY_EVENT_CLEARED)
2022-01-08 12:13:29 +00:00
{
2022-01-12 21:50:37 +00:00
newState = ROTARY_EVENT_OCCURRED;
if ((this->action != ROTARY_ACTION_PRESSED)
&& (this->action != action))
2022-01-08 12:13:29 +00:00
{
2022-01-12 21:50:37 +00:00
this->action = action;
DEBUG_MSG("Rotary action\n");
2022-01-08 12:13:29 +00:00
}
}
}
2022-01-12 21:50:37 +00:00
else if (!actualPinRaising && (otherPinLevel == HIGH))
2022-01-08 12:13:29 +00:00
{
// Logic to prevent bouncing.
2022-01-12 21:50:37 +00:00
newState = ROTARY_EVENT_CLEARED;
2022-01-08 12:13:29 +00:00
}
runned(millis());
setInterval(50);
2022-01-12 21:50:37 +00:00
return newState;
2022-01-08 12:13:29 +00:00
}