matrix.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 {
  37. setPinInputHigh(pin);
  38. }
  39. }
  40. // matrix code
  41. #ifdef DIRECT_PINS
  42. static void init_pins(void) {
  43. for (int row = 0; row < MATRIX_ROWS; row++) {
  44. for (int col = 0; col < MATRIX_COLS; col++) {
  45. pin_t pin = direct_pins[row][col];
  46. if (pin != NO_PIN) {
  47. setPinInputHigh(pin);
  48. }
  49. }
  50. }
  51. }
  52. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  53. // Start with a clear matrix row
  54. matrix_row_t current_row_value = 0;
  55. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  56. pin_t pin = direct_pins[current_row][col_index];
  57. if (pin != NO_PIN) {
  58. current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  59. }
  60. }
  61. // If the row has changed, store the row and return the changed flag.
  62. if (current_matrix[current_row] != current_row_value) {
  63. current_matrix[current_row] = current_row_value;
  64. return true;
  65. }
  66. return false;
  67. }
  68. #elif defined(DIODE_DIRECTION)
  69. # if (DIODE_DIRECTION == COL2ROW)
  70. static void select_row(uint8_t row) {
  71. setPinOutput_writeLow(row_pins[row]);
  72. }
  73. static void unselect_row(uint8_t row) {
  74. setPinInputHigh_atomic(row_pins[row]);
  75. }
  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. static void 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. static bool 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 and wait for row selecton to stabilize
  91. select_row(current_row);
  92. matrix_io_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. // If the row has changed, store the row and return the changed flag.
  103. if (current_matrix[current_row] != current_row_value) {
  104. current_matrix[current_row] = current_row_value;
  105. return true;
  106. }
  107. return false;
  108. }
  109. # elif (DIODE_DIRECTION == ROW2COL)
  110. static void select_col(uint8_t col) {
  111. setPinOutput_writeLow(col_pins[col]);
  112. }
  113. static void unselect_col(uint8_t col) {
  114. setPinInputHigh_atomic(col_pins[col]);
  115. }
  116. static void unselect_cols(void) {
  117. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  118. setPinInputHigh_atomic(col_pins[x]);
  119. }
  120. }
  121. static void init_pins(void) {
  122. unselect_cols();
  123. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  124. setPinInputHigh_atomic(row_pins[x]);
  125. }
  126. }
  127. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  128. bool matrix_changed = false;
  129. // Select col and wait for col selecton to stabilize
  130. select_col(current_col);
  131. matrix_io_delay();
  132. // For each row...
  133. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  134. // Store last value of row prior to reading
  135. matrix_row_t last_row_value = current_matrix[row_index];
  136. matrix_row_t current_row_value = last_row_value;
  137. // Check row pin state
  138. if (readPin(row_pins[row_index]) == 0) {
  139. // Pin LO, set col bit
  140. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  141. } else {
  142. // Pin HI, clear col bit
  143. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  144. }
  145. // Determine if the matrix changed state
  146. if ((last_row_value != current_row_value)) {
  147. matrix_changed |= true;
  148. current_matrix[row_index] = current_row_value;
  149. }
  150. }
  151. // Unselect col
  152. unselect_col(current_col);
  153. return matrix_changed;
  154. }
  155. # else
  156. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  157. # endif
  158. #else
  159. # error DIODE_DIRECTION is not defined!
  160. #endif
  161. void matrix_init(void) {
  162. // initialize key pins
  163. init_pins();
  164. // initialize matrix state: all keys off
  165. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  166. raw_matrix[i] = 0;
  167. matrix[i] = 0;
  168. }
  169. debounce_init(MATRIX_ROWS);
  170. matrix_init_quantum();
  171. }
  172. uint8_t matrix_scan(void) {
  173. bool changed = false;
  174. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  175. // Set row, read cols
  176. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  177. changed |= read_cols_on_row(raw_matrix, current_row);
  178. }
  179. #elif (DIODE_DIRECTION == ROW2COL)
  180. // Set col, read rows
  181. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  182. changed |= read_rows_on_col(raw_matrix, current_col);
  183. }
  184. #endif
  185. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  186. matrix_scan_quantum();
  187. return (uint8_t)changed;
  188. }