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.

80 lines
1.4 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. #include "pincontrol.h"
  13. #ifdef USE_I2C
  14. # include "i2c.h"
  15. #else
  16. # include "serial.h"
  17. #endif
  18. volatile bool isLeftHand = true;
  19. static void setup_handedness(void) {
  20. // Test D2 pin for handedness, if D2 is grounded, it's the right hand
  21. pinMode(D2, PinDirectionInput);
  22. isLeftHand = digitalRead(D2);
  23. }
  24. static void keyboard_master_setup(void) {
  25. #ifdef USE_I2C
  26. i2c_master_init();
  27. #ifdef SSD1306OLED
  28. matrix_master_OLED_init();
  29. #endif
  30. #else
  31. serial_master_init();
  32. #endif
  33. }
  34. static void keyboard_slave_setup(void) {
  35. timer_init();
  36. #ifdef USE_I2C
  37. i2c_slave_init(SLAVE_I2C_ADDRESS);
  38. #else
  39. serial_slave_init();
  40. #endif
  41. }
  42. bool has_usb(void) {
  43. USBCON |= (1 << OTGPADE); //enables VBUS pad
  44. _delay_us(5);
  45. return (USBSTA & (1<<VBUS)); //checks state of VBUS
  46. }
  47. void split_keyboard_setup(void) {
  48. setup_handedness();
  49. if (has_usb()) {
  50. keyboard_master_setup();
  51. } else {
  52. keyboard_slave_setup();
  53. }
  54. sei();
  55. }
  56. void keyboard_slave_loop(void) {
  57. matrix_init();
  58. while (1) {
  59. matrix_slave_scan();
  60. }
  61. }
  62. // this code runs before the usb and keyboard is initialized
  63. void matrix_setup(void) {
  64. split_keyboard_setup();
  65. if (!has_usb()) {
  66. keyboard_slave_loop();
  67. }
  68. }