split_util.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 "serial.h"
  11. volatile bool isLeftHand = true;
  12. static void setup_handedness(void) {
  13. #ifdef EE_HANDS
  14. isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
  15. #else
  16. #if defined(MASTER_RIGHT)
  17. isLeftHand = !has_usb();
  18. #else
  19. isLeftHand = has_usb();
  20. #endif
  21. #endif
  22. }
  23. static void keyboard_master_setup(void) {
  24. serial_master_init();
  25. }
  26. static void keyboard_slave_setup(void) {
  27. serial_slave_init();
  28. }
  29. bool has_usb(void) {
  30. USBCON |= (1 << OTGPADE); //enables VBUS pad
  31. _delay_us(5);
  32. return (USBSTA & (1<<VBUS)); //checks state of VBUS
  33. }
  34. void split_keyboard_setup(void) {
  35. setup_handedness();
  36. if (has_usb()) {
  37. keyboard_master_setup();
  38. } else {
  39. keyboard_slave_setup();
  40. }
  41. sei();
  42. }
  43. // this code runs before the usb and keyboard is initialized
  44. void matrix_setup(void) {
  45. split_keyboard_setup();
  46. }