matrix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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, matrix_row_t row_shifter);
  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 setPinOutput_writeHigh(pin_t pin) {
  75. ATOMIC_BLOCK_FORCEON {
  76. setPinOutput(pin);
  77. writePinHigh(pin);
  78. }
  79. }
  80. static inline void setPinInputHigh_atomic(pin_t pin) {
  81. ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
  82. }
  83. static inline uint8_t readMatrixPin(pin_t pin) {
  84. if (pin != NO_PIN) {
  85. return readPin(pin);
  86. } else {
  87. return 1;
  88. }
  89. }
  90. // matrix code
  91. #ifdef DIRECT_PINS
  92. __attribute__((weak)) void matrix_init_pins(void) {
  93. for (int row = 0; row < MATRIX_ROWS; row++) {
  94. for (int col = 0; col < MATRIX_COLS; col++) {
  95. pin_t pin = direct_pins[row][col];
  96. if (pin != NO_PIN) {
  97. setPinInputHigh(pin);
  98. }
  99. }
  100. }
  101. }
  102. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  103. // Start with a clear matrix row
  104. matrix_row_t current_row_value = 0;
  105. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  106. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) {
  107. pin_t pin = direct_pins[current_row][col_index];
  108. if (pin != NO_PIN) {
  109. current_row_value |= readPin(pin) ? 0 : row_shifter;
  110. }
  111. }
  112. // Update the matrix
  113. current_matrix[current_row] = current_row_value;
  114. }
  115. #elif defined(DIODE_DIRECTION)
  116. # if defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
  117. # if (DIODE_DIRECTION == COL2ROW)
  118. static bool select_row(uint8_t row) {
  119. pin_t pin = row_pins[row];
  120. if (pin != NO_PIN) {
  121. setPinOutput_writeLow(pin);
  122. return true;
  123. }
  124. return false;
  125. }
  126. static void unselect_row(uint8_t row) {
  127. pin_t pin = row_pins[row];
  128. if (pin != NO_PIN) {
  129. # ifdef MATRIX_UNSELECT_DRIVE_HIGH
  130. setPinOutput_writeHigh(pin);
  131. # else
  132. setPinInputHigh_atomic(pin);
  133. # endif
  134. }
  135. }
  136. static void unselect_rows(void) {
  137. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  138. unselect_row(x);
  139. }
  140. }
  141. __attribute__((weak)) void matrix_init_pins(void) {
  142. unselect_rows();
  143. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  144. if (col_pins[x] != NO_PIN) {
  145. setPinInputHigh_atomic(col_pins[x]);
  146. }
  147. }
  148. }
  149. __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  150. // Start with a clear matrix row
  151. matrix_row_t current_row_value = 0;
  152. if (!select_row(current_row)) { // Select row
  153. return; // skip NO_PIN row
  154. }
  155. matrix_output_select_delay();
  156. // For each col...
  157. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  158. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) {
  159. uint8_t pin_state = readMatrixPin(col_pins[col_index]);
  160. // Populate the matrix row with the state of the col pin
  161. current_row_value |= pin_state ? 0 : row_shifter;
  162. }
  163. // Unselect row
  164. unselect_row(current_row);
  165. matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for all Col signals to go HIGH
  166. // Update the matrix
  167. current_matrix[current_row] = current_row_value;
  168. }
  169. # elif (DIODE_DIRECTION == ROW2COL)
  170. static bool select_col(uint8_t col) {
  171. pin_t pin = col_pins[col];
  172. if (pin != NO_PIN) {
  173. setPinOutput_writeLow(pin);
  174. return true;
  175. }
  176. return false;
  177. }
  178. static void unselect_col(uint8_t col) {
  179. pin_t pin = col_pins[col];
  180. if (pin != NO_PIN) {
  181. # ifdef MATRIX_UNSELECT_DRIVE_HIGH
  182. setPinOutput_writeHigh(pin);
  183. # else
  184. setPinInputHigh_atomic(pin);
  185. # endif
  186. }
  187. }
  188. static void unselect_cols(void) {
  189. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  190. unselect_col(x);
  191. }
  192. }
  193. __attribute__((weak)) void matrix_init_pins(void) {
  194. unselect_cols();
  195. for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  196. if (row_pins[x] != NO_PIN) {
  197. setPinInputHigh_atomic(row_pins[x]);
  198. }
  199. }
  200. }
  201. __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
  202. bool key_pressed = false;
  203. // Select col
  204. if (!select_col(current_col)) { // select col
  205. return; // skip NO_PIN col
  206. }
  207. matrix_output_select_delay();
  208. // For each row...
  209. for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
  210. // Check row pin state
  211. if (readMatrixPin(row_pins[row_index]) == 0) {
  212. // Pin LO, set col bit
  213. current_matrix[row_index] |= row_shifter;
  214. key_pressed = true;
  215. } else {
  216. // Pin HI, clear col bit
  217. current_matrix[row_index] &= ~row_shifter;
  218. }
  219. }
  220. // Unselect col
  221. unselect_col(current_col);
  222. matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH
  223. }
  224. # else
  225. # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
  226. # endif
  227. # endif // defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
  228. #else
  229. # error DIODE_DIRECTION is not defined!
  230. #endif
  231. void matrix_init(void) {
  232. #ifdef SPLIT_KEYBOARD
  233. split_pre_init();
  234. // Set pinout for right half if pinout for that half is defined
  235. if (!isLeftHand) {
  236. # ifdef DIRECT_PINS_RIGHT
  237. const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT;
  238. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  239. for (uint8_t j = 0; j < MATRIX_COLS; j++) {
  240. direct_pins[i][j] = direct_pins_right[i][j];
  241. }
  242. }
  243. # endif
  244. # ifdef MATRIX_ROW_PINS_RIGHT
  245. const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
  246. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  247. row_pins[i] = row_pins_right[i];
  248. }
  249. # endif
  250. # ifdef MATRIX_COL_PINS_RIGHT
  251. const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
  252. for (uint8_t i = 0; i < MATRIX_COLS; i++) {
  253. col_pins[i] = col_pins_right[i];
  254. }
  255. # endif
  256. }
  257. thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
  258. thatHand = ROWS_PER_HAND - thisHand;
  259. #endif
  260. // initialize key pins
  261. matrix_init_pins();
  262. // initialize matrix state: all keys off
  263. memset(matrix, 0, sizeof(matrix));
  264. memset(raw_matrix, 0, sizeof(raw_matrix));
  265. debounce_init(ROWS_PER_HAND);
  266. matrix_init_quantum();
  267. #ifdef SPLIT_KEYBOARD
  268. split_post_init();
  269. #endif
  270. }
  271. #ifdef SPLIT_KEYBOARD
  272. // Fallback implementation for keyboards not using the standard split_util.c
  273. __attribute__((weak)) bool transport_master_if_connected(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  274. transport_master(master_matrix, slave_matrix);
  275. return true; // Treat the transport as always connected
  276. }
  277. bool matrix_post_scan(void) {
  278. bool changed = false;
  279. if (is_keyboard_master()) {
  280. static bool last_connected = false;
  281. matrix_row_t slave_matrix[ROWS_PER_HAND] = {0};
  282. if (transport_master_if_connected(matrix + thisHand, slave_matrix)) {
  283. changed = memcmp(matrix + thatHand, slave_matrix, sizeof(slave_matrix)) != 0;
  284. last_connected = true;
  285. } else if (last_connected) {
  286. // reset other half when disconnected
  287. memset(slave_matrix, 0, sizeof(slave_matrix));
  288. changed = true;
  289. last_connected = false;
  290. }
  291. if (changed) memcpy(matrix + thatHand, slave_matrix, sizeof(slave_matrix));
  292. matrix_scan_quantum();
  293. } else {
  294. transport_slave(matrix + thatHand, matrix + thisHand);
  295. matrix_slave_scan_kb();
  296. }
  297. return changed;
  298. }
  299. #endif
  300. uint8_t matrix_scan(void) {
  301. matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
  302. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  303. // Set row, read cols
  304. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  305. matrix_read_cols_on_row(curr_matrix, current_row);
  306. }
  307. #elif (DIODE_DIRECTION == ROW2COL)
  308. // Set col, read rows
  309. matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
  310. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
  311. matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
  312. }
  313. #endif
  314. bool changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
  315. if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix));
  316. #ifdef SPLIT_KEYBOARD
  317. debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed);
  318. changed = (changed || matrix_post_scan());
  319. #else
  320. debounce(raw_matrix, matrix, ROWS_PER_HAND, changed);
  321. matrix_scan_quantum();
  322. #endif
  323. return (uint8_t)changed;
  324. }