multi_button.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 {
  24. uint16_t ticks;
  25. uint16_t short_ticks;
  26. uint16_t long_ticks;
  27. uint8_t repeat : 4;
  28. uint8_t event : 4;
  29. uint8_t state : 3;
  30. uint8_t debounce_cnt : 3;
  31. uint8_t active_level : 1;
  32. uint8_t button_level : 1;
  33. uint8_t (*hal_button_Level)(void);
  34. BtnCallback cb[number_of_event];
  35. struct button* next;
  36. }button;
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t active_level);
  41. void button_attach(struct button* handle, PressEvent event, BtnCallback cb);
  42. PressEvent get_button_event(struct button* handle);
  43. int button_start(struct button* handle);
  44. void button_stop(struct button* handle);
  45. void button_ticks(void);
  46. void button_set_short_ticks(struct button* handle, uint16_t ticks);
  47. void button_set_long_ticks(struct button* handle, uint16_t ticks);
  48. #ifdef __cplusplus
  49. }
  50. #endif
  51. #endif