No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

buttons.c 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * buttons.c
  3. */
  4. #include "pico/stdlib.h"
  5. #include "config.h"
  6. #include "controls.h"
  7. #include "buttons.h"
  8. #define BUTTONS_COUNT 4
  9. uint gpio_num[BUTTONS_COUNT] = { 21, 22, 26, 27 };
  10. struct button_state {
  11. uint32_t last_time;
  12. bool current_state, last_state;
  13. };
  14. struct button_state buttons[BUTTONS_COUNT];
  15. void buttons_init(void) {
  16. for (int i = 0; i < BUTTONS_COUNT; i++) {
  17. gpio_init(gpio_num[i]);
  18. gpio_set_dir(gpio_num[i], GPIO_IN);
  19. gpio_pull_up(gpio_num[i]);
  20. buttons[i].last_time = 0;
  21. buttons[i].current_state = false;
  22. buttons[i].last_state = false;
  23. }
  24. }
  25. void buttons_run(void) {
  26. for (int i = 0; i < BUTTONS_COUNT; i++) {
  27. bool state = !gpio_get(gpio_num[i]);
  28. uint32_t now = to_ms_since_boot(get_absolute_time());
  29. if (state != buttons[i].last_state) {
  30. buttons[i].last_time = now;
  31. }
  32. if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
  33. if (state != buttons[i].current_state) {
  34. buttons[i].current_state = state;
  35. controls_mouse_new(i, state);
  36. }
  37. }
  38. buttons[i].last_state = state;
  39. }
  40. }