]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/split_util.c
rgblight.c: add remap feature (#5243)
[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 "transport.h"
7 #include "quantum.h"
8
9 #ifdef EE_HANDS
10 #   include "tmk_core/common/eeprom.h"
11 #   include "eeconfig.h"
12 #endif
13
14 volatile bool isLeftHand = true;
15
16 __attribute__((weak))
17 bool is_keyboard_left(void) {
18   #ifdef SPLIT_HAND_PIN
19     // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
20     setPinInput(SPLIT_HAND_PIN);
21     return readPin(SPLIT_HAND_PIN);
22   #else
23     #ifdef EE_HANDS
24       return eeprom_read_byte(EECONFIG_HANDEDNESS);
25     #else
26       #ifdef MASTER_RIGHT
27         return !is_keyboard_master();
28       #else
29         return is_keyboard_master();
30       #endif
31     #endif
32   #endif
33 }
34
35 bool is_keyboard_master(void)
36 {
37 #ifdef __AVR__
38   static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
39
40   // only check once, as this is called often
41   if (usbstate == UNKNOWN)
42   {
43     USBCON |= (1 << OTGPADE);  // enables VBUS pad
44     wait_us(5);
45
46     usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE;  // checks state of VBUS
47   }
48
49   return (usbstate == MASTER);
50 #else
51   return true;
52 #endif
53 }
54
55 static void keyboard_master_setup(void) {
56 #if defined(USE_I2C) || defined(EH)
57   #ifdef SSD1306OLED
58     matrix_master_OLED_init ();
59   #endif
60 #endif
61   transport_master_init();
62 }
63
64 static void keyboard_slave_setup(void)
65 {
66   transport_slave_init();
67 }
68
69 // this code runs before the usb and keyboard is initialized
70 void matrix_setup(void)
71 {
72   isLeftHand = is_keyboard_left();
73
74   if (is_keyboard_master())
75   {
76     keyboard_master_setup();
77   }
78   else
79   {
80     keyboard_slave_setup();
81   }
82 }