split_util.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // Max number of consecutive failed communications (one per scan cycle) before the communication is seen as disconnected.
  38. // Set to 0 to disable the disconnection check altogether.
  39. #ifndef SPLIT_MAX_CONNECTION_ERRORS
  40. # define SPLIT_MAX_CONNECTION_ERRORS 10
  41. #endif // SPLIT_MAX_CONNECTION_ERRORS
  42. // How long (in milliseconds) to block all connection attempts after the communication has been flagged as disconnected.
  43. // One communication attempt will be allowed everytime this amount of time has passed since the last attempt. If that attempt succeeds, the communication is seen as working again.
  44. // Set to 0 to disable communication throttling while disconnected
  45. #ifndef SPLIT_CONNECTION_CHECK_TIMEOUT
  46. # define SPLIT_CONNECTION_CHECK_TIMEOUT 500
  47. #endif // SPLIT_CONNECTION_CHECK_TIMEOUT
  48. static uint8_t connection_errors = 0;
  49. volatile bool isLeftHand = true;
  50. #if defined(SPLIT_USB_DETECT)
  51. static bool usbIsActive(void) {
  52. for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) {
  53. // This will return true if a USB connection has been established
  54. if (usb_connected_state()) {
  55. return true;
  56. }
  57. wait_ms(SPLIT_USB_TIMEOUT_POLL);
  58. }
  59. return false;
  60. }
  61. #else
  62. static inline bool usbIsActive(void) { return usb_vbus_state(); }
  63. #endif
  64. #ifdef SPLIT_HAND_MATRIX_GRID
  65. void matrix_io_delay(void);
  66. static uint8_t peek_matrix_intersection(pin_t out_pin, pin_t in_pin) {
  67. setPinInputHigh(in_pin);
  68. setPinOutput(out_pin);
  69. writePinLow(out_pin);
  70. // It's almost unnecessary, but wait until it's down to low, just in case.
  71. wait_us(1);
  72. uint8_t pin_state = readPin(in_pin);
  73. // Set out_pin to a setting that is less susceptible to noise.
  74. setPinInputHigh(out_pin);
  75. matrix_io_delay(); // Wait for the pull-up to go HIGH.
  76. return pin_state;
  77. }
  78. #endif
  79. __attribute__((weak)) bool is_keyboard_left(void) {
  80. #if defined(SPLIT_HAND_PIN)
  81. // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
  82. setPinInput(SPLIT_HAND_PIN);
  83. # ifdef SPLIT_HAND_PIN_LOW_IS_LEFT
  84. return !readPin(SPLIT_HAND_PIN);
  85. # else
  86. return readPin(SPLIT_HAND_PIN);
  87. # endif
  88. #elif defined(SPLIT_HAND_MATRIX_GRID)
  89. # ifdef SPLIT_HAND_MATRIX_GRID_LOW_IS_RIGHT
  90. return peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID);
  91. # else
  92. return !peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID);
  93. # endif
  94. #elif defined(EE_HANDS)
  95. return eeconfig_read_handedness();
  96. #elif defined(MASTER_RIGHT)
  97. return !is_keyboard_master();
  98. #endif
  99. return is_keyboard_master();
  100. }
  101. __attribute__((weak)) bool is_keyboard_master(void) {
  102. static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
  103. // only check once, as this is called often
  104. if (usbstate == UNKNOWN) {
  105. usbstate = usbIsActive() ? MASTER : SLAVE;
  106. // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
  107. if (usbstate == SLAVE) {
  108. usb_disconnect();
  109. }
  110. }
  111. return (usbstate == MASTER);
  112. }
  113. // this code runs before the keyboard is fully initialized
  114. void split_pre_init(void) {
  115. isLeftHand = is_keyboard_left();
  116. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  117. uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
  118. if (isLeftHand) {
  119. rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
  120. } else {
  121. rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
  122. }
  123. #endif
  124. if (is_keyboard_master()) {
  125. #if defined(USE_I2C) && defined(SSD1306OLED)
  126. matrix_master_OLED_init();
  127. #endif
  128. transport_master_init();
  129. }
  130. }
  131. // this code runs after the keyboard is fully initialized
  132. // - avoids race condition during matrix_init_quantum where slave can start
  133. // receiving before the init process has completed
  134. void split_post_init(void) {
  135. if (!is_keyboard_master()) {
  136. transport_slave_init();
  137. }
  138. }
  139. bool is_transport_connected(void) { return connection_errors < SPLIT_MAX_CONNECTION_ERRORS; }
  140. bool transport_master_if_connected(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
  141. #if SPLIT_MAX_CONNECTION_ERRORS > 0 && SPLIT_CONNECTION_CHECK_TIMEOUT > 0
  142. // Throttle transaction attempts if target doesn't seem to be connected
  143. // Without this, a solo half becomes unusable due to constant read timeouts
  144. static uint16_t connection_check_timer = 0;
  145. const bool is_disconnected = !is_transport_connected();
  146. if (is_disconnected && timer_elapsed(connection_check_timer) < SPLIT_CONNECTION_CHECK_TIMEOUT) {
  147. return false;
  148. }
  149. #endif // SPLIT_MAX_CONNECTION_ERRORS > 0 && SPLIT_CONNECTION_CHECK_TIMEOUT > 0
  150. __attribute__((unused)) bool okay = transport_master(master_matrix, slave_matrix);
  151. #if SPLIT_MAX_CONNECTION_ERRORS > 0
  152. if (!okay) {
  153. if (connection_errors < UINT8_MAX) {
  154. connection_errors++;
  155. }
  156. # if SPLIT_CONNECTION_CHECK_TIMEOUT > 0
  157. bool connected = is_transport_connected();
  158. if (!connected) {
  159. connection_check_timer = timer_read();
  160. dprintln("Target disconnected, throttling connection attempts");
  161. }
  162. return connected;
  163. } else if (is_disconnected) {
  164. dprintln("Target connected");
  165. # endif // SPLIT_CONNECTION_CHECK_TIMEOUT > 0
  166. }
  167. connection_errors = 0;
  168. #endif // SPLIT_MAX_CONNECTION_ERRORS > 0
  169. return true;
  170. }