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

callback functions like in OneButton #42

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
68 changes: 68 additions & 0 deletions examples/LikeOneButton/LikeOneButton.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// -----
// SimplePollRotator.ino - Example for the RotaryEncoder library.
// This class is implemented for use with the Arduino environment.
//
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD 3-Clause License. See http://www.mathertel.de/License.aspx
// More information on: http://www.mathertel.de/Arduino
// -----
// 18.01.2014 created by Matthias Hertel
// 04.02.2021 conditions and settings added for ESP8266
// -----

// This example checks the state of the rotary encoder in the loop() function.
// The current position and direction is printed on output when changed.

// Hardware setup:
// Attach a rotary encoder with output pins to
// * A2 and A3 on Arduino UNO.
// * D5 and D6 on ESP8266 board (e.g. NodeMCU).
// Swap the pins when direction is detected wrong.
// The common contact should be attached to ground.

#include <Arduino.h>
#include <RotaryEncoder.h>

#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO_EVERY)
// Example for Arduino UNO with input signals on pin 2 and 3
#define PIN_IN1 A2
#define PIN_IN2 A3

#elif defined(ESP8266)
// Example for ESP8266 NodeMCU with input signals on pin D5 and D6
#define PIN_IN1 D5
#define PIN_IN2 D6

#endif

// Setup a RotaryEncoder with 4 steps per latch for the 2 signal input pins:
// RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3);

// Setup a RotaryEncoder with 2 steps per latch for the 2 signal input pins:
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);

void setup()
{
Serial.begin(115200);
while (! Serial);
Serial.println("SimplePollRotator example for the RotaryEncoder library.");

encoder.attachTurnLeft([]() {
Serial.println("Encoder turn left");
});
encoder.attachTurnRight([]() {
Serial.println("Encoder turn right");
});
} // setup()


// Read the current position of the encoder and print out when changed.
void loop()
{

encoder.tick();

} // loop ()

// The End

46 changes: 46 additions & 0 deletions src/RotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,36 @@ RotaryEncoder::RotaryEncoder(int pin1, int pin2, LatchMode mode)
_positionExtPrev = 0;
} // RotaryEncoder()

// save function for rotate event
void RotaryEncoder::attachTurnLeft(callbackFunction newFunction)
{
_turnLeft = newFunction;
} // attachClick


// save function for rotate event
void RotaryEncoder::attachTurnRight(callbackFunction newFunction)
{
_turnRight = newFunction;
} // attachClick


// save function for parameterized rotate_left event
void RotaryEncoder::attachTurnLeft(parameterizedCallbackFunction newFunction, void *parameter)
{
_param_f_turnLeft = newFunction;
_turnLeftFuncParam = parameter;
} // attachClick


// save function for parameterized rotate_right event
void RotaryEncoder::attachTurnRight(parameterizedCallbackFunction newFunction, void *parameter)
{
_param_f_turnRight = newFunction;
_turnRightFuncParam = parameter;
} // attachClick



long RotaryEncoder::getPosition()
{
Expand Down Expand Up @@ -146,6 +176,22 @@ void RotaryEncoder::tick(void)
}
break;
} // switch

switch( getDirection() ) {

case Direction::NOROTATION:
break;

case Direction::CLOCKWISE:
if (_turnRight) _turnRight();
if (_param_f_turnRight) _param_f_turnRight(_turnRightFuncParam);
break;

case Direction::COUNTERCLOCKWISE:
if (_turnLeft) _turnLeft();
if (_param_f_turnLeft) _param_f_turnLeft(_turnLeftFuncParam);
break;
}
} // if
} // tick()

Expand Down
21 changes: 21 additions & 0 deletions src/RotaryEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

#include "Arduino.h"

extern "C" {
typedef void (*callbackFunction)(void);
typedef void (*parameterizedCallbackFunction)(void *);
}

class RotaryEncoder
{
public:
Expand All @@ -41,6 +46,14 @@ class RotaryEncoder
// retrieve the current position
long getPosition();

void attachTurnLeft(callbackFunction newFunction);

void attachTurnLeft(parameterizedCallbackFunction newFunction, void *parameter);

void attachTurnRight(callbackFunction newFunction);

void attachTurnRight(parameterizedCallbackFunction newFunction, void *parameter);

// simple retrieve of the direction the knob was rotated last time. 0 = No rotation, 1 = Clockwise, -1 = Counter Clockwise
Direction getDirection();

Expand Down Expand Up @@ -69,6 +82,14 @@ class RotaryEncoder

unsigned long _positionExtTime; // The time the last position change was detected.
unsigned long _positionExtTimePrev; // The time the previous position change was detected.

callbackFunction _turnRight = NULL;
parameterizedCallbackFunction _param_f_turnRight = NULL;
void *_turnRightFuncParam = NULL;

callbackFunction _turnLeft = NULL;
parameterizedCallbackFunction _param_f_turnLeft = NULL;
void *_turnLeftFuncParam = NULL;
};

#endif
Expand Down