split_util.c 1.6 KB

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