]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/lets_split/split_util.c
46586fbc00bb6e7b15ab35eef0440705e0909eb4
[qmk_firmware.git] / keyboards / lets_split / split_util.c
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
12 #ifdef USE_I2C
13 #  include "i2c.h"
14 #else
15 #  include "serial.h"
16 #endif
17
18 volatile bool isLeftHand = true;
19
20 static void setup_handedness(void) {
21   #ifdef EE_HANDS
22     isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
23   #else
24     // I2c_MASTER_RIGHT ish deprecate use MASTER_RIGHT instead since this works for both serial and i2c:
25     #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
26       isLeftHand = !has_usb();
27     #else
28       isLeftHand = has_usb();
29     #endif
30   #endif
31 }
32
33 static void keyboard_master_setup(void) {
34 #ifdef USE_I2C
35     i2c_master_init();
36 #ifdef SSD1306OLED
37     matrix_master_OLED_init ();
38 #endif
39 #else
40     serial_master_init();
41 #endif
42 }
43
44 static void keyboard_slave_setup(void) {
45 #ifdef USE_I2C
46     i2c_slave_init(SLAVE_I2C_ADDRESS);
47 #else
48     serial_slave_init();
49 #endif
50 }
51
52 bool has_usb(void) {
53    USBCON |= (1 << OTGPADE); //enables VBUS pad
54    _delay_us(5);
55    return (USBSTA & (1<<VBUS));  //checks state of VBUS
56 }
57
58 void split_keyboard_setup(void) {
59    setup_handedness();
60
61    if (has_usb()) {
62       keyboard_master_setup();
63    } else {
64       keyboard_slave_setup();
65    }
66    sei();
67 }
68
69 void keyboard_slave_loop(void) {
70    matrix_init();
71
72    while (1) {
73       matrix_slave_scan();
74    }
75 }
76
77 // this code runs before the usb and keyboard is initialized
78 void matrix_setup(void) {
79     split_keyboard_setup();
80
81     if (!has_usb()) {
82         keyboard_slave_loop();
83     }
84 }