split_util.c 3.6 KB

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