-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRotaryEncoder.cpp
81 lines (65 loc) · 1.66 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
#include "RotaryEncoder.h"
RotaryEncoder *RotaryEncoder::enc=NULL;
char RotaryEncoder::ENCPIN0=3;
char RotaryEncoder::ENCPIN1=2;
RotaryEncoder::RotaryEncoder(bool rev)
{
enc=this;
delta=0;
laststate=0;
trans=0;
// pin-swap will revese operation
if(rev){
int tmp=ENCPIN0;
ENCPIN0=ENCPIN1;
ENCPIN1=tmp;
}
}
void RotaryEncoder::begin()
{
// inputs with pullup resistors
pinMode(ENCPIN0, INPUT);
pinMode(ENCPIN1, INPUT);
digitalWrite(ENCPIN0, HIGH);
digitalWrite(ENCPIN1, HIGH);
// init state
laststate=(digitalRead(ENCPIN1) << 1) | digitalRead(ENCPIN0);
lastdetent=laststate;
delta=0;
trans=0;
// attach interrupt handler
attachInterrupt(ENCINT0, &RotaryEncoder::changeInterrupt, CHANGE);
attachInterrupt(ENCINT1, &RotaryEncoder::changeInterrupt, CHANGE);
}
void RotaryEncoder::changeInterrupt()
{
if(NULL == enc)
return;
char p0=digitalRead(ENCPIN0);
char p1=digitalRead(ENCPIN1);
char newstate=(p1<<1) | p0;
if(newstate != enc->laststate){
if(p0 == p1){
// in detent, make sure we only change if we're
// going to different detent, not bouncing back to
// previous one
if(newstate != enc->lastdetent)
enc->delta+=enc->trans;
enc->lastdetent=newstate;
enc->trans=0;
}
else{
// leaving detent
enc->trans=(((newstate ^ enc->laststate) & 1)<<1)-1;
}
// remember state
enc->laststate=newstate;
}
// else wtf?
}
int RotaryEncoder::getDelta()
{
int res=delta;
delta=0;
return res;
}