forked from ZheWana/CommonKey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comKey.c
197 lines (172 loc) · 4.61 KB
/
comKey.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
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
/**
* @file comKey.c
* @author ZheWana
* @date 2022/3/6 006
*/
#include "comKey.h"
comkey_t key0, key1;
__attribute__((weak)) void ComKey_SyncValue(comkey_t* key)
{
// if your key was pressed,"key->val" should be 1.
if (key == &key0) {
key->val = !HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);
}
if (key == &key1) {
key->val = !HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1);
}
// IMPORTANT!!!DO NOT MODIFIED!!!
key->preVal = key->val;
}
__attribute__((weak)) void ComKey_FirstLongTriggerCallback(comkey_t* key)
{
if (key == &key0)
printf("key0: ");
if (key == &key1) {
printf("key1: ");
}
printf("was triggerred!\n", key->holdTime);
}
__attribute__((weak)) void ComKey_LongHoldCallback(comkey_t* key)
{
if (key == &key0)
printf("key0: ");
if (key == &key1) {
printf("key1: ");
}
printf("hold %ldms\n", key->holdTime);
}
__attribute__((weak)) void ComKey_HoldTriggerCallback(comkey_t* key)
{
if (key == &key0)
printf("key0: HoldTrigger\n");
if (key == &key1) {
printf("key1: HoldTrigger\n");
}
}
__attribute__((weak)) void ComKey_MultipleClickCallback(comkey_t* key)
{
if (key == &key0)
printf("key0: ");
if (key == &key1) {
printf("key1: ");
}
printf("click: %d\n", key->clickCnt);
}
__attribute__((weak)) void ComKey_KeyReleaseCallback(comkey_t* key)
{
if (key == &key0) {
printf("key0: ");
}
if (key == &key1) {
printf("key1: ");
}
if (key->state == LongHold) {
printf("LongHold");
} else if (key->state == MultiClick) {
printf("Click");
}
printf(" Release!\n");
}
__attribute__((weak)) void ComKey_KeyPressCallback(comkey_t* key)
{
if (key == &key0) {
printf("key0: ");
}
if (key == &key1) {
printf("key1: ");
}
printf("keyPress!\n");
}
/*************** DO NOT MODIFY THE FOLLOWING CODE *******************/
static pcomkey_t head = NULL, tail = NULL;
static uint8_t ITPeriod = 1;
void ComKey_Init(comkey_t* key, int pollingPeriod)
{
ITPeriod = pollingPeriod;
key->state = Release;
key->next = NULL;
if (head == NULL) {
head = key;
tail = key;
} else {
tail->next = key;
tail = tail->next;
}
}
void ComKey_Handler()
{
for (pcomkey_t key = head; key != NULL; key = key->next) {
//键值同步
ComKey_SyncValue(key);
//按下计时
if (!key->val) {
if (key->state == LongHold) {
key->holdTime = key->preTimer;
}
key->preTimer = 0;
}
if (key->preVal & key->val) {
key->preTimer += ITPeriod;
}
//间隔计时
if (key->state == MultiClick) {
key->intervalTimer += ITPeriod;
} else {
key->intervalTimer = 0;
}
//事件生成
switch (key->state) {
case Release:
key->clickCnt = 0;
if (key->val) {
key->state = PrePress;
}
break;
case PrePress:
if (!key->val) {
key->state = Release;
} else if (key->preTimer > COMKEY_ClickThreshold) {
key->state = Prelong;
ComKey_KeyPressCallback(key);
}
break;
case Prelong:
if (!key->val) {
key->state = MultiClick;
key->clickCnt++;
} else if (key->preTimer > COMKEY_HoldThreshold) {
key->state = LongHold;
key->triggerTimer = COMKEY_HoldTriggerThreshold;
ComKey_FirstLongTriggerCallback(key);
}
break;
case LongHold: {
if (key->triggerTimer > 0)
key->triggerTimer -= ITPeriod;
else {
key->triggerTimer = COMKEY_HoldTriggerThreshold;
ComKey_HoldTriggerCallback(key);
}
if (!key->val) {
ComKey_LongHoldCallback(key);
ComKey_KeyReleaseCallback(key);
key->state = Release;
}
} break;
case MultiClick:
if (key->intervalTimer > COMKEY_IntervalVal) {
ComKey_MultipleClickCallback(key);
ComKey_KeyReleaseCallback(key);
key->state = Release;
} else if (key->preTimer > COMKEY_ClickThreshold) {
key->state = Prelong;
}
break;
default:
#ifdef USE_HAL_DRIVER
Error_Handler();
#endif // USE_HAL_DRIVER
break;
}
}
}