You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
11 KiB

8 years ago
8 years ago
8 years ago
Simplify split_common Code significantly (#4772) * Eliminate separate slave loop Both master and slave run the standard keyboard_task main loop now. * Refactor i2c/serial specific code Simplify some of the preprocessor mess by using common function names. * Fix missing #endif * Move direct pin mapping support from miniaxe to split_common For boards with more pins than sense--sorry, switches. * Reordering and reformatting only * Don't run matrix_scan_quantum on slave side * Clean up the offset/slaveOffset calculations * Cut undebounced matrix size in half * Refactor debouncing * Minor fixups * Split split_common transport and debounce code into their own files Can now be replaced with custom versions per keyboard using CUSTOM_TRANSPORT = yes and CUSTOM_DEBOUNCE = yes * Refactor debounce for non-split keyboards too * Update handwired/xealous to build using new split_common * Fix debounce breaking basic test * Dodgy method to allow a split kb to only include one of i2c/serial SPLIT_TRANSPORT = serial or SPLIT_TRANSPORT = i2c will include only that driver code in the binary. SPLIT_TRANSPORT = custom (or anything else) will include neither, the keyboard must supply it's own code if SPLIT_TRANSPORT is not defined then the original behaviour (include both avr i2c and serial code) is maintained. This could be better but it would require explicitly updating all the existing split keyboards. * Enable LTO to get lets_split/sockets under the line * Add docs for SPLIT_TRANSPORT, CUSTOM_MATRIX, CUSTOM_DEBOUNCE * Remove avr-specific sei() from split matrix_setup Not needed now that slave doesn't have a separate main loop. Both sides (on avr) call sei() in lufa's main() after exiting keyboard_setup(). * Fix QUANTUM_LIB_SRC references and simplify SPLIT_TRANSPORT. * Add comments and fix formatting.
5 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
8 years ago
8 years ago
Make solo half of split keyboards (more) usable. (#13523) * Make solo half of split keyboards (more) usable. Using only one half of a split keyboard (that's using the split_common framework to communicate) is not a great experience, since several read timeouts per scan cycle cause an unusably slow scan rate. This change blocks all split communication attempts for 500 ms (configurable) after an error occurs, causing the scan rate to become at least _more_ usable, but might need some tweaking to work fully on most keyboards. One read timeout still needs to occur after the 500 ms has passed, and if that timeout isn't low enough, some scan cycles may still be too slow. * Fix lint complaint. * Require 25 consecutive comm errors to see comms as disconnected. The number of max errors can be overridden by defining `SPLIT_MAX_CONNECTION_ERRORS`. * Add comments to new defines, and ability to disable disconnection check. Also increase `SPLIT_MAX_CONNECTION_ERRORS` to 40, since it's divisible by most relevant numbers for the description. * Make lint happy ...again * Only update `connection_check_timer` when needed. * Add new defines to split keyboard documentation. * Move connection timeout logic to transport.c, add `is_transport_connected`. * Use split_common disconnection logic in matrix.c. Instead of doing more or less the same thing twice. * Move disconnection logic to `transport_master`. Is a cleaner implementation, and causes the scan rate while disconnected to increase instead of decrease. * Lint fixes. * Lower default `SERIAL_USART_TIMEOUT` to 20 ms. The read timeout must be low enough to not cause exessively long scan cycles when using a solo split half. 10 ms was determined from testing to work fine even with the slowest defined baudrate of 19200 (5 ms was too low for that case), so 20 ms should be fine for most cases. * Remove `SERIAL_USART_TIMEOUT` from ergodox_infinity/config.h Was somewhat mistakenly included in an earlier PR. * Fix building with `USE_I2C`. * Reduce built firmware size. Not really sure why this works, the idea was taken from tzarc's work on split disconnection. * Tweak and improve opt-out for split disconnection logic. There are now two ways to opt out from this feature: * Set `SPLIT_MAX_CONNECTION_ERRORS` to 0. This will completely disable the connection status checks (also affects the slave matrix reset logic in matrix.c, though). * Set `SPLIT_CONNECTION_CHECK_TIMEOUT` to 0. This will only disable the communication throttling while disconnected. Will make the firmware smaller. * Make split disconnection logic work with custom transports. Includes a fallback implementation for keyboards using a custom split_util.c but not a custom matrix.c (currently no such keyboard seems to be merged, though). * Remove unnecessary include of timer.h Co-authored-by: Joel Challis <git@zvecr.com> Co-authored-by: Joel Challis <git@zvecr.com>
2 years ago
Make solo half of split keyboards (more) usable. (#13523) * Make solo half of split keyboards (more) usable. Using only one half of a split keyboard (that's using the split_common framework to communicate) is not a great experience, since several read timeouts per scan cycle cause an unusably slow scan rate. This change blocks all split communication attempts for 500 ms (configurable) after an error occurs, causing the scan rate to become at least _more_ usable, but might need some tweaking to work fully on most keyboards. One read timeout still needs to occur after the 500 ms has passed, and if that timeout isn't low enough, some scan cycles may still be too slow. * Fix lint complaint. * Require 25 consecutive comm errors to see comms as disconnected. The number of max errors can be overridden by defining `SPLIT_MAX_CONNECTION_ERRORS`. * Add comments to new defines, and ability to disable disconnection check. Also increase `SPLIT_MAX_CONNECTION_ERRORS` to 40, since it's divisible by most relevant numbers for the description. * Make lint happy ...again * Only update `connection_check_timer` when needed. * Add new defines to split keyboard documentation. * Move connection timeout logic to transport.c, add `is_transport_connected`. * Use split_common disconnection logic in matrix.c. Instead of doing more or less the same thing twice. * Move disconnection logic to `transport_master`. Is a cleaner implementation, and causes the scan rate while disconnected to increase instead of decrease. * Lint fixes. * Lower default `SERIAL_USART_TIMEOUT` to 20 ms. The read timeout must be low enough to not cause exessively long scan cycles when using a solo split half. 10 ms was determined from testing to work fine even with the slowest defined baudrate of 19200 (5 ms was too low for that case), so 20 ms should be fine for most cases. * Remove `SERIAL_USART_TIMEOUT` from ergodox_infinity/config.h Was somewhat mistakenly included in an earlier PR. * Fix building with `USE_I2C`. * Reduce built firmware size. Not really sure why this works, the idea was taken from tzarc's work on split disconnection. * Tweak and improve opt-out for split disconnection logic. There are now two ways to opt out from this feature: * Set `SPLIT_MAX_CONNECTION_ERRORS` to 0. This will completely disable the connection status checks (also affects the slave matrix reset logic in matrix.c, though). * Set `SPLIT_CONNECTION_CHECK_TIMEOUT` to 0. This will only disable the communication throttling while disconnected. Will make the firmware smaller. * Make split disconnection logic work with custom transports. Includes a fallback implementation for keyboards using a custom split_util.c but not a custom matrix.c (currently no such keyboard seems to be merged, though). * Remove unnecessary include of timer.h Co-authored-by: Joel Challis <git@zvecr.com> Co-authored-by: Joel Challis <git@zvecr.com>
2 years ago
Make solo half of split keyboards (more) usable. (#13523) * Make solo half of split keyboards (more) usable. Using only one half of a split keyboard (that's using the split_common framework to communicate) is not a great experience, since several read timeouts per scan cycle cause an unusably slow scan rate. This change blocks all split communication attempts for 500 ms (configurable) after an error occurs, causing the scan rate to become at least _more_ usable, but might need some tweaking to work fully on most keyboards. One read timeout still needs to occur after the 500 ms has passed, and if that timeout isn't low enough, some scan cycles may still be too slow. * Fix lint complaint. * Require 25 consecutive comm errors to see comms as disconnected. The number of max errors can be overridden by defining `SPLIT_MAX_CONNECTION_ERRORS`. * Add comments to new defines, and ability to disable disconnection check. Also increase `SPLIT_MAX_CONNECTION_ERRORS` to 40, since it's divisible by most relevant numbers for the description. * Make lint happy ...again * Only update `connection_check_timer` when needed. * Add new defines to split keyboard documentation. * Move connection timeout logic to transport.c, add `is_transport_connected`. * Use split_common disconnection logic in matrix.c. Instead of doing more or less the same thing twice. * Move disconnection logic to `transport_master`. Is a cleaner implementation, and causes the scan rate while disconnected to increase instead of decrease. * Lint fixes. * Lower default `SERIAL_USART_TIMEOUT` to 20 ms. The read timeout must be low enough to not cause exessively long scan cycles when using a solo split half. 10 ms was determined from testing to work fine even with the slowest defined baudrate of 19200 (5 ms was too low for that case), so 20 ms should be fine for most cases. * Remove `SERIAL_USART_TIMEOUT` from ergodox_infinity/config.h Was somewhat mistakenly included in an earlier PR. * Fix building with `USE_I2C`. * Reduce built firmware size. Not really sure why this works, the idea was taken from tzarc's work on split disconnection. * Tweak and improve opt-out for split disconnection logic. There are now two ways to opt out from this feature: * Set `SPLIT_MAX_CONNECTION_ERRORS` to 0. This will completely disable the connection status checks (also affects the slave matrix reset logic in matrix.c, though). * Set `SPLIT_CONNECTION_CHECK_TIMEOUT` to 0. This will only disable the communication throttling while disconnected. Will make the firmware smaller. * Make split disconnection logic work with custom transports. Includes a fallback implementation for keyboards using a custom split_util.c but not a custom matrix.c (currently no such keyboard seems to be merged, though). * Remove unnecessary include of timer.h Co-authored-by: Joel Challis <git@zvecr.com> Co-authored-by: Joel Challis <git@zvecr.com>
2 years ago
  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. }