S&B Volcano vaporizer remote control with Pi Pico W
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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "buttons.h"
  21. static const uint gpio_num[NUM_BTNS] = {
  22. 15, // BTN_A
  23. 17, // BTN_B
  24. 19, // BTN_X
  25. 21, // BTN_Y
  26. 2, // BTN_UP
  27. 18, // BTN_DOWN
  28. 16, // BTN_LEFT
  29. 20, // BTN_RIGHT
  30. 3, // BTN_ENTER
  31. };
  32. struct button_state {
  33. uint32_t last_time;
  34. bool current_state, last_state;
  35. };
  36. static struct button_state buttons[NUM_BTNS];
  37. void buttons_init(void) {
  38. for (uint i = 0; i < NUM_BTNS; i++) {
  39. gpio_init(gpio_num[i]);
  40. gpio_set_dir(gpio_num[i], GPIO_IN);
  41. gpio_pull_up(gpio_num[i]);
  42. buttons[i].last_time = 0;
  43. buttons[i].current_state = false;
  44. buttons[i].last_state = false;
  45. }
  46. }
  47. void buttons_run(void) {
  48. for (uint i = 0; i < NUM_BTNS; i++) {
  49. bool state = !gpio_get(gpio_num[i]);
  50. uint32_t now = to_ms_since_boot(get_absolute_time());
  51. if (state != buttons[i].last_state) {
  52. buttons[i].last_time = now;
  53. }
  54. if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
  55. if (state != buttons[i].current_state) {
  56. buttons[i].current_state = state;
  57. }
  58. }
  59. buttons[i].last_state = state;
  60. }
  61. }