-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasyEncoder.h
60 lines (52 loc) · 1016 Bytes
/
EasyEncoder.h
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
/*
*
* Library created by StillMT
*
*/
#ifndef EASYENCODER_H
#define EASYENCODER_H
#define EASYENCODERLIB_VER "1.0.1"
class EasyEncoder
{
private:
byte pinA, pinB;
bool lastState, beginned, inverted;
public:
EasyEncoder();
bool begin(byte pinA, byte pinB);
void invert(bool invert);
short check();
};
EasyEncoder::EasyEncoder()
{
lastState = HIGH; //You have to set pins on INPUT_PULLUP
beginned = false;
}
bool EasyEncoder::begin(byte pinA, byte pinB)
{
if (!digitalRead(pinA) || !digitalRead(pinB))
return false;
beginned = true;
inverted = false;
EasyEncoder::pinA = pinA;
EasyEncoder::pinB = pinB;
return true;
}
void EasyEncoder::invert(bool invert)
{
inverted = invert;
}
short EasyEncoder::check()
{
if (!beginned)
return -2;
bool read = digitalRead(pinA);
if (lastState && !read)
if (digitalRead(pinB))
return inverted ? -1 : 1;
else
return inverted ? 1 : -1;
lastState = read;
return 0;
}
#endif