matrix.c 9.7 KB

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