matrix.c 4.3 KB

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