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