-
Notifications
You must be signed in to change notification settings - Fork 0
/
StevesAwesomeRotaryEncoder.cpp
54 lines (44 loc) · 1.26 KB
/
StevesAwesomeRotaryEncoder.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
#include "Arduino.h"
#include "StevesAwesomeRotaryEncoder.h"
StevesAwesomeRotaryEncoder::StevesAwesomeRotaryEncoder(int _pin1, int _pin2, int _encoderNumb)
{
pin1 = _pin1;
pin2 = _pin2;
encoderNumb = _encoderNumb;
oldPosition = -1;
newPosition = -1;
subCount = 0;
totalCount = 0;
pinMode(pin1, INPUT);
digitalWrite(pin1, HIGH);
pinMode(pin2, INPUT);
digitalWrite(pin2, HIGH);
}
void StevesAwesomeRotaryEncoder::leftClickHandler(void (*f)(int))
{
leftClickCallback = *f;
}
void StevesAwesomeRotaryEncoder::rightClickHandler(void (*f)(int))
{
rightClickCallback = *f;
}
void StevesAwesomeRotaryEncoder::process()
{
newPosition = (digitalRead(pin2) * 2) + digitalRead(pin1);
if (newPosition != oldPosition) {
int isFwd = ((oldPosition == 0) && (newPosition == 1)) ||
((oldPosition == 1) && (newPosition == 3)) ||
((oldPosition == 3) && (newPosition == 2)) ||
((oldPosition == 2) && (newPosition == 0));
if (isFwd == true) subCount++;
else if (isFwd == false) subCount--;
if (subCount == -4) {
subCount = 0;
rightClickCallback(encoderNumb);
} else if (subCount == 4) {
subCount = 0;
leftClickCallback(encoderNumb);
}
oldPosition = newPosition;
}
}