matrix.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright 2012-2023 Jun Wako <wakojun@gmail.com> Kyrremann <kyrremann@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. Copied from here: https://github.com/e3w2q/qmk_firmware/blob/762fe3e0a7cbea768245a75520f06ff5a2f00b9f/keyboards/2x3test/matrix.c
  14. */
  15. /*
  16. * scan matrix
  17. */
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include "wait.h"
  21. #include "util.h"
  22. #include "matrix.h"
  23. #include "quantum.h"
  24. static const pin_t row_pins[] = MATRIX_ROW_PINS;
  25. static const pin_t col_pins[] = MATRIX_COL_PINS;
  26. static void select_row(uint8_t row) {
  27. gpio_set_pin_output(row_pins[row]);
  28. gpio_write_pin_low(row_pins[row]);
  29. }
  30. static void unselect_row(uint8_t row) {
  31. gpio_set_pin_input_high(row_pins[row]);
  32. }
  33. static void unselect_rows(void) {
  34. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  35. gpio_set_pin_input_high(row_pins[x]);
  36. }
  37. }
  38. static void select_col(uint8_t col) {
  39. gpio_set_pin_output(col_pins[col]);
  40. gpio_write_pin_low(col_pins[col]);
  41. }
  42. static void unselect_col(uint8_t col) {
  43. gpio_set_pin_input_high(col_pins[col]);
  44. }
  45. static void unselect_cols(void) {
  46. for (uint8_t x = 0; x < MATRIX_COLS/2; x++) {
  47. gpio_set_pin_input_high(col_pins[x*2]);
  48. }
  49. }
  50. static void init_pins(void) {
  51. unselect_cols();
  52. unselect_rows();
  53. }
  54. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  55. // Store last value of row prior to reading
  56. matrix_row_t last_row_value = current_matrix[current_row];
  57. // Select row and wait for row selection to stabilize
  58. select_row(current_row);
  59. wait_us(30);
  60. // For each col...
  61. for (uint8_t col_index = 0; col_index < MATRIX_COLS / 2; col_index++) {
  62. uint16_t column_index_bitmask = MATRIX_ROW_SHIFTER << ((col_index * 2) + 1);
  63. // Check row pin state
  64. if (gpio_read_pin(col_pins[col_index*2])) {
  65. // Pin HI, clear col bit
  66. current_matrix[current_row] &= ~column_index_bitmask;
  67. } else {
  68. // Pin LO, set col bit
  69. current_matrix[current_row] |= column_index_bitmask;
  70. }
  71. }
  72. // Unselect row
  73. unselect_row(current_row);
  74. return (last_row_value != current_matrix[current_row]);
  75. }
  76. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  77. bool matrix_changed = false;
  78. // Select col and wait for col selection to stabilize
  79. select_col(current_col*2);
  80. wait_us(30);
  81. // For each row...
  82. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  83. // Store last value of row prior to reading
  84. matrix_row_t last_row_value = current_matrix[row_index];
  85. uint16_t column_index_bitmask = MATRIX_ROW_SHIFTER << (current_col * 2);
  86. // Check row pin state
  87. if (gpio_read_pin(row_pins[row_index])) {
  88. // Pin HI, clear col bit
  89. current_matrix[row_index] &= ~column_index_bitmask;
  90. } else {
  91. // Pin LO, set col bit
  92. current_matrix[row_index] |= column_index_bitmask;
  93. }
  94. // Determine if the matrix changed state
  95. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  96. matrix_changed = true;
  97. }
  98. }
  99. // Unselect col
  100. unselect_col(current_col*2);
  101. return matrix_changed;
  102. }
  103. void matrix_init_custom(void) {
  104. // initialize key pins
  105. init_pins();
  106. }
  107. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  108. bool changed = false;
  109. // Set row, read cols
  110. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  111. changed |= read_cols_on_row(current_matrix, current_row);
  112. }
  113. // Set col, read rows
  114. for (uint8_t current_col = 0; current_col < MATRIX_COLS/2; current_col++) {
  115. changed |= read_rows_on_col(current_matrix, current_col);
  116. }
  117. return changed;
  118. }