-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotaryEncoder.cpp
96 lines (74 loc) · 1.57 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
88
89
90
91
92
93
94
95
96
// Rotary Encoder class
#include "Arduino.h"
#include "RotaryEncoder.h"
// Debugging flag
const bool DEBUGGING = false;
// Pulse counter
long _pulses = 0;
// Rotary encoder signals
int _A_SIG = 0, _B_SIG = 0;
// Performs the necessary interrupt setup
void re_setup() {
attachInterrupt(0, re_aRise, RISING);
attachInterrupt(1, re_bRise, RISING);
}
// Returns the number of pulses counted so far
long re_getPulses() {
return _pulses;
}
// Manually set the number of pulses
void re_setPulses(int number) {
_pulses = number;
}
// Short-cut method to set pulses to zero
void re_resetPulses() {
_pulses = 0;
}
void re_aRise(){
detachInterrupt(0);
_A_SIG = 1;
if (_B_SIG == 0) {
_pulses--; //moving forward
}
if (_B_SIG == 1) {
_pulses++; //moving reverse
}
if (DEBUGGING) Serial.println(_pulses);
attachInterrupt(0, re_aFall, FALLING);
}
void re_aFall(){
detachInterrupt(0);
_A_SIG = 0;
if (_B_SIG == 1) {
_pulses--; //moving forward
}
if (_B_SIG == 0) {
_pulses++; //moving reverse
}
if (DEBUGGING) Serial.println(_pulses);
attachInterrupt(0, re_aRise, RISING);
}
void re_bRise(){
detachInterrupt(1);
_B_SIG = 1;
if (_A_SIG == 1) {
_pulses--; //moving forward
}
if (_A_SIG == 0) {
_pulses++; //moving reverse
}
if (DEBUGGING) Serial.println(_pulses);
attachInterrupt(1, re_bFall, FALLING);
}
void re_bFall(){
detachInterrupt(1);
_B_SIG = 0;
if (_A_SIG == 0) {
_pulses--; //moving forward
}
if (_A_SIG == 1) {
_pulses++; //moving reverse
}
if (DEBUGGING) Serial.println(_pulses);
attachInterrupt(1, re_bRise, RISING);
}