multi_button.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 8
  8. #define SHORT_TICKS (300 / TICKS_INTERVAL)
  9. #define LONG_TICKS (1000 / TICKS_INTERVAL)
  10. typedef void (*BtnCallback)(void*);
  11. typedef enum {
  12. PRESS_DOWN = 0,
  13. PRESS_UP,
  14. PRESS_REPEAT,
  15. SINGLE_CLICK,
  16. DOUBLE_CLICK,
  17. LONG_RRESS_START,
  18. LONG_PRESS_HOLD,
  19. number_of_event,
  20. NONE_PRESS
  21. }PressEvent;
  22. typedef struct button {
  23. uint16_t ticks;
  24. uint8_t repeat : 4;
  25. uint8_t event : 4;
  26. uint8_t state : 3;
  27. uint8_t debounce_cnt : 3;
  28. uint8_t active_level : 1;
  29. uint8_t button_level : 1;
  30. uint8_t (*hal_button_Level)(void);
  31. BtnCallback cb[number_of_event];
  32. struct button* next;
  33. }button;
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. void button_init(struct button* handle, uint8_t(*pin_level)(void), uint8_t active_level);
  38. void button_attach(struct button* handle, PressEvent event, BtnCallback cb);
  39. PressEvent get_button_event(struct button* handle);
  40. int button_start(struct button* handle);
  41. void button_stop(struct button* handle);
  42. void button_ticks(void);
  43. #ifdef __cplusplus
  44. }
  45. #endif
  46. #endif