split_util.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 2500
  16. #endif
  17. volatile bool isLeftHand = true;
  18. bool waitForUsb(void) {
  19. for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / 100); i++) {
  20. // This will return true of a USB connection has been established
  21. #if defined(__AVR__)
  22. if (UDADDR & _BV(ADDEN)) {
  23. #else
  24. if (usbGetDriverStateI(&USBD1) == USB_ACTIVE) {
  25. #endif
  26. return true;
  27. }
  28. wait_ms(100);
  29. }
  30. // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
  31. #if defined(__AVR__)
  32. (USBCON &= ~(_BV(USBE) | _BV(OTGPADE)));
  33. #else
  34. usbStop(&USBD1);
  35. #endif
  36. return false;
  37. }
  38. __attribute__((weak)) bool is_keyboard_left(void) {
  39. #if defined(SPLIT_HAND_PIN)
  40. // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
  41. setPinInput(SPLIT_HAND_PIN);
  42. return readPin(SPLIT_HAND_PIN);
  43. #elif defined(EE_HANDS)
  44. return eeconfig_read_handedness();
  45. #elif defined(MASTER_RIGHT)
  46. return !is_keyboard_master();
  47. #endif
  48. return is_keyboard_master();
  49. }
  50. __attribute__((weak)) bool is_keyboard_master(void) {
  51. static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
  52. // only check once, as this is called often
  53. if (usbstate == UNKNOWN) {
  54. #if defined(SPLIT_USB_DETECT) || defined(PROTOCOL_CHIBIOS)
  55. usbstate = waitForUsb() ? MASTER : SLAVE;
  56. #elif defined(__AVR__)
  57. USBCON |= (1 << OTGPADE); // enables VBUS pad
  58. wait_us(5);
  59. usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
  60. #else
  61. usbstate = MASTER;
  62. #endif
  63. }
  64. return (usbstate == MASTER);
  65. }
  66. static void keyboard_master_setup(void) {
  67. #if defined(USE_I2C)
  68. # ifdef SSD1306OLED
  69. matrix_master_OLED_init();
  70. # endif
  71. #endif
  72. transport_master_init();
  73. }
  74. static void keyboard_slave_setup(void) { transport_slave_init(); }
  75. // this code runs before the keyboard is fully initialized
  76. void keyboard_split_setup(void) {
  77. isLeftHand = is_keyboard_left();
  78. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  79. uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
  80. if (isLeftHand) {
  81. rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
  82. } else {
  83. rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
  84. }
  85. #endif
  86. if (is_keyboard_master()) {
  87. keyboard_master_setup();
  88. } else {
  89. keyboard_slave_setup();
  90. }
  91. }