split_util.c 1.5 KB

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