split_util.c 2.0 KB

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