Açıklama Yok
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

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