split_util.c 3.3 KB

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