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
#include "Key.h"
/*------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/*-----按键服务函数调用周期。单位ms--------*/
#define KEY_CALL_CYCLE 10U
/*-----------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/
typedef struct
{
uint16_t u16KeyShortPressTime; /*¶Ì°´¼ì²âʱ¼ä*/
uint16_t u16KeyLongPressTime; /*³¤°´¼ì²âʱ¼ä*/
uint16_t u16KeySuperLongPressTime; /*³¬³¤°´¼ì²âʱ¼ä*/
uint16_t u16KeyLoosenTime; /*ËÉ¿ª¼ì²âʱ¼ä*/
uint16_t u16KeyValidOFFTime; /*ÓÐЧOFF¼ì²âʱ¼ä*/
uint16_t u16KeyValidONTime; /*ÓÐЧON¼ì²âʱ¼ä*/
} Key_Parameter_st_t;
typedef struct
{
Key_Real_Status_en_t enKeyTypeLast;
uint16_t u16KeyInvalidTimeCount;
uint16_t u16KeyValidTimeCount;
uint16_t u16KeyFirstIGNTimeRecord;
Key_IGN_en_t enKeyIGNFirstStatus;
Key_IGN_en_t enKeyIGNStatusLast;
uint8_t u8KeyIGNChangeFlag;
uint16_t u16KeyIGNTimeCount;
} Key_Ctr_st_t;
/*------------------------------------------------------------------------------------------*/
#pragma ghs section bss = ".myNonInitArea"
static Key_Parameter_st_t stKeyParameter;
#pragma ghs section bss = default
static Key_Ctr_st_t stKeyCtrVariate[KEY_NUM_MAX];
//static Key_IGN_en_t enKeyIGNFirstStatus = KEY_IG_INVALID;
//static Key_IGN_en_t enKeyIGNStatusLast = KEY_IG_INVALID;
//static uint8_t u8KeyIGNChangeFlag = 0U;
//static uint16_t u16KeyIGNTimeCount;
/*-----------------------------------------------------------------------------------------*/
static void Key_Process(Key_Num_en_t enKeyIndex);
void Key_Service(void);
void Key_Init(void);
Key_Set_en_t Key_Parameter_Set_Short_Press_Time(uint16_t u16Time);
Key_Set_en_t Key_Parameter_Set_Long_Press_Time(uint16_t u16Time);
Key_Set_en_t Key_Parameter_Set_Super_Long_Press_Time(uint16_t u16Time);
Key_Set_en_t Key_Parameter_Set_OFF_Press_Time(uint16_t u16Time);
Key_Set_en_t Key_Parameter_Set_ON_Press_Time(uint16_t u16Time);
/*-----------------------------------------------------*/
static void Key_Process(Key_Num_en_t enKeyIndex)
{
if (enKeyIndex < KEY_NUM_MAX)
{
/*Key_Real_Status_en_t enKeyRealType = (*stKeyAttribute[enKeyIndex].penKeyRealStatus);*/
Key_Real_Status_en_t enKeyRealType = KEY_CAN_NONE;
Key_IGN_en_t enKeyIGNRealStatus = Key_Get_IGN_Status();
if (stKeyAttribute[enKeyIndex].pfnKeyReadStatusCallBack != 0U)
{
enKeyRealType = stKeyAttribute[enKeyIndex].pfnKeyReadStatusCallBack();
}
/*CAN°´¼ü*/
if (stKeyAttribute[enKeyIndex].enKeyType == KEY_NO_DEBOUNCE_RISE)
{
if (enKeyRealType <= KEY_CAN_LONG_PRESS_NOT_RELEASE)
{
if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
{
if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_NONE) &&
(enKeyRealType == KEY_CAN_SHORT_PRESS))
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SHORT_PRESS);
}
else if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_NONE) &&
(enKeyRealType == KEY_CAN_LONG_PRESS))
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_LONG_PRESS);
}
else if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_NONE) &&
(enKeyRealType == KEY_CAN_LONG_PRESS_NOT_RELEASE))
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SUPER_LONG_PRESS);
}
else
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_NONE);
}
}
stKeyCtrVariate[enKeyIndex].enKeyTypeLast = enKeyRealType;
}
else
{
stKeyCtrVariate[enKeyIndex].enKeyTypeLast = KEY_CAN_NONE;
}
}
else if (stKeyAttribute[enKeyIndex].enKeyType == KEY_NO_DEBOUNCE_FALL)
{
if (enKeyRealType <= KEY_CAN_LONG_PRESS_NOT_RELEASE)
{
if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
{
if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_SHORT_PRESS) &&
(enKeyRealType == KEY_CAN_NONE))
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SHORT_PRESS);
}
else if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_LONG_PRESS) &&
(enKeyRealType == KEY_CAN_NONE))
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_LONG_PRESS);
}
else if ((stKeyCtrVariate[enKeyIndex].enKeyTypeLast == KEY_CAN_LONG_PRESS_NOT_RELEASE) &&
(enKeyRealType == KEY_CAN_NONE))
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SUPER_LONG_PRESS);
}
else
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_NONE);
}
}
stKeyCtrVariate[enKeyIndex].enKeyTypeLast = enKeyRealType;
}
else
{
stKeyCtrVariate[enKeyIndex].enKeyTypeLast = KEY_CAN_NONE;
}
}
/*Ó²Ïß°´¼ü*/
else if (stKeyAttribute[enKeyIndex].enKeyType == KEY_NEED_DEBOUNCE_NORMAL)
{
if (enKeyRealType == KEY_LINE_LOOSEN) /*空闲*/
{
if (stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount <= stKeyParameter.u16KeyLoosenTime)
{
stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount += KEY_CALL_CYCLE;
}
else
{
stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount = 0U;
stKeyCtrVariate[enKeyIndex].enKeyTypeLast = KEY_LINE_LOOSEN;
stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = 0U;
stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus = KEY_IG_INVALID;
stKeyCtrVariate[enKeyIndex].enKeyIGNStatusLast = KEY_IG_INVALID;
stKeyCtrVariate[enKeyIndex].u8KeyIGNChangeFlag = 0U;
stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount = 0U;
}
if ((stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount >= stKeyParameter.u16KeyLoosenTime) &&
(stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount >= stKeyParameter.u16KeyShortPressTime) &&
(stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount < stKeyParameter.u16KeyLongPressTime))
{
stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount = 0U;
/* call back */
if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SHORT_PRESS);
}
}
}
else if (enKeyRealType == KEY_LINE_PRESS) /*按键按下*/
{
if (stKeyCtrVariate[enKeyIndex].enKeyTypeLast != enKeyRealType)
{
stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount = 0U;
stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = 0U;
stKeyCtrVariate[enKeyIndex].enKeyTypeLast = enKeyRealType;
stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount = 0U;
stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus = enKeyIGNRealStatus;
stKeyCtrVariate[enKeyIndex].enKeyIGNStatusLast = enKeyIGNRealStatus;
}
else
{
if (stKeyCtrVariate[enKeyIndex].enKeyIGNStatusLast != enKeyIGNRealStatus)
{
stKeyCtrVariate[enKeyIndex].enKeyIGNStatusLast = enKeyIGNRealStatus;
if (stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus != enKeyIGNRealStatus)
{
stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount;
}
stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount = 0U;
}
else
{
if (stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount < 0xFFF0U)
{
stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount += KEY_CALL_CYCLE;
}
}
if (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount < 0xFFF0U)
{
stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount += KEY_CALL_CYCLE;
}
if (stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus != enKeyIGNRealStatus)
{
stKeyCtrVariate[enKeyIndex].u8KeyIGNChangeFlag = 1U;
}
if (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount >= stKeyParameter.u16KeyShortPressTime)
{
stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount = 0U;
}
if (stKeyCtrVariate[enKeyIndex].u8KeyIGNChangeFlag == 0U)
{
if (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount >= stKeyParameter.u16KeySuperLongPressTime)
{
if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_SUPER_LONG_PRESS);
}
}
else if (stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount >= stKeyParameter.u16KeyLongPressTime)
{
if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_LONG_PRESS);
}
}
}
else
{
if (stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus == KEY_IG_OFF)
{
if (enKeyIGNRealStatus == KEY_IG_ON)
{
if ((stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord >= stKeyParameter.u16KeyValidOFFTime) &&
(stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount >= stKeyParameter.u16KeyValidONTime))
{
if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_OFF_TO_ON);
}
stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = 0U;
}
}
}
else if (stKeyCtrVariate[enKeyIndex].enKeyIGNFirstStatus == KEY_IG_ON)
{
if (enKeyIGNRealStatus == KEY_IG_OFF)
{
if ((stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord >= stKeyParameter.u16KeyValidONTime) &&
(stKeyCtrVariate[enKeyIndex].u16KeyIGNTimeCount >= stKeyParameter.u16KeyValidOFFTime))
{
if (stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack != 0U)
{
stKeyAttribute[enKeyIndex].pfnKeyOperationCallBack(KEY_EVENT_ON_TO_OFF);
}
stKeyCtrVariate[enKeyIndex].u16KeyFirstIGNTimeRecord = 0U;
}
}
}
}
}
}
else
{
/*ERROR*/
stKeyCtrVariate[enKeyIndex].u16KeyInvalidTimeCount = 0U;
stKeyCtrVariate[enKeyIndex].u16KeyValidTimeCount = 0U;
stKeyCtrVariate[enKeyIndex].enKeyTypeLast = (Key_Real_Status_en_t)0U;
}
}
else
{
}
}
}
/*10ms调用一次*/
void Key_Service(void)
{
uint8_t i = 0U;
for (i = 0U; i < KEY_NUM_MAX; i++)
{
Key_Process((Key_Num_en_t)i); /*Key_Num_en_t*/
}
}
/*首次上电时调用一次即可*/
void Key_Init(void)
{
uint8_t i = 0U;
for (i = 0U; i < KEY_NUM_MAX; i++)
{
stKeyCtrVariate[i].enKeyTypeLast = KEY_CAN_NONE;
stKeyCtrVariate[i].u16KeyInvalidTimeCount = 0U;
stKeyCtrVariate[i].u16KeyValidTimeCount = 0U;
stKeyCtrVariate[i].u16KeyFirstIGNTimeRecord = 0U;
stKeyCtrVariate[i].enKeyIGNFirstStatus = KEY_IG_INVALID;
stKeyCtrVariate[i].enKeyIGNStatusLast = KEY_IG_INVALID;
stKeyCtrVariate[i].u8KeyIGNChangeFlag = 0U;
stKeyCtrVariate[i].u16KeyIGNTimeCount = 0U;
}
stKeyParameter.u16KeyLoosenTime = 50U;
stKeyParameter.u16KeyShortPressTime = 40U;
stKeyParameter.u16KeyLongPressTime = 2000U;
stKeyParameter.u16KeySuperLongPressTime = 30000U;
stKeyParameter.u16KeyValidOFFTime = 200U;/*按键动作时间*/
stKeyParameter.u16KeyValidONTime = 1000U;
}
/*对于硬线按键而言,判断短按的时间参数*/
Key_Set_en_t Key_Parameter_Set_Short_Press_Time(uint16_t u16Time)
{
Key_Set_en_t enKeySetResult = KEY_SET_OK;
if (u16Time < stKeyParameter.u16KeyLoosenTime)
{
enKeySetResult = KEY_SET_ERROR;
}
else if (u16Time > stKeyParameter.u16KeyLongPressTime)
{
enKeySetResult = KEY_SET_ERROR;
}
else
{
stKeyParameter.u16KeyShortPressTime = u16Time;
}
return enKeySetResult;
}
/*对于硬线按键而言,判断长按的时间参数*/
Key_Set_en_t Key_Parameter_Set_Long_Press_Time(uint16_t u16Time)
{
Key_Set_en_t enKeySetResult = KEY_SET_OK;
if (u16Time < stKeyParameter.u16KeyShortPressTime)
{
enKeySetResult = KEY_SET_ERROR;
}
else if (u16Time > stKeyParameter.u16KeySuperLongPressTime)
{
enKeySetResult = KEY_SET_ERROR;
}
else
{
stKeyParameter.u16KeyLongPressTime = u16Time;
}
return enKeySetResult;
}
/*对于硬线按键而言,判断超长按的时间参数*/
Key_Set_en_t Key_Parameter_Set_Super_Long_Press_Time(uint16_t u16Time)
{
Key_Set_en_t enKeySetResult = KEY_SET_OK;
if (u16Time < stKeyParameter.u16KeyLongPressTime)
{
enKeySetResult = KEY_SET_ERROR;
}
else
{
stKeyParameter.u16KeySuperLongPressTime = u16Time;
}
return enKeySetResult;
}