-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathky040.cpp
More file actions
333 lines (278 loc) · 9.6 KB
/
ky040.cpp
File metadata and controls
333 lines (278 loc) · 9.6 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//
// ky040.cpp
//
// Circle - A C++ bare metal environment for Raspberry Pi
// Copyright (C) 2022 R. Stange <rsta2@o2online.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "ky040.h"
#include <assert.h>
static const unsigned SwitchDebounceDelayMillis = 50;
static const unsigned SwitchTickDelayMillis = 500;
// Encoder direction lookup table
// Indexed by 4-bit pattern: (oldCLK << 3) | (oldDT << 2) | (newCLK << 1) | (newDT << 0)
// Returns: -1 for counter-clockwise, 0 for invalid/no-change, +1 for clockwise
static const s8 s_EncoderDirection[16] =
{
0, // 0000: 00→00 no change
-1, // 0001: 00→01 counter-clockwise
1, // 0010: 00→10 clockwise
0, // 0011: 00→11 invalid (both pins changed)
1, // 0100: 01→00 clockwise
0, // 0101: 01→01 no change
0, // 0110: 01→10 invalid (both pins changed)
-1, // 0111: 01→11 counter-clockwise
-1, // 1000: 10→00 counter-clockwise
0, // 1001: 10→01 invalid (both pins changed)
0, // 1010: 10→10 no change
1, // 1011: 10→11 clockwise
0, // 1100: 11→00 invalid (both pins changed)
1, // 1101: 11→01 clockwise
-1, // 1110: 11→10 counter-clockwise
0 // 1111: 11→11 no change
};
CKY040::TSwitchState CKY040::s_NextSwitchState[SwitchStateUnknown][SwitchEventUnknown] =
{
// {SwitchEventDown, SwitchEventUp, SwitchEventTick}
{SwitchStateDown, SwitchStateStart, SwitchStateStart}, // SwitchStateStart
{SwitchStateDown, SwitchStateClick, SwitchStateHold}, // SwitchStateDown
{SwitchStateDown2, SwitchStateClick, SwitchStateStart}, // SwitchStateClick
{SwitchStateDown2, SwitchStateClick2, SwitchStateInvalid}, // SwitchStateDown2
{SwitchStateDown3, SwitchStateClick2, SwitchStateStart}, // SwitchStateClick2
{SwitchStateDown3, SwitchStateClick3, SwitchStateInvalid}, // SwitchStateDown3
{SwitchStateInvalid, SwitchStateClick3, SwitchStateStart}, // SwitchStateClick3
{SwitchStateHold, SwitchStateStart, SwitchStateHold}, // SwitchStateHold
{SwitchStateInvalid, SwitchStateStart, SwitchStateInvalid} // SwitchStateInvalid
};
CKY040::TEvent CKY040::s_SwitchOutput[SwitchStateUnknown][SwitchEventUnknown] =
{
// {SwitchEventDown, SwitchEventUp, SwitchEventTick}
{EventUnknown, EventUnknown, EventUnknown}, // SwitchStateStart
{EventUnknown, EventUnknown, EventSwitchHold}, // SwitchStateDown
{EventUnknown, EventUnknown, EventSwitchClick}, // SwitchStateClick
{EventUnknown, EventUnknown, EventUnknown}, // SwitchStateDown2
{EventUnknown, EventUnknown, EventSwitchDoubleClick}, // SwitchStateClick2
{EventUnknown, EventUnknown, EventUnknown}, // SwitchStateDown3
{EventUnknown, EventUnknown, EventSwitchTripleClick}, // SwitchStateClick3
{EventUnknown, EventUnknown, EventSwitchHold}, // SwitchStateHold
{EventUnknown, EventUnknown, EventUnknown} // SwitchStateInvalid
};
CKY040::CKY040 (unsigned nCLKPin, unsigned nDTPin, unsigned nSWPin,
CGPIOManager *pGPIOManager, unsigned nEncoderDetents)
: m_CLKPin (nCLKPin, GPIOModeInputPullUp, pGPIOManager),
m_DTPin (nDTPin, GPIOModeInputPullUp, pGPIOManager),
m_SWPin (nSWPin, GPIOModeInputPullUp, pGPIOManager),
m_bPollingMode (!pGPIOManager),
m_bInterruptConnected (FALSE),
m_pEventHandler (nullptr),
m_nEncoderDetents (nEncoderDetents),
m_nStepCounter (0),
m_nLastCLK (0),
m_nLastDT (0),
m_hDebounceTimer (0),
m_hTickTimer (0),
m_nLastSWLevel (HIGH),
m_bDebounceActive (FALSE),
m_SwitchState (SwitchStateStart),
m_nSwitchLastTicks (0)
{
// Read initial encoder pin states to prevent spurious step detection on first interrupt
m_nLastCLK = m_CLKPin.Read ();
m_nLastDT = m_DTPin.Read ();
}
CKY040::~CKY040 (void)
{
if (m_bInterruptConnected)
{
m_pEventHandler = nullptr;
m_CLKPin.DisableInterrupt2 ();
m_CLKPin.DisableInterrupt ();
m_CLKPin.DisconnectInterrupt ();
m_DTPin.DisableInterrupt2 ();
m_DTPin.DisableInterrupt ();
m_DTPin.DisconnectInterrupt ();
m_SWPin.DisableInterrupt2 ();
m_SWPin.DisableInterrupt ();
m_SWPin.DisconnectInterrupt ();
}
if (m_hDebounceTimer)
{
CTimer::Get ()->CancelKernelTimer (m_hDebounceTimer);
}
if (m_hTickTimer)
{
CTimer::Get ()->CancelKernelTimer (m_hTickTimer);
}
}
boolean CKY040::Initialize (void)
{
if (!m_bPollingMode)
{
assert (!m_bInterruptConnected);
m_bInterruptConnected = TRUE;
m_CLKPin.ConnectInterrupt (EncoderInterruptHandler, this);
m_DTPin.ConnectInterrupt (EncoderInterruptHandler, this);
m_SWPin.ConnectInterrupt (SwitchInterruptHandler, this);
m_CLKPin.EnableInterrupt (GPIOInterruptOnFallingEdge);
m_CLKPin.EnableInterrupt2 (GPIOInterruptOnRisingEdge);
m_DTPin.EnableInterrupt (GPIOInterruptOnFallingEdge);
m_DTPin.EnableInterrupt2 (GPIOInterruptOnRisingEdge);
m_SWPin.EnableInterrupt (GPIOInterruptOnFallingEdge);
m_SWPin.EnableInterrupt2 (GPIOInterruptOnRisingEdge);
}
return TRUE;
}
void CKY040::RegisterEventHandler (TEventHandler *pHandler, void *pParam)
{
assert (!m_pEventHandler);
m_pEventHandler = pHandler;
assert (m_pEventHandler);
m_pEventParam = pParam;
}
unsigned CKY040::GetHoldSeconds (void) const
{
return m_nHoldCounter / 2;
}
void CKY040::Update (void)
{
assert (m_bPollingMode);
EncoderInterruptHandler (this);
// handle switch
unsigned nTicks = CTimer::GetClockTicks ();
unsigned nSW = m_SWPin.Read ();
if (nSW != m_nLastSWLevel)
{
m_nLastSWLevel = nSW;
m_bDebounceActive = TRUE;
m_nDebounceLastTicks = CTimer::GetClockTicks ();
}
else
{
if ( m_bDebounceActive
&& nTicks - m_nDebounceLastTicks >= SwitchDebounceDelayMillis * (CLOCKHZ / 1000))
{
m_bDebounceActive = FALSE;
m_nSwitchLastTicks = nTicks;
if (m_pEventHandler)
{
(*m_pEventHandler) (nSW ? EventSwitchUp : EventSwitchDown,
m_pEventParam);
}
HandleSwitchEvent (nSW ? SwitchEventUp : SwitchEventDown);
}
if (nTicks - m_nSwitchLastTicks >= SwitchTickDelayMillis * (CLOCKHZ / 1000))
{
m_nSwitchLastTicks = nTicks;
HandleSwitchEvent (SwitchEventTick);
}
}
}
// generates the higher level switch events
void CKY040::HandleSwitchEvent (TSwitchEvent SwitchEvent)
{
assert (SwitchEvent < SwitchEventUnknown);
TEvent Event = s_SwitchOutput[m_SwitchState][SwitchEvent];
TSwitchState NextState = s_NextSwitchState[m_SwitchState][SwitchEvent];
if (NextState == SwitchStateHold)
{
if (m_SwitchState != SwitchStateHold)
{
m_nHoldCounter = 0;
}
m_nHoldCounter++;
}
m_SwitchState = NextState;
if ( Event != EventUnknown
&& (Event != EventSwitchHold || !(m_nHoldCounter & 1)) // emit hold event each second
&& m_pEventHandler)
{
(*m_pEventHandler) (Event, m_pEventParam);
}
}
void CKY040::EncoderInterruptHandler (void *pParam)
{
CKY040 *pThis = static_cast<CKY040 *> (pParam);
assert (pThis != 0);
unsigned nCLK = pThis->m_CLKPin.Read ();
unsigned nDT = pThis->m_DTPin.Read ();
assert (nCLK <= 1);
assert (nDT <= 1);
// Use lookup table to determine direction
// Build 4-bit index from old and new pin states
unsigned nIndex = (pThis->m_nLastCLK << 3) | (pThis->m_nLastDT << 2) | (nCLK << 1) | nDT;
s8 nDirection = s_EncoderDirection[nIndex];
// Update step counter if we have a valid transition
if (nDirection != 0)
{
pThis->m_nStepCounter += nDirection;
}
pThis->m_nLastCLK = nCLK; // Remember current CLK state for next interrupt
pThis->m_nLastDT = nDT; // Remember current DT state for next interrupt
// Fire event when step count reaches threshold
TEvent Event = EventUnknown;
if (pThis->m_nStepCounter >= (s8)pThis->m_nEncoderDetents)
{
Event = EventClockwise;
pThis->m_nStepCounter = 0;
}
else if (pThis->m_nStepCounter <= -(s8)pThis->m_nEncoderDetents)
{
Event = EventCounterclockwise;
pThis->m_nStepCounter = 0;
}
if (Event != EventUnknown && pThis->m_pEventHandler)
{
(*pThis->m_pEventHandler) (Event, pThis->m_pEventParam);
}
}
void CKY040::SwitchInterruptHandler (void *pParam)
{
CKY040 *pThis = static_cast<CKY040 *> (pParam);
assert (pThis != 0);
if (pThis->m_hDebounceTimer)
{
CTimer::Get ()->CancelKernelTimer (pThis->m_hDebounceTimer);
}
pThis->m_hDebounceTimer =
CTimer::Get ()->StartKernelTimer (MSEC2HZ (SwitchDebounceDelayMillis),
SwitchDebounceHandler, pThis, 0);
}
void CKY040::SwitchDebounceHandler (TKernelTimerHandle hTimer, void *pParam, void *pContext)
{
CKY040 *pThis = static_cast<CKY040 *> (pParam);
assert (pThis != 0);
pThis->m_hDebounceTimer = 0;
if (pThis->m_hTickTimer)
{
CTimer::Get ()->CancelKernelTimer (pThis->m_hTickTimer);
}
pThis->m_hTickTimer = CTimer::Get ()->StartKernelTimer (MSEC2HZ (SwitchTickDelayMillis),
SwitchTickHandler, pThis, 0);
unsigned nSW = pThis->m_SWPin.Read ();
if (pThis->m_pEventHandler)
{
(*pThis->m_pEventHandler) (nSW ? EventSwitchUp : EventSwitchDown,
pThis->m_pEventParam);
}
pThis->HandleSwitchEvent (nSW ? SwitchEventUp : SwitchEventDown);
}
void CKY040::SwitchTickHandler (TKernelTimerHandle hTimer, void *pParam, void *pContext)
{
CKY040 *pThis = static_cast<CKY040 *> (pParam);
assert (pThis != 0);
pThis->m_hTickTimer = CTimer::Get ()->StartKernelTimer (MSEC2HZ (SwitchTickDelayMillis),
SwitchTickHandler, pThis, 0);
pThis->HandleSwitchEvent (SwitchEventTick);
}