The EventEncoderButton
class combines the EventEncoder
class with the EventButton
class and is used for the common encoder buttons.
The class provides all the encoder events with the addition of CHANGED_PRESSED
which is fired when the encoder is pressed and turned.
All the EventButton
events are also fired but if the encoder is turned while pressed the following changes to events occurs:
- on release, the button's
RELEASED
event is translated to aCHANGED_RELEASED
event. - the
LONG_PRESS
is not fired. - Neither
CLICKED
orLONG_CLICKED
are fired.
When the encoder is not pressed and turned, its button behaves just like a regular EventButton
.
Note: In the Arduino IDE, you must explicitly #include <Encoder.h>
before EventEncoderButton.h
. In PlatformIO, InputEvents will include it for you if you have PJRC's Encoder library installed in your project.
#include <Encoder.h>
#include <EventEncoderButton.h>
// Create an EventEncoderButton input
EventEncoderButton myEncoderButton(2,3, 7); //First two should be interrupt pins
// Create a callback handler function
void onEncoderButtonEvent(InputEventType et, EventEncoderButton& eeb) {
Serial.print("Encoder button event fired. Position is: ");
Serial.print(eeb.position());
Serial.print(" Pressed position is: ");
Serial.println(eeb.pressedPosition());
}
void setup() {
Serial.begin(9600);
// Link the encoder button's callback to function defined above
myEncoderButton.setCallback(onEncoderButtonEvent);
}
void loop() {
// Call 'update' for every EventEncoder
myEncoderButton.update();
}
See example EncoderButton.ino for a slightly more detailed sketch.
In addition to the common events (Enabled, Disabled and Idle) and the EventEncoder
& EventButton
events, the following event types are fired by EventEncoderButton:
Will be fired on each change of encoder increment is pressed and turned.
Will be fired in place of the button's RELEASED
event if the encoder is pressed and turned.
Construct an EventEncoderButton
EventEncoder(byte encoderPin1, encoderPin2, buttonPin);
long pressedPosition();
In addition to the common methods the following are available for EventAnalog:
Must be called within loop()
. See common methods for details.
Setup methods are typically called from within setup()
but can be updated at runtime.
Reset the counted position of the encoder.
Please see EventEncoder
setup and EventButton
setup documentation for details about the other setup methods.
The current pressed position of the encoder. Can be reset by resetPressedPosition()
.
Please see EventEncoder
state and EventButton
sstate documentation for details about the other state methods.