matrix.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 <stdint.h>
  15. #include <stdbool.h>
  16. #include "wait.h"
  17. #include "util.h"
  18. #include "matrix.h"
  19. #include "debounce.h"
  20. #include "quantum.h"
  21. #ifdef DIRECT_PINS
  22. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  23. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  24. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  25. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  26. #endif
  27. /* matrix state(1:on, 0:off) */
  28. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  29. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  30. // matrix code
  31. #ifdef DIRECT_PINS
  32. static void init_pins(void) {
  33. for (int row = 0; row < MATRIX_ROWS; row++) {
  34. for (int col = 0; col < MATRIX_COLS; col++) {
  35. pin_t pin = direct_pins[row][col];
  36. if (pin != NO_PIN) {
  37. setPinInputHigh(pin);
  38. }
  39. }
  40. }
  41. }
  42. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  43. matrix_row_t last_row_value = current_matrix[current_row];
  44. current_matrix[current_row] = 0;
  45. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  46. pin_t pin = direct_pins[current_row][col_index];
  47. if (pin != NO_PIN) {
  48. current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  49. }
  50. }
  51. return (last_row_value != current_matrix[current_row]);
  52. }
  53. #elif (DIODE_DIRECTION == COL2ROW)
  54. static void select_row(uint8_t row) {
  55. setPinOutput(row_pins[row]);
  56. writePinLow(row_pins[row]);
  57. }
  58. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  59. static void unselect_rows(void) {
  60. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  61. setPinInputHigh(row_pins[x]);
  62. }
  63. }
  64. static void init_pins(void) {
  65. unselect_rows();
  66. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  67. setPinInputHigh(col_pins[x]);
  68. }
  69. }
  70. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  71. // Store last value of row prior to reading
  72. matrix_row_t last_row_value = current_matrix[current_row];
  73. // Clear data in matrix row
  74. current_matrix[current_row] = 0;
  75. // Select row and wait for row selecton to stabilize
  76. select_row(current_row);
  77. wait_us(30);
  78. // For each col...
  79. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  80. // Select the col pin to read (active low)
  81. uint8_t pin_state = readPin(col_pins[col_index]);
  82. // Populate the matrix row with the state of the col pin
  83. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  84. }
  85. // Unselect row
  86. unselect_row(current_row);
  87. return (last_row_value != current_matrix[current_row]);
  88. }
  89. #elif (DIODE_DIRECTION == ROW2COL)
  90. static void select_col(uint8_t col) {
  91. setPinOutput(col_pins[col]);
  92. writePinLow(col_pins[col]);
  93. }
  94. static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
  95. static void unselect_cols(void) {
  96. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  97. setPinInputHigh(col_pins[x]);
  98. }
  99. }
  100. static void init_pins(void) {
  101. unselect_cols();
  102. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  103. setPinInputHigh(row_pins[x]);
  104. }
  105. }
  106. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  107. bool matrix_changed = false;
  108. // Select col and wait for col selecton to stabilize
  109. select_col(current_col);
  110. wait_us(30);
  111. // For each row...
  112. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  113. // Store last value of row prior to reading
  114. matrix_row_t last_row_value = current_matrix[row_index];
  115. // Check row pin state
  116. if (readPin(row_pins[row_index]) == 0) {
  117. // Pin LO, set col bit
  118. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  119. } else {
  120. // Pin HI, clear col bit
  121. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  122. }
  123. // Determine if the matrix changed state
  124. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  125. matrix_changed = true;
  126. }
  127. }
  128. // Unselect col
  129. unselect_col(current_col);
  130. return matrix_changed;
  131. }
  132. #endif
  133. void matrix_init(void) {
  134. // initialize key pins
  135. init_pins();
  136. // initialize matrix state: all keys off
  137. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  138. raw_matrix[i] = 0;
  139. matrix[i] = 0;
  140. }
  141. debounce_init(MATRIX_ROWS);
  142. matrix_init_quantum();
  143. }
  144. uint8_t matrix_scan(void) {
  145. bool changed = false;
  146. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  147. // Set row, read cols
  148. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  149. changed |= read_cols_on_row(raw_matrix, current_row);
  150. }
  151. #elif (DIODE_DIRECTION == ROW2COL)
  152. // Set col, read rows
  153. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  154. changed |= read_rows_on_col(raw_matrix, current_col);
  155. }
  156. #endif
  157. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  158. matrix_scan_quantum();
  159. return (uint8_t)changed;
  160. }