split_util.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. __attribute__((weak))
  30. bool is_keyboard_master(void)
  31. {
  32. #ifdef __AVR__
  33. static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
  34. // only check once, as this is called often
  35. if (usbstate == UNKNOWN)
  36. {
  37. USBCON |= (1 << OTGPADE); // enables VBUS pad
  38. wait_us(5);
  39. usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
  40. }
  41. return (usbstate == MASTER);
  42. #else
  43. return true;
  44. #endif
  45. }
  46. static void keyboard_master_setup(void) {
  47. #if defined(USE_I2C) || defined(EH)
  48. #ifdef SSD1306OLED
  49. matrix_master_OLED_init ();
  50. #endif
  51. #endif
  52. transport_master_init();
  53. }
  54. static void keyboard_slave_setup(void)
  55. {
  56. transport_slave_init();
  57. }
  58. // this code runs before the usb and keyboard is initialized
  59. void matrix_setup(void)
  60. {
  61. isLeftHand = is_keyboard_left();
  62. #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
  63. uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
  64. if (isLeftHand) {
  65. rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
  66. }
  67. else {
  68. rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
  69. }
  70. #endif
  71. if (is_keyboard_master())
  72. {
  73. keyboard_master_setup();
  74. }
  75. else
  76. {
  77. keyboard_slave_setup();
  78. }
  79. }