split_util.c 1.4 KB

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