matrix.c 6.2 KB

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