split_util.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. # ifdef SPLIT_HAND_PIN_LOW_IS_LEFT
  72. return !readPin(SPLIT_HAND_PIN);
  73. # else
  74. return readPin(SPLIT_HAND_PIN);
  75. # endif
  76. #elif defined(SPLIT_HAND_MATRIX_GRID)
  77. # ifdef SPLIT_HAND_MATRIX_GRID_LOW_IS_RIGHT
  78. return peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID);
  79. # else
  80. return !peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID);
  81. # endif
  82. #elif defined(EE_HANDS)
  83. return eeconfig_read_handedness();
  84. #elif defined(MASTER_RIGHT)
  85. return !is_keyboard_master();
  86. #endif
  87. return is_keyboard_master();
  88. }
  89. __attribute__((weak)) bool is_keyboard_master(void) {
  90. static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
  91. // only check once, as this is called often
  92. if (usbstate == UNKNOWN) {
  93. usbstate = usbIsActive() ? MASTER : SLAVE;
  94. // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
  95. if (usbstate == SLAVE) {
  96. usb_disconnect();
  97. }
  98. }
  99. return (usbstate == MASTER);
  100. }
  101. // this code runs before the keyboard is fully initialized
  102. void split_pre_init(void) {
  103. isLeftHand = is_keyboard_left();
  104. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  105. uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
  106. if (isLeftHand) {
  107. rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
  108. } else {
  109. rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
  110. }
  111. #endif
  112. if (is_keyboard_master()) {
  113. #if defined(USE_I2C) && defined(SSD1306OLED)
  114. matrix_master_OLED_init();
  115. #endif
  116. transport_master_init();
  117. }
  118. }
  119. // this code runs after the keyboard is fully initialized
  120. // - avoids race condition during matrix_init_quantum where slave can start
  121. // receiving before the init process has completed
  122. void split_post_init(void) {
  123. if (!is_keyboard_master()) {
  124. transport_slave_init();
  125. }
  126. }