LibDriver BUTTON
Loading...
Searching...
No Matches
driver_button.c
Go to the documentation of this file.
1
36
37#include "driver_button.h"
38
42#define CHIP_NAME "General BUTTON"
43#define MANUFACTURER_NAME "General"
44#define SUPPLY_VOLTAGE_MIN 1.8f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 1.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
54#define BUTTON_SHORT_TIME (1000 * 1000)
55#define BUTTON_LONG_TIME (3 * 1000 * 1000)
56#define BUTTON_REPEAT_TIME (200 * 1000)
57#define BUTTON_INTERVAL (5 * 1000)
58#define BUTTON_TIMEOUT (1000 * 1000)
59
65static void a_button_set_param(button_handle_t *handle)
66{
67 handle->short_time = BUTTON_SHORT_TIME; /* set default short time */
68 handle->long_time = BUTTON_LONG_TIME; /* set default long time */
69 handle->repeat_time = BUTTON_REPEAT_TIME; /* set default repeat time */
70 handle->interval = BUTTON_INTERVAL; /* set default interval */
71 handle->timeout = BUTTON_TIMEOUT; /* set default timeout */
72}
73
79static void a_button_reset(button_handle_t *handle)
80{
81 uint8_t res;
83
84 handle->decode_len = 0; /* reset the decode */
85 handle->short_triggered = 0; /* init 0 */
86 handle->long_triggered = 0; /* init 0 */
87
88 res = handle->timestamp_read(&t); /* timestamp read */
89 if (res != 0) /* check result */
90 {
91 return; /* return error */
92 }
93 handle->last_time.s = t.s; /* save last time */
94 handle->last_time.us = t.us; /* save last time */
95}
96
109{
110 uint8_t res;
111 uint16_t i;
112 uint16_t len;
113 int64_t diff;
115
116 if (handle == NULL) /* check handle */
117 {
118 return 2; /* return error */
119 }
120 if (handle->inited != 1) /* check handle initialization */
121 {
122 return 3; /* return error */
123 }
124
125 if (handle->decode_len != 0) /* if decode len is not 0 */
126 {
127 res = handle->timestamp_read(&t); /* timestamp read */
128 if (res != 0) /* check result */
129 {
130 handle->debug_print("button: timestamp read failed.\n"); /* timestamp read failed */
131
132 return 1; /* return error */
133 }
134 if (handle->decode_len == 1) /* short or long press */
135 {
136 diff = (int64_t)((int64_t)t.s -
137 (int64_t)handle->decode[0].t.s) * 1000000 +
138 (int64_t)((int64_t)t.us - (int64_t)handle->decode[0].t.us); /* now - last time */
139 if ((uint32_t)(diff) >= handle->short_time) /* check short time */
140 {
141 if (handle->short_triggered == 0) /* if no triggered */
142 {
143 button_t button;
144
145 button.status = BUTTON_STATUS_SHORT_PRESS_START; /* short press start */
146 button.times = 0; /* 0 times */
147 handle->receive_callback(&button); /* run the reception callback */
148 handle->short_triggered = 1; /* set triggered */
149 }
150 }
151 if ((uint32_t)(diff) >= handle->long_time) /* check long time */
152 {
153 if (handle->long_triggered == 0) /* if no triggered */
154 {
155 button_t button;
156
157 button.status = BUTTON_STATUS_LONG_PRESS_START; /* long press start */
158 button.times = 0; /* 0 times */
159 handle->receive_callback(&button); /* run the reception callback */
160 handle->long_triggered = 1; /* set triggered */
161 }
162 else
163 {
164 button_t button;
165
166 button.status = BUTTON_STATUS_LONG_PRESS_HOLD; /* long press hold */
167 button.times = 0; /* 0 times */
168 handle->receive_callback(&button); /* run the reception callback */
169 }
170 }
171 }
172 else if (handle->decode_len == 2) /* single click, short or long press */
173 {
174 diff = (int64_t)((int64_t)t.s -
175 (int64_t)handle->decode[1].t.s) * 1000000 +
176 (int64_t)((int64_t)t.us - (int64_t)handle->decode[1].t.us); /* now - last time */
177 if (handle->long_triggered != 0) /* if long no triggered */
178 {
179 button_t button;
180
181 button.status = BUTTON_STATUS_LONG_PRESS_END; /* long press end */
182 button.times = 0; /* 0 times */
183 handle->receive_callback(&button); /* run the reception callback */
184 a_button_reset(handle); /* reset all */
185 }
186 else if (handle->short_triggered != 0) /* if short no triggered */
187 {
188 button_t button;
189
190 button.status = BUTTON_STATUS_SHORT_PRESS_END; /* long press end */
191 button.times = 0; /* 0 times */
192 handle->receive_callback(&button); /* run the reception callback */
193 a_button_reset(handle); /* reset all */
194 }
195 else
196 {
197 if ((uint32_t)(diff) >= handle->repeat_time) /* check repeat time */
198 {
199 button_t button;
200
201 button.status = BUTTON_STATUS_SINGLE_CLICK; /* single click */
202 button.times = 1; /* 1 times */
203 handle->receive_callback(&button); /* run the reception callback */
204 a_button_reset(handle); /* reset all */
205 }
206 }
207 }
208 else if (handle->decode_len == 4) /* double click */
209 {
210 diff = (int64_t)((int64_t)t.s -
211 (int64_t)handle->last_time.s) * 1000000 +
212 (int64_t)((int64_t)t.us - (int64_t)handle->last_time.us); /* now - last time */
213 if ((uint32_t)(diff) >= handle->repeat_time) /* check repeat time */
214 {
215 button_t button;
216
217 len = handle->decode_len - 1; /* len - 1 */
218 for (i = 0; i < len; i++) /* diff all time */
219 {
220 int64_t diff2;
221
222 diff2 = (int64_t)((int64_t)handle->decode[i + 1].t.s -
223 (int64_t)handle->decode[i].t.s) * 1000000 +
224 (int64_t)((int64_t)handle->decode[i + 1].t.us -
225 (int64_t)handle->decode[i].t.us); /* diff time */
226 handle->decode[i].diff_us = (uint32_t)diff2; /* save the time diff2 */
227 }
228 handle->decode[3].diff_us = (uint32_t)diff; /* save the time diff */
229
230 for (i = 1; i < len; i += 2) /* check decode length */
231 {
232 if (handle->decode[i].diff_us >= handle->repeat_time) /* check repeat time */
233 {
234 handle->debug_print("button: double click error.\n"); /* double click error */
235 a_button_reset(handle); /* reset all */
236
237 return 4; /* return error */
238 }
239 }
240
241 button.status = BUTTON_STATUS_DOUBLE_CLICK; /* double click */
242 button.times = 2; /* 2 times */
243 handle->receive_callback(&button); /* run the reception callback */
244 a_button_reset(handle); /* reset all */
245 }
246 }
247 else if (handle->decode_len == 6) /* triple click */
248 {
249 diff = (int64_t)((int64_t)t.s -
250 (int64_t)handle->last_time.s) * 1000000 +
251 (int64_t)((int64_t)t.us - (int64_t)handle->last_time.us); /* now - last time */
252 if ((uint32_t)(diff) >= handle->repeat_time) /* check repeat time */
253 {
254 button_t button;
255
256 len = handle->decode_len - 1; /* len - 1 */
257 for (i = 0; i < len; i++) /* diff all time */
258 {
259 int64_t diff2;
260
261 diff2 = (int64_t)((int64_t)handle->decode[i + 1].t.s -
262 (int64_t)handle->decode[i].t.s) * 1000000 +
263 (int64_t)((int64_t)handle->decode[i + 1].t.us -
264 (int64_t)handle->decode[i].t.us); /* diff time */
265 handle->decode[i].diff_us = (uint32_t)diff2; /* save the time diff2 */
266 }
267 handle->decode[5].diff_us = (uint32_t)diff; /* save the time diff */
268
269 for (i = 1; i < len; i += 2) /* check decode length */
270 {
271 if (handle->decode[i].diff_us >= handle->repeat_time) /* check repeat time */
272 {
273 handle->debug_print("button: triple click error.\n"); /* triple click error */
274 a_button_reset(handle); /* reset all */
275
276 return 4; /* return error */
277 }
278 }
279
280 button.status = BUTTON_STATUS_TRIPLE_CLICK; /* triple click */
281 button.times = 3; /* 3 times */
282 handle->receive_callback(&button); /* run the reception callback */
283 a_button_reset(handle); /* reset all */
284 }
285 }
286 else
287 {
288 if ((handle->decode_len > 6) && (handle->decode_len % 2 == 0)) /* check time */
289 {
290 diff = (int64_t)((int64_t)t.s -
291 (int64_t)handle->last_time.s) * 1000000 +
292 (int64_t)((int64_t)t.us - (int64_t)handle->last_time.us); /* now - last time */
293 if ((uint32_t)(diff) >= handle->repeat_time) /* check repeat time */
294 {
295 button_t button;
296
297 len = handle->decode_len - 1; /* len - 1 */
298 for (i = 0; i < len; i++) /* diff all time */
299 {
300 int64_t diff2;
301
302 diff2 = (int64_t)((int64_t)handle->decode[i + 1].t.s -
303 (int64_t)handle->decode[i].t.s) * 1000000 +
304 (int64_t)((int64_t)handle->decode[i + 1].t.us -
305 (int64_t)handle->decode[i].t.us); /* diff time */
306 handle->decode[i].diff_us = (uint32_t)diff2; /* save the time diff2 */
307 }
308 handle->decode[len].diff_us = (uint32_t)diff; /* save the time diff */
309
310 for (i = 1; i < len; i += 2) /* check decode length */
311 {
312 if (handle->decode[i].diff_us >= handle->repeat_time) /* check repeat time */
313 {
314 handle->debug_print("button: repeat click error.\n"); /* repeat click error */
315 a_button_reset(handle); /* reset all */
316
317 return 4; /* return error */
318 }
319 }
320
321 button.status = BUTTON_STATUS_REPEAT_CLICK; /* repeat click */
322 button.times = handle->decode_len / 2; /* decode times */
323 handle->receive_callback(&button); /* run the reception callback */
324 a_button_reset(handle); /* reset all */
325 }
326 }
327 else
328 {
329 diff = (int64_t)((int64_t)t.s -
330 (int64_t)handle->last_time.s) * 1000000 +
331 (int64_t)((int64_t)t.us - (int64_t)handle->last_time.us); /* now - last time */
332 if ((uint32_t)(diff) >= handle->timeout) /* check timeout */
333 {
334 handle->debug_print("button: reset checking.\n"); /* reset checking */
335 a_button_reset(handle); /* reset all */
336
337 return 4; /* return error */
338 }
339 }
340 }
341 }
342
343 return 0; /* success return 0 */
344}
345
360uint8_t button_irq_handler(button_handle_t *handle, uint8_t press_release)
361{
362 uint8_t res;
363 int64_t diff;
365
366 if (handle == NULL) /* check handle */
367 {
368 return 2; /* return error */
369 }
370 if (handle->inited != 1) /* check handle initialization */
371 {
372 return 3; /* return error */
373 }
374
375 res = handle->timestamp_read(&t); /* timestamp read */
376 if (res != 0) /* check result */
377 {
378 handle->debug_print("button: timestamp read failed.\n"); /* timestamp read failed */
379
380 return 1; /* return error */
381 }
382 diff = (int64_t)((int64_t)t.s -
383 (int64_t)handle->last_time.s) * 1000000 +
384 (int64_t)((int64_t)t.us - (int64_t)handle->last_time.us); /* now - last time */
385 if (press_release != 0) /* if press */
386 {
387 if ((handle->decode_len % 2) == 0) /* press */
388 {
389 if ((uint32_t)(diff) < handle->interval) /* check diff */
390 {
391 handle->debug_print("button: press too fast.\n"); /* trigger too fast */
392 a_button_reset(handle); /* reset all */
393
394 return 5; /* success return 0 */
395 }
396 }
397 }
398 else /* if release */
399 {
400 if ((handle->decode_len % 2) != 0) /* release */
401 {
402 if ((uint32_t)(diff) < handle->interval) /* check diff */
403 {
404 handle->debug_print("button: release too fast.\n"); /* release too fast */
405 a_button_reset(handle); /* reset all */
406
407 return 5; /* success return 0 */
408 }
409 }
410 }
411
412 if (press_release != 0) /* check press release */
413 {
414 if (handle->receive_callback != NULL) /* if not null */
415 {
416 button_t button;
417
418 button.status = BUTTON_STATUS_PRESS; /* press */
419 button.times = 0; /* 0 times */
420 handle->receive_callback(&button); /* run the reception callback */
421 }
422 }
423 else
424 {
425 if (handle->receive_callback != NULL) /* if not null */
426 {
427 button_t button;
428
429 button.status = BUTTON_STATUS_RELEASE; /* release */
430 button.times = 0; /* 0 times */
431 handle->receive_callback(&button); /* run the reception callback */
432 }
433 }
434
435 if (handle->decode_len >= (BUTTON_LENGTH - 1)) /* check the max length */
436 {
437 a_button_reset(handle); /* reset all */
438 }
439 if (press_release != 0) /* if press */
440 {
441 if ((handle->decode_len % 2) == 0) /* press */
442 {
443 handle->decode[handle->decode_len].t.s = t.s; /* save s */
444 handle->decode[handle->decode_len].t.us = t.us; /* save us */
445 handle->decode_len++; /* length++ */
446 }
447 else
448 {
449 handle->debug_print("button: double press.\n"); /* double press */
450 a_button_reset(handle); /* reset all */
451
452 return 4; /* return error */
453 }
454 }
455 else /* if release */
456 {
457 if ((handle->decode_len % 2) != 0) /* release */
458 {
459 handle->decode[handle->decode_len].t.s = t.s; /* save s */
460 handle->decode[handle->decode_len].t.us = t.us; /* save us */
461 handle->decode_len++; /* length++ */
462 }
463 else
464 {
465 handle->debug_print("button: double release.\n"); /* double release */
466 a_button_reset(handle); /* reset all */
467
468 return 4; /* return error */
469 }
470 }
471 handle->last_time.s = t.s; /* save last time */
472 handle->last_time.us = t.us; /* save last time */
473
474 return 0; /* success return 0 */
475}
476
488{
489 uint8_t res;
491
492 if (handle == NULL) /* check handle */
493 {
494 return 2; /* return error */
495 }
496 if (handle->debug_print == NULL) /* check debug_print */
497 {
498 return 3; /* return error */
499 }
500 if (handle->timestamp_read == NULL) /* check timestamp_read */
501 {
502 handle->debug_print("button: timestamp_read is null.\n"); /* timestamp_read is null */
503
504 return 3; /* return error */
505 }
506 if (handle->delay_ms == NULL) /* check delay_ms */
507 {
508 handle->debug_print("button: delay_ms is null.\n"); /* delay_ms is null */
509
510 return 3; /* return error */
511 }
512 if (handle->receive_callback == NULL) /* check receive_callback */
513 {
514 handle->debug_print("button: receive_callback is null.\n"); /* receive_callback is null */
515
516 return 3; /* return error */
517 }
518
519 res = handle->timestamp_read(&t); /* timestamp read */
520 if (res != 0) /* check result */
521 {
522 handle->debug_print("button: timestamp read failed.\n"); /* timestamp read failed */
523
524 return 1; /* return error */
525 }
526 a_button_reset(handle); /* reset all */
527 a_button_set_param(handle); /* set params */
528 handle->last_time.s = t.s; /* save last time */
529 handle->last_time.us = t.us; /* save last time */
530 handle->inited = 1; /* flag inited */
531
532 return 0; /* success return 0 */
533}
534
545{
546 if (handle == NULL) /* check handle */
547 {
548 return 2; /* return error */
549 }
550 if (handle->inited != 1) /* check handle initialization */
551 {
552 return 3; /* return error */
553 }
554
555 handle->inited = 0; /* flag close */
556
557 return 0; /* success return 0 */
558}
559
570uint8_t button_set_timeout(button_handle_t *handle, uint32_t us)
571{
572 if (handle == NULL) /* check handle */
573 {
574 return 2; /* return error */
575 }
576 if (handle->inited != 1) /* check handle initialization */
577 {
578 return 3; /* return error */
579 }
580
581 handle->timeout = us; /* set timeout */
582
583 return 0; /* success return 0 */
584}
585
596uint8_t button_get_timeout(button_handle_t *handle, uint32_t *us)
597{
598 if (handle == NULL) /* check handle */
599 {
600 return 2; /* return error */
601 }
602 if (handle->inited != 1) /* check handle initialization */
603 {
604 return 3; /* return error */
605 }
606
607 *us = handle->timeout; /* get timeout */
608
609 return 0; /* success return 0 */
610}
611
622uint8_t button_set_interval(button_handle_t *handle, uint32_t us)
623{
624 if (handle == NULL) /* check handle */
625 {
626 return 2; /* return error */
627 }
628 if (handle->inited != 1) /* check handle initialization */
629 {
630 return 3; /* return error */
631 }
632
633 handle->interval = us; /* set interval */
634
635 return 0; /* success return 0 */
636}
637
648uint8_t button_get_interval(button_handle_t *handle, uint32_t *us)
649{
650 if (handle == NULL) /* check handle */
651 {
652 return 2; /* return error */
653 }
654 if (handle->inited != 1) /* check handle initialization */
655 {
656 return 3; /* return error */
657 }
658
659 *us = handle->interval; /* get interval */
660
661 return 0; /* success return 0 */
662}
663
674uint8_t button_set_short_time(button_handle_t *handle, uint32_t us)
675{
676 if (handle == NULL) /* check handle */
677 {
678 return 2; /* return error */
679 }
680 if (handle->inited != 1) /* check handle initialization */
681 {
682 return 3; /* return error */
683 }
684
685 handle->short_time = us; /* set short time */
686
687 return 0; /* success return 0 */
688}
689
700uint8_t button_get_short_time(button_handle_t *handle, uint32_t *us)
701{
702 if (handle == NULL) /* check handle */
703 {
704 return 2; /* return error */
705 }
706 if (handle->inited != 1) /* check handle initialization */
707 {
708 return 3; /* return error */
709 }
710
711 *us = handle->short_time; /* get short time */
712
713 return 0; /* success return 0 */
714}
715
726uint8_t button_set_long_time(button_handle_t *handle, uint32_t us)
727{
728 if (handle == NULL) /* check handle */
729 {
730 return 2; /* return error */
731 }
732 if (handle->inited != 1) /* check handle initialization */
733 {
734 return 3; /* return error */
735 }
736
737 handle->long_time = us; /* set long time */
738
739 return 0; /* success return 0 */
740}
741
752uint8_t button_get_long_time(button_handle_t *handle, uint32_t *us)
753{
754 if (handle == NULL) /* check handle */
755 {
756 return 2; /* return error */
757 }
758 if (handle->inited != 1) /* check handle initialization */
759 {
760 return 3; /* return error */
761 }
762
763 *us = handle->long_time; /* get long time */
764
765 return 0; /* success return 0 */
766}
767
778uint8_t button_set_repeat_time(button_handle_t *handle, uint32_t us)
779{
780 if (handle == NULL) /* check handle */
781 {
782 return 2; /* return error */
783 }
784 if (handle->inited != 1) /* check handle initialization */
785 {
786 return 3; /* return error */
787 }
788
789 handle->repeat_time = us; /* set repeat time */
790
791 return 0; /* success return 0 */
792}
793
804uint8_t button_get_repeat_time(button_handle_t *handle, uint32_t *us)
805{
806 if (handle == NULL) /* check handle */
807 {
808 return 2; /* return error */
809 }
810 if (handle->inited != 1) /* check handle initialization */
811 {
812 return 3; /* return error */
813 }
814
815 *us = handle->repeat_time; /* get repeat time */
816
817 return 0; /* success return 0 */
818}
819
829{
830 if (info == NULL) /* check handle */
831 {
832 return 2; /* return error */
833 }
834
835 memset(info, 0, sizeof(button_info_t)); /* initialize button info structure */
836 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
837 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
838 strncpy(info->interface, "GPIO", 8); /* copy interface name */
839 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
840 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
841 info->max_current_ma = MAX_CURRENT; /* set maximum current */
842 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
843 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
844 info->driver_version = DRIVER_VERSION; /* set driver version */
845
846 return 0; /* success return 0 */
847}
#define MAX_CURRENT
#define BUTTON_LONG_TIME
#define BUTTON_INTERVAL
#define BUTTON_TIMEOUT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define BUTTON_REPEAT_TIME
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define BUTTON_SHORT_TIME
button check definition
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver button header file
uint8_t button_get_timeout(button_handle_t *handle, uint32_t *us)
get timeout
uint8_t button_info(button_info_t *info)
get chip's information
struct button_info_s button_info_t
button information structure definition
uint8_t button_get_interval(button_handle_t *handle, uint32_t *us)
get interval
uint8_t button_deinit(button_handle_t *handle)
close the chip
uint8_t button_set_repeat_time(button_handle_t *handle, uint32_t us)
set repeat time
#define BUTTON_LENGTH
button length definition
uint8_t button_irq_handler(button_handle_t *handle, uint8_t press_release)
irq handler
struct button_handle_s button_handle_t
button handle structure definition
uint8_t button_init(button_handle_t *handle)
initialize the chip
uint8_t button_set_timeout(button_handle_t *handle, uint32_t us)
set timeout
uint8_t button_set_short_time(button_handle_t *handle, uint32_t us)
set short time
uint8_t button_get_repeat_time(button_handle_t *handle, uint32_t *us)
get repeat time
uint8_t button_period_handler(button_handle_t *handle)
period handler
struct button_s button_t
button structure definition
uint8_t button_set_long_time(button_handle_t *handle, uint32_t us)
set long time
struct button_time_s button_time_t
button time structure definition
uint8_t button_set_interval(button_handle_t *handle, uint32_t us)
set interval
uint8_t button_get_long_time(button_handle_t *handle, uint32_t *us)
get long time
uint8_t button_get_short_time(button_handle_t *handle, uint32_t *us)
get short time
@ BUTTON_STATUS_DOUBLE_CLICK
@ BUTTON_STATUS_SHORT_PRESS_START
@ BUTTON_STATUS_PRESS
@ BUTTON_STATUS_LONG_PRESS_START
@ BUTTON_STATUS_TRIPLE_CLICK
@ BUTTON_STATUS_SHORT_PRESS_END
@ BUTTON_STATUS_REPEAT_CLICK
@ BUTTON_STATUS_LONG_PRESS_HOLD
@ BUTTON_STATUS_SINGLE_CLICK
@ BUTTON_STATUS_RELEASE
@ BUTTON_STATUS_LONG_PRESS_END
button_time_t t
button_decode_t decode[BUTTON_LENGTH]
void(* delay_ms)(uint32_t ms)
button_time_t last_time
void(* receive_callback)(button_t *data)
void(* debug_print)(const char *const fmt,...)
uint8_t(* timestamp_read)(button_time_t *t)
uint8_t short_triggered
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]
uint16_t status
uint16_t times