matrix.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "util.h"
  17. #include "matrix.h"
  18. #include "debounce.h"
  19. #include "quantum.h"
  20. #ifdef DIRECT_PINS
  21. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  22. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  23. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  24. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  25. #endif
  26. /* matrix state(1:on, 0:off) */
  27. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  28. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  29. static inline void setPinOutput_writeLow(pin_t pin) {
  30. ATOMIC_BLOCK_FORCEON {
  31. setPinOutput(pin);
  32. writePinLow(pin);
  33. }
  34. }
  35. static inline void setPinInputHigh_atomic(pin_t pin) {
  36. ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
  37. }
  38. // matrix code
  39. #ifdef DIRECT_PINS
  40. static void init_pins(void) {
  41. for (int row = 0; row < MATRIX_ROWS; row++) {
  42. for (int col = 0; col < MATRIX_COLS; col++) {
  43. pin_t pin = direct_pins[row][col];
  44. if (pin != NO_PIN) {
  45. setPinInputHigh(pin);
  46. }
  47. }
  48. }
  49. }
  50. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  51. // Start with a clear matrix row
  52. matrix_row_t current_row_value = 0;
  53. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  54. pin_t pin = direct_pins[current_row][col_index];
  55. if (pin != NO_PIN) {
  56. current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  57. }
  58. }
  59. // If the row has changed, store the row and return the changed flag.
  60. if (current_matrix[current_row] != current_row_value) {
  61. current_matrix[current_row] = current_row_value;
  62. return true;
  63. }
  64. return false;
  65. }
  66. #elif defined(DIODE_DIRECTION)
  67. # if (DIODE_DIRECTION == COL2ROW)
  68. static void select_row(uint8_t row) { setPinOutput_writeLow(row_pins[row]); }
  69. static void unselect_row(uint8_t row) { setPinInputHigh_atomic(row_pins[row]); }
  70. static void unselect_rows(void) {
  71. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  72. setPinInputHigh_atomic(row_pins[x]);
  73. }
  74. }
  75. static void init_pins(void) {
  76. unselect_rows();
  77. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  78. setPinInputHigh_atomic(col_pins[x]);
  79. }
  80. }
  81. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  82. // Start with a clear matrix row
  83. matrix_row_t current_row_value = 0;
  84. // Select row
  85. select_row(current_row);
  86. matrix_output_select_delay();
  87. // For each col...
  88. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  89. // Select the col pin to read (active low)
  90. uint8_t pin_state = readPin(col_pins[col_index]);
  91. // Populate the matrix row with the state of the col pin
  92. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  93. }
  94. // Unselect row
  95. unselect_row(current_row);
  96. if (current_row + 1 < MATRIX_ROWS) {
  97. matrix_output_unselect_delay(); // wait for row signal to go HIGH
  98. }
  99. // If the row has changed, store the row and return the changed flag.
  100. if (current_matrix[current_row] != current_row_value) {
  101. current_matrix[current_row] = current_row_value;
  102. return true;
  103. }
  104. return false;
  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. static void 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. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  121. bool matrix_changed = false;
  122. // Select col
  123. select_col(current_col);
  124. matrix_output_select_delay();
  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. matrix_row_t current_row_value = last_row_value;
  130. // Check row pin state
  131. if (readPin(row_pins[row_index]) == 0) {
  132. // Pin LO, set col bit
  133. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  134. } else {
  135. // Pin HI, clear col bit
  136. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  137. }
  138. // Determine if the matrix changed state
  139. if ((last_row_value != current_row_value)) {
  140. matrix_changed |= true;
  141. current_matrix[row_index] = current_row_value;
  142. }
  143. }
  144. // Unselect col
  145. unselect_col(current_col);
  146. if (current_col + 1 < MATRIX_COLS) {
  147. matrix_output_unselect_delay(); // wait for col signal to go HIGH
  148. }
  149. return matrix_changed;
  150. }
  151. # else
  152. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  153. # endif
  154. #else
  155. # error DIODE_DIRECTION is not defined!
  156. #endif
  157. void matrix_init(void) {
  158. // initialize key pins
  159. init_pins();
  160. // initialize matrix state: all keys off
  161. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  162. raw_matrix[i] = 0;
  163. matrix[i] = 0;
  164. }
  165. debounce_init(MATRIX_ROWS);
  166. matrix_init_quantum();
  167. }
  168. uint8_t matrix_scan(void) {
  169. bool changed = false;
  170. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  171. // Set row, read cols
  172. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  173. changed |= read_cols_on_row(raw_matrix, current_row);
  174. }
  175. #elif (DIODE_DIRECTION == ROW2COL)
  176. // Set col, read rows
  177. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  178. changed |= read_rows_on_col(raw_matrix, current_col);
  179. }
  180. #endif
  181. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  182. matrix_scan_quantum();
  183. return (uint8_t)changed;
  184. }