multi_button.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) 2016 Zibin Zheng <[email protected]>
  3. * All rights reserved
  4. */
  5. #ifndef _MULTI_BUTTON_H_
  6. #define _MULTI_BUTTON_H_
  7. //#include "stdint.h"
  8. #include "TypeDef.h"
  9. #include "string.h"
  10. //According to your need to modify the constants.
  11. #define TICKS_INTERVAL 6 //ms
  12. #define DEBOUNCE_TICKS 3 //MAX 8
  13. #define SHORT_TICKS (120 /TICKS_INTERVAL)
  14. #define LONG_TICKS (2000 /TICKS_INTERVAL)
  15. #define LONG_TICKS_TRIG (120 /TICKS_INTERVAL) //³¤°´,ÖÜÆÚÐÔµ÷ÓÃ
  16. typedef void (*BtnCallback)(void*);
  17. typedef enum {
  18. PRESS_DOWN = 0,
  19. PRESS_UP,
  20. PRESS_REPEAT,
  21. SINGLE_CLICK,
  22. DOUBLE_CLICK,
  23. LONG_RRESS_START,
  24. LONG_PRESS_HOLD,
  25. number_of_event,
  26. NONE_PRESS
  27. }PressEvent;
  28. typedef struct Button {
  29. uint16_t ticks;
  30. uint8_t repeat : 4;
  31. uint8_t event : 4;
  32. uint8_t state : 3;
  33. uint8_t debounce_cnt : 3;
  34. uint8_t active_level : 1;
  35. uint8_t button_level : 1;
  36. uint8_t (*hal_button_Level)(void);
  37. BtnCallback cb[number_of_event];
  38. struct Button* next;
  39. }Button;
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. void button_init_Head(void);
  44. void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level);
  45. void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
  46. PressEvent get_button_event(struct Button* handle);
  47. int button_start(struct Button* handle);
  48. void button_stop(struct Button* handle);
  49. void button_ticks(void);
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #endif