split_util.c 1.4 KB

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