example_callback.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "multi_button.h"
  2. enum Button_IDs {
  3. btn1_id,
  4. btn2_id,
  5. };
  6. struct Button btn1;
  7. struct Button btn2;
  8. uint8_t read_button_GPIO(uint8_t button_id)
  9. {
  10. // you can share the GPIO read function with multiple Buttons
  11. switch(button_id)
  12. {
  13. case btn1_id:
  14. return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
  15. case btn2_id:
  16. return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin);
  17. default:
  18. return 0;
  19. }
  20. }
  21. int main()
  22. {
  23. button_init(&btn1, read_button_GPIO, 0, btn1_id);
  24. button_init(&btn2, read_button_GPIO, 0, btn2_id);
  25. button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler);
  26. button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);
  27. button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);
  28. button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler);
  29. button_attach(&btn1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler);
  30. button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_PRESS_START_Handler);
  31. button_attach(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
  32. button_attach(&btn2, PRESS_DOWN, BTN2_PRESS_DOWN_Handler);
  33. button_attach(&btn2, PRESS_UP, BTN2_PRESS_UP_Handler);
  34. button_attach(&btn2, PRESS_REPEAT, BTN2_PRESS_REPEAT_Handler);
  35. button_attach(&btn2, SINGLE_CLICK, BTN2_SINGLE_Click_Handler);
  36. button_attach(&btn2, DOUBLE_CLICK, BTN2_DOUBLE_Click_Handler);
  37. button_attach(&btn2, LONG_PRESS_START, BTN2_LONG_PRESS_START_Handler);
  38. button_attach(&btn2, LONG_PRESS_HOLD, BTN2_LONG_PRESS_HOLD_Handler);
  39. button_start(&btn1);
  40. button_start(&btn2);
  41. //make the timer invoking the button_ticks() interval 5ms.
  42. //This function is implemented by yourself.
  43. __timer_start(button_ticks, 0, 5);
  44. while(1)
  45. {}
  46. }
  47. void BTN1_PRESS_DOWN_Handler(void* btn)
  48. {
  49. //do something...
  50. }
  51. void BTN1_PRESS_UP_Handler(void* btn)
  52. {
  53. //do something...
  54. }