split_util.c 3.4 KB

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