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 960B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 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_pull_up(gpio_num[i]);
  19. }
  20. }
  21. void buttons_run(void) {
  22. for (int i = 0; i < BUTTONS_COUNT; i++) {
  23. bool state = gpio_get(gpio_num[i]);
  24. uint32_t now = to_ms_since_boot(get_absolute_time());
  25. if (state != buttons[i].last_state) {
  26. buttons[i].last_time = now;
  27. }
  28. if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
  29. if (state != buttons[i].last_state) {
  30. buttons[i].last_state = state;
  31. controls_new(i, state);
  32. }
  33. }
  34. }
  35. }