-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotaryEncoder.cpp
More file actions
62 lines (49 loc) · 1.66 KB
/
RotaryEncoder.cpp
File metadata and controls
62 lines (49 loc) · 1.66 KB
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
#include "RotaryEncoder.h"
RotaryEncoder::RotaryEncoder(int ckPin, int dtPin, int swPin, int sampleTime) :
pinCk(ckPin),
pinDt(dtPin),
pinSw(swPin),
encQuiescing(false),
swQuiescing(false),
sampleDelay(sampleTime) {
pinMode(pinCk, INPUT);
pinMode(pinDt, INPUT);
if (pinSw)
pinMode(pinSw, INPUT);
};
RotaryEncoder::Value RotaryEncoder::Poll(unsigned long now) {
int ckVal;
int dtVal;
bool swVal = false;
// If we're waiting for the button push to end, test that.
if (swQuiescing && digitalRead(pinSw))
// It ended. Allow polling for the next push to continue.
swQuiescing = false;
// If we're waiting for the encoder contact to leave the trigger mark, test that.
if (encQuiescing) {
if (digitalRead(pinCk) && digitalRead(pinDt))
// It has left the mark. Allow polling the enocder to resume.
encQuiescing = false;
return {increment: ENC_NONE, switchState: false};
}
// Implement the sampling delay
if (now - timeLastUpdate <= ((unsigned long)sampleDelay * 1000))
return {increment: ENC_NONE, switchState: false};
if (!swQuiescing)
// Poll the button if we're not still processing the previous push
if (swVal = pinSw ? !digitalRead(pinSw) : false)
swQuiescing = true;
Value value = {increment: ENC_NONE, switchState: swVal};
// Poll the encoder pins
ckVal = digitalRead(pinCk);
dtVal = digitalRead(pinDt);
if (ckVal == LOW) {
// We've detected a movement. Determine which direction and let quiesce.
value.increment = (dtVal == LOW) ? ENC_DOWN: ENC_UP;
encQuiescing = true;
}
else
value.increment = ENC_NONE;
timeLastUpdate = now;
return value;
}