split_util.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "split_util.h"
  2. #include "matrix.h"
  3. #include "keyboard.h"
  4. #include "config.h"
  5. #include "timer.h"
  6. #include "transport.h"
  7. #include "quantum.h"
  8. #ifdef EE_HANDS
  9. # include "eeconfig.h"
  10. #endif
  11. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  12. # include "rgblight.h"
  13. #endif
  14. #ifndef SPLIT_USB_TIMEOUT
  15. # define SPLIT_USB_TIMEOUT 2000
  16. #endif
  17. #ifndef SPLIT_USB_TIMEOUT_POLL
  18. # define SPLIT_USB_TIMEOUT_POLL 10
  19. #endif
  20. volatile bool isLeftHand = true;
  21. bool waitForUsb(void) {
  22. for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) {
  23. // This will return true if a USB connection has been established
  24. #if defined(__AVR__)
  25. if (UDADDR & _BV(ADDEN)) {
  26. #else
  27. if (usbGetDriverStateI(&USBD1) == USB_ACTIVE) {
  28. #endif
  29. return true;
  30. }
  31. wait_ms(SPLIT_USB_TIMEOUT_POLL);
  32. }
  33. // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
  34. #if defined(__AVR__)
  35. (USBCON &= ~(_BV(USBE) | _BV(OTGPADE)));
  36. #else
  37. usbStop(&USBD1);
  38. #endif
  39. return false;
  40. }
  41. __attribute__((weak)) bool is_keyboard_left(void) {
  42. #if defined(SPLIT_HAND_PIN)
  43. // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
  44. setPinInput(SPLIT_HAND_PIN);
  45. return readPin(SPLIT_HAND_PIN);
  46. #elif defined(EE_HANDS)
  47. return eeconfig_read_handedness();
  48. #elif defined(MASTER_RIGHT)
  49. return !is_keyboard_master();
  50. #endif
  51. return is_keyboard_master();
  52. }
  53. __attribute__((weak)) bool is_keyboard_master(void) {
  54. static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
  55. // only check once, as this is called often
  56. if (usbstate == UNKNOWN) {
  57. #if defined(SPLIT_USB_DETECT) || defined(PROTOCOL_CHIBIOS)
  58. usbstate = waitForUsb() ? MASTER : SLAVE;
  59. #elif defined(__AVR__)
  60. USBCON |= (1 << OTGPADE); // enables VBUS pad
  61. wait_us(5);
  62. usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
  63. #else
  64. usbstate = MASTER;
  65. #endif
  66. }
  67. return (usbstate == MASTER);
  68. }
  69. // this code runs before the keyboard is fully initialized
  70. void split_pre_init(void) {
  71. isLeftHand = is_keyboard_left();
  72. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  73. uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
  74. if (isLeftHand) {
  75. rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
  76. } else {
  77. rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
  78. }
  79. #endif
  80. if (is_keyboard_master()) {
  81. #if defined(USE_I2C) && defined(SSD1306OLED)
  82. matrix_master_OLED_init();
  83. #endif
  84. transport_master_init();
  85. }
  86. }
  87. // this code runs after the keyboard is fully initialized
  88. // - avoids race condition during matrix_init_quantum where slave can start
  89. // receiving before the init process has completed
  90. void split_post_init(void) {
  91. if (!is_keyboard_master()) {
  92. transport_slave_init();
  93. }
  94. }