-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuKey.c
More file actions
601 lines (499 loc) · 11.3 KB
/
uKey.c
File metadata and controls
601 lines (499 loc) · 11.3 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/** ,-... ..-.
* ./:::::\ /:::::ヽ
* /::::::::::::;ゝ--──-- .._/:::::::ヽ
* /,.-‐''"′ \:::::::|
* / ヽ.::|
* / ● ヽ|
* l ... ● l
* .| (_人__丿 ... |
* l l
* ` . /
* `. .__ ./
* /`'''.‐‐──‐‐‐┬---
* File : uKey.c
* This file is part of aRTOS
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Log :
* 2017/6/23 [FlandreUNX] 源码构建.
* 基于线程的按键扫描,使用回调隔离操作
* 支持最大32个普通按键或31普通+1Shift按键.
* 支持每个按键的短按连发或长按功能,包括Shift+按键的短按连发/长按功能.
* 支持每个按键的触发电平类型.
* 2017/7/7 [FlandreUNX] 添加虚拟按键.
* 增加对多个按键同时按下时,触发一个新的按键值.
*
*/
#include "./uKey.h"
/**
* @addtogroup hal
*/
/*@{*/
#include "stm32f0xx.h" // Device header
/*@}*/
/**
* @addtogroup os include
*/
/*@{*/
#if KEY_OS_TYPE == 1
#include "cmsis_os.h" // ARM::CMSIS:RTOS:Keil RTX
#endif
/*@}*/
/**
* @addtogroup Key var
*/
/*@{*/
/**
* 前次按键扫描值
* @note none
*/
static uKey_Bitmap_t preScanKey = 0;
/**
* 前次按键读取值
* @note none
*/
static uKey_Bitmap_t preReadKey = 0;
/**
* 按键掩码
* @note none
*/
static uKey_Bitmap_t keyMask = 0;
#if USE_SHIFT_KEY == 1
/**
* shift按键记录
* @note none
*/
static uKey_Bitmap_t keyShift = 0;
/**
* shift按键对象
* @note none
*/
static uKeyDef_t key_ShiftObj = {
.port = 0,
.value = 0x01
};
#endif
static uKey_Bitmap_t keyPressTmr = KEY_PRESS_DLY;
/**
* 长按键编码掩码
* @note none
*/
static uKey_Bitmap_t key_LongMask;
/**
* 按键对象集合
* @note none
*/
static struct __list_t keysList;
/*@}*/
/**
* @addtogroup FIFO Buffer var
*/
/*@{*/
/**
* 环形buf
* @note none
*/
static uKey_Bitmap_t keyBuf[KEY_BUFFER_SIZE];
/**
* buf入指针
* @note none
*/
static uKey_Bitmap_t keyBufInIx = 0;
/**
* buf出指针
* @note none
*/
static uKey_Bitmap_t keyBufOutIx = 0;
/**
* buf中按键数量
* @note none
*/
static uKey_Bitmap_t keyNRead = 0;
/*@}*/
/**
* @addtogroup simplify list Functions
*/
/*@{*/
/**
* 获取当前list_head链表节点所在的宿主结构项
*
* @param ptr 宿主结构体下的osList_t*
* @param type 宿主类型
* @param name 宿主结构类型定义中osList_t成员名
*
* @retval 宿主结构体指针
*/
#define list_Entry(ptr, type, name) \
(type*)((uint8_t*)(ptr) - ((uint32_t) &((type *)0)->name))
/**
* 遍历链表中的所有list_head节点
*
* @param pos 位置
* @param head 节点头
*
* @retval none
*/
#define list_ForEach(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* 初始化节点
*
* @param head 节点
*
* @retval none
*/
static __inline void list_HeadInit(struct __list_t* head) {
head->next = head;
head->previous = head;
}
/**
* 将newNode添加到list节点尾部
*
* @param list 原节点
* @param newNode 新节点
*
* @retval none
*/
static __inline void list_AddTail(struct __list_t* list, struct __list_t* newNode) {
list->previous->next = newNode;
newNode->previous = list->previous;
list->previous = newNode;
newNode->next = list;
}
/**
* 将newNode添加到list节点头部
*
* @param list 原节点
* @param newNode 新节点
*
* @retval none
*/
static __inline void list_Add(struct __list_t* list, struct __list_t* newNode) {
list->next->previous = newNode;
newNode->next = list->next;
list->next = newNode;
newNode->previous = list;
}
/*@}*/
/**
* @addtogroup Key Functions
*/
/*@{*/
/**
* 读取一次GPIO状态
*
* @param none
*
* @return uint8_t key扫描结果
*/
static __inline uKey_Bitmap_t GPIORead(void) {
uKey_Bitmap_t keyScanCode = 0;
uKeyDef_t *obj;
struct __list_t *index;
#if USE_SHIFT_KEY == 1
keyScanCode |= ((((GPIO_TypeDef *)key_ShiftObj.port)->IDR) & key_ShiftObj.gpio) ?
(key_ShiftObj.level ? key_ShiftObj.value : 0) :
(key_ShiftObj.level ? 0 : key_ShiftObj.value);
#endif
list_ForEach(index, &keysList) {
obj = list_Entry(index, uKeyDef_t, list);
if (obj->type & KEY_TYPE_MULTI) {
if (keyScanCode == obj->value2) {
keyScanCode = obj->value;
}
}
else {
keyScanCode |= ((((GPIO_TypeDef *)obj->port)->IDR) & obj->gpio) ?
(obj->level ? obj->value : 0) :
(obj->level ? 0 : obj->value);
}
}
return keyScanCode;
}
/**
* 读取缓冲区中按键信息
*
* @param none
*
* @return uKey_Bitmap_t key
*/
static uKey_Bitmap_t keyBufferOut(void) {
uKey_Bitmap_t code = 0;
if (keyNRead > 0) {
/*-1表示取走一个按键值*/
keyNRead--;
/*从buf中取走一个扫描码*/
code = keyBuf[keyBufOutIx];
keyBufOutIx++;
/*buf满则指针循环回到起点*/
if (keyBufOutIx >= KEY_BUFFER_SIZE) {
keyBufOutIx = 0;
}
/*返回按键扫描值*/
return (code);
}
else {
/*没有按键,则返回0*/
return 0;
}
}
/**
* 写入一个按键信息到缓冲区
*
* @param none
*
* @return none
*/
static void keyBufferIn(uKey_Bitmap_t code) {
/*buf满则放弃最早的一个按键值*/
if (keyNRead >= KEY_BUFFER_SIZE) {
keyBufferOut();
}
keyNRead++;
/*输入按键扫描码*/
keyBuf[keyBufInIx++] = code;
/*buf满则指针循环回到起点*/
if (keyBufInIx >= KEY_BUFFER_SIZE) {
keyBufInIx = 0;
}
}
/**
* 周期扫描按键
*
* @param none
*
* @return none
*/
static void uKey_Scan(void) {
/*当前按键值扫描值*/
uKey_Bitmap_t nowScanKey = 0;
/*当前按键值*/
uKey_Bitmap_t nowReadKey = 0;
/*按键按下*/
//uKey_Bitmap_t keyPressDown = 0;
/*按键释放*/
uKey_Bitmap_t keyRelease = 0;
nowScanKey = GPIORead();
/*电平触发 | 采样保持(即消抖)*/
nowReadKey = (preScanKey & nowScanKey) | (preReadKey & (preScanKey ^ nowScanKey));
/*上升沿触发*/
//KeyPressDown = NowReadKey & (NowReadKey ^ PreReadKey);
/*下降沿触发*/
keyRelease = (preReadKey & (nowReadKey ^ preReadKey));
/*用电平触发做长按键的有效判据*/
if (nowReadKey == preReadKey && nowReadKey) {
keyPressTmr--;
/*长按判断周期到,保存相应长按键值*/
if (!keyPressTmr) {
/*长按键-连发模式*/
if (nowReadKey & ~(key_LongMask)) {
#if USE_SHIFT_KEY == 1
keyBufferIn(nowReadKey | keyShift);
#else
keyBufferIn(nowReadKey);
#endif
}
/*长按键-反码模式*/
else if (nowReadKey & (key_LongMask) & ~keyMask) {
#if USE_SHIFT_KEY == 1
keyBufferIn(~(nowReadKey | keyShift));
#else
keyBufferIn(~nowReadKey);
#endif
}
/*重置连按周期,准备获取下1个长按键*/
keyPressTmr = KEY_PRESS_TMR;
keyMask = nowReadKey;
}
}
else {
/*按键变化,重置按键判断周期*/
keyPressTmr = KEY_PRESS_DLY;
}
/*短按键判断*/
if (keyRelease) {
if (keyRelease & (~keyMask)) {
/*shift按键码(边缘触发)*/
#if USE_SHIFT_KEY == 1
keyShift ^= (keyRelease & (key_ShiftObj.keyValue));
keyBufferIn(keyRelease | keyShift);
#else
keyBufferIn(keyRelease);
#endif
}
else {
keyMask = 0;
}
}
preScanKey = nowScanKey;
preReadKey = nowReadKey;
}
/**
* 获取按键
*
* @param none
*
* @return uKey_Bitmap_t 按键bitmap
*/
static __inline uKey_Bitmap_t uKey_Get(void) {
return keyBufferOut();
}
/**
* 判断是否有按键按下
*
* @param none
*
* @return uint8_t 返回结果
*/
static __inline uint8_t uKey_IsHit(void) {
return keyNRead > 0 ? 1 : 0;
}
/*@}*/
/**
* @addtogroup Key Thread def
*/
/*@{*/
#if KEY_OS_TYPE == 1
void thread_uKey(const void *arg) {
for (;;) {
osDelay(20);
uKey_Scan();
if (!uKey_IsHit()) {
continue;
}
uKey_Bitmap_t keyGet;
keyGet = uKey_Get();
uKeyDef_t *obj = 0;
struct __list_t *index = 0;
uint8_t keyPressType = KEY_IS_NONE;
list_ForEach(index, &keysList) {
obj = list_Entry(index, uKeyDef_t, list);
keyPressType = uKey_GetPressType(obj, keyGet);
if (!keyPressType) {
continue;
}
if (obj->callback) {
obj->callback(obj, keyPressType);
}
}
}
}
osThreadDef(thread_uKey, osPriorityBelowNormal , 1, 0);
#endif
/*@}*/
/**
* @addtogroup uKey user functions
*/
/*@{*/
/**
* 按键模块启动
*
* @param none
*
* @return none
*/
void uKey_Startup(void) {
#if USE_SHIFT_KEY == 1
/*检查shift是否初始化*/
if (!key_ShiftObj.port) {
for (;;);
}
#endif
list_HeadInit(&keysList);
#if KEY_OS_TYPE == 1
osThreadCreate(osThread(thread_uKey), NULL);
#endif
}
#if USE_SHIFT_KEY == 1
/**
* shift按键初始化
*
* @param port
* @param gpio
*
* @return none
*/
void uKey_ShiftKeySet(uint32_t port, uint32_t gpio, uint8_t level) {
key_ShiftObj.port = port;
key_ShiftObj.gpio = gpio;
key_ShiftObj.level = level;
}
#endif
/**
* 按键对象插入
*
* @param obj 按键对象指针
*
* @return none
*/
void uKey_ObjInsert(uKeyDef_t *obj) {
list_HeadInit(&obj->list);
if (obj->type & KEY_TYPE_LONG) {
key_LongMask |= obj->value;
obj->valueLong = ~obj->value;
}
if (obj->type & KEY_TYPE_MULTI) {
list_AddTail(&keysList, &obj->list);
}
else {
list_Add(&keysList, &obj->list);
}
}
/**
* 判断按键是否输出类型(短/长按/shift)
*
* @param obj 按键对象指针
*
* @return 类型
*/
uint8_t uKey_GetPressType(uKeyDef_t *obj, uKey_Bitmap_t value) {
#if USE_SHIFT_KEY == 1
/*短按*/
if (value == obj->value) {
return KEY_IS_SP;
}
/*长按*/
else if (value == obj->valueLong)) {
return KEY_IS_LP;
}
/*shift + 短按*/
else if (value == (obj->vaule | key_ShiftObj.vaule)) {
return KEY_IS_SSP;
}
/*shift + 长按 */
else if (value == (~(obj->vaule | key_ShiftObj.vaule))) {
return KEY_IS_SLP;
}
/*无类型*/
else {
return KEY_IS_NONE;
}
#else
/*短按*/
if (value == obj->value) {
return KEY_IS_SP;
}
/*长按*/
else if (value == obj->valueLong) {
return KEY_IS_LP;
}
/*无类型*/
else {
return KEY_IS_NONE;
}
#endif
}
/*@}*/