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