]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/lets_split/split_util.c
Merge remote-tracking branch 'upstream/master'
[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     #ifdef I2C_MASTER_RIGHT
25       isLeftHand = !has_usb();
26     #else
27       isLeftHand = has_usb();
28     #endif
29   #endif
30 }
31
32 static void keyboard_master_setup(void) {
33 #ifdef USE_I2C
34     i2c_master_init();
35 #else
36     serial_master_init();
37 #endif
38 }
39
40 static void keyboard_slave_setup(void) {
41 #ifdef USE_I2C
42     i2c_slave_init(SLAVE_I2C_ADDRESS);
43 #else
44     serial_slave_init();
45 #endif
46 }
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
54 void split_keyboard_setup(void) {
55    setup_handedness();
56
57    if (has_usb()) {
58       keyboard_master_setup();
59    } else {
60       keyboard_slave_setup();
61    }
62    sei();
63 }
64
65 void keyboard_slave_loop(void) {
66    matrix_init();
67
68    while (1) {
69       matrix_slave_scan();
70    }
71 }
72
73 // this code runs before the usb and keyboard is initialized
74 void matrix_setup(void) {
75     split_keyboard_setup();
76
77     if (!has_usb()) {
78         keyboard_slave_loop();
79     }
80 }