matrix.c 10.0 KB

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