-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbutton.c
552 lines (478 loc) · 15.4 KB
/
button.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
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
/************************************************************
* @brief button_drive
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0.1
* @note button.c
***********************************************************/
#include "button.h"
#ifdef PKG_USING_BUTTON
/*******************************************************************
* Variable Declaration
*******************************************************************/
static struct button* Head_Button = RT_NULL;
/*******************************************************************
* Function Declaration
*******************************************************************/
static char *StrnCopy(char *dst, const char *src, rt_uint32_t n);
static void Print_Btn_Info(Button_t* btn);
static void Add_Button(Button_t* btn);
/************************************************************
* @brief Create a Button
* @param name:button name
* @param btn:button structure
* @param read_btn_level:Button trigger level reading function,
* Return the level of the rt_uint8_t type by yourself
* @param btn_trigger_level:Button trigger level
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
* @note RT_NULL
***********************************************************/
void Button_Create(const char *name,
Button_t *btn,
rt_uint8_t(*read_btn_level)(void),
rt_uint8_t btn_trigger_level)
{
if( btn == RT_NULL)
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("struct button is RT_NULL!"));
}
memset(btn, 0, sizeof(struct button)); //Clear structure information
StrnCopy(btn->Name, name, BTN_NAME_MAX); //button name
btn->Button_State = NONE_TRIGGER; //Button status
btn->Button_Last_State = NONE_TRIGGER; //Button last status
btn->Button_Trigger_Event = NONE_TRIGGER; //Button trigger event
btn->Read_Button_Level = read_btn_level; //Button trigger level reading function
btn->Button_Trigger_Level = btn_trigger_level; //Button trigger level
btn->Button_Last_Level = btn->Read_Button_Level(); //Button current level
btn->Debounce_Time = 0;
RT_DEBUG_LOG(RT_DEBUG_THREAD,("button create success!"));
Add_Button(btn); //Added to the singly linked list when button created
Print_Btn_Info(btn); //printf info
}
/************************************************************
* @brief burron trigger events are attach to callback function
* @param btn:button structure
* @param btn_event:button events
* @param btn_callback : Callback handler after the button is triggered.Need user implementation
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
***********************************************************/
void Button_Attach(Button_t *btn,Button_Event btn_event,Button_CallBack btn_callback)
{
if( btn == RT_NULL)
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("struct button is RT_NULL!"));
}
if(BUTTON_ALL_RIGGER == btn_event)
{
for(rt_uint8_t i = 0 ; i < number_of_event-1 ; i++)
/*A callback function triggered by a button event ,Used to handle button events */
btn->CallBack_Function[i] = btn_callback;
}
else
{
btn->CallBack_Function[btn_event] = btn_callback;
}
}
/************************************************************
* @brief Delete an already created button
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
* @note RT_NULL
***********************************************************/
void Button_Delete(Button_t *btn)
{
struct button** curr;
for(curr = &Head_Button; *curr;)
{
struct button* entry = *curr;
if (entry == btn)
{
*curr = entry->Next;
}
else
{
curr = &entry->Next;
}
}
}
/************************************************************
* @brief Get Button Event Info
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
***********************************************************/
void Get_Button_EventInfo(Button_t *btn)
{
for(rt_uint8_t i = 0 ; i < number_of_event-1 ; i++)
{
if(btn->CallBack_Function[i] != 0)
{
/* print */
RT_DEBUG_LOG(RT_DEBUG_THREAD,("Button_Event:%d",i));
}
}
}
/************************************************************
* @brief Get Button Event
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
***********************************************************/
rt_uint8_t Get_Button_Event(Button_t *btn)
{
return (rt_uint8_t)(btn->Button_Trigger_Event);
}
/************************************************************
* @brief Get Button State
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
***********************************************************/
rt_uint8_t Get_Button_State(Button_t *btn)
{
return (rt_uint8_t)(btn->Button_State);
}
/************************************************************
* @brief button cycle processing function
* @param btn:button structure
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
* @note This function must be called in a certain period. The recommended period is 20~50ms.
***********************************************************/
void Button_Cycle_Process(Button_t *btn)
{
/* Get the current button level */
rt_uint8_t current_level = (rt_uint8_t)btn->Read_Button_Level();
/* Button level changes, debounce */
if((current_level != btn->Button_Last_Level)&&(++(btn->Debounce_Time) >= BUTTON_DEBOUNCE_TIME))
{
/* Update current button level */
btn->Button_Last_Level = current_level;
/* button is pressed */
btn->Debounce_Time = 0;
/* If the button is not pressed, change the button state to press (first press / double trigger) */
if((btn->Button_State == NONE_TRIGGER)||(btn->Button_State == BUTTON_DOUBLE))
{
btn->Button_State = BUTTON_DOWM;
}
//free button
else if(btn->Button_State == BUTTON_DOWM)
{
btn->Button_State = BUTTON_UP;
RT_DEBUG_LOG(RT_DEBUG_THREAD,("button release"));
}
}
switch(btn->Button_State)
{
/* button dowm */
case BUTTON_DOWM :
{
if(btn->Button_Last_Level == btn->Button_Trigger_Level)
{
/* Support continuous triggering */
#ifdef CONTINUOS_TRIGGER
if(++(btn->Button_Cycle) >= BUTTON_CONTINUOS_CYCLE)
{
btn->Button_Cycle = 0;
btn->Button_Trigger_Event = BUTTON_CONTINUOS;
/* continuous triggering */
TRIGGER_CB(BUTTON_CONTINUOS);
RT_DEBUG_LOG(RT_DEBUG_THREAD,("continuous triggering"));
}
#else
btn->Button_Trigger_Event = BUTTON_DOWM;
/* Update the trigger event before releasing the button as long press */
if(++(btn->Long_Time) >= BUTTON_LONG_TIME)
{
#ifdef LONG_FREE_TRIGGER
btn->Button_Trigger_Event = BUTTON_LONG;
#else
/* Continuous triggering of long press cycles */
if(++(btn->Button_Cycle) >= BUTTON_LONG_CYCLE)
{
btn->Button_Cycle = 0;
btn->Button_Trigger_Event = BUTTON_LONG;
/* long triggering */
TRIGGER_CB(BUTTON_LONG);
}
#endif
/* Update time overflow */
if(btn->Long_Time == 0xFF)
{
btn->Long_Time = BUTTON_LONG_TIME;
}
RT_DEBUG_LOG(RT_DEBUG_THREAD,("Long press"));
}
#endif
}
break;
}
/* button up */
case BUTTON_UP :
{
/* Trigger click */
if(btn->Button_Trigger_Event == BUTTON_DOWM)
{
/* double click */
if((btn->Timer_Count <= BUTTON_DOUBLE_TIME)&&(btn->Button_Last_State == BUTTON_DOUBLE))
{
btn->Button_Trigger_Event = BUTTON_DOUBLE;
TRIGGER_CB(BUTTON_DOUBLE);
RT_DEBUG_LOG(RT_DEBUG_THREAD,("double click"));
btn->Button_State = NONE_TRIGGER;
btn->Button_Last_State = NONE_TRIGGER;
}
else
{
btn->Timer_Count=0;
/* Detection long press failed, clear 0 */
btn->Long_Time = 0;
#ifndef SINGLE_AND_DOUBLE_TRIGGER
/* click */
TRIGGER_CB(BUTTON_DOWM);
#endif
btn->Button_State = BUTTON_DOUBLE;
btn->Button_Last_State = BUTTON_DOUBLE;
}
}
else if(btn->Button_Trigger_Event == BUTTON_LONG)
{
#ifdef LONG_FREE_TRIGGER
/* Long press */
TRIGGER_CB(BUTTON_LONG);
#else
/* Long press free */
TRIGGER_CB(BUTTON_LONG_FREE);
#endif
btn->Long_Time = 0;
btn->Button_State = NONE_TRIGGER;
btn->Button_Last_State = BUTTON_LONG;
}
#ifdef CONTINUOS_TRIGGER
/* Press continuously */
else if(btn->Button_Trigger_Event == BUTTON_CONTINUOS)
{
btn->Long_Time = 0;
/* Press continuously free */
TRIGGER_CB(BUTTON_CONTINUOS_FREE);
btn->Button_State = NONE_TRIGGER;
btn->Button_Last_State = BUTTON_CONTINUOS;
}
#endif
break;
}
case BUTTON_DOUBLE :
{
/* Update time */
btn->Timer_Count++;
if(btn->Timer_Count>=BUTTON_DOUBLE_TIME)
{
btn->Button_State = NONE_TRIGGER;
btn->Button_Last_State = NONE_TRIGGER;
}
#ifdef SINGLE_AND_DOUBLE_TRIGGER
if((btn->Timer_Count>=BUTTON_DOUBLE_TIME)&&(btn->Button_Last_State != BUTTON_DOWM))
{
btn->Timer_Count=0;
TRIGGER_CB(BUTTON_DOWM);
btn->Button_State = NONE_TRIGGER;
btn->Button_Last_State = BUTTON_DOWM;
}
#endif
break;
}
default :
break;
}
}
/************************************************************
* @brief Traversing the way to scan the button without losing each button
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
* @note This function is called periodically, it is recommended to call 20-50ms once.
***********************************************************/
void Button_Process(void)
{
struct button* pass_btn;
for(pass_btn = Head_Button; pass_btn != RT_NULL; pass_btn = pass_btn->Next)
{
Button_Cycle_Process(pass_btn);
}
}
/************************************************************
* @brief Search Button
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
* @note RT_NULL
***********************************************************/
void Search_Button(void)
{
struct button* pass_btn;
for(pass_btn = Head_Button; pass_btn != RT_NULL; pass_btn = pass_btn->Next)
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("button node have %s",pass_btn->Name));
}
}
/************************************************************
* @brief Handle all button callback functions
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
* @note Not implemented yet
***********************************************************/
void Button_Process_CallBack(void *btn)
{
rt_uint8_t btn_event = Get_Button_Event(btn);
switch(btn_event)
{
case BUTTON_DOWM:
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("Add processing logic for your press trigger"));
break;
}
case BUTTON_UP:
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("Add processing logic for your trigger release"));
break;
}
case BUTTON_DOUBLE:
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("Add processing logic for your double-click trigger"));
break;
}
case BUTTON_LONG:
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("Add processing logic for your long press trigger"));
break;
}
case BUTTON_LONG_FREE:
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("Add processing logic for your long press trigger free"));
break;
}
case BUTTON_CONTINUOS:
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("Add your continuous trigger processing logic"));
break;
}
case BUTTON_CONTINUOS_FREE:
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("Add processing logic for your continuous trigger release"));
break;
}
}
}
/**************************** The following is the internal call function ********************/
/************************************************************
* @brief Copy the specified length string
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
* @note RT_NULL
***********************************************************/
static char *StrnCopy(char *dst, const char *src, rt_uint32_t n)
{
if (n != 0)
{
char *d = dst;
const char *s = src;
do
{
if ((*d++ = *s++) == 0)
{
while (--n != 0)
*d++ = 0;
break;
}
} while (--n != 0);
}
return (dst);
}
/************************************************************
* @brief Print button related information
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
* @note RT_NULL
***********************************************************/
static void Print_Btn_Info(Button_t* btn)
{
RT_DEBUG_LOG(RT_DEBUG_THREAD,("button struct information:\n\
btn->Name:%s \n\
btn->Button_State:%d \n\
btn->Button_Trigger_Event:%d \n\
btn->Button_Trigger_Level:%d \n\
btn->Button_Last_Level:%d \n\
",
btn->Name,
btn->Button_State,
btn->Button_Trigger_Event,
btn->Button_Trigger_Level,
btn->Button_Last_Level));
Search_Button();
}
/************************************************************
* @brief Connect buttons with a single linked list
* @param RT_NULL
* @return RT_NULL
* @author jiejie
* @github https://github.com/jiejieTop
* @date 2018-xx-xx
* @version v1.0
* @note RT_NULL
***********************************************************/
static void Add_Button(Button_t* btn)
{
struct button *pass_btn = Head_Button;
while(pass_btn)
{
pass_btn = pass_btn->Next;
}
btn->Next = Head_Button;
Head_Button = btn;
}
#endif