split_util.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. static void keyboard_master_setup(void) {
  70. #if defined(USE_I2C)
  71. # ifdef SSD1306OLED
  72. matrix_master_OLED_init();
  73. # endif
  74. #endif
  75. transport_master_init();
  76. }
  77. static void keyboard_slave_setup(void) { transport_slave_init(); }
  78. // this code runs before the keyboard is fully initialized
  79. void keyboard_split_setup(void) {
  80. isLeftHand = is_keyboard_left();
  81. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  82. uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
  83. if (isLeftHand) {
  84. rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
  85. } else {
  86. rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
  87. }
  88. #endif
  89. if (is_keyboard_master()) {
  90. keyboard_master_setup();
  91. } else {
  92. keyboard_slave_setup();
  93. }
  94. }