split_util.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "tmk_core/common/eeprom.h"
  10. # include "eeconfig.h"
  11. #endif
  12. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  13. #include "rgblight.h"
  14. #endif
  15. volatile bool isLeftHand = true;
  16. __attribute__((weak))
  17. bool is_keyboard_left(void) {
  18. #if defined(SPLIT_HAND_PIN)
  19. // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
  20. setPinInput(SPLIT_HAND_PIN);
  21. return readPin(SPLIT_HAND_PIN);
  22. #elif defined(EE_HANDS)
  23. return eeprom_read_byte(EECONFIG_HANDEDNESS);
  24. #elif defined(MASTER_RIGHT)
  25. return !is_keyboard_master();
  26. #endif
  27. return is_keyboard_master();
  28. }
  29. bool is_keyboard_master(void)
  30. {
  31. #ifdef __AVR__
  32. static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
  33. // only check once, as this is called often
  34. if (usbstate == UNKNOWN)
  35. {
  36. USBCON |= (1 << OTGPADE); // enables VBUS pad
  37. wait_us(5);
  38. usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
  39. }
  40. return (usbstate == MASTER);
  41. #else
  42. return true;
  43. #endif
  44. }
  45. static void keyboard_master_setup(void) {
  46. #if defined(USE_I2C) || defined(EH)
  47. #ifdef SSD1306OLED
  48. matrix_master_OLED_init ();
  49. #endif
  50. #endif
  51. transport_master_init();
  52. }
  53. static void keyboard_slave_setup(void)
  54. {
  55. transport_slave_init();
  56. }
  57. // this code runs before the usb and keyboard is initialized
  58. void matrix_setup(void)
  59. {
  60. isLeftHand = is_keyboard_left();
  61. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  62. uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
  63. if (isLeftHand) {
  64. rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
  65. }
  66. else {
  67. rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
  68. }
  69. #endif
  70. if (is_keyboard_master())
  71. {
  72. keyboard_master_setup();
  73. }
  74. else
  75. {
  76. keyboard_slave_setup();
  77. }
  78. }