]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/split_util.c
Small fix to allow board to override split keyboard master check
[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 #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
15 #include "rgblight.h"
16 #endif
17
18 volatile bool isLeftHand = true;
19
20 __attribute__((weak))
21 bool is_keyboard_left(void) {
22   #if defined(SPLIT_HAND_PIN)
23     // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
24     setPinInput(SPLIT_HAND_PIN);
25     return readPin(SPLIT_HAND_PIN);
26   #elif defined(EE_HANDS)
27     return eeprom_read_byte(EECONFIG_HANDEDNESS);
28   #elif defined(MASTER_RIGHT)
29     return !is_keyboard_master();
30   #endif
31
32   return is_keyboard_master();
33 }
34
35 __attribute__((weak))
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
65 static void keyboard_slave_setup(void)
66 {
67   transport_slave_init();
68 }
69
70 // this code runs before the usb and keyboard is initialized
71 void matrix_setup(void)
72 {
73   isLeftHand = is_keyboard_left();
74
75 #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
76   uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
77   if (isLeftHand) {
78     rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
79   }
80   else {
81     rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);
82   }
83 #endif
84
85   if (is_keyboard_master())
86   {
87     keyboard_master_setup();
88   }
89   else
90   {
91     keyboard_slave_setup();
92   }
93 }