forked from 0xPIT/encoder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClickEncoder.cpp
240 lines (211 loc) · 6.17 KB
/
ClickEncoder.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// ----------------------------------------------------------------------------
// Rotary Encoder Driver with Acceleration
// Supports Click, DoubleClick, Held, LongPressRepeat
//
// Refactored, logic upgraded and feature added (LongPressRepeat) by Schallbert 2021
// ----------------------------------------------------------------------------
#include "ClickEncoder.h"
// ----------------------------------------------------------------------------
// Encoders typically have 3 pins: A, B, C (GND)
// Most of them have notches and register 4 steps (ticks) per notch.
// If mixed up A and B, encoder will turn "backwards".
Encoder::Encoder(uint8_t A,
uint8_t B,
uint8_t stepsPerNotch,
bool active) : pinA(A),
pinB(B),
stepsPerNotch(stepsPerNotch),
pinActiveState(active)
{
uint8_t configType = (pinActiveState == LOW) ? INPUT_PULLUP : INPUT;
pinMode(pinA, configType);
pinMode(pinB, configType);
}
// Button pin BTN and active state to be defined.
Button::Button(uint8_t BTN,
bool active) : pinBTN(BTN),
pinActiveState(active)
{
uint8_t configType = (pinActiveState == LOW) ? INPUT_PULLUP : INPUT;
pinMode(pinBTN, configType);
}
/// ClickEncoders typically have 5 pins: A, B, C (enc GND), BTN, GND
ClickEncoder::ClickEncoder(
uint8_t A,
uint8_t B,
uint8_t BTN,
uint8_t stepsPerNotch,
bool active)
{
enc = new Encoder(A, B, stepsPerNotch, active);
btn = new Button(BTN, active);
}
ClickEncoder::~ClickEncoder()
{
delete enc;
delete btn;
enc = nullptr;
btn = nullptr;
}
// ----------------------------------------------------------------------------
// call this every 1 millisecond via timer ISR
void ClickEncoder::service(void)
{
enc->service();
btn->service();
}
// call this every 1 millisecond via timer ISR
void Encoder::service()
{
handleEncoder();
}
// call this every 1 millisecond via timer ISR
void Button::service()
{
++lastGetButtonCount;
handleButton();
}
// ----------------------------------------------------------------------------
void Encoder::handleEncoder()
{
uint8_t encoderRead = getBitCode();
// bit0 set = status changed, bit1 set = "overflow 3" where it goes 0->3 or 3->0
uint8_t rawMovement = encoderRead - lastEncoderRead;
lastEncoderRead = encoderRead;
// This is the uint->int magic, converts raw to: -1 counterclockwise, 0 no turn, 1 clockwise
int8_t signedMovement = ((rawMovement & 1) - (rawMovement & 2));
encoderAccumulate += signedMovement;
encoderAccumulate += handleAcceleration(signedMovement);
}
uint8_t Encoder::getBitCode()
{
// GrayCode convert
// !A && !B --> 0
// !A && B --> 1
// A && B --> 2
// A && !B --> 3
uint8_t currentEncoderRead = digitalRead(pinA);
currentEncoderRead |= (currentEncoderRead << 1);
// invert result's 0th bit if set
currentEncoderRead ^= digitalRead(pinB);
return currentEncoderRead;
}
int8_t Encoder::handleAcceleration(int8_t direction)
{
if (lastMovedCount < ENC_ACCEL_START)
{
++lastMovedCount;
}
if (direction == 0 || !accelerationEnabled || (encoderAccumulate % stepsPerNotch))
{
return 0;
}
// only when moved, only with enabled acceleration, only accelerate for "full notches", no movements in between
int16_t acceleration = ((ENC_ACCEL_START / ENC_ACCEL_SLOPE) - (lastMovedCount / ENC_ACCEL_SLOPE));
lastMovedCount = 0;
if (direction > 0)
{
return acceleration;
}
else
{
return -acceleration;
}
}
// ----------------------------------------------------------------------------
// returns number of notches that the encoder was turned since the last poll
// takes acceleration into account if configured
int16_t Encoder::getIncrement()
{
int16_t accu = getAccumulate();
int16_t encoderIncrements = accu - lastEncoderAccumulate;
lastEncoderAccumulate = accu;
return (encoderIncrements);
}
// returns sum of notches that the encoder was turned since startup
// takes acceleration into account if configured
int16_t Encoder::getAccumulate()
{
return (encoderAccumulate / stepsPerNotch);
}
// ----------------------------------------------------------------------------
void Button::handleButton()
{
if (lastGetButtonCount < ENC_BUTTONINTERVAL)
{
return;
}
lastGetButtonCount = 0;
if (digitalRead(pinBTN) == pinActiveState)
{
handleButtonPressed();
}
else
{
handleButtonReleased();
}
if (doubleClickTicks > 0)
{
--doubleClickTicks;
}
}
void Button::handleButtonPressed()
{
buttonState = Closed;
++keyDownTicks;
if (keyDownTicks >= (ENC_HOLDTIME / ENC_BUTTONINTERVAL))
{
buttonState = Held;
if (!longPressRepeatEnabled)
{
return;
}
// Blip out LongPressRepeat once per interval
if (keyDownTicks > ((ENC_LONGPRESSREPEATINTERVAL + ENC_HOLDTIME) / ENC_BUTTONINTERVAL))
{
buttonState = LongPressRepeat;
}
}
}
void Button::handleButtonReleased()
{
keyDownTicks = 0;
if (buttonState == Held)
{
buttonState = Released;
}
else if (buttonState == Closed)
{
buttonState = Clicked;
if (!doubleClickEnabled)
{
return;
}
if (doubleClickTicks == 0)
{
// reset counter and wait for another click
doubleClickTicks = (ENC_DOUBLECLICKTIME / ENC_BUTTONINTERVAL);
}
else
{
//doubleclick active and not elapsed!
buttonState = DoubleClicked;
doubleClickTicks = 0;
}
}
}
Button::eButtonStates Button::getButton(void)
{
volatile Button::eButtonStates result{buttonState};
if (result == LongPressRepeat)
{
// Reset to "Held"
keyDownTicks = (ENC_HOLDTIME / ENC_BUTTONINTERVAL);
}
// reset after readout. Conditional to neither miss nor repeat DoubleClicks or Helds
if (buttonState != Closed)
{
buttonState = Open;
}
return result;
}