matrix.c 11 KB

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