split_util.c 2.6 KB

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