forked from philipperemy/digital-setting-circles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoder.cpp
62 lines (48 loc) · 1.13 KB
/
Encoder.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
#include "Encoder.h"
std::vector<Encoder*> Encoder::_encoders_ptr;
Encoder::Encoder(int pin_a, int pin_b)
: pin_a(pin_a), pin_b(pin_b)
{
setup_encoder();
}
Encoder::~Encoder()
{
//dtor
}
void Encoder::update_encoder()
{
int MSB = digitalRead(pin_a);
int LSB = digitalRead(pin_b);
int encoded = (MSB << 1) | LSB;
int sum = (last_encoded << 2) | encoded;
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011)
{
value++;
}
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000)
{
value--;
}
last_encoded = encoded;
}
void Encoder::register_to_callback()
{
call_back_encoder_ptr = &encoderNameSpace::_callback;
_encoders_ptr.push_back(this);
}
void Encoder::setup_encoder()
{
register_to_callback();
value = 0;
last_encoded = 0;
pinMode(pin_a, INPUT);
pinMode(pin_b, INPUT);
pullUpDnControl(pin_a, PUD_UP);
pullUpDnControl(pin_b, PUD_UP);
wiringPiISR(pin_a, INT_EDGE_BOTH, call_back_encoder_ptr);
wiringPiISR(pin_b, INT_EDGE_BOTH, call_back_encoder_ptr);
}
long Encoder::get_value()
{
return value;
}