LibDriver MULTI_BUTTON
Loading...
Searching...
No Matches
driver_multi_button.c
Go to the documentation of this file.
1
36
37#include "driver_multi_button.h"
38
42#define CHIP_NAME "General MULTI_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 MULTI_BUTTON_SHORT_TIME (1000 * 1000)
55#define MULTI_BUTTON_LONG_TIME (3 * 1000 * 1000)
56#define MULTI_BUTTON_REPEAT_TIME (200 * 1000)
57#define MULTI_BUTTON_INTERVAL (5 * 1000)
58#define MULTI_BUTTON_TIMEOUT (1000 * 1000)
59#define MULTI_BUTTON_REPEAT_CNT (2)
60#define MULTI_BUTTON_PERIOD (200 * 1000)
61
67static void a_multi_button_set_param(multi_button_handle_t *handle)
68{
69 handle->short_time = MULTI_BUTTON_SHORT_TIME; /* set default short time */
70 handle->long_time = MULTI_BUTTON_LONG_TIME; /* set default long time */
71 handle->repeat_time = MULTI_BUTTON_REPEAT_TIME; /* set default repeat time */
72 handle->interval = MULTI_BUTTON_INTERVAL; /* set default interval */
73 handle->timeout = MULTI_BUTTON_TIMEOUT; /* set default timeout */
74 handle->repeat_cnt = MULTI_BUTTON_REPEAT_CNT; /* set default repeat cnt */
75 handle->period = MULTI_BUTTON_PERIOD; /* set default period */
76}
77
85static void a_multi_button_reset(multi_button_handle_t *handle, uint8_t row, uint8_t col)
86{
87 uint8_t res;
88 uint16_t offset;
90
91 offset = handle->col * row + col; /* get offset */
92 handle->button[offset].decode_len = 0; /* reset the decode */
93 handle->button[offset].short_triggered = 0; /* init 0 */
94 handle->button[offset].long_triggered = 0; /* init 0 */
95
96 res = handle->timestamp_read(&t); /* timestamp read */
97 if (res != 0) /* check result */
98 {
99 return; /* return error */
100 }
101 handle->button[offset].last_time.s = t.s; /* save last time */
102 handle->button[offset].last_time.us = t.us; /* save last time */
103}
104
118static uint8_t a_multi_button_single_period(multi_button_handle_t *handle, uint8_t row, uint8_t col)
119{
120 uint8_t res;
121 uint16_t i;
122 uint16_t len;
123 uint16_t offset;
124 int64_t diff;
126
127 offset = handle->col * row + col; /* get offset */
128 if (handle->button[offset].decode_len != 0) /* if decode len is not 0 */
129 {
130 res = handle->timestamp_read(&t); /* timestamp read */
131 if (res != 0) /* check result */
132 {
133 handle->debug_print("multi_button: timestamp read failed.\n"); /* timestamp read failed */
134
135 return 1; /* return error */
136 }
137 if (handle->button[offset].decode_len == 1) /* short or long press */
138 {
139 diff = (int64_t)((int64_t)t.s - (int64_t)handle->button[offset].decode[0].t.s) * 1000000 +
140 (int64_t)((int64_t)t.us - (int64_t)handle->button[offset].decode[0].t.us); /* now - last time */
141 if ((uint32_t)(diff) >= handle->short_time) /* check short time */
142 {
143 if (handle->button[offset].short_triggered == 0) /* if no triggered */
144 {
145 multi_button_t multi_button;
146
147 multi_button.status = MULTI_BUTTON_STATUS_SHORT_PRESS_START; /* short press start */
148 multi_button.times = 0; /* 0 times */
149 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
150 handle->button[offset].short_triggered = 1; /* set triggered */
151 }
152 }
153 if ((uint32_t)(diff) >= handle->long_time) /* check long time */
154 {
155 if (handle->button[offset].long_triggered == 0) /* if no triggered */
156 {
157 multi_button_t multi_button;
158
159 multi_button.status = MULTI_BUTTON_STATUS_LONG_PRESS_START; /* long press start */
160 multi_button.times = 0; /* 0 times */
161 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
162 handle->button[offset].long_triggered = 1; /* set triggered */
163 }
164 else
165 {
166 multi_button_t multi_button;
167
168 multi_button.status = MULTI_BUTTON_STATUS_LONG_PRESS_HOLD; /* long press hold */
169 multi_button.times = 0; /* 0 times */
170 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
171 }
172 }
173 }
174 else if (handle->button[offset].decode_len == 2) /* single click, short or long press */
175 {
176 diff = (int64_t)((int64_t)t.s - (int64_t)handle->button[offset].decode[1].t.s) * 1000000 +
177 (int64_t)((int64_t)t.us - (int64_t)handle->button[offset].decode[1].t.us); /* now - last time */
178 if (handle->button[offset].long_triggered != 0) /* if long no triggered */
179 {
180 multi_button_t multi_button;
181
182 multi_button.status = MULTI_BUTTON_STATUS_LONG_PRESS_END; /* long press end */
183 multi_button.times = 0; /* 0 times */
184 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
185 a_multi_button_reset(handle, row, col); /* reset all */
186 }
187 else if (handle->button[offset].short_triggered != 0) /* if short no triggered */
188 {
189 multi_button_t multi_button;
190
191 multi_button.status = MULTI_BUTTON_STATUS_SHORT_PRESS_END; /* long press end */
192 multi_button.times = 0; /* 0 times */
193 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
194 a_multi_button_reset(handle, row, col); /* reset all */
195 }
196 else
197 {
198 if ((uint32_t)(diff) >= handle->repeat_time) /* check repeat time */
199 {
200 multi_button_t multi_button;
201
202 multi_button.status = MULTI_BUTTON_STATUS_SINGLE_CLICK; /* single click */
203 multi_button.times = 1; /* 1 times */
204 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
205 a_multi_button_reset(handle, row, col); /* reset all */
206 }
207 }
208 }
209 else if (handle->button[offset].decode_len == 4) /* double click */
210 {
211 diff = (int64_t)((int64_t)t.s - (int64_t)handle->button[offset].last_time.s) * 1000000 +
212 (int64_t)((int64_t)t.us - (int64_t)handle->button[offset].last_time.us); /* now - last time */
213 if ((uint32_t)(diff) >= handle->repeat_time) /* check repeat time */
214 {
215 multi_button_t multi_button;
216
217 len = handle->button[offset].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->button[offset].decode[i + 1].t.s -
223 (int64_t)handle->button[offset].decode[i].t.s) * 1000000 +
224 (int64_t)((int64_t)handle->button[offset].decode[i + 1].t.us -
225 (int64_t)handle->button[offset].decode[i].t.us); /* diff time */
226 handle->button[offset].decode[i].diff_us = (uint32_t)diff2; /* save the time diff2 */
227 }
228 handle->button[offset].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->button[offset].decode[i].diff_us >= handle->repeat_time) /* check repeat time */
233 {
234 handle->debug_print("multi_button: double click error.\n"); /* double click error */
235 a_multi_button_reset(handle, row, col); /* reset all */
236
237 return 4; /* return error */
238 }
239 }
240
241 multi_button.status = MULTI_BUTTON_STATUS_DOUBLE_CLICK; /* double click */
242 multi_button.times = 2; /* 2 times */
243 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
244 a_multi_button_reset(handle, row, col); /* reset all */
245 }
246 }
247 else if (handle->button[offset].decode_len == 6) /* triple click */
248 {
249 diff = (int64_t)((int64_t)t.s - (int64_t)handle->button[offset].last_time.s) * 1000000 +
250 (int64_t)((int64_t)t.us - (int64_t)handle->button[offset].last_time.us); /* now - last time */
251 if ((uint32_t)(diff) >= handle->repeat_time) /* check repeat time */
252 {
253 multi_button_t multi_button;
254
255 len = handle->button[offset].decode_len - 1; /* len - 1 */
256 for (i = 0; i < len; i++) /* diff all time */
257 {
258 int64_t diff2;
259
260 diff2 = (int64_t)((int64_t)handle->button[offset].decode[i + 1].t.s -
261 (int64_t)handle->button[offset].decode[i].t.s) * 1000000 +
262 (int64_t)((int64_t)handle->button[offset].decode[i + 1].t.us -
263 (int64_t)handle->button[offset].decode[i].t.us); /* diff time */
264 handle->button[offset].decode[i].diff_us = (uint32_t)diff2; /* save the time diff2 */
265 }
266 handle->button[offset].decode[5].diff_us = (uint32_t)diff; /* save the time diff */
267
268 for (i = 1; i < len; i += 2) /* check decode length */
269 {
270 if (handle->button[offset].decode[i].diff_us >= handle->repeat_time) /* check repeat time */
271 {
272 handle->debug_print("multi_button: triple click error.\n"); /* triple click error */
273 a_multi_button_reset(handle, row, col); /* reset all */
274
275 return 4; /* return error */
276 }
277 }
278
279 multi_button.status = MULTI_BUTTON_STATUS_TRIPLE_CLICK; /* triple click */
280 multi_button.times = 3; /* 3 times */
281 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
282 a_multi_button_reset(handle, row, col); /* reset all */
283 }
284 }
285 else
286 {
287 if ((handle->button[offset].decode_len > 6) && (handle->button[offset].decode_len % 2 == 0)) /* check time */
288 {
289 diff = (int64_t)((int64_t)t.s - (int64_t)handle->button[offset].last_time.s) * 1000000 +
290 (int64_t)((int64_t)t.us - (int64_t)handle->button[offset].last_time.us); /* now - last time */
291 if ((uint32_t)(diff) >= handle->repeat_time) /* check repeat time */
292 {
293 multi_button_t multi_button;
294
295 len = handle->button[offset].decode_len - 1; /* len - 1 */
296 for (i = 0; i < len; i++) /* diff all time */
297 {
298 int64_t diff2;
299
300 diff2 = (int64_t)((int64_t)handle->button[offset].decode[i + 1].t.s -
301 (int64_t)handle->button[offset].decode[i].t.s) * 1000000 +
302 (int64_t)((int64_t)handle->button[offset].decode[i + 1].t.us -
303 (int64_t)handle->button[offset].decode[i].t.us); /* diff time */
304 handle->button[offset].decode[i].diff_us = (uint32_t)diff2; /* save the time diff2 */
305 }
306 handle->button[offset].decode[len].diff_us = (uint32_t)diff; /* save the time diff */
307
308 for (i = 1; i < len; i += 2) /* check decode length */
309 {
310 if (handle->button[offset].decode[i].diff_us >= handle->repeat_time) /* check repeat time */
311 {
312 handle->debug_print("multi_button: repeat click error.\n"); /* repeat click error */
313 a_multi_button_reset(handle, row, col); /* reset all */
314
315 return 4; /* return error */
316 }
317 }
318
319 multi_button.status = MULTI_BUTTON_STATUS_REPEAT_CLICK; /* repeat click */
320 multi_button.times = handle->button[offset].decode_len / 2; /* decode times */
321 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
322 a_multi_button_reset(handle, row, col); /* reset all */
323 }
324 }
325 else
326 {
327 diff = (int64_t)((int64_t)t.s - (int64_t)handle->button[offset].last_time.s) * 1000000 +
328 (int64_t)((int64_t)t.us - (int64_t)handle->button[offset].last_time.us); /* now - last time */
329 if ((uint32_t)(diff) >= handle->timeout) /* check timeout */
330 {
331 handle->debug_print("multi_button: reset checking.\n"); /* reset checking */
332 a_multi_button_reset(handle, row, col); /* reset all */
333
334 return 4; /* return error */
335 }
336 }
337 }
338 }
339
340 return 0; /* success return 0 */
341}
342
359static uint8_t a_multi_button_single(multi_button_handle_t *handle, uint8_t row, uint8_t col, uint8_t level)
360{
361 uint8_t res;
362 uint8_t press_release;
363 uint16_t offset;
364 int64_t diff;
366
367 offset = handle->col * row + col; /* get offset */
368 if (handle->button[offset].level != level) /* if level changed */
369 {
370 handle->button[offset].cnt++; /* cnt++ */
371 if (handle->button[offset].cnt > handle->repeat_cnt) /* check repeat cnt */
372 {
373 handle->button[offset].level = level; /* set level */
374 handle->button[offset].cnt = 0; /* init cnt 0 */
375 if (handle->button[offset].level != 0) /* if now is high level */
376 {
377 press_release = 0; /* release */
378 }
379 else
380 {
381 press_release = 1; /* press */
382 }
383 }
384 else
385 {
386 return 0; /* not reach cnt */
387 }
388 }
389 else
390 {
391 handle->button[offset].cnt = 0; /* init cnt 0 */
392
393 return 0; /* no change */
394 }
395
396 res = handle->timestamp_read(&t); /* timestamp read */
397 if (res != 0) /* check result */
398 {
399 handle->debug_print("multi_button: timestamp read failed.\n"); /* timestamp read failed */
400
401 return 1; /* return error */
402 }
403 diff = (int64_t)((int64_t)t.s - (int64_t)handle->button[offset].last_time.s) * 1000000 +
404 (int64_t)((int64_t)t.us - (int64_t)handle->button[offset].last_time.us); /* now - last time */
405 if (press_release != 0) /* if press */
406 {
407 if ((handle->button[offset].decode_len % 2) == 0) /* press */
408 {
409 if ((uint32_t)(diff) < handle->interval) /* check diff */
410 {
411 handle->debug_print("multi_button: press too fast.\n"); /* trigger too fast */
412 a_multi_button_reset(handle, row, col); /* reset all */
413
414 return 5; /* success return 0 */
415 }
416 }
417 }
418 else /* if release */
419 {
420 if ((handle->button[offset].decode_len % 2) != 0) /* release */
421 {
422 if ((uint32_t)(diff) < handle->interval) /* check diff */
423 {
424 handle->debug_print("multi_button: release too fast.\n"); /* release too fast */
425 a_multi_button_reset(handle, row, col); /* reset all */
426
427 return 5; /* success return 0 */
428 }
429 }
430 }
431
432 if (press_release != 0) /* check press release */
433 {
434 if (handle->receive_callback != NULL) /* if not null */
435 {
436 multi_button_t multi_button;
437
438 multi_button.status = MULTI_BUTTON_STATUS_PRESS; /* press */
439 multi_button.times = 0; /* 0 times */
440 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
441 }
442 }
443 else
444 {
445 if (handle->receive_callback != NULL) /* if not null */
446 {
447 multi_button_t multi_button;
448
449 multi_button.status = MULTI_BUTTON_STATUS_RELEASE; /* release */
450 multi_button.times = 0; /* 0 times */
451 handle->receive_callback(row, col, &multi_button); /* run the reception callback */
452 }
453 }
454
455 if (handle->button[offset].decode_len >= (MULTI_BUTTON_EACH_LENGTH - 1)) /* check the max length */
456 {
457 a_multi_button_reset(handle, row, col); /* reset all */
458 }
459 if (press_release != 0) /* if press */
460 {
461 if ((handle->button[offset].decode_len % 2) == 0) /* press */
462 {
463 handle->button[offset].decode[handle->button[offset].decode_len].t.s = t.s; /* save s */
464 handle->button[offset].decode[handle->button[offset].decode_len].t.us = t.us; /* save us */
465 handle->button[offset].decode_len++; /* length++ */
466 }
467 else
468 {
469 handle->debug_print("multi_button: double press.\n"); /* double press */
470 a_multi_button_reset(handle, row, col); /* reset all */
471
472 return 4; /* return error */
473 }
474 }
475 else /* if release */
476 {
477 if ((handle->button[offset].decode_len % 2) != 0) /* release */
478 {
479 handle->button[offset].decode[handle->button[offset].decode_len].t.s = t.s; /* save s */
480 handle->button[offset].decode[handle->button[offset].decode_len].t.us = t.us; /* save us */
481 handle->button[offset].decode_len++; /* length++ */
482 }
483 else
484 {
485 handle->debug_print("multi_button: double release.\n"); /* double release */
486 a_multi_button_reset(handle, row, col); /* reset all */
487
488 return 4; /* return error */
489 }
490 }
491 handle->button[offset].last_time.s = t.s; /* save last time */
492 handle->button[offset].last_time.us = t.us; /* save last time */
493
494 return 0; /* success return 0 */
495}
496
503static uint8_t a_multi_button_set_matrix_level(multi_button_handle_t *handle, uint8_t row)
504{
505 uint8_t i;
506 uint8_t res;
507
508 for (i = 0; i < handle->row; i++) /* loop all row */
509 {
510 if (i != row) /* not set row */
511 {
512 res = handle->matrix_write_row(i, 1); /* set row high */
513 if (res != 0) /* check result */
514 {
515 handle->debug_print("multi_button: matrix write row failed.\n"); /* matrix write row failed */
516
517 return 1; /* return error */
518 }
519 }
520 else
521 {
522 res = handle->matrix_write_row(i, 0); /* set row low */
523 if (res != 0) /* check result */
524 {
525 handle->debug_print("multi_button: matrix write row failed.\n"); /* matrix write row failed */
526
527 return 1; /* return error */
528 }
529 }
530 }
531 handle->delay_ms(1); /* delay 1ms */
532
533 return 0; /* success return 0 */
534}
535
547{
548 uint8_t i;
549 uint8_t j;
550 uint8_t res;
551 uint32_t col_array;
552 int64_t diff;
554
555 res = handle->timestamp_read(&t); /* timestamp read */
556 if (res != 0) /* check result */
557 {
558 handle->debug_print("multi_button: timestamp read failed.\n"); /* timestamp read failed */
559
560 return 1; /* return error */
561 }
562 diff = (int64_t)((int64_t)t.s - (int64_t)handle->check_time.s) * 1000000 +
563 (int64_t)((int64_t)t.us - (int64_t)handle->check_time.us); /* now - last time */
564 if ((uint32_t)(diff) > handle->period) /* check period */
565 {
566 handle->check_time.s = t.s; /* save s */
567 handle->check_time.us = t.us; /* save us */
568 }
569 for (i = 0; i < handle->row; i++) /* loop all row */
570 {
571 res = a_multi_button_set_matrix_level(handle, i); /* set matrix level */
572 if (res != 0) /* check result */
573 {
574 return 1; /* return error */
575 }
576 res = handle->matrix_read_row(&col_array); /* read row */
577 if (res != 0) /* check result */
578 {
579 handle->debug_print("multi_button: matrix read row failed.\n"); /* matrix read failed */
580
581 return 1; /* return error */
582 }
583 for (j = 0; j < handle->col; j++) /* loop col all */
584 {
585 res = a_multi_button_single(handle, i, j, (col_array >> j) & 0x1); /* single process */
586 if (res != 0) /* check result */
587 {
588 return 1; /* return error */
589 }
590 if ((uint32_t)(diff) >= handle->period) /* check period */
591 {
592 res = a_multi_button_single_period(handle, i, j); /* read period */
593 if (res != 0) /* check result */
594 {
595 return 1; /* return error */
596 }
597 }
598 }
599 }
600
601 return 0; /* success return 0 */
602}
603
618uint8_t multi_button_init(multi_button_handle_t *handle, uint8_t row, uint8_t col)
619{
620 uint8_t res;
621 uint8_t i;
622 uint8_t j;
623 uint16_t offset;
625
626 if (handle == NULL) /* check handle */
627 {
628 return 2; /* return error */
629 }
630 if (handle->debug_print == NULL) /* check debug_print */
631 {
632 return 3; /* return error */
633 }
634
635 if (row == 0) /* check row */
636 {
637 handle->debug_print("multi_button: row can't be 0.\n"); /* row can't be 0 */
638
639 return 4; /* return error */
640 }
641 if (col == 0) /* check col */
642 {
643 handle->debug_print("multi_button: col can't be 0.\n"); /* col can't be 0 */
644
645 return 4; /* return error */
646 }
647 if (row > 32) /* check row */
648 {
649 handle->debug_print("multi_button: row > 32.\n"); /* row > 32 */
650
651 return 4; /* return error */
652 }
653 if (col > 32) /* check col */
654 {
655 handle->debug_print("multi_button: col > 32.\n"); /* col > 32 */
656
657 return 4; /* return error */
658 }
659 if (row * col > MULTI_BUTTON_NUMBER) /* check row and col size */
660 {
661 handle->debug_print("multi_button: row * col > %d.\n", MULTI_BUTTON_NUMBER); /* row * col > MULTI_BUTTON_NUMBER */
662
663 return 4; /* return error */
664 }
665
666 if (handle->matrix_init == NULL) /* check matrix_init */
667 {
668 handle->debug_print("multi_button: matrix_init is null.\n"); /* matrix_init is null */
669
670 return 3; /* return error */
671 }
672 if (handle->matrix_deinit == NULL) /* check matrix_deinit */
673 {
674 handle->debug_print("multi_button: matrix_deinit is null.\n"); /* matrix_deinit is null */
675
676 return 3; /* return error */
677 }
678 if (handle->matrix_write_row == NULL) /* check matrix_write_row */
679 {
680 handle->debug_print("multi_button: matrix_write_row is null.\n"); /* matrix_write_row is null */
681
682 return 3; /* return error */
683 }
684 if (handle->matrix_read_row == NULL) /* check matrix_read_row */
685 {
686 handle->debug_print("multi_button: matrix_read_row is null.\n"); /* matrix_read_row is null */
687
688 return 3; /* return error */
689 }
690 if (handle->timestamp_read == NULL) /* check timestamp_read */
691 {
692 handle->debug_print("multi_button: timestamp_read is null.\n"); /* timestamp_read is null */
693
694 return 3; /* return error */
695 }
696 if (handle->delay_ms == NULL) /* check delay_ms */
697 {
698 handle->debug_print("multi_button: delay_ms is null.\n"); /* delay_ms is null */
699
700 return 3; /* return error */
701 }
702 if (handle->receive_callback == NULL) /* check receive_callback */
703 {
704 handle->debug_print("multi_button: receive_callback is null.\n"); /* receive_callback is null */
705
706 return 3; /* return error */
707 }
708
709 handle->row = row; /* set row */
710 handle->col = col; /* set col */
711 res = handle->timestamp_read(&t); /* timestamp read */
712 if (res != 0) /* check result */
713 {
714 handle->debug_print("multi_button: timestamp read failed.\n"); /* timestamp read failed */
715
716 return 1; /* return error */
717 }
718 handle->check_time.s = t.s; /* save the current s */
719 handle->check_time.us = t.us; /* save the current us */
720 a_multi_button_set_param(handle); /* set params */
721 for (i = 0; i < row; i++) /* loop row */
722 {
723 for (j = 0; j < col; j++) /* loop col */
724 {
725 offset = handle->row * i + j; /* get offset */
726 a_multi_button_reset(handle, row, col); /* reset all */
727 memset(&handle->button[offset], 0, sizeof(multi_button_single_t)); /* clear button */
728 handle->button[offset].cnt = 0; /* init cnt 0 */
729 handle->button[offset].level = 1; /* init level high */
730 handle->button[offset].last_time.s = t.s; /* save last time */
731 handle->button[offset].last_time.us = t.us; /* save last time */
732 }
733 }
734 res = handle->matrix_init(); /* matrix init */
735 if (res != 0) /* check the result */
736 {
737 handle->debug_print("multi_button: matrix init failed.\n"); /* matrix init failed */
738
739 return 5; /* return error */
740 }
741
742 handle->inited = 1; /* flag inited */
743
744 return 0; /* success return 0 */
745}
746
757{
758 uint8_t res;
759
760 if (handle == NULL) /* check handle */
761 {
762 return 2; /* return error */
763 }
764 if (handle->inited != 1) /* check handle initialization */
765 {
766 return 3; /* return error */
767 }
768
769 res = handle->matrix_deinit(); /* matrix deinit */
770 if (res != 0) /* check the result */
771 {
772 handle->debug_print("multi_button: matrix deinit failed.\n"); /* matrix deinit failed */
773
774 return 1; /* return error */
775 }
776 handle->inited = 0; /* flag close */
777
778 return 0; /* success return 0 */
779}
780
792{
793 if (handle == NULL) /* check handle */
794 {
795 return 2; /* return error */
796 }
797 if (handle->inited != 1) /* check handle initialization */
798 {
799 return 3; /* return error */
800 }
801
802 handle->timeout = us; /* set timeout */
803
804 return 0; /* success return 0 */
805}
806
818{
819 if (handle == NULL) /* check handle */
820 {
821 return 2; /* return error */
822 }
823 if (handle->inited != 1) /* check handle initialization */
824 {
825 return 3; /* return error */
826 }
827
828 *us = handle->timeout; /* get timeout */
829
830 return 0; /* success return 0 */
831}
832
844{
845 if (handle == NULL) /* check handle */
846 {
847 return 2; /* return error */
848 }
849 if (handle->inited != 1) /* check handle initialization */
850 {
851 return 3; /* return error */
852 }
853
854 handle->interval = us; /* set interval */
855
856 return 0; /* success return 0 */
857}
858
870{
871 if (handle == NULL) /* check handle */
872 {
873 return 2; /* return error */
874 }
875 if (handle->inited != 1) /* check handle initialization */
876 {
877 return 3; /* return error */
878 }
879
880 *us = handle->interval; /* get interval */
881
882 return 0; /* success return 0 */
883}
884
896{
897 if (handle == NULL) /* check handle */
898 {
899 return 2; /* return error */
900 }
901 if (handle->inited != 1) /* check handle initialization */
902 {
903 return 3; /* return error */
904 }
905
906 handle->short_time = us; /* set short time */
907
908 return 0; /* success return 0 */
909}
910
922{
923 if (handle == NULL) /* check handle */
924 {
925 return 2; /* return error */
926 }
927 if (handle->inited != 1) /* check handle initialization */
928 {
929 return 3; /* return error */
930 }
931
932 *us = handle->short_time; /* get short time */
933
934 return 0; /* success return 0 */
935}
936
948{
949 if (handle == NULL) /* check handle */
950 {
951 return 2; /* return error */
952 }
953 if (handle->inited != 1) /* check handle initialization */
954 {
955 return 3; /* return error */
956 }
957
958 handle->long_time = us; /* set long time */
959
960 return 0; /* success return 0 */
961}
962
974{
975 if (handle == NULL) /* check handle */
976 {
977 return 2; /* return error */
978 }
979 if (handle->inited != 1) /* check handle initialization */
980 {
981 return 3; /* return error */
982 }
983
984 *us = handle->long_time; /* get long time */
985
986 return 0; /* success return 0 */
987}
988
1000{
1001 if (handle == NULL) /* check handle */
1002 {
1003 return 2; /* return error */
1004 }
1005 if (handle->inited != 1) /* check handle initialization */
1006 {
1007 return 3; /* return error */
1008 }
1009
1010 handle->repeat_time = us; /* set repeat time */
1011
1012 return 0; /* success return 0 */
1013}
1014
1026{
1027 if (handle == NULL) /* check handle */
1028 {
1029 return 2; /* return error */
1030 }
1031 if (handle->inited != 1) /* check handle initialization */
1032 {
1033 return 3; /* return error */
1034 }
1035
1036 *us = handle->repeat_time; /* get repeat time */
1037
1038 return 0; /* success return 0 */
1039}
1040
1052{
1053 if (handle == NULL) /* check handle */
1054 {
1055 return 2; /* return error */
1056 }
1057 if (handle->inited != 1) /* check handle initialization */
1058 {
1059 return 3; /* return error */
1060 }
1061
1062 handle->repeat_cnt = cnt; /* set repeat cnt */
1063
1064 return 0; /* success return 0 */
1065}
1066
1078{
1079 if (handle == NULL) /* check handle */
1080 {
1081 return 2; /* return error */
1082 }
1083 if (handle->inited != 1) /* check handle initialization */
1084 {
1085 return 3; /* return error */
1086 }
1087
1088 *cnt = handle->repeat_cnt; /* get repeat cnt */
1089
1090 return 0; /* success return 0 */
1091}
1092
1103uint8_t multi_button_set_period(multi_button_handle_t *handle, uint32_t period)
1104{
1105 if (handle == NULL) /* check handle */
1106 {
1107 return 2; /* return error */
1108 }
1109 if (handle->inited != 1) /* check handle initialization */
1110 {
1111 return 3; /* return error */
1112 }
1113
1114 handle->period = period; /* set period */
1115
1116 return 0; /* success return 0 */
1117}
1118
1129uint8_t multi_button_get_period(multi_button_handle_t *handle, uint32_t *period)
1130{
1131 if (handle == NULL) /* check handle */
1132 {
1133 return 2; /* return error */
1134 }
1135 if (handle->inited != 1) /* check handle initialization */
1136 {
1137 return 3; /* return error */
1138 }
1139
1140 *period = handle->period; /* get period */
1141
1142 return 0; /* success return 0 */
1143}
1144
1154{
1155 if (info == NULL) /* check handle */
1156 {
1157 return 2; /* return error */
1158 }
1159
1160 memset(info, 0, sizeof(multi_button_info_t)); /* initialize multi_button info structure */
1161 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1162 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1163 strncpy(info->interface, "GPIO", 8); /* copy interface name */
1164 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1165 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1166 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1167 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1168 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1169 info->driver_version = DRIVER_VERSION; /* set driver version */
1170
1171 return 0; /* success return 0 */
1172}
#define MULTI_BUTTON_INTERVAL
#define MAX_CURRENT
#define MULTI_BUTTON_REPEAT_TIME
#define MULTI_BUTTON_TIMEOUT
#define MULTI_BUTTON_SHORT_TIME
multi_button check definition
#define SUPPLY_VOLTAGE_MAX
#define MULTI_BUTTON_REPEAT_CNT
#define TEMPERATURE_MAX
#define MULTI_BUTTON_PERIOD
#define MULTI_BUTTON_LONG_TIME
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver multi_button header file
#define MULTI_BUTTON_NUMBER
multi_button max number definition
uint8_t multi_button_get_repeat_time(multi_button_handle_t *handle, uint32_t *us)
get repeat time
uint8_t multi_button_get_interval(multi_button_handle_t *handle, uint32_t *us)
get interval
uint8_t multi_button_init(multi_button_handle_t *handle, uint8_t row, uint8_t col)
initialize the chip
uint8_t multi_button_process(multi_button_handle_t *handle)
process
uint8_t multi_button_deinit(multi_button_handle_t *handle)
close the chip
struct multi_button_handle_s multi_button_handle_t
multi_button handle structure definition
uint8_t multi_button_get_repeat_cnt(multi_button_handle_t *handle, uint32_t *cnt)
get repeat cnt
#define MULTI_BUTTON_EACH_LENGTH
multi_button each length definition
uint8_t multi_button_get_short_time(multi_button_handle_t *handle, uint32_t *us)
get short time
uint8_t multi_button_set_short_time(multi_button_handle_t *handle, uint32_t us)
set short time
struct multi_button_single_s multi_button_single_t
multi_button single structure definition
uint8_t multi_button_get_timeout(multi_button_handle_t *handle, uint32_t *us)
get timeout
uint8_t multi_button_get_long_time(multi_button_handle_t *handle, uint32_t *us)
get long time
uint8_t multi_button_info(multi_button_info_t *info)
get chip's information
uint8_t multi_button_set_long_time(multi_button_handle_t *handle, uint32_t us)
set long time
uint8_t multi_button_set_repeat_time(multi_button_handle_t *handle, uint32_t us)
set repeat time
uint8_t multi_button_set_repeat_cnt(multi_button_handle_t *handle, uint32_t cnt)
set repeat cnt
uint8_t multi_button_get_period(multi_button_handle_t *handle, uint32_t *period)
get period
struct multi_button_time_s multi_button_time_t
multi_button time structure definition
uint8_t multi_button_set_period(multi_button_handle_t *handle, uint32_t period)
set period
struct multi_button_s multi_button_t
multi_button structure definition
uint8_t multi_button_set_timeout(multi_button_handle_t *handle, uint32_t us)
set timeout
uint8_t multi_button_set_interval(multi_button_handle_t *handle, uint32_t us)
set interval
struct multi_button_info_s multi_button_info_t
multi_button information structure definition
@ MULTI_BUTTON_STATUS_SHORT_PRESS_END
@ MULTI_BUTTON_STATUS_REPEAT_CLICK
@ MULTI_BUTTON_STATUS_DOUBLE_CLICK
@ MULTI_BUTTON_STATUS_LONG_PRESS_END
@ MULTI_BUTTON_STATUS_SINGLE_CLICK
@ MULTI_BUTTON_STATUS_LONG_PRESS_HOLD
@ MULTI_BUTTON_STATUS_TRIPLE_CLICK
@ MULTI_BUTTON_STATUS_PRESS
@ MULTI_BUTTON_STATUS_SHORT_PRESS_START
@ MULTI_BUTTON_STATUS_RELEASE
@ MULTI_BUTTON_STATUS_LONG_PRESS_START
uint8_t(* matrix_deinit)(void)
multi_button_time_t check_time
void(* delay_ms)(uint32_t ms)
multi_button_single_t button[MULTI_BUTTON_NUMBER]
uint8_t(* matrix_read_row)(uint32_t *col_array)
uint8_t(* timestamp_read)(multi_button_time_t *t)
void(* debug_print)(const char *const fmt,...)
void(* receive_callback)(uint16_t row, uint16_t col, multi_button_t *data)
uint8_t(* matrix_write_row)(uint16_t num, uint8_t level)
multi_button_time_t last_time
multi_button_decode_t decode[MULTI_BUTTON_EACH_LENGTH]