split_util.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "quantum.h"
  8. #ifdef BACKLIGHT_ENABLE
  9. # include "backlight.h"
  10. #endif
  11. #if defined(USE_I2C) || defined(EH)
  12. # include "i2c.h"
  13. #endif
  14. volatile bool isLeftHand = true;
  15. volatile uint8_t setTries = 0;
  16. static void setup_handedness(void) {
  17. #ifdef SPLIT_HAND_PIN
  18. // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
  19. setPinInput(SPLIT_HAND_PIN);
  20. isLeftHand = readPin(SPLIT_HAND_PIN);
  21. #else
  22. #ifdef EE_HANDS
  23. isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
  24. #else
  25. #ifdef MASTER_RIGHT
  26. isLeftHand = !has_usb();
  27. #else
  28. isLeftHand = has_usb();
  29. #endif
  30. #endif
  31. #endif
  32. }
  33. static void keyboard_master_setup(void) {
  34. #if defined(USE_I2C) || defined(EH)
  35. i2c_master_init();
  36. #ifdef SSD1306OLED
  37. matrix_master_OLED_init ();
  38. #endif
  39. #else
  40. serial_master_init();
  41. #endif
  42. // For master the Backlight info needs to be sent on startup
  43. // Otherwise the salve won't start with the proper info until an update
  44. BACKLIT_DIRTY = true;
  45. }
  46. static void keyboard_slave_setup(void) {
  47. timer_init();
  48. #if defined(USE_I2C) || defined(EH)
  49. i2c_slave_init(SLAVE_I2C_ADDRESS);
  50. #else
  51. serial_slave_init();
  52. #endif
  53. }
  54. bool has_usb(void) {
  55. USBCON |= (1 << OTGPADE); //enables VBUS pad
  56. _delay_us(5);
  57. return (USBSTA & (1<<VBUS)); //checks state of VBUS
  58. }
  59. void split_keyboard_setup(void) {
  60. setup_handedness();
  61. if (has_usb()) {
  62. keyboard_master_setup();
  63. } else {
  64. keyboard_slave_setup();
  65. }
  66. sei();
  67. }
  68. void keyboard_slave_loop(void) {
  69. matrix_init();
  70. //Init RGB
  71. #ifdef RGBLIGHT_ENABLE
  72. rgblight_init();
  73. #endif
  74. while (1) {
  75. // Matrix Slave Scan
  76. matrix_slave_scan();
  77. // Read Backlight Info
  78. #ifdef BACKLIGHT_ENABLE
  79. #ifdef USE_I2C
  80. if (BACKLIT_DIRTY) {
  81. backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]);
  82. BACKLIT_DIRTY = false;
  83. }
  84. #else // USE_SERIAL
  85. backlight_set(serial_m2s_buffer.backlight_level);
  86. #endif
  87. #endif
  88. // Read RGB Info
  89. #ifdef RGBLIGHT_ENABLE
  90. #ifdef USE_I2C
  91. if (RGB_DIRTY) {
  92. // Disable interupts (RGB data is big)
  93. cli();
  94. // Create new DWORD for RGB data
  95. uint32_t dword;
  96. // Fill the new DWORD with the data that was sent over
  97. uint8_t *dword_dat = (uint8_t *)(&dword);
  98. for (int i = 0; i < 4; i++) {
  99. dword_dat[i] = i2c_slave_buffer[I2C_RGB_START+i];
  100. }
  101. // Update the RGB now with the new data and set RGB_DIRTY to false
  102. rgblight_update_dword(dword);
  103. RGB_DIRTY = false;
  104. // Re-enable interupts now that RGB is set
  105. sei();
  106. }
  107. #else // USE_SERIAL
  108. #ifdef RGBLIGHT_SPLIT
  109. // Add serial implementation for RGB here
  110. #endif
  111. #endif
  112. #endif
  113. }
  114. }
  115. // this code runs before the usb and keyboard is initialized
  116. void matrix_setup(void) {
  117. split_keyboard_setup();
  118. if (!has_usb()) {
  119. //rgblight_init();
  120. keyboard_slave_loop();
  121. }
  122. }