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.

86 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. #ifdef USE_MATRIX_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. #ifdef SSD1306OLED
  32. matrix_master_OLED_init ();
  33. #endif
  34. #endif
  35. #ifdef USE_MATRIX_I2C
  36. i2c_master_init();
  37. #else
  38. serial_master_init();
  39. #endif
  40. }
  41. static void keyboard_slave_setup(void) {
  42. #ifdef USE_MATRIX_I2C
  43. i2c_slave_init(SLAVE_I2C_ADDRESS);
  44. #else
  45. serial_slave_init();
  46. #endif
  47. }
  48. bool has_usb(void) {
  49. USBCON |= (1 << OTGPADE); //enables VBUS pad
  50. _delay_us(5);
  51. return (USBSTA & (1<<VBUS)); //checks state of VBUS
  52. }
  53. void split_keyboard_setup(void) {
  54. setup_handedness();
  55. if (has_usb()) {
  56. keyboard_master_setup();
  57. } else {
  58. keyboard_slave_setup();
  59. }
  60. sei();
  61. }
  62. void keyboard_slave_loop(void) {
  63. matrix_init();
  64. while (1) {
  65. matrix_slave_scan();
  66. }
  67. }
  68. // this code runs before the usb and keyboard is initialized
  69. void matrix_setup(void) {
  70. split_keyboard_setup();
  71. if (!has_usb()) {
  72. keyboard_slave_loop();
  73. }
  74. }