matrix.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  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 "wait.h"
  17. #include "util.h"
  18. #include "matrix.h"
  19. #include "split_util.h"
  20. #include "config.h"
  21. #include "quantum.h"
  22. #include "debounce.h"
  23. #include "transport.h"
  24. #ifdef ENCODER_ENABLE
  25. # include "encoder.h"
  26. #endif
  27. #define ERROR_DISCONNECT_COUNT 5
  28. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  29. #ifdef DIRECT_PINS
  30. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  31. #else
  32. static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  33. static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  34. #endif
  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. // row offsets for each hand
  39. uint8_t thisHand, thatHand;
  40. // user-defined overridable functions
  41. __attribute__((weak)) void matrix_slave_scan_user(void) {}
  42. // helper functions
  43. inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
  44. inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
  45. // matrix code
  46. #ifdef DIRECT_PINS
  47. static void init_pins(void) {
  48. for (int row = 0; row < MATRIX_ROWS; row++) {
  49. for (int col = 0; col < MATRIX_COLS; col++) {
  50. pin_t pin = direct_pins[row][col];
  51. if (pin != NO_PIN) {
  52. setPinInputHigh(pin);
  53. }
  54. }
  55. }
  56. }
  57. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  58. matrix_row_t last_row_value = current_matrix[current_row];
  59. current_matrix[current_row] = 0;
  60. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  61. pin_t pin = direct_pins[current_row][col_index];
  62. if (pin != NO_PIN) {
  63. current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  64. }
  65. }
  66. return (last_row_value != current_matrix[current_row]);
  67. }
  68. #elif (DIODE_DIRECTION == COL2ROW)
  69. static void select_row(uint8_t row) {
  70. setPinOutput(row_pins[row]);
  71. writePinLow(row_pins[row]);
  72. }
  73. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  74. static void unselect_rows(void) {
  75. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  76. setPinInputHigh(row_pins[x]);
  77. }
  78. }
  79. static void init_pins(void) {
  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. // Store last value of row prior to reading
  87. matrix_row_t last_row_value = current_matrix[current_row];
  88. // Clear data in matrix row
  89. current_matrix[current_row] = 0;
  90. // Select row and wait for row selecton to stabilize
  91. select_row(current_row);
  92. wait_us(30);
  93. // For each col...
  94. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  95. // Populate the matrix row with the state of the col pin
  96. current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  97. }
  98. // Unselect row
  99. unselect_row(current_row);
  100. return (last_row_value != current_matrix[current_row]);
  101. }
  102. #elif (DIODE_DIRECTION == ROW2COL)
  103. static void select_col(uint8_t col) {
  104. setPinOutput(col_pins[col]);
  105. writePinLow(col_pins[col]);
  106. }
  107. static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
  108. static void unselect_cols(void) {
  109. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  110. setPinInputHigh(col_pins[x]);
  111. }
  112. }
  113. static void init_pins(void) {
  114. unselect_cols();
  115. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  116. setPinInputHigh(row_pins[x]);
  117. }
  118. }
  119. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  120. bool matrix_changed = false;
  121. // Select col and wait for col selecton to stabilize
  122. select_col(current_col);
  123. wait_us(30);
  124. // For each row...
  125. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  126. // Store last value of row prior to reading
  127. matrix_row_t last_row_value = current_matrix[row_index];
  128. // Check row pin state
  129. if (readPin(row_pins[row_index])) {
  130. // Pin HI, clear col bit
  131. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  132. } else {
  133. // Pin LO, set col bit
  134. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  135. }
  136. // Determine if the matrix changed state
  137. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  138. matrix_changed = true;
  139. }
  140. }
  141. // Unselect col
  142. unselect_col(current_col);
  143. return matrix_changed;
  144. }
  145. #endif
  146. void matrix_init(void) {
  147. keyboard_split_setup();
  148. // Set pinout for right half if pinout for that half is defined
  149. if (!isLeftHand) {
  150. #ifdef DIRECT_PINS_RIGHT
  151. const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT;
  152. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  153. for (uint8_t j = 0; j < MATRIX_COLS; j++) {
  154. direct_pins[i][j] = direct_pins_right[i][j];
  155. }
  156. }
  157. #endif
  158. #ifdef MATRIX_ROW_PINS_RIGHT
  159. const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
  160. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  161. row_pins[i] = row_pins_right[i];
  162. }
  163. #endif
  164. #ifdef MATRIX_COL_PINS_RIGHT
  165. const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
  166. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  167. col_pins[i] = col_pins_right[i];
  168. }
  169. #endif
  170. }
  171. thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
  172. thatHand = ROWS_PER_HAND - thisHand;
  173. // initialize key pins
  174. init_pins();
  175. // initialize matrix state: all keys off
  176. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  177. raw_matrix[i] = 0;
  178. matrix[i] = 0;
  179. }
  180. debounce_init(ROWS_PER_HAND);
  181. matrix_init_quantum();
  182. }
  183. void matrix_post_scan(void) {
  184. if (is_keyboard_master()) {
  185. static uint8_t error_count;
  186. if (!transport_master(matrix + thatHand)) {
  187. error_count++;
  188. if (error_count > ERROR_DISCONNECT_COUNT) {
  189. // reset other half if disconnected
  190. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  191. matrix[thatHand + i] = 0;
  192. }
  193. }
  194. } else {
  195. error_count = 0;
  196. }
  197. matrix_scan_quantum();
  198. } else {
  199. transport_slave(matrix + thisHand);
  200. #ifdef ENCODER_ENABLE
  201. encoder_read();
  202. #endif
  203. matrix_slave_scan_user();
  204. }
  205. }
  206. uint8_t matrix_scan(void) {
  207. bool changed = false;
  208. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  209. // Set row, read cols
  210. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  211. changed |= read_cols_on_row(raw_matrix, current_row);
  212. }
  213. #elif (DIODE_DIRECTION == ROW2COL)
  214. // Set col, read rows
  215. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  216. changed |= read_rows_on_col(raw_matrix, current_col);
  217. }
  218. #endif
  219. debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed);
  220. matrix_post_scan();
  221. return (uint8_t)changed;
  222. }