matrix.c 10 KB

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