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.

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