split_util.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "serial.h"
  13. volatile bool isLeftHand = true;
  14. static void setup_handedness(void) {
  15. #ifdef EE_HANDS
  16. isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
  17. #else
  18. // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c
  19. #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
  20. isLeftHand = !has_usb();
  21. #else
  22. isLeftHand = has_usb();
  23. #endif
  24. #endif
  25. }
  26. static void keyboard_master_setup(void) {
  27. #ifdef USE_I2C
  28. i2c_master_init();
  29. #ifdef SSD1306OLED
  30. matrix_master_OLED_init ();
  31. #endif
  32. #else
  33. serial_master_init();
  34. #endif
  35. }
  36. static void keyboard_slave_setup(void) {
  37. timer_init();
  38. #ifdef USE_I2C
  39. i2c_slave_init(SLAVE_I2C_ADDRESS);
  40. #else
  41. serial_slave_init();
  42. #endif
  43. }
  44. bool has_usb(void) {
  45. /* return (UDADDR & _BV(ADDEN)); */
  46. USBCON |= (1 << OTGPADE); //enables VBUS pad
  47. _delay_us(5);
  48. return (USBSTA & (1<<VBUS)); //checks state of VBUS
  49. }
  50. void split_keyboard_setup(void) {
  51. setup_handedness();
  52. if (isLeftHand) {
  53. /* if (has_usb()) { */
  54. keyboard_master_setup();
  55. } else {
  56. keyboard_slave_setup();
  57. }
  58. sei();
  59. }
  60. void keyboard_slave_loop(void) {
  61. matrix_init();
  62. while (1) {
  63. matrix_slave_scan();
  64. }
  65. }
  66. // this code runs before the usb and keyboard is initialized
  67. void matrix_setup(void) {
  68. split_keyboard_setup();
  69. if (!isLeftHand) {
  70. /* if (!has_usb()) { */
  71. keyboard_slave_loop();
  72. }
  73. }