Newer
Older
RotaryEncoder::RotaryEncoder(int pinA, int pinB, int id = 99) : _pinA(pinA), _pinB(pinB), _id(id) {
pinMode(_pinA, INPUT_PULLUP);
pinMode(_pinB, INPUT_PULLUP);
_previousTimer = millis();
_setState(digitalRead(_pinA), digitalRead(_pinB));
setPosition(0);
}
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
103
104
105
106
void RotaryEncoder::check() {
unsigned long timer = millis();
unsigned long deltaTime = timer - _previousTimer;
if (_inputFlag == true) {
_inputFlag = false;
_changeFlag = true;
_previousTimer = timer;
_cb(this, kEventUnstableUpdate, getPostition());
}
if (_changeFlag == true && deltaTime > _interval) {
_changeFlag = false;
_cb(this, kEventStableUpdate, getPostition());
}
};
int RotaryEncoder::getId() {
return _id;
};
void RotaryEncoder::_setState(int a, int b) {
_state[0] = a;
_state[1] = b;
};
void RotaryEncoder::setPosition(int value) {
_position = value;
};
void RotaryEncoder::_incrementPosition(int delta) {
_position = _position + delta;
};
int RotaryEncoder::_compareArrays(int a[4], int b[4]) {
if (a[0] != b[0]) {
return false;
}
if (a[1] != b[1]) {
return false;
}
if (a[2] != b[2]) {
return false;
}
if (a[3] != b[3]) {
return false;
}
return true;
};
void RotaryEncoder::initInterrupts(void(*function)()) {
attachInterrupt(_pinA, function, CHANGE);
attachInterrupt(_pinB, function, CHANGE);
};
int RotaryEncoder::getPostition() {
return _position;
};
int RotaryEncoder::_findChange(int state1[2], volatile int state2[2]) {
int stateAppend[] = {state1[1], state1[0], state2[1], state2[0]};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
if (_compareArrays(stateAppend, movements[i][j])) {
if (i == 0) {
return 0;
}
else if (i == 1) {
return 1;
}
else if (i == 2) {
return -1;
}
else if (i == 3) {
return 2;
}
else if (i == 4) {
return -2;
}
}
}
}
for (int i = 3; i < 5; i++) {
for (int j = 0; j < 2; j++) {
if (_compareArrays(stateAppend, movements[i][j])) {
if (i == 3) {
return 2;
}
else if (i == 4) {
return -2;
}
}
}
}
Serial.println("INVALID DATA");
return 0;
};
void RotaryEncoder::setEventHandler(void(*function)(RotaryEncoder*, uint8_t, int)) {
_cb = function;
};