example_poll.c 870 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "multi_button.h"
  2. unit8_t btn1_id = 0;
  3. struct Button btn1;
  4. uint8_t read_button_GPIO(uint8_t button_id)
  5. {
  6. // you can share the GPIO read function with multiple Buttons
  7. switch(button_id)
  8. {
  9. case btn1_id:
  10. return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
  11. default:
  12. return 0;
  13. }
  14. }
  15. int main()
  16. {
  17. static uint8_t btn1_event_val;
  18. button_init(&btn1, read_button_GPIO, 0, btn1_id);
  19. button_start(&btn1);
  20. //make the timer invoking the button_ticks() interval 5ms.
  21. //This function is implemented by yourself.
  22. __timer_start(button_ticks, 0, 5);
  23. while(1)
  24. {
  25. if(btn1_event_val != get_button_event(&btn1)) {
  26. btn1_event_val = get_button_event(&btn1);
  27. if(btn1_event_val == PRESS_DOWN) {
  28. //do something
  29. } else if(btn1_event_val == PRESS_UP) {
  30. //do something
  31. } else if(btn1_event_val == LONG_PRESS_HOLD) {
  32. //do something
  33. }
  34. }
  35. }
  36. }