matrix.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. # ifdef MATRIX_MUL_SELECT
  26. static const pin_t col_sel[MATRIX_COLS] = MATRIX_MUL_SEL;
  27. # ifndef MATRIX_MUL_SELECT_DELAY
  28. # define MATRIX_MUL_SELECT_DELAY 1
  29. # endif
  30. # endif
  31. #endif
  32. /* matrix state(1:on, 0:off) */
  33. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  34. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  35. // matrix code
  36. #ifdef DIRECT_PINS
  37. static void init_pins(void) {
  38. for (int row = 0; row < MATRIX_ROWS; row++) {
  39. for (int col = 0; col < MATRIX_COLS; col++) {
  40. pin_t pin = direct_pins[row][col];
  41. if (pin != NO_PIN) {
  42. setPinInputHigh(pin);
  43. }
  44. }
  45. }
  46. }
  47. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  48. // Start with a clear matrix row
  49. matrix_row_t current_row_value = 0;
  50. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  51. pin_t pin = direct_pins[current_row][col_index];
  52. if (pin != NO_PIN) {
  53. current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  54. }
  55. }
  56. // If the row has changed, store the row and return the changed flag.
  57. if (current_matrix[current_row] != current_row_value) {
  58. current_matrix[current_row] = current_row_value;
  59. return true;
  60. }
  61. return false;
  62. }
  63. #elif defined(DIODE_DIRECTION)
  64. # if (DIODE_DIRECTION == COL2ROW)
  65. static void select_row(uint8_t row) {
  66. setPinOutput(row_pins[row]);
  67. writePinLow(row_pins[row]);
  68. }
  69. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  70. static void unselect_rows(void) {
  71. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  72. setPinInputHigh(row_pins[x]);
  73. }
  74. }
  75. static void init_pins(void) {
  76. #ifdef MATRIX_MUL_SELECT
  77. setPinOutput(MATRIX_MUL_SELECT);
  78. writePinLow(MATRIX_MUL_SELECT);
  79. #endif
  80. unselect_rows();
  81. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  82. setPinInputHigh(col_pins[x]);
  83. }
  84. }
  85. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  86. // Start with a clear matrix row
  87. matrix_row_t current_row_value = 0;
  88. // Select row and wait for row selecton to stabilize
  89. select_row(current_row);
  90. matrix_io_delay();
  91. // For each col...
  92. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  93. // Select the col pin to read (active low)
  94. #ifdef MATRIX_MUL_SELECT
  95. writePin(MATRIX_MUL_SELECT,col_sel[col_index]);
  96. __builtin_avr_delay_cycles(MATRIX_MUL_SELECT_DELAY);
  97. #endif
  98. uint8_t pin_state = readPin(col_pins[col_index]);
  99. // Populate the matrix row with the state of the col pin
  100. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  101. }
  102. // Unselect row
  103. unselect_row(current_row);
  104. // If the row has changed, store the row and return the changed flag.
  105. if (current_matrix[current_row] != current_row_value) {
  106. current_matrix[current_row] = current_row_value;
  107. return true;
  108. }
  109. return false;
  110. }
  111. # elif (DIODE_DIRECTION == ROW2COL)
  112. static void select_col(uint8_t col) {
  113. setPinOutput(col_pins[col]);
  114. writePinLow(col_pins[col]);
  115. }
  116. static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
  117. static void unselect_cols(void) {
  118. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  119. setPinInputHigh(col_pins[x]);
  120. }
  121. }
  122. static void init_pins(void) {
  123. unselect_cols();
  124. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  125. setPinInputHigh(row_pins[x]);
  126. }
  127. }
  128. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  129. bool matrix_changed = false;
  130. // Select col and wait for col selecton to stabilize
  131. select_col(current_col);
  132. matrix_io_delay();
  133. // For each row...
  134. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  135. // Store last value of row prior to reading
  136. matrix_row_t last_row_value = current_matrix[row_index];
  137. matrix_row_t current_row_value = last_row_value;
  138. // Check row pin state
  139. if (readPin(row_pins[row_index]) == 0) {
  140. // Pin LO, set col bit
  141. current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
  142. } else {
  143. // Pin HI, clear col bit
  144. current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
  145. }
  146. // Determine if the matrix changed state
  147. if ((last_row_value != current_row_value)) {
  148. matrix_changed |= true;
  149. current_matrix[row_index] = current_row_value;
  150. }
  151. }
  152. // Unselect col
  153. unselect_col(current_col);
  154. return matrix_changed;
  155. }
  156. # else
  157. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  158. # endif
  159. #else
  160. # error DIODE_DIRECTION is not defined!
  161. #endif
  162. void matrix_init_custom(void) {
  163. // initialize key pins
  164. init_pins();
  165. // initialize matrix state: all keys off
  166. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  167. raw_matrix[i] = 0;
  168. matrix[i] = 0;
  169. }
  170. }
  171. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  172. bool changed = false;
  173. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  174. // Set row, read cols
  175. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  176. changed |= read_cols_on_row(current_matrix, current_row);
  177. }
  178. #elif (DIODE_DIRECTION == ROW2COL)
  179. // Set col, read rows
  180. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  181. changed |= read_rows_on_col(current_matrix, current_col);
  182. }
  183. #endif
  184. return changed;
  185. }