]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergoinu/split_util.c
Usbasploader bootloader option addition (#6304)
[qmk_firmware.git] / keyboards / ergoinu / 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
11 #include "serial.h"
12
13 volatile bool isLeftHand = true;
14
15 static void setup_handedness(void) {
16   #ifdef EE_HANDS
17     isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
18   #else
19     // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c
20     #if defined(I2C_MASTER_RIGHT) || defined(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   serial_master_init();
30 }
31
32 static void keyboard_slave_setup(void) {
33   serial_slave_init();
34 }
35
36 bool has_usb(void) {
37    USBCON |= (1 << OTGPADE); //enables VBUS pad
38    _delay_us(5);
39    return (USBSTA & (1<<VBUS));  //checks state of VBUS
40 }
41
42 void split_keyboard_setup(void) {
43    setup_handedness();
44
45    if (has_usb()) {
46       keyboard_master_setup();
47    } else {
48       keyboard_slave_setup();
49    }
50    sei();
51 }
52
53 // this code runs before the usb and keyboard is initialized
54 void matrix_setup(void) {
55     split_keyboard_setup();
56 }