button.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef BUTTON_H
  2. #define BUTTON_H
  3. #include <rthw.h>
  4. #include <rtthread.h>
  5. #include <rtdevice.h>
  6. #include <string.h>
  7. #define BTN_NAME_MAX 32 //Ãû×Ö×î´óΪ32×Ö½Ú
  8. /*
  9. The button debounce time is 40ms, the recommended calling period is 20ms.
  10. It is considered valid only if the 40ms state is continuously detected, including bounce and press.
  11. */
  12. //#define CONTINUOS_TRIGGER //Whether to support continuous triggering, do not detect single-click and long-press if you send even
  13. /* Whether to support click and double-click while there is a trigger,
  14. if you choose to enable the macro definition, single-click double-clickback,
  15. but click will delay the response.Because it must be judged whether a
  16. double click is triggered after clicking, the delay time is the double-click
  17. interval BUTTON_DOUBLE_TIME.If you do not enable this macro definition,
  18. it is recommended that there is only one click/double click in the project.
  19. Otherwise, a click will be triggered when the response is double-clicked.
  20. Because the double click must be generated after one press and release.
  21. */
  22. //#define SINGLE_AND_DOUBLE_TRIGGER
  23. /* Whether long-press release is supported or not. If this macro definition is turned on, a long press is triggered after a long press release.
  24. Otherwise, long press is triggered for a long time, the trigger period is determined by BUTTON_LONG_CYCLE*/
  25. //#define LONG_FREE_TRIGGER
  26. #ifndef BUTTON_DEBOUNCE_TIME
  27. #define BUTTON_DEBOUNCE_TIME 2 //Debounce time (n-1)*call cycle
  28. #endif
  29. #ifndef BUTTON_CONTINUOS_CYCLE
  30. #define BUTTON_CONTINUOS_CYCLE 1 //Double-click the trigger cycle time (n-1)*call cycle
  31. #endif
  32. #ifndef BUTTON_LONG_CYCLE
  33. #define BUTTON_LONG_CYCLE 1 //Long press trigger cycle time (n-1)*call cycle
  34. #endif
  35. #ifndef BUTTON_DOUBLE_TIME
  36. #define BUTTON_DOUBLE_TIME 15 //Double click interval (n-1)*call cycle Recommended at 200-600ms
  37. #endif
  38. #ifndef BUTTON_LONG_TIME
  39. #define BUTTON_LONG_TIME 50 //For n seconds ((n-1)*call cycle)ms, think long press event
  40. #endif
  41. #define TRIGGER_CB(event) \
  42. if(btn->CallBack_Function[event]) \
  43. btn->CallBack_Function[event]((Button_t*)btn)
  44. typedef void (*Button_CallBack)(void*); //The button triggers the callback function and needs to be implemented by the user.
  45. typedef enum {
  46. BUTTON_DOWM = 0,
  47. BUTTON_UP,
  48. BUTTON_CLICK,
  49. BUTTON_DOUBLE,
  50. BUTTON_LONG,
  51. BUTTON_LONG_FREE,
  52. BUTTON_CONTINUOS,
  53. BUTTON_CONTINUOS_FREE,
  54. BUTTON_ALL_RIGGER,
  55. number_of_event, /* The event that triggered the callback */
  56. NONE_TRIGGER
  57. }Button_Event;
  58. /*
  59. Each button corresponds to a global structure variable.
  60. Its member variables are necessary to implement filtering and multiple button states.
  61. */
  62. typedef struct button
  63. {
  64. /* The following is a function pointer pointing to the function that determines whether the button is pressed or not. */
  65. rt_uint8_t (*Read_Button_Level)(void); /* Read the button level function, you need to implement */
  66. char Name[BTN_NAME_MAX];
  67. rt_uint8_t Button_State : 4; /* The current state of the button (pressed or bounced) */
  68. rt_uint8_t Button_Last_State : 4; /* The last button state used to determine the double click */
  69. rt_uint8_t Button_Trigger_Level : 2; /* Button trigger level */
  70. rt_uint8_t Button_Last_Level : 2; /* Button current level */
  71. rt_uint8_t Button_Trigger_Event; /* Button trigger event, click, double click, long press, etc. */
  72. Button_CallBack CallBack_Function[number_of_event];
  73. rt_uint8_t Button_Cycle; /* Continuous button cycle */
  74. rt_uint8_t Timer_Count; /* Timing */
  75. rt_uint8_t Debounce_Time; /* Debounce time */
  76. rt_uint8_t Long_Time; /* Button press duration */
  77. struct button *Next;
  78. }Button_t;
  79. /* Function declaration for external calls */
  80. void Button_Create(const char *name,
  81. Button_t *btn,
  82. rt_uint8_t(*read_btn_level)(void),
  83. rt_uint8_t btn_trigger_level);
  84. void Button_Attach(Button_t *btn,Button_Event btn_event,Button_CallBack btn_callback);
  85. void Button_Cycle_Process(Button_t *btn);
  86. void Button_Process(void);
  87. void Button_Delete(Button_t *btn);
  88. void Search_Button(void);
  89. void Get_Button_EventInfo(Button_t *btn);
  90. rt_uint8_t Get_Button_Event(Button_t *btn);
  91. rt_uint8_t Get_Button_State(Button_t *btn);
  92. void Button_Process_CallBack(void *btn);
  93. #endif