multi_button.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "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 button_id;
  35. uint8_t (*hal_button_Level)(uint8_t button_id_);
  36. BtnCallback cb[number_of_event];
  37. struct Button* next;
  38. }Button;
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. void button_init(struct Button* handle, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t button_id);
  43. void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
  44. PressEvent get_button_event(struct Button* handle);
  45. int button_start(struct Button* handle);
  46. void button_stop(struct Button* handle);
  47. void button_ticks(void);
  48. #ifdef __cplusplus
  49. }
  50. #endif
  51. #endif