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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. static const uint gpio_num_proto[NUM_BTNS] = {
  23. 8, // BTN_A
  24. 9, // BTN_B
  25. 12, // BTN_C
  26. // not existing on prototype
  27. 0xFF, // BTN_D
  28. 0xFF, // BTN_E
  29. 0xFF, // BTN_F
  30. 0xFF, // BTN_G
  31. 0xFF, // BTN_H
  32. 14, // BTN_REC
  33. 16, // BTN_CLICK
  34. };
  35. #define NUM_ROWS 3
  36. #define NUM_COLS 3
  37. static const uint gpio_num_v2[NUM_BTNS] = {
  38. // handled as matrix
  39. 0xFF, // BTN_A
  40. 0xFF, // BTN_B
  41. 0xFF, // BTN_C
  42. 0xFF, // BTN_D
  43. 0xFF, // BTN_E
  44. 0xFF, // BTN_F
  45. 0xFF, // BTN_G
  46. 0xFF, // BTN_H
  47. 0xFF, // BTN_REC
  48. 20, // BTN_CLICK
  49. };
  50. static const uint gpio_rows[NUM_ROWS] = {
  51. 0, 2, 3
  52. };
  53. static const uint gpio_cols[NUM_COLS] = {
  54. 1, 4, 5
  55. };
  56. struct button_state {
  57. uint32_t last_time;
  58. bool current_state, last_state;
  59. };
  60. static struct button_state buttons[NUM_BTNS];
  61. static void (*callback)(enum buttons, bool) = NULL;
  62. static int last_col = 0;
  63. void buttons_init(void) {
  64. for (uint i = 0; i < NUM_BTNS; i++) {
  65. uint n = 0xFF;
  66. if (hw_type == HW_PROTOTYPE) {
  67. n = gpio_num_proto[i];
  68. } else if (hw_type == HW_V2) {
  69. n = gpio_num_v2[i];
  70. }
  71. if (n >= 0xFF) {
  72. continue;
  73. }
  74. gpio_init(n);
  75. gpio_set_dir(n, GPIO_IN);
  76. gpio_pull_up(n);
  77. buttons[i].last_time = 0;
  78. buttons[i].current_state = false;
  79. buttons[i].last_state = false;
  80. }
  81. if (hw_type != HW_V2) {
  82. return;
  83. }
  84. for (uint i = 0; i < NUM_ROWS; i++) {
  85. uint n = gpio_rows[i];
  86. gpio_init(n);
  87. gpio_set_dir(n, GPIO_IN);
  88. gpio_pull_up(n);
  89. }
  90. for (uint i = 0; i < NUM_COLS; i++) {
  91. uint n = gpio_cols[i];
  92. gpio_init(n);
  93. gpio_set_dir(n, GPIO_IN);
  94. gpio_disable_pulls(n);
  95. }
  96. }
  97. void buttons_callback(void (*fp)(enum buttons, bool)) {
  98. callback = fp;
  99. }
  100. static void button_run_single(bool state, uint i) {
  101. uint32_t now = to_ms_since_boot(get_absolute_time());
  102. if (state != buttons[i].last_state) {
  103. buttons[i].last_time = now;
  104. }
  105. if ((now - buttons[i].last_time) > DEBOUNCE_DELAY_MS) {
  106. if (state != buttons[i].current_state) {
  107. printf("btn %d now %s\n", i, state ? "pressed" : "released");
  108. buttons[i].current_state = state;
  109. if (callback) {
  110. callback(i, state);
  111. }
  112. }
  113. }
  114. buttons[i].last_state = state;
  115. }
  116. static void button_run_proto(void) {
  117. for (uint i = 0; i < NUM_BTNS; i++) {
  118. if (gpio_num_proto[i] >= 0xFF) {
  119. continue;
  120. }
  121. bool state = !gpio_get(gpio_num_proto[i]);
  122. button_run_single(state, i);
  123. }
  124. }
  125. static void button_run_matrix(void) {
  126. gpio_set_dir(gpio_cols[last_col], GPIO_IN);
  127. last_col = (last_col + 1) % NUM_COLS;
  128. gpio_set_dir(gpio_cols[last_col], GPIO_OUT);
  129. gpio_put(gpio_cols[last_col], false);
  130. sleep_us(2);
  131. for (uint i = 0; i < NUM_ROWS; i++) {
  132. button_run_single(!gpio_get(gpio_rows[i]), last_col * NUM_ROWS + i);
  133. }
  134. }
  135. void buttons_run(void) {
  136. if (hw_type == HW_PROTOTYPE) {
  137. button_run_proto();
  138. } else if (hw_type == HW_V2) {
  139. button_run_matrix();
  140. button_run_single(!gpio_get(gpio_num_v2[BTN_CLICK]), BTN_CLICK);
  141. }
  142. }