matrix.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. static inline uint8_t readMatrixPin(pin_t pin) {
  48. if (pin != NO_PIN) {
  49. return readPin(pin);
  50. } else {
  51. return 1;
  52. }
  53. }
  54. // matrix code
  55. #ifdef DIRECT_PINS
  56. __attribute__((weak)) void matrix_init_pins(void) {
  57. for (int row = 0; row < MATRIX_ROWS; row++) {
  58. for (int col = 0; col < MATRIX_COLS; col++) {
  59. pin_t pin = direct_pins[row][col];
  60. if (pin != NO_PIN) {
  61. setPinInputHigh(pin);
  62. }
  63. }
  64. }
  65. }
  66. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  67. // Start with a clear matrix row
  68. matrix_row_t current_row_value = 0;
  69. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  70. pin_t pin = direct_pins[current_row][col_index];
  71. if (pin != NO_PIN) {
  72. current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  73. }
  74. }
  75. // Update the matrix
  76. current_matrix[current_row] = current_row_value;
  77. }
  78. #elif defined(DIODE_DIRECTION)
  79. # if defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
  80. # if (DIODE_DIRECTION == COL2ROW)
  81. static bool select_row(uint8_t row) {
  82. pin_t pin = row_pins[row];
  83. if (pin != NO_PIN) {
  84. setPinOutput_writeLow(pin);
  85. return true;
  86. }
  87. return false;
  88. }
  89. static void unselect_row(uint8_t row) {
  90. pin_t pin = row_pins[row];
  91. if (pin != NO_PIN) {
  92. setPinInputHigh_atomic(pin);
  93. }
  94. }
  95. static void unselect_rows(void) {
  96. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  97. unselect_row(x);
  98. }
  99. }
  100. __attribute__((weak)) void matrix_init_pins(void) {
  101. unselect_rows();
  102. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  103. if (col_pins[x] != NO_PIN) {
  104. setPinInputHigh_atomic(col_pins[x]);
  105. }
  106. }
  107. }
  108. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  109. // Start with a clear matrix row
  110. matrix_row_t current_row_value = 0;
  111. if (!select_row(current_row)) { // Select row
  112. return; // skip NO_PIN row
  113. }
  114. matrix_output_select_delay();
  115. // For each col...
  116. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  117. uint8_t pin_state = readMatrixPin(col_pins[col_index]);
  118. // Populate the matrix row with the state of the col pin
  119. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  120. }
  121. // Unselect row
  122. unselect_row(current_row);
  123. matrix_output_unselect_delay(); // wait for all Col signals to go HIGH
  124. // Update the matrix
  125. current_matrix[current_row] = current_row_value;
  126. }
  127. # elif (DIODE_DIRECTION == ROW2COL)
  128. static bool select_col(uint8_t col) {
  129. pin_t pin = col_pins[col];
  130. if (pin != NO_PIN) {
  131. setPinOutput_writeLow(pin);
  132. return true;
  133. }
  134. return false;
  135. }
  136. static void unselect_col(uint8_t col) {
  137. pin_t pin = col_pins[col];
  138. if (pin != NO_PIN) {
  139. setPinInputHigh_atomic(pin);
  140. }
  141. }
  142. static void unselect_cols(void) {
  143. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  144. unselect_col(x);
  145. }
  146. }
  147. __attribute__((weak)) void matrix_init_pins(void) {
  148. unselect_cols();
  149. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  150. if (row_pins[x] != NO_PIN) {
  151. setPinInputHigh_atomic(row_pins[x]);
  152. }
  153. }
  154. }
  155. __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  156. // Select col
  157. if (!select_col(current_col)) { // select col
  158. return; // skip NO_PIN col
  159. }
  160. matrix_output_select_delay();
  161. // For each row...
  162. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  163. // Check row pin state
  164. if (readMatrixPin(row_pins[row_index]) == 0) {
  165. // Pin LO, set col bit
  166. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  167. } else {
  168. // Pin HI, clear col bit
  169. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  170. }
  171. }
  172. // Unselect col
  173. unselect_col(current_col);
  174. matrix_output_unselect_delay(); // wait for all Row signals to go HIGH
  175. }
  176. # else
  177. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  178. # endif
  179. # endif // defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
  180. #else
  181. # error DIODE_DIRECTION is not defined!
  182. #endif
  183. void matrix_init(void) {
  184. // initialize key pins
  185. matrix_init_pins();
  186. // initialize matrix state: all keys off
  187. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  188. raw_matrix[i] = 0;
  189. matrix[i] = 0;
  190. }
  191. debounce_init(MATRIX_ROWS);
  192. matrix_init_quantum();
  193. }
  194. uint8_t matrix_scan(void) {
  195. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  196. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  197. // Set row, read cols
  198. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  199. matrix_read_cols_on_row(curr_matrix, current_row);
  200. }
  201. #elif (DIODE_DIRECTION == ROW2COL)
  202. // Set col, read rows
  203. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  204. matrix_read_rows_on_col(curr_matrix, current_col);
  205. }
  206. #endif
  207. bool changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  208. if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix));
  209. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  210. matrix_scan_quantum();
  211. return (uint8_t)changed;
  212. }