Skip to content
Snippets Groups Projects
RotaryEncoder.h 1.83 KiB
Newer Older
#ifndef ROTARYENCODER_h
#define ROTARYENCODER_h

Matthew's avatar
Matthew committed
/**
 * [void description]
 * @param _cb [description]
 */
Matthew's avatar
Matthew committed
class RotaryEncoder
{
Matthew's avatar
Matthew committed
	// Private members:
Matthew's avatar
Matthew committed
	int _pinA;
	int _pinB;
	int _id;
Matthew's avatar
Matthew committed
	volatile int _state[2];
	volatile int _position;
Matthew's avatar
Matthew committed
	volatile bool _inputFlag = false;
	bool _changeFlag = false;
Matthew's avatar
Matthew committed
	unsigned long _previousTimer;
	int _interval = 200;
Matthew's avatar
Matthew committed
	void (*_cb)(RotaryEncoder*, uint8_t, int); // Callback function
Matthew's avatar
Matthew committed
	 // {newPin2, newPin1, oldPin2, oldPin1}
	int movements[5][4][4] = {
		{ // No movement
			{0, 0, 0, 0},
			{0, 1, 0, 1},
			{1, 0, 1, 0},
			{1, 1, 1, 1}
		},
		{ // +1
			{0, 0, 0, 1},
			{0, 1, 1, 1},
			{1, 0, 0, 0},
			{1, 1, 1, 0}
		},
		{ // -1
			{0, 0, 1, 0},
			{0, 1, 0, 0},
			{1, 0, 1, 1},
			{1, 1, 0, 1}
		},
		{ // +2
			{0, 0, 1, 1},
			{1, 1, 0, 0}
		},
		{ // -2
			{0, 1, 1, 0},
			{1, 0, 0, 1}
		},
	};
Matthew's avatar
Matthew committed
	// Private Functions:
Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	void _setState(int a, int b);
Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	void _incrementPosition(int delta);
Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	int _findChange(int state1[2], volatile int state2[2]);
Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	boolean _compareArrays(int a[4], int b[4]);
Matthew's avatar
Matthew committed
public:
Matthew's avatar
Matthew committed
	// Public members:
Matthew's avatar
Matthew committed
	static const uint8_t kEventStableUpdate = 0;
	static const uint8_t kEventUnstableUpdate = 1;
Matthew's avatar
Matthew committed
	//Public Functions:
	RotaryEncoder(int pinA, int pinB, int id = 99);

Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	void initInterrupts(void (*function)());
Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	void setEventHandler(void (*function)(RotaryEncoder*, uint8_t, int));
Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	int getPostition();
Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	int getId();
Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	void setPosition(int value);
Matthew's avatar
Matthew committed
	/**
	   @brief What does this do?
	 */
Matthew's avatar
Matthew committed
	void check();
Matthew's avatar
Matthew committed
	void ICACHE_RAM_ATTR tick();