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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * buttons.c
  3. *
  4. * Copyright (c) 2024 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 <stdio.h>
  19. #include "pico/stdlib.h"
  20. #include "main.h"
  21. #include "buttons.h"
  22. #define DEBOUNCE_DELAY_MS 50
  23. static const uint gpio_num_proto[NUM_BTNS] = {
  24. 8, // BTN_A
  25. 9, // BTN_B
  26. 12, // BTN_C
  27. // not existing on prototype
  28. 0xFF, // BTN_D
  29. 0xFF, // BTN_E
  30. 0xFF, // BTN_F
  31. 0xFF, // BTN_G
  32. 0xFF, // BTN_H
  33. 14, // BTN_REC
  34. 16, // BTN_CLICK
  35. };
  36. static const uint gpio_num_v2[NUM_BTNS] = {
  37. 1, // BTN_A / COL 0
  38. 4, // BTN_B / COL 1
  39. 5, // BTN_C / COL 2
  40. 0, // BTN_D / ROW 0
  41. 2, // BTN_E / ROW 1
  42. 3, // BTN_F / ROW 2
  43. 0xFF, // BTN_G
  44. 0xFF, // BTN_H
  45. 0xFF, // BTN_REC
  46. 20, // BTN_CLICK
  47. };
  48. struct button_state {
  49. uint32_t last_time;
  50. bool current_state, last_state;
  51. };
  52. static struct button_state buttons[NUM_BTNS];
  53. static void (*callback)(enum buttons, bool) = NULL;
  54. void buttons_init(void) {
  55. for (uint i = 0; i < NUM_BTNS; i++) {
  56. uint n = 0xFF;
  57. if (hw_type == HW_PROTOTYPE) {
  58. n = gpio_num_proto[i];
  59. } else if (hw_type == HW_V2) {
  60. n = gpio_num_v2[i];
  61. }
  62. if (n >= 0xFF) {
  63. continue;
  64. }
  65. gpio_init(n);
  66. gpio_set_dir(n, GPIO_IN);
  67. gpio_pull_up(n);
  68. buttons[i].last_time = 0;
  69. buttons[i].current_state = false;
  70. buttons[i].last_state = false;
  71. }
  72. }
  73. void buttons_callback(void (*fp)(enum buttons, bool)) {
  74. callback = fp;
  75. }
  76. static void button_run_single(bool state, uint i) {
  77. uint32_t now = to_ms_since_boot(get_absolute_time());
  78. if (state != buttons[i].last_state) {
  79. buttons[i].last_time = now;
  80. }
  81. if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
  82. if (state != buttons[i].current_state) {
  83. printf("btn %d now %s\n", i, state ? "pressed" : "released");
  84. buttons[i].current_state = state;
  85. if (callback) {
  86. callback(i, state);
  87. }
  88. }
  89. }
  90. buttons[i].last_state = state;
  91. }
  92. static void buttons_run_proto(void) {
  93. for (uint i = 0; i < NUM_BTNS; i++) {
  94. if (gpio_num_proto[i] >= 0xFF) {
  95. continue;
  96. }
  97. bool state = !gpio_get(gpio_num_proto[i]);
  98. button_run_single(state, i);
  99. }
  100. }
  101. void buttons_run(void) {
  102. if (hw_type == HW_PROTOTYPE) {
  103. buttons_run_proto();
  104. } else if (hw_type == HW_V2) {
  105. // TODO read matrix
  106. button_run_single(!gpio_get(gpio_num_v2[BTN_CLICK]), BTN_CLICK);
  107. }
  108. }