]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/split_util.c
DO NOT USE Merge branch 'master' into debounce_refactor
[qmk_firmware.git] / quantum / split_common / split_util.c
1 #include "split_util.h"
2 #include "matrix.h"
3 #include "keyboard.h"
4 #include "config.h"
5 #include "timer.h"
6 #include "split_flags.h"
7 #include "transport.h"
8 #include "quantum.h"
9
10 #ifdef EE_HANDS
11 #   include "tmk_core/common/eeprom.h"
12 #   include "eeconfig.h"
13 #endif
14
15 volatile bool isLeftHand = true;
16
17 __attribute__((weak))
18 bool is_keyboard_left(void) {
19   #ifdef SPLIT_HAND_PIN
20     // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
21     setPinInput(SPLIT_HAND_PIN);
22     return readPin(SPLIT_HAND_PIN);
23   #else
24     #ifdef EE_HANDS
25       return eeprom_read_byte(EECONFIG_HANDEDNESS);
26     #else
27       #ifdef MASTER_RIGHT
28         return !is_keyboard_master();
29       #else
30         return is_keyboard_master();
31       #endif
32     #endif
33   #endif
34 }
35
36 bool is_keyboard_master(void)
37 {
38 #ifdef __AVR__
39   static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
40
41   // only check once, as this is called often
42   if (usbstate == UNKNOWN)
43   {
44     USBCON |= (1 << OTGPADE);  // enables VBUS pad
45     wait_us(5);
46
47     usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE;  // checks state of VBUS
48   }
49
50   return (usbstate == MASTER);
51 #else
52   return true;
53 #endif
54 }
55
56 static void keyboard_master_setup(void) {
57 #if defined(USE_I2C) || defined(EH)
58   #ifdef SSD1306OLED
59     matrix_master_OLED_init ();
60   #endif
61 #endif
62   transport_master_init();
63
64   // For master the Backlight info needs to be sent on startup
65   // Otherwise the salve won't start with the proper info until an update
66   BACKLIT_DIRTY = true;
67 }
68
69 static void keyboard_slave_setup(void)
70 {
71   transport_slave_init();
72 }
73
74 // this code runs before the usb and keyboard is initialized
75 void matrix_setup(void)
76 {
77   isLeftHand = is_keyboard_left();
78
79   if (is_keyboard_master())
80   {
81     keyboard_master_setup();
82   }
83   else
84   {
85     keyboard_slave_setup();
86   }
87 }