matrix.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  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. */
  14. #include "matrix.h"
  15. #include "quantum.h"
  16. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  17. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  18. static void select_row(uint8_t row)
  19. {
  20. setPinOutput(row_pins[row]);
  21. writePinLow(row_pins[row]);
  22. }
  23. static void unselect_row(uint8_t row)
  24. {
  25. setPinInputHigh(row_pins[row]);
  26. }
  27. static void unselect_rows(void)
  28. {
  29. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  30. setPinInputHigh(row_pins[x]);
  31. }
  32. }
  33. static void select_col(uint8_t col)
  34. {
  35. setPinOutput(col_pins[col]);
  36. writePinLow(col_pins[col]);
  37. }
  38. static void unselect_col(uint8_t col)
  39. {
  40. setPinInputHigh(col_pins[col]);
  41. }
  42. static void unselect_cols(void)
  43. {
  44. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  45. setPinInputHigh(col_pins[x]);
  46. }
  47. }
  48. static void init_pins(void) {
  49. unselect_rows();
  50. unselect_cols();
  51. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  52. setPinInputHigh(col_pins[x]);
  53. }
  54. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  55. setPinInputHigh(row_pins[x]);
  56. }
  57. }
  58. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  59. {
  60. // Store last value of row prior to reading
  61. matrix_row_t last_row_value = current_matrix[current_row];
  62. // Clear data in matrix row
  63. current_matrix[current_row] = 0;
  64. // Select row and wait for row selecton to stabilize
  65. select_row(current_row);
  66. matrix_io_delay();
  67. // For each col...
  68. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  69. // Select the col pin to read (active low)
  70. uint8_t pin_state = readPin(col_pins[col_index]);
  71. // Populate the matrix row with the state of the col pin
  72. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  73. }
  74. // Unselect row
  75. unselect_row(current_row);
  76. return (last_row_value != current_matrix[current_row]);
  77. }
  78. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  79. {
  80. bool matrix_changed = false;
  81. // Select col and wait for col selecton to stabilize
  82. select_col(current_col);
  83. matrix_io_delay();
  84. // For each row...
  85. for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++)
  86. {
  87. uint8_t tmp = row_index + MATRIX_ROWS/2;
  88. // Store last value of row prior to reading
  89. matrix_row_t last_row_value = current_matrix[tmp];
  90. // Check row pin state
  91. if (readPin(row_pins[row_index]) == 0)
  92. {
  93. // Pin LO, set col bit
  94. current_matrix[tmp] |= (MATRIX_ROW_SHIFTER << current_col);
  95. }
  96. else
  97. {
  98. // Pin HI, clear col bit
  99. current_matrix[tmp] &= ~(MATRIX_ROW_SHIFTER << current_col);
  100. }
  101. // Determine if the matrix changed state
  102. if ((last_row_value != current_matrix[tmp]) && !(matrix_changed))
  103. {
  104. matrix_changed = true;
  105. }
  106. }
  107. // Unselect col
  108. unselect_col(current_col);
  109. return matrix_changed;
  110. }
  111. void matrix_init_custom(void) {
  112. // initialize key pins
  113. init_pins();
  114. }
  115. bool matrix_scan_custom(matrix_row_t current_matrix[])
  116. {
  117. bool changed = false;
  118. // Set row, read cols
  119. for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
  120. changed |= read_cols_on_row(current_matrix, current_row);
  121. }
  122. //else
  123. // Set col, read rows
  124. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  125. changed |= read_rows_on_col(current_matrix, current_col);
  126. }
  127. return changed;
  128. }