-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotaryEncoder.cpp
137 lines (119 loc) · 2.68 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// rotaryEncoder.cpp
//
#include "rotaryEncoder.h"
using namespace std;
int _guardrail = 100;
int _maxTime = 50;
int _last1 = 0;
int _last2 = 0;
int _counter1 = 0;
int _counter2 = 0;
int _pinClk1 = 0;
int _pinClk2 = 0;
static void callback1()
{
if (millis() - _last1 > _guardrail)
{
_last1 = millis();
int clk = digitalRead(_pinClk1);
int start = millis();
int flag = 0;
while (millis() - start < _maxTime && !clk)
{
clk = digitalRead(_pinClk1);
flag = 1;
}
if (clk && flag)
{
_counter1--;
cout << "ROTATION 1, " << _counter1 << endl;
}
else
{
_counter1++;
cout << "ROTATION 1, " << _counter1 << endl;
}
}
}
static void callback2()
{
if (millis() - _last2 > _guardrail)
{
_last2 = millis();
int clk = digitalRead(_pinClk2);
int start = millis();
int flag = 0;
while (millis() - start < _maxTime && !clk)
{
clk = digitalRead(_pinClk2);
flag = 1;
}
if (clk && flag == 1)
{
_counter2--;
cout << "ROTATION 2, " << _counter2 << endl;
}
else if (clk && flag == 2)
{
_counter2++;
cout << "ROTATION 2, " << _counter2 << endl;
}
}
}
static void callbackClic()
{
cout << "clic" << endl;
}
RotaryEncoder::RotaryEncoder(int dataPin, int clkPin, int pushPin, std::string name)
{
static bool once = false;
if (!once)
{
wiringPiSetupGpio();
once = true;
}
dataPin_ = physPinToGpio(dataPin);
clkPin_ = physPinToGpio(clkPin);
pushPin_ = physPinToGpio(pushPin);
name_ = name;
setup();
}
void RotaryEncoder::setup()
{
pinMode(dataPin_, INPUT);
pinMode(clkPin_, INPUT);
pinMode(pushPin_, INPUT);
if (name_ == "1")
{
int err = 0;
err = wiringPiISR(dataPin_, INT_EDGE_RISING, callback1);
cout << "err CB1 = " << err << endl;
_pinClk1 = clkPin_;
}
else if (name_ == "2")
{
int err = 0;
err = wiringPiISR(dataPin_, INT_EDGE_RISING, callback2);
cout << "err CB2 = " << err << endl;
err = wiringPiISR(pushPin_, INT_EDGE_RISING, callbackClic);
cout << "err CB2 = " << err << endl;
_pinClk2 = clkPin_;
}
}
int main()
{
wiringPiSetupGpio();
RotaryEncoder A = RotaryEncoder(13, 11, 15, "1");
RotaryEncoder B = RotaryEncoder(22, 31, 7, "2");
cout << "You can start" << endl;
int k = 0;
while (1)
{
k++;
delay(50);
if (k > 1000)
{
}
}
}