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. #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. #ifdef SSD1306OLED
  34. matrix_master_OLED_init ();
  35. #endif
  36. #else
  37. serial_master_init();
  38. #endif
  39. }
  40. static void keyboard_slave_setup(void) {
  41. timer_init();
  42. #ifdef USE_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. }