-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
RotaryEncoder.cpp
164 lines (133 loc) · 2.84 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Rotary Encoder Lib for Arduino
by Watterott electronic (www.watterott.com)
reading routine by Peter Dannegger
http://www.mikrocontroller.net/articles/Drehgeber
*/
#include <inttypes.h>
#if (defined(__AVR__) || defined(ARDUINO_ARCH_AVR))
# include <avr/io.h>
#endif
#if ARDUINO >= 100
# include "Arduino.h"
#else
# include "WProgram.h"
#endif
#include "digitalWriteFast.h"
#include "RotaryEncoder.h"
#define STEPS 4 //1, 2 or 4 steps
#define RE_SW_PIN 5
#define RE_PHA_PIN 6
#define RE_PHB_PIN 7
#define RE_SW_READ() digitalReadFast(RE_SW_PIN)
#define RE_PHA_READ() digitalReadFast(RE_PHA_PIN)
#define RE_PHB_READ() digitalReadFast(RE_PHB_PIN)
//-------------------- Constructor --------------------
RotaryEncoder::RotaryEncoder(void)
{
return;
}
//-------------------- Public --------------------
void RotaryEncoder::init(void)
{
//init pins
pinMode(RE_SW_PIN, INPUT);
digitalWrite(RE_SW_PIN, HIGH); //pull-up
pinMode(RE_PHA_PIN, INPUT);
digitalWrite(RE_PHA_PIN, HIGH); //pull-up
pinMode(RE_PHB_PIN, INPUT);
digitalWrite(RE_PHB_PIN, HIGH); //pull-up
//init global vars
re_sw = 0;
re_delta = 0;
re_last = 0;
if(RE_PHA_READ())
{
re_last = 3;
}
if(RE_PHB_READ())
{
re_last ^= 1;
}
return;
}
void RotaryEncoder::service(void) //called every 1ms
{
int_least8_t re_new, re_diff;
static int_least16_t re_pressed=0;
re_new = 0;
if(RE_PHA_READ())
{
re_new = 3;
}
if(RE_PHB_READ())
{
re_new ^= 1; //convert gray to binary
}
re_diff = re_last-re_new; //difference last - new
if(re_diff & 1) //bit 0 = value (1)
{
re_last = re_new; //store new as next last
re_delta += (re_diff&2)-1; //bit 1 = direction (+/-)
}
if(RE_SW_READ())
{
if(re_pressed > 800) //800 ms
{
if(re_sw == 0)
{
re_sw = SW_PRESSEDLONG;
}
}
else if(re_pressed > 80) //80 ms
{
if(re_sw == 0)
{
re_sw = SW_PRESSED;
}
}
re_pressed = 0;
}
else
{
re_pressed++;
}
return;
}
int_least8_t RotaryEncoder::step(void)
{
int_least8_t val;
cli();
val = re_delta;
#if STEPS == 1
re_delta = 0; //1step:0 / 2step:val&1 / 4step:val&3
#elif STEPS == 2
re_delta = val&1; //1step:0 / 2step:val&1 / 4step:val&3
#elif STEPS == 4
re_delta = val&3; //1step:0 / 2step:val&1 / 4step:val&3
#endif
sei();
#if STEPS == 1
val = val>>0; //1step:val / 2step:val>>1 / 4step:val>>2
#elif STEPS == 2
val = val>>1; //1step:val / 2step:val>>1 / 4step:val>>2
#elif STEPS == 4
val = val>>2; //1step:val / 2step:val>>1 / 4step:val>>2
#endif
if(val < 0)
{
return +1;
}
else if(val > 0)
{
return -1;
}
return 0;
}
int_least8_t RotaryEncoder::sw(void)
{
int_least8_t val;
val = re_sw;
re_sw = 0;
return val;
}