You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.7 KiB

  1. #include "split_util.h"
  2. #include "matrix.h"
  3. #include "keyboard.h"
  4. #include "config.h"
  5. #include "timer.h"
  6. #include "split_flags.h"
  7. #include "transport.h"
  8. #include "quantum.h"
  9. #ifdef EE_HANDS
  10. # include "eeprom.h"
  11. # include "eeconfig.h"
  12. #endif
  13. volatile bool isLeftHand = true;
  14. __attribute__((weak))
  15. bool is_keyboard_left(void) {
  16. #ifdef 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. #else
  21. #ifdef EE_HANDS
  22. return eeprom_read_byte(EECONFIG_HANDEDNESS);
  23. #else
  24. #ifdef MASTER_RIGHT
  25. return !is_keyboard_master();
  26. #else
  27. return is_keyboard_master();
  28. #endif
  29. #endif
  30. #endif
  31. }
  32. bool is_keyboard_master(void)
  33. {
  34. #ifdef __AVR__
  35. static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
  36. // only check once, as this is called often
  37. if (usbstate == UNKNOWN)
  38. {
  39. USBCON |= (1 << OTGPADE); // enables VBUS pad
  40. wait_us(5);
  41. usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
  42. }
  43. return (usbstate == MASTER);
  44. #else
  45. return true;
  46. #endif
  47. }
  48. static void keyboard_master_setup(void) {
  49. #if defined(USE_I2C)
  50. #ifdef SSD1306OLED
  51. matrix_master_OLED_init ();
  52. #endif
  53. #endif
  54. transport_master_init();
  55. // For master the Backlight info needs to be sent on startup
  56. // Otherwise the salve won't start with the proper info until an update
  57. BACKLIT_DIRTY = true;
  58. }
  59. static void keyboard_slave_setup(void)
  60. {
  61. transport_slave_init();
  62. }
  63. // this code runs before the usb and keyboard is initialized
  64. void matrix_setup(void)
  65. {
  66. isLeftHand = is_keyboard_left();
  67. if (is_keyboard_master())
  68. {
  69. keyboard_master_setup();
  70. }
  71. else
  72. {
  73. keyboard_slave_setup();
  74. }
  75. }