matrix.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <string.h>
  17. #include "util.h"
  18. #include "matrix.h"
  19. #include "debounce.h"
  20. #include "quantum.h"
  21. #include "split_util.h"
  22. #include "config.h"
  23. #include "transport.h"
  24. #define ERROR_DISCONNECT_COUNT 5
  25. #define ROWS_PER_HAND (MATRIX_ROWS / 2)
  26. #ifdef DIRECT_PINS
  27. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  28. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  29. # ifdef MATRIX_ROW_PINS
  30. static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  31. # endif // MATRIX_ROW_PINS
  32. # ifdef MATRIX_COL_PINS
  33. static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  34. # endif // MATRIX_COL_PINS
  35. #endif
  36. /* matrix state(1:on, 0:off) */
  37. extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
  38. extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
  39. // row offsets for each hand
  40. uint8_t thisHand, thatHand;
  41. // user-defined overridable functions
  42. __attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); }
  43. __attribute__((weak)) void matrix_slave_scan_user(void) {}
  44. __attribute__((weak)) void matrix_init_pins(void);
  45. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  46. __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  47. static inline void setPinOutput_writeLow(pin_t pin) {
  48. ATOMIC_BLOCK_FORCEON {
  49. setPinOutput(pin);
  50. writePinLow(pin);
  51. }
  52. }
  53. static inline void setPinInputHigh_atomic(pin_t pin) {
  54. ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
  55. }
  56. // matrix code
  57. #ifdef DIRECT_PINS
  58. __attribute__((weak)) void matrix_init_pins(void) {
  59. for (int row = 0; row < MATRIX_ROWS; row++) {
  60. for (int col = 0; col < MATRIX_COLS; col++) {
  61. pin_t pin = direct_pins[row][col];
  62. if (pin != NO_PIN) {
  63. setPinInputHigh(pin);
  64. }
  65. }
  66. }
  67. }
  68. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  69. // Start with a clear matrix row
  70. matrix_row_t current_row_value = 0;
  71. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  72. pin_t pin = direct_pins[current_row][col_index];
  73. if (pin != NO_PIN) {
  74. current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  75. }
  76. }
  77. // Update the matrix
  78. current_matrix[current_row] = current_row_value;
  79. }
  80. #elif defined(DIODE_DIRECTION)
  81. # if defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
  82. # if (DIODE_DIRECTION == COL2ROW)
  83. static void select_row(uint8_t row) { setPinOutput_writeLow(row_pins[row]); }
  84. static void unselect_row(uint8_t row) { setPinInputHigh_atomic(row_pins[row]); }
  85. static void unselect_rows(void) {
  86. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  87. setPinInputHigh_atomic(row_pins[x]);
  88. }
  89. }
  90. __attribute__((weak)) void matrix_init_pins(void) {
  91. unselect_rows();
  92. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  93. setPinInputHigh_atomic(col_pins[x]);
  94. }
  95. }
  96. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  97. // Start with a clear matrix row
  98. matrix_row_t current_row_value = 0;
  99. // Select row
  100. select_row(current_row);
  101. matrix_output_select_delay();
  102. // For each col...
  103. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  104. // Select the col pin to read (active low)
  105. uint8_t pin_state = readPin(col_pins[col_index]);
  106. // Populate the matrix row with the state of the col pin
  107. current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  108. }
  109. // Unselect row
  110. unselect_row(current_row);
  111. matrix_output_unselect_delay(); // wait for all Col signals to go HIGH
  112. // Update the matrix
  113. current_matrix[current_row] = current_row_value;
  114. }
  115. # elif (DIODE_DIRECTION == ROW2COL)
  116. static void select_col(uint8_t col) { setPinOutput_writeLow(col_pins[col]); }
  117. static void unselect_col(uint8_t col) { setPinInputHigh_atomic(col_pins[col]); }
  118. static void unselect_cols(void) {
  119. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  120. setPinInputHigh_atomic(col_pins[x]);
  121. }
  122. }
  123. __attribute__((weak)) void matrix_init_pins(void) {
  124. unselect_cols();
  125. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  126. setPinInputHigh_atomic(row_pins[x]);
  127. }
  128. }
  129. __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  130. // Select col
  131. select_col(current_col);
  132. matrix_output_select_delay();
  133. // For each row...
  134. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  135. // Check row pin state
  136. if (readPin(row_pins[row_index]) == 0) {
  137. // Pin LO, set col bit
  138. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  139. } else {
  140. // Pin HI, clear col bit
  141. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  142. }
  143. }
  144. // Unselect col
  145. unselect_col(current_col);
  146. matrix_output_unselect_delay(); // wait for all Row signals to go HIGH
  147. }
  148. # else
  149. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  150. # endif
  151. # endif // defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
  152. #else
  153. # error DIODE_DIRECTION is not defined!
  154. #endif
  155. void matrix_init(void) {
  156. split_pre_init();
  157. // Set pinout for right half if pinout for that half is defined
  158. if (!isLeftHand) {
  159. #ifdef DIRECT_PINS_RIGHT
  160. const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT;
  161. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  162. for (uint8_t j = 0; j < MATRIX_COLS; j++) {
  163. direct_pins[i][j] = direct_pins_right[i][j];
  164. }
  165. }
  166. #endif
  167. #ifdef MATRIX_ROW_PINS_RIGHT
  168. const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
  169. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  170. row_pins[i] = row_pins_right[i];
  171. }
  172. #endif
  173. #ifdef MATRIX_COL_PINS_RIGHT
  174. const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
  175. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  176. col_pins[i] = col_pins_right[i];
  177. }
  178. #endif
  179. }
  180. thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
  181. thatHand = ROWS_PER_HAND - thisHand;
  182. // initialize key pins
  183. matrix_init_pins();
  184. // initialize matrix state: all keys off
  185. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  186. raw_matrix[i] = 0;
  187. matrix[i] = 0;
  188. }
  189. debounce_init(ROWS_PER_HAND);
  190. matrix_init_quantum();
  191. split_post_init();
  192. }
  193. bool matrix_post_scan(void) {
  194. bool changed = false;
  195. if (is_keyboard_master()) {
  196. static uint8_t error_count;
  197. matrix_row_t slave_matrix[ROWS_PER_HAND] = {0};
  198. if (!transport_master(matrix + thisHand, slave_matrix)) {
  199. error_count++;
  200. if (error_count > ERROR_DISCONNECT_COUNT) {
  201. // reset other half if disconnected
  202. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  203. matrix[thatHand + i] = 0;
  204. slave_matrix[i] = 0;
  205. }
  206. changed = true;
  207. }
  208. } else {
  209. error_count = 0;
  210. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  211. if (matrix[thatHand + i] != slave_matrix[i]) {
  212. matrix[thatHand + i] = slave_matrix[i];
  213. changed = true;
  214. }
  215. }
  216. }
  217. matrix_scan_quantum();
  218. } else {
  219. transport_slave(matrix + thatHand, matrix + thisHand);
  220. matrix_slave_scan_kb();
  221. }
  222. return changed;
  223. }
  224. uint8_t matrix_scan(void) {
  225. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  226. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  227. // Set row, read cols
  228. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  229. matrix_read_cols_on_row(curr_matrix, current_row);
  230. }
  231. #elif (DIODE_DIRECTION == ROW2COL)
  232. // Set col, read rows
  233. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  234. matrix_read_rows_on_col(curr_matrix, current_col);
  235. }
  236. #endif
  237. bool local_changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  238. if (local_changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix));
  239. debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, local_changed);
  240. bool remote_changed = matrix_post_scan();
  241. return (uint8_t)(local_changed || remote_changed);
  242. }