MultiButton.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2016 Zibin Zheng <znbin@qq.com>
  3. * All rights reserved
  4. */
  5. #ifndef _MULTI_BUTTON_H_
  6. #define _MULTI_BUTTON_H_
  7. #include "stdint.h"
  8. #include "string.h"
  9. //According to your need to modify the constants.
  10. #define TICKS_INTERVAL 5 //ms
  11. #define DEBOUNCE_TICKS 3 //MAX 8
  12. #define SHORT_TICKS (300 /TICKS_INTERVAL)
  13. #define LONG_TICKS (1000 /TICKS_INTERVAL)
  14. typedef void (*BtnCallback)(void*);
  15. typedef enum {
  16. PRESS_DOWN = 0,
  17. PRESS_UP,
  18. PRESS_REPEAT,
  19. SINGLE_CLICK,
  20. DOUBLE_CLICK,
  21. LONG_PRESS_START,
  22. LONG_PRESS_HOLD,
  23. number_of_event,
  24. NONE_PRESS
  25. }PressEvent;
  26. typedef struct Button {
  27. uint16_t 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. struct Button* next;
  37. }Button;
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level);
  42. void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
  43. PressEvent get_button_event(struct Button* handle);
  44. int button_start(struct Button* handle);
  45. void button_stop(struct Button* handle);
  46. void button_ticks(void);
  47. #ifdef __cplusplus
  48. }
  49. #endif
  50. #endif