-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotaryEncoder.cpp
87 lines (57 loc) · 1.69 KB
/
RotaryEncoder.cpp
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
/*
RotaryEncoder.h - Library for interpreting Data/Clock pin values from a KY040 rotary encoder and translating these into CW or CCW turns.
Created by Ferrie Bank, 16 January 2020.
Released into the public domain.
*/
#include "Arduino.h"
#include "RotaryEncoder.h"
RotaryEncoder::RotaryEncoder(int dataPin, int clockPin, int bufferSize) {
_bufferSize = bufferSize; // take multiple consequentive movements to report 1 full turn (5 matches the notch distance for KY040)
pinMode(dataPin, INPUT);
pinMode(clockPin, INPUT);
_dataPin = dataPin;
_clockPin = clockPin;
_dataValue = -1;
_clockValue = -1;
_dataPrev = -1;
_clockPrev = -1;
_bufferLimit = 2 * _bufferSize;
_bufferLevel = _bufferSize;
_rotationStatus = 0;
}
int RotaryEncoder::sampleRotationStatus() {
_rotationStatus = 0; // presume static
_dataPrev = _dataValue;
_clockPrev = _clockValue;
_dataValue = digitalRead(_dataPin);
_clockValue = digitalRead(_clockPin);
if ((_dataValue != _dataPrev) || (_clockValue != _clockPrev)) _determineDirection(); // only true if a change was detected
return _rotationStatus;
}
void RotaryEncoder::_determineDirection() {
boolean _isCW;
if (_dataValue == _clockValue) {
_isCW = (_dataValue == _dataPrev);
} else {
_isCW = (_clockValue == _clockPrev);
}
if (_isCW) {
_bufferLevel++;
} else {
_bufferLevel--;
}
_interpretBuffer();
}
void RotaryEncoder::_interpretBuffer() {
if (_bufferLevel == 0) { // full complement of CCW movements
_bufferLevel = _bufferSize;
_rotationStatus = -1;
return;
}
if (_bufferLevel == _bufferLimit) { // full complement of CW movements
_bufferLevel = _bufferSize;
_rotationStatus = 1;
return;
}
_rotationStatus = 0;
}