event_async.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "multi_button.h"
  2. struct Button btn1;
  3. struct Button btn2;
  4. int read_button1_GPIO()
  5. {
  6. return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
  7. }
  8. int read_button2_GPIO()
  9. {
  10. return HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin);
  11. }
  12. int main()
  13. {
  14. button_init(&btn1, read_button1_GPIO, 0);
  15. button_init(&btn2, read_button2_GPIO, 0);
  16. button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler);
  17. button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);
  18. button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);
  19. button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler);
  20. button_attach(&btn1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler);
  21. button_attach(&btn1, LONG_RRESS_START, BTN1_LONG_RRESS_START_Handler);
  22. button_attach(&btn2, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
  23. button_attach(&btn2, PRESS_DOWN, BTN2_PRESS_DOWN_Handler);
  24. button_attach(&btn2, PRESS_UP, BTN2_PRESS_UP_Handler);
  25. button_attach(&btn2, PRESS_REPEAT, BTN2_PRESS_REPEAT_Handler);
  26. button_attach(&btn2, SINGLE_CLICK, BTN2_SINGLE_Click_Handler);
  27. button_attach(&btn2, DOUBLE_CLICK, BTN2_DOUBLE_Click_Handler);
  28. button_attach(&btn2, LONG_RRESS_START, BTN2_LONG_RRESS_START_Handler);
  29. button_attach(&btn2, LONG_PRESS_HOLD, BTN2_LONG_PRESS_HOLD_Handler);
  30. button_start(&btn1);
  31. button_start(&btn2);
  32. //make the timer invoking the button_ticks() interval 5ms.
  33. //This function is implemented by yourself.
  34. __timer_start(button_ticks, 0, 5);
  35. while(1)
  36. {}
  37. }
  38. void BTN1_PRESS_DOWN_Handler(void* btn)
  39. {
  40. //do something...
  41. }
  42. void BTN1_PRESS_UP_Handler(void* btn)
  43. {
  44. //do something...
  45. }
  46. ...