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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * buttons.c
  3. *
  4. * Copyright (c) 2022 - 2023 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include "pico/stdlib.h"
  19. #include "config.h"
  20. #include "controls.h"
  21. #include "buttons.h"
  22. #define BUTTONS_COUNT 4
  23. uint gpio_num[BUTTONS_COUNT] = { 21, 22, 26, 27 };
  24. struct button_state {
  25. uint32_t last_time;
  26. bool current_state, last_state;
  27. };
  28. struct button_state buttons[BUTTONS_COUNT];
  29. void buttons_init(void) {
  30. for (int i = 0; i < BUTTONS_COUNT; i++) {
  31. gpio_init(gpio_num[i]);
  32. gpio_set_dir(gpio_num[i], GPIO_IN);
  33. gpio_pull_up(gpio_num[i]);
  34. buttons[i].last_time = 0;
  35. buttons[i].current_state = false;
  36. buttons[i].last_state = false;
  37. }
  38. }
  39. void buttons_run(void) {
  40. for (int i = 0; i < BUTTONS_COUNT; i++) {
  41. bool state = !gpio_get(gpio_num[i]);
  42. uint32_t now = to_ms_since_boot(get_absolute_time());
  43. if (state != buttons[i].last_state) {
  44. buttons[i].last_time = now;
  45. }
  46. if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
  47. if (state != buttons[i].current_state) {
  48. buttons[i].current_state = state;
  49. controls_mouse_new(i, state);
  50. }
  51. }
  52. buttons[i].last_state = state;
  53. }
  54. }