flexible_button.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @File: flexible_button.h
  3. * @Author: MurphyZhao
  4. * @Date: 2018-09-29
  5. *
  6. * Copyright (c) 2018-2019 MurphyZhao <d2014zjt@163.com>
  7. * https://github.com/murphyzhao
  8. * All rights reserved.
  9. * License-Identifier: Apache-2.0
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  12. * not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  19. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. * Change logs:
  24. * Date Author Notes
  25. * 2018-09-29 MurphyZhao First add
  26. * 2019-08-02 MurphyZhao Migrate code to github.com/murphyzhao account
  27. * 2019-12-26 MurphyZhao Refactor code and implement multiple clicks
  28. *
  29. */
  30. #ifndef __FLEXIBLE_BUTTON_H__
  31. #define __FLEXIBLE_BUTTON_H__
  32. #include "stdint.h"
  33. // 20ms
  34. #define FLEX_BTN_SCAN_FREQ_HZ 50 // How often flex_button_scan () is called
  35. #define FLEX_MS_TO_SCAN_CNT(ms) (ms / (1000 / FLEX_BTN_SCAN_FREQ_HZ))
  36. /* Multiple clicks interval, default 300ms */
  37. #define MAX_MULTIPLE_CLICKS_INTERVAL (FLEX_MS_TO_SCAN_CNT(300))
  38. typedef void (*flex_button_response_callback)(void*);
  39. typedef enum
  40. {
  41. FLEX_BTN_PRESS_DOWN = 0,
  42. FLEX_BTN_PRESS_CLICK,
  43. FLEX_BTN_PRESS_DOUBLE_CLICK,
  44. FLEX_BTN_PRESS_REPEAT_CLICK,
  45. FLEX_BTN_PRESS_SHORT_START,
  46. FLEX_BTN_PRESS_SHORT_UP,
  47. FLEX_BTN_PRESS_LONG_START,
  48. FLEX_BTN_PRESS_LONG_UP,
  49. FLEX_BTN_PRESS_LONG_HOLD,
  50. FLEX_BTN_PRESS_LONG_HOLD_10000,
  51. FLEX_BTN_PRESS_LONG_HOLD_UP,
  52. FLEX_BTN_PRESS_LONG_HOLD_UP_10000,
  53. FLEX_BTN_PRESS_MAX,
  54. FLEX_BTN_PRESS_NONE,
  55. } flex_button_event_t;
  56. /**
  57. * flex_button_t
  58. *
  59. * @brief Button data structure
  60. * Below are members that need to user init before scan.
  61. *
  62. * @member next
  63. * Internal use.
  64. * One-way linked list, pointing to the next button.
  65. *
  66. * @member usr_button_read
  67. * User function is used to read button vaule.
  68. *
  69. * @member cb
  70. * Button event callback function.
  71. *
  72. * @member scan_cnt
  73. * Internal use, user read-only.
  74. * Number of scans, counted when the button is pressed, plus one per scan cycle.
  75. *
  76. * @member click_cnt
  77. * Internal use, user read-only.
  78. * Number of button clicks
  79. *
  80. * @member max_multiple_clicks_interval
  81. * Multiple click interval. Default 'MAX_MULTIPLE_CLICKS_INTERVAL'.
  82. * Need to use FLEX_MS_TO_SCAN_CNT to convert milliseconds into scan cnts.
  83. *
  84. * @member debounce_tick
  85. * Debounce. Not used yet.
  86. * Need to use FLEX_MS_TO_SCAN_CNT to convert milliseconds into scan cnts.
  87. *
  88. * @member short_press_start_tick
  89. * Short press start time. Requires user configuration.
  90. * Need to use FLEX_MS_TO_SCAN_CNT to convert milliseconds into scan cnts.
  91. *
  92. * @member long_press_start_tick
  93. * Long press start time. Requires user configuration.
  94. * Need to use FLEX_MS_TO_SCAN_CNT to convert milliseconds into scan cnts.
  95. *
  96. * @member long_hold_start_tick
  97. * Long hold press start time. Requires user configuration.
  98. *
  99. * @member id
  100. * Button id. Requires user configuration.
  101. * When multiple buttons use the same button callback function,
  102. * they are used to distinguish the buttons.
  103. * Each button id must be unique.
  104. *
  105. * @member pressed_logic_level
  106. * Requires user configuration.
  107. * The logic level of the button pressed, each bit represents a button.
  108. *
  109. * @member event
  110. * Internal use, users can call 'flex_button_event_read' to get current button event.
  111. * Used to record the current button event.
  112. *
  113. * @member status
  114. * Internal use, user unavailable.
  115. * Used to record the current state of buttons.
  116. *
  117. */
  118. typedef struct flex_button
  119. {
  120. struct flex_button* next;
  121. uint8_t (*usr_button_read)(void *);
  122. flex_button_response_callback cb;
  123. uint16_t scan_cnt;
  124. uint16_t click_cnt;
  125. uint16_t max_multiple_clicks_interval;
  126. uint16_t debounce_tick;
  127. uint16_t short_press_start_tick;
  128. uint16_t long_press_start_tick;
  129. uint16_t long_hold_start_tick;
  130. uint16_t long_hold_start_tick_10000;
  131. uint8_t id;
  132. uint8_t pressed_logic_level : 1;
  133. uint8_t event : 4;
  134. uint8_t status : 3;
  135. } flex_button_t;
  136. #ifdef __cplusplus
  137. extern "C" {
  138. #endif
  139. int32_t flex_button_register(flex_button_t *button);
  140. flex_button_event_t flex_button_event_read(flex_button_t* button);
  141. uint8_t flex_button_scan(void);
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif /* __FLEXIBLE_BUTTON_H__ */