You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.6 KiB

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