matrix.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <string.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. # ifdef MATRIX_ROW_PINS
  25. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  26. # endif // MATRIX_ROW_PINS
  27. # ifdef MATRIX_COL_PINS
  28. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  29. # endif // MATRIX_COL_PINS
  30. #endif
  31. /* matrix state(1:on, 0:off) */
  32. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  33. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  34. // user-defined overridable functions
  35. __attribute__((weak)) void matrix_init_pins(void);
  36. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  37. __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  38. static inline void setPinOutput_writeLow(pin_t pin) {
  39. ATOMIC_BLOCK_FORCEON {
  40. setPinOutput(pin);
  41. writePinLow(pin);
  42. }
  43. }
  44. static inline void setPinInputHigh_atomic(pin_t pin) {
  45. ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
  46. }
  47. // matrix code
  48. #ifdef DIRECT_PINS
  49. __attribute__((weak)) void matrix_init_pins(void) {
  50. for (int row = 0; row < MATRIX_ROWS; row++) {
  51. for (int col = 0; col < MATRIX_COLS; col++) {
  52. pin_t pin = direct_pins[row][col];
  53. if (pin != NO_PIN) {
  54. setPinInputHigh(pin);
  55. }
  56. }
  57. }
  58. }
  59. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  60. // Start with a clear matrix row
  61. matrix_row_t current_row_value = 0;
  62. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  63. pin_t pin = direct_pins[current_row][col_index];
  64. if (pin != NO_PIN) {
  65. current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  66. }
  67. }
  68. // Update the matrix
  69. current_matrix[current_row] = current_row_value;
  70. }
  71. #elif defined(DIODE_DIRECTION)
  72. # if defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
  73. # if (DIODE_DIRECTION == COL2ROW)
  74. static void select_row(uint8_t row) { setPinOutput_writeLow(row_pins[row]); }
  75. static void unselect_row(uint8_t row) { setPinInputHigh_atomic(row_pins[row]); }
  76. static void unselect_rows(void) {
  77. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  78. setPinInputHigh_atomic(row_pins[x]);
  79. }
  80. }
  81. __attribute__((weak)) void matrix_init_pins(void) {
  82. unselect_rows();
  83. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  84. setPinInputHigh_atomic(col_pins[x]);
  85. }
  86. }
  87. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  88. // Start with a clear matrix row
  89. matrix_row_t current_row_value = 0;
  90. // Select row
  91. select_row(current_row);
  92. matrix_output_select_delay();
  93. // For each col...
  94. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  95. // Select the col pin to read (active low)
  96. uint8_t pin_state = readPin(col_pins[col_index]);
  97. // Populate the matrix row with the state of the col pin
  98. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  99. }
  100. // Unselect row
  101. unselect_row(current_row);
  102. matrix_output_unselect_delay(); // wait for all Col signals to go HIGH
  103. // Update the matrix
  104. current_matrix[current_row] = current_row_value;
  105. }
  106. # elif (DIODE_DIRECTION == ROW2COL)
  107. static void select_col(uint8_t col) { setPinOutput_writeLow(col_pins[col]); }
  108. static void unselect_col(uint8_t col) { setPinInputHigh_atomic(col_pins[col]); }
  109. static void unselect_cols(void) {
  110. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  111. setPinInputHigh_atomic(col_pins[x]);
  112. }
  113. }
  114. __attribute__((weak)) void matrix_init_pins(void) {
  115. unselect_cols();
  116. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  117. setPinInputHigh_atomic(row_pins[x]);
  118. }
  119. }
  120. __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  121. // Select col
  122. select_col(current_col);
  123. matrix_output_select_delay();
  124. // For each row...
  125. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  126. // Check row pin state
  127. if (readPin(row_pins[row_index]) == 0) {
  128. // Pin LO, set col bit
  129. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  130. } else {
  131. // Pin HI, clear col bit
  132. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  133. }
  134. }
  135. // Unselect col
  136. unselect_col(current_col);
  137. matrix_output_unselect_delay(); // wait for all Row signals to go HIGH
  138. }
  139. # else
  140. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  141. # endif
  142. # endif // defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
  143. #else
  144. # error DIODE_DIRECTION is not defined!
  145. #endif
  146. void matrix_init(void) {
  147. // initialize key pins
  148. matrix_init_pins();
  149. // initialize matrix state: all keys off
  150. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  151. raw_matrix[i] = 0;
  152. matrix[i] = 0;
  153. }
  154. debounce_init(MATRIX_ROWS);
  155. matrix_init_quantum();
  156. }
  157. uint8_t matrix_scan(void) {
  158. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  159. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  160. // Set row, read cols
  161. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  162. matrix_read_cols_on_row(curr_matrix, current_row);
  163. }
  164. #elif (DIODE_DIRECTION == ROW2COL)
  165. // Set col, read rows
  166. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  167. matrix_read_rows_on_col(curr_matrix, current_col);
  168. }
  169. #endif
  170. bool changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  171. if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix));
  172. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  173. matrix_scan_quantum();
  174. return (uint8_t)changed;
  175. }