multi_button.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef __MULTI_BUTTON_H_
  2. #define __MULTI_BUTTON_H_
  3. #include <stdint.h>
  4. #include <string.h>
  5. //According to your need to modify the constants.
  6. #define TICKS_INTERVAL 5 //ms
  7. #define DEBOUNCE_TICKS 3 //MAX 7 (0 ~ 7)
  8. #define SHORT_TICKS (300 / TICKS_INTERVAL)
  9. #define LONG_TICKS (1000 / TICKS_INTERVAL)
  10. #define LONG_HOLD_CYC (500 / TICKS_INTERVAL)
  11. typedef void (*BtnCallback)(void*);
  12. typedef enum {
  13. PRESS_DOWN = 0,
  14. PRESS_UP,
  15. PRESS_REPEAT,
  16. SINGLE_CLICK,
  17. DOUBLE_CLICK,
  18. LONG_PRESS_START,
  19. LONG_PRESS_HOLD,
  20. number_of_event,
  21. NONE_PRESS
  22. }PressEvent;
  23. typedef struct button *button_t;
  24. struct button {
  25. uint16_t ticks;
  26. uint16_t short_ticks;
  27. uint16_t long_ticks;
  28. uint8_t repeat : 4;
  29. uint8_t event : 4;
  30. uint8_t state : 3;
  31. uint8_t debounce_cnt : 3;
  32. uint8_t active_level : 1;
  33. uint8_t button_level : 1;
  34. uint8_t (*hal_button_Level)(void);
  35. BtnCallback cb[number_of_event];
  36. button_t next;
  37. };
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. void button_init(button_t handle, uint8_t(*pin_level)(void), uint8_t active_level);
  42. void button_attach(button_t handle, PressEvent event, BtnCallback cb);
  43. PressEvent get_button_event(button_t handle);
  44. int button_start(button_t handle);
  45. void button_stop(button_t handle);
  46. void button_ticks(void);
  47. void button_set_short_ticks(button_t handle, uint16_t ticks);
  48. void button_set_long_ticks(button_t handle, uint16_t ticks);
  49. #ifdef __cplusplus
  50. }
  51. #endif
  52. #endif