split_util.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright 2021 QMK
  2. *
  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 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "split_util.h"
  17. #include "matrix.h"
  18. #include "keyboard.h"
  19. #include "config.h"
  20. #include "timer.h"
  21. #include "transport.h"
  22. #include "quantum.h"
  23. #include "wait.h"
  24. #include "usb_util.h"
  25. #ifdef EE_HANDS
  26. # include "eeconfig.h"
  27. #endif
  28. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  29. # include "rgblight.h"
  30. #endif
  31. #ifndef SPLIT_USB_TIMEOUT
  32. # define SPLIT_USB_TIMEOUT 2000
  33. #endif
  34. #ifndef SPLIT_USB_TIMEOUT_POLL
  35. # define SPLIT_USB_TIMEOUT_POLL 10
  36. #endif
  37. volatile bool isLeftHand = true;
  38. #if defined(SPLIT_USB_DETECT)
  39. static bool usbIsActive(void) {
  40. for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) {
  41. // This will return true if a USB connection has been established
  42. if (usb_connected_state()) {
  43. return true;
  44. }
  45. wait_ms(SPLIT_USB_TIMEOUT_POLL);
  46. }
  47. return false;
  48. }
  49. #else
  50. static inline bool usbIsActive(void) { return usb_vbus_state(); }
  51. #endif
  52. #ifdef SPLIT_HAND_MATRIX_GRID
  53. void matrix_io_delay(void);
  54. static uint8_t peek_matrix_intersection(pin_t out_pin, pin_t in_pin) {
  55. setPinInputHigh(in_pin);
  56. setPinOutput(out_pin);
  57. writePinLow(out_pin);
  58. // It's almost unnecessary, but wait until it's down to low, just in case.
  59. wait_us(1);
  60. uint8_t pin_state = readPin(in_pin);
  61. // Set out_pin to a setting that is less susceptible to noise.
  62. setPinInputHigh(out_pin);
  63. matrix_io_delay(); // Wait for the pull-up to go HIGH.
  64. return pin_state;
  65. }
  66. #endif
  67. __attribute__((weak)) bool is_keyboard_left(void) {
  68. #if defined(SPLIT_HAND_PIN)
  69. // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
  70. setPinInput(SPLIT_HAND_PIN);
  71. return readPin(SPLIT_HAND_PIN);
  72. #elif defined(SPLIT_HAND_MATRIX_GRID)
  73. # ifdef SPLIT_HAND_MATRIX_GRID_LOW_IS_RIGHT
  74. return peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID);
  75. # else
  76. return !peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID);
  77. # endif
  78. #elif defined(EE_HANDS)
  79. return eeconfig_read_handedness();
  80. #elif defined(MASTER_RIGHT)
  81. return !is_keyboard_master();
  82. #endif
  83. return is_keyboard_master();
  84. }
  85. __attribute__((weak)) bool is_keyboard_master(void) {
  86. static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
  87. // only check once, as this is called often
  88. if (usbstate == UNKNOWN) {
  89. usbstate = usbIsActive() ? MASTER : SLAVE;
  90. // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
  91. if (usbstate == SLAVE) {
  92. usb_disable();
  93. }
  94. }
  95. return (usbstate == MASTER);
  96. }
  97. // this code runs before the keyboard is fully initialized
  98. void split_pre_init(void) {
  99. isLeftHand = is_keyboard_left();
  100. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  101. uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
  102. if (isLeftHand) {
  103. rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
  104. } else {
  105. rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
  106. }
  107. #endif
  108. if (is_keyboard_master()) {
  109. #if defined(USE_I2C) && defined(SSD1306OLED)
  110. matrix_master_OLED_init();
  111. #endif
  112. transport_master_init();
  113. }
  114. }
  115. // this code runs after the keyboard is fully initialized
  116. // - avoids race condition during matrix_init_quantum where slave can start
  117. // receiving before the init process has completed
  118. void split_post_init(void) {
  119. if (!is_keyboard_master()) {
  120. transport_slave_init();
  121. }
  122. }