matrix.c 9.6 KB

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