matrix.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "wait.h"
  15. #include "print.h"
  16. #include "debug.h"
  17. #include "util.h"
  18. #include "matrix.h"
  19. #include "debounce.h"
  20. #if (MATRIX_COLS <= 8)
  21. # define print_matrix_header() print("\nr/c 01234567\n")
  22. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  23. #elif (MATRIX_COLS <= 16)
  24. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  25. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  26. #elif (MATRIX_COLS <= 32)
  27. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  28. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  29. #endif
  30. #ifdef MATRIX_MASKED
  31. extern const matrix_row_t matrix_mask[];
  32. #endif
  33. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  34. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  35. /* matrix state(1:on, 0:off) */
  36. static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  37. static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  38. __attribute__((weak)) void matrix_init_kb(void) {
  39. matrix_init_user();
  40. }
  41. __attribute__((weak)) void matrix_scan_kb(void) {
  42. matrix_scan_user();
  43. }
  44. __attribute__((weak)) void matrix_init_user(void) {}
  45. __attribute__((weak)) void matrix_scan_user(void) {}
  46. inline uint8_t matrix_rows(void) {
  47. return MATRIX_ROWS;
  48. }
  49. inline uint8_t matrix_cols(void) {
  50. return MATRIX_COLS;
  51. }
  52. inline bool matrix_is_on(uint8_t row, uint8_t col) {
  53. return (matrix[row] & ((matrix_row_t)1 << col));
  54. }
  55. inline matrix_row_t matrix_get_row(uint8_t row) {
  56. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  57. // switch blocker installed and the switch is always pressed.
  58. #ifdef MATRIX_MASKED
  59. return matrix[row] & matrix_mask[row];
  60. #else
  61. return matrix[row];
  62. #endif
  63. }
  64. void matrix_print(void) {
  65. print_matrix_header();
  66. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  67. print_hex8(row);
  68. print(": ");
  69. print_matrix_row(row);
  70. print("\n");
  71. }
  72. }
  73. static void select_row(uint8_t row) {
  74. gpio_set_pin_output(row_pins[row]);
  75. gpio_write_pin_low(row_pins[row]);
  76. }
  77. static void unselect_row(uint8_t row) {
  78. gpio_set_pin_input_high(row_pins[row]);
  79. }
  80. static void unselect_rows(void) {
  81. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  82. gpio_set_pin_input_high(row_pins[x]);
  83. }
  84. }
  85. static void select_col(uint8_t col) {
  86. gpio_set_pin_output(col_pins[col]);
  87. gpio_write_pin_low(col_pins[col]);
  88. }
  89. static void unselect_col(uint8_t col) {
  90. gpio_set_pin_input_high(col_pins[col]);
  91. }
  92. static void unselect_cols(void) {
  93. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  94. gpio_set_pin_input_high(col_pins[x]);
  95. }
  96. }
  97. static void init_pins(void) {
  98. unselect_rows();
  99. unselect_cols();
  100. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  101. gpio_set_pin_input_high(col_pins[x]);
  102. }
  103. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  104. gpio_set_pin_input_high(row_pins[x]);
  105. }
  106. }
  107. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  108. // Store last value of row prior to reading
  109. matrix_row_t last_row_value = current_matrix[current_row];
  110. // Clear data in matrix row
  111. current_matrix[current_row] = 0;
  112. // Select row and wait for row selecton to stabilize
  113. select_row(current_row);
  114. wait_us(30);
  115. // For each col...
  116. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  117. // Select the col pin to read (active low)
  118. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  119. // Populate the matrix row with the state of the col pin
  120. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  121. }
  122. // Unselect row
  123. unselect_row(current_row);
  124. return (last_row_value != current_matrix[current_row]);
  125. }
  126. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  127. bool matrix_changed = false;
  128. // Select col and wait for col selecton to stabilize
  129. select_col(current_col);
  130. wait_us(30);
  131. // For each row...
  132. for (uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
  133. uint8_t tmp = row_index + MATRIX_ROWS / 2;
  134. // Store last value of row prior to reading
  135. matrix_row_t last_row_value = current_matrix[tmp];
  136. // Check row pin state
  137. if (gpio_read_pin(row_pins[row_index]) == 0) {
  138. // Pin LO, set col bit
  139. current_matrix[tmp] |= (MATRIX_ROW_SHIFTER << current_col);
  140. } else {
  141. // Pin HI, clear col bit
  142. current_matrix[tmp] &= ~(MATRIX_ROW_SHIFTER << current_col);
  143. }
  144. // Determine if the matrix changed state
  145. if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
  146. matrix_changed = true;
  147. }
  148. }
  149. // Unselect col
  150. unselect_col(current_col);
  151. return matrix_changed;
  152. }
  153. void matrix_init(void) {
  154. // initialize key pins
  155. init_pins();
  156. // initialize matrix state: all keys off
  157. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  158. raw_matrix[i] = 0;
  159. matrix[i] = 0;
  160. }
  161. debounce_init();
  162. matrix_init_kb();
  163. }
  164. uint8_t matrix_scan(void) {
  165. bool changed = false;
  166. // Set row, read cols
  167. for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
  168. changed |= read_cols_on_row(raw_matrix, current_row);
  169. }
  170. // else
  171. // Set col, read rows
  172. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  173. changed |= read_rows_on_col(raw_matrix, current_col);
  174. }
  175. debounce(raw_matrix, matrix, changed);
  176. matrix_scan_kb();
  177. return (uint8_t)changed;
  178. }