split_util.c 1.9 KB

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