multi_button.h 1.3 KB

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