-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.c
73 lines (64 loc) · 1.72 KB
/
key.c
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
/**
******************************************************************************
* File Name : key.c
* Description : This file contains some thoughts about the key event.
******************************************************************************
* @attention
*
* Copyright (c) 2019 PQDUSA Inc.
* All rights reserved.
* Author: yaoyu
* Date: 2019/09/04
******************************************************************************
**/
#include "key.h"
#include "usart.h"
unsigned char trg = 0x00;
unsigned char cnt = 0x00; //check is key press continued
unsigned char rls = 0x00;
unsigned char cnt_plus = 0x00;
unsigned char lock = 0x00;//lock for longpress
void keyScan(){
/**
* TouchDown trg 1 cnt 1 rls 0
* Touch trg 0 cnt 1 rls 0
* TouchUp trg 0 cnt 0 rls 1
**/
#ifdef ACTIVE_HIGH
unsigned char read = KEY ^ 0x01;//pull up resistor
#else
unsigned char read = KEY;
#endif
// unsigned char read = KEY ^ 0x01;//pull down resistor
trg = read & ( read ^ cnt);
rls = ~read & ( read ^ cnt);
cnt = read;//old data
// log_printf("============= keyScan: read %d\n", read);
// log_printf("============= keyScan: trg %d cnt %d rls %d\n", trg, cnt, rls);
if(trg){
//clear status
lock = 0x00;
cnt_plus = 0x00;
}
if(rls & ~lock){
onClick();
//consume event
lock = 0x00;
cnt_plus = 0x00;
}
if(cnt & ~lock){//not yet long press, add counter
cnt_plus++;
if(cnt_plus >= LONG_PRESS_INTERVAL / DEBOUNCING){
onLongPress();
//consume event
lock = 0x01;
cnt_plus = 0x00;
}
}
}
__weak void onClick(){
/** Inplement this in your code **/
}
__weak void onLongPress(){
/** Inplement this in your code **/
}