-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRotaryEncoderAdvanced.h
102 lines (81 loc) · 4.87 KB
/
RotaryEncoderAdvanced.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/***************************************************************************************************/
/*
This is an Arduino library for Quadrature Rotary Encoder
written by : enjoyneering
sourse code: https://github.com/enjoyneering/RotaryEncoder
This library uses interrupts, specials pins are required to interface
Board: int.0 int.1 int.2 int.3 int.4 int.5 Level
Uno, Mini, Pro, ATmega168, ATmega328..... 2 3 x x x x 5v
Mega2560................................. 2 3 21 20 19 18 5v
Leonardo, Micro, ATmega32U4.............. 3 2 0 1 7 x 5v
Digistump, Trinket, ATtiny85............. 2/physical pin 7 5v
Due, SAM3X8E............................. all digital pins 3v
Zero, ATSAMD21G18........................ all digital pins, except pin 4 3v
Blue Pill, STM32F103xxxx boards.......... all digital pins, maximum 16 pins at the same time 3v
ESP8266.................................. all digital pins, except gpio6 - gpio11 & gpio16 3v/5v
ESP32.................................... all digital pins 3v
NOTE:
- Quadrature encoder makes two waveforms that are 90 degree out of phase:
_______ _______ __
PinA ___| |_______| |_______| PinA
CCW <-- _______ _______
PinB _______| |_______| |______ PinB
_______ _______
PinA _______| |_______| |______ PinA
CW --> _______ _______ __
PinB ___| |_______| |_______| PinB
The half of the pulses from top to bottom create full state array:
prev.A+B cur.A+B (prev.AB+cur.AB) Array Encoder State
------- --------- -------------- ----- -------------
00 00 0000 0 stop/idle
00 01 0001 1 CW, 0x01
00 10 0010 -1 CCW, 0x02
00 11 0011 0 invalid state
01 00 0100 -1 CCW, 0x04
01 01 0101 0 stop/idle
01 10 0110 0 invalid state
01 11 0111 1 CW, 0x07
10 00 1000 1 CW, 0x08
10 01 1001 0 invalid state
10 10 1010 0 stop/idle
10 11 1011 -1 CCW, 0x0B
11 00 1100 0 invalid state
11 01 1101 -1 CCW, 0x0D
11 10 1110 1 CW, 0x0E
11 11 1111 0 stop/idle
- CW states 0b0001, 0b0111, 0b1000, 0b1110
- CCW states 0b0010, 0b0100, 0b1011, 0b1101
Frameworks & Libraries:
AVR Core - https://github.com/arduino/ArduinoCore-avr
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
ESP32 Core - https://github.com/espressif/arduino-esp32
ESP8266 Core - https://github.com/esp8266/Arduino
STM32 Core - https://github.com/stm32duino/Arduino_Core_STM32
- https://github.com/rogerclarkmelbourne/Arduino_STM32
GNU GPL license, all text above must be included in any redistribution,
see link for details - https://www.gnu.org/licenses/licenses.html
*/
/***************************************************************************************************/
#ifndef RotaryEncoderAdvanced_h
#define RotaryEncoderAdvanced_h
#include "RotaryEncoder.h"
template <typename T> class RotaryEncoderAdvanced : public RotaryEncoder
{
public:
RotaryEncoderAdvanced(uint8_t encoderA, uint8_t encoderB, uint8_t encoderButton, T stepsPerClick, T minValue, T maxValue);
T getValue();
void setValue(T value);
T getStepsPerClick();
void setStepsPerClick(T value);
T getMinValue();
void setMinValue(T value);
T getMaxValue();
void setMaxValue(T value);
void setValues(T currValue, T stepsPerClick, T minValue, T maxValue);
private:
T _stepsPerClick; //steps per click
T _minValue; //min returned value
T _maxValue; //max returned value
};
#include "RotaryEncoderAdvanced.tpp" //linker can't find the *.cpp, see https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
#endif