split_util.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 RGBLIGHT_ENABLE
  14. # include "rgblight.h"
  15. #endif
  16. #ifdef BACKLIGHT_ENABLE
  17. # include "backlight.h"
  18. #endif
  19. #ifdef SPLIT_HAND_PIN
  20. # include "pincontrol.h"
  21. #endif
  22. #if defined(USE_I2C) || defined(EH)
  23. # include "i2c.h"
  24. #else
  25. # include "serial.h"
  26. #endif
  27. volatile bool isLeftHand = true;
  28. volatile uint8_t setTries = 0;
  29. static void setup_handedness(void) {
  30. #ifdef SPLIT_HAND_PIN
  31. // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
  32. pinMode(SPLIT_HAND_PIN, PinDirectionInput);
  33. isLeftHand = digitalRead(SPLIT_HAND_PIN);
  34. #else
  35. #ifdef EE_HANDS
  36. isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
  37. #else
  38. // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c
  39. #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
  40. isLeftHand = !has_usb();
  41. #else
  42. isLeftHand = has_usb();
  43. #endif
  44. #endif
  45. #endif
  46. }
  47. static void keyboard_master_setup(void) {
  48. #if defined(USE_I2C) || defined(EH)
  49. i2c_master_init();
  50. #ifdef SSD1306OLED
  51. matrix_master_OLED_init ();
  52. #endif
  53. #else
  54. serial_master_init();
  55. #endif
  56. // For master the Backlight info needs to be sent on startup
  57. // Otherwise the salve won't start with the proper info until an update
  58. BACKLIT_DIRTY = true;
  59. }
  60. static void keyboard_slave_setup(void) {
  61. timer_init();
  62. #if defined(USE_I2C) || defined(EH)
  63. i2c_slave_init(SLAVE_I2C_ADDRESS);
  64. #else
  65. serial_slave_init();
  66. #endif
  67. }
  68. bool has_usb(void) {
  69. USBCON |= (1 << OTGPADE); //enables VBUS pad
  70. _delay_us(5);
  71. return (USBSTA & (1<<VBUS)); //checks state of VBUS
  72. }
  73. void split_keyboard_setup(void) {
  74. setup_handedness();
  75. if (has_usb()) {
  76. keyboard_master_setup();
  77. } else {
  78. keyboard_slave_setup();
  79. }
  80. sei();
  81. }
  82. void keyboard_slave_loop(void) {
  83. matrix_init();
  84. //Init RGB
  85. #ifdef RGBLIGHT_ENABLE
  86. rgblight_init();
  87. #endif
  88. while (1) {
  89. matrix_slave_scan();
  90. // read backlight info
  91. #ifdef BACKLIGHT_ENABLE
  92. if (BACKLIT_DIRTY) {
  93. backlight_set(i2c_slave_buffer[I2C_BACKLIT_START]);
  94. BACKLIT_DIRTY = false;
  95. }
  96. #endif
  97. #ifdef RGBLIGHT_ENABLE
  98. if (RGB_DIRTY) {
  99. cli();
  100. uint32_t dword;
  101. /*dword = i2c_slave_buffer[I2C_RGB_START + 3];
  102. dword = (dword << 8) + i2c_slave_buffer[I2C_RGB_START + 2];
  103. dword = (dword << 8) + i2c_slave_buffer[I2C_RGB_START + 1];
  104. dword = (dword << 8) + i2c_slave_buffer[I2C_RGB_START];*/
  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. rgblight_update_dword(dword);
  110. RGB_DIRTY = false;
  111. sei();
  112. }
  113. #endif
  114. }
  115. }
  116. // this code runs before the usb and keyboard is initialized
  117. void matrix_setup(void) {
  118. split_keyboard_setup();
  119. if (!has_usb()) {
  120. //rgblight_init();
  121. keyboard_slave_loop();
  122. }
  123. }