diff options
Diffstat (limited to 'quantum/split_common')
| -rw-r--r-- | quantum/split_common/split_util.c | 73 |
1 files changed, 40 insertions, 33 deletions
diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c index 5d044c4ea0..874339361d 100644 --- a/quantum/split_common/split_util.c +++ b/quantum/split_common/split_util.c | |||
| @@ -56,6 +56,11 @@ static uint8_t connection_errors = 0; | |||
| 56 | 56 | ||
| 57 | volatile bool isLeftHand = true; | 57 | volatile bool isLeftHand = true; |
| 58 | 58 | ||
| 59 | static struct { | ||
| 60 | bool master; | ||
| 61 | bool left; | ||
| 62 | } split_config; | ||
| 63 | |||
| 59 | #if defined(SPLIT_USB_DETECT) | 64 | #if defined(SPLIT_USB_DETECT) |
| 60 | _Static_assert((SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL) <= UINT16_MAX, "Please lower SPLIT_USB_TIMEOUT and/or increase SPLIT_USB_TIMEOUT_POLL."); | 65 | _Static_assert((SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL) <= UINT16_MAX, "Please lower SPLIT_USB_TIMEOUT and/or increase SPLIT_USB_TIMEOUT_POLL."); |
| 61 | static bool usbIsActive(void) { | 66 | static bool usbIsActive(void) { |
| @@ -131,8 +136,10 @@ static uint8_t peek_matrix_intersection(pin_t out_pin, pin_t in_pin) { | |||
| 131 | } | 136 | } |
| 132 | #endif | 137 | #endif |
| 133 | 138 | ||
| 134 | __attribute__((weak)) bool is_keyboard_left(void) { | 139 | __attribute__((weak)) bool is_keyboard_left_impl(void) { |
| 135 | #if defined(SPLIT_HAND_PIN) | 140 | #if defined(SPLIT_HAND_PIN) |
| 141 | setPinInput(SPLIT_HAND_PIN); | ||
| 142 | wait_us(100); | ||
| 136 | // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand | 143 | // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand |
| 137 | # ifdef SPLIT_HAND_PIN_LOW_IS_LEFT | 144 | # ifdef SPLIT_HAND_PIN_LOW_IS_LEFT |
| 138 | return !readPin(SPLIT_HAND_PIN); | 145 | return !readPin(SPLIT_HAND_PIN); |
| @@ -146,36 +153,6 @@ __attribute__((weak)) bool is_keyboard_left(void) { | |||
| 146 | return !peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID); | 153 | return !peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID); |
| 147 | # endif | 154 | # endif |
| 148 | #elif defined(EE_HANDS) | 155 | #elif defined(EE_HANDS) |
| 149 | return eeconfig_read_handedness(); | ||
| 150 | #elif defined(MASTER_RIGHT) | ||
| 151 | return !is_keyboard_master(); | ||
| 152 | #endif | ||
| 153 | |||
| 154 | return is_keyboard_master(); | ||
| 155 | } | ||
| 156 | |||
| 157 | __attribute__((weak)) bool is_keyboard_master(void) { | ||
| 158 | static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN; | ||
| 159 | |||
| 160 | // only check once, as this is called often | ||
| 161 | if (usbstate == UNKNOWN) { | ||
| 162 | usbstate = usbIsActive() ? MASTER : SLAVE; | ||
| 163 | |||
| 164 | // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow | ||
| 165 | if (usbstate == SLAVE) { | ||
| 166 | usb_disconnect(); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | return (usbstate == MASTER); | ||
| 171 | } | ||
| 172 | |||
| 173 | // this code runs before the keyboard is fully initialized | ||
| 174 | void split_pre_init(void) { | ||
| 175 | #if defined(SPLIT_HAND_PIN) | ||
| 176 | setPinInput(SPLIT_HAND_PIN); | ||
| 177 | wait_us(100); | ||
| 178 | #elif defined(EE_HANDS) | ||
| 179 | if (!eeconfig_is_enabled()) { | 156 | if (!eeconfig_is_enabled()) { |
| 180 | eeconfig_init(); | 157 | eeconfig_init(); |
| 181 | } | 158 | } |
| @@ -193,12 +170,42 @@ void split_pre_init(void) { | |||
| 193 | eeconfig_update_handedness(should_be_left); | 170 | eeconfig_update_handedness(should_be_left); |
| 194 | } | 171 | } |
| 195 | # endif // defined(INIT_EE_HANDS_LEFT) || defined(INIT_EE_HANDS_RIGHT) | 172 | # endif // defined(INIT_EE_HANDS_LEFT) || defined(INIT_EE_HANDS_RIGHT) |
| 173 | return eeconfig_read_handedness(); | ||
| 174 | #elif defined(MASTER_RIGHT) | ||
| 175 | return !is_keyboard_master(); | ||
| 176 | #else | ||
| 177 | return is_keyboard_master(); | ||
| 196 | #endif | 178 | #endif |
| 197 | isLeftHand = is_keyboard_left(); | 179 | } |
| 180 | |||
| 181 | __attribute__((weak)) bool is_keyboard_master_impl(void) { | ||
| 182 | bool is_master = usbIsActive(); | ||
| 183 | |||
| 184 | // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow | ||
| 185 | if (!is_master) { | ||
| 186 | usb_disconnect(); | ||
| 187 | } | ||
| 188 | return is_master; | ||
| 189 | } | ||
| 190 | |||
| 191 | __attribute__((weak)) bool is_keyboard_left(void) { | ||
| 192 | return split_config.left; | ||
| 193 | } | ||
| 194 | |||
| 195 | __attribute__((weak)) bool is_keyboard_master(void) { | ||
| 196 | return split_config.master; | ||
| 197 | } | ||
| 198 | |||
| 199 | // this code runs before the keyboard is fully initialized | ||
| 200 | void split_pre_init(void) { | ||
| 201 | split_config.master = is_keyboard_master_impl(); | ||
| 202 | split_config.left = is_keyboard_left_impl(); | ||
| 203 | |||
| 204 | isLeftHand = is_keyboard_left(); // TODO: Remove isLeftHand | ||
| 198 | 205 | ||
| 199 | #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) | 206 | #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) |
| 200 | uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT; | 207 | uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT; |
| 201 | if (isLeftHand) { | 208 | if (is_keyboard_left()) { |
| 202 | rgblight_set_clipping_range(0, num_rgb_leds_split[0]); | 209 | rgblight_set_clipping_range(0, num_rgb_leds_split[0]); |
| 203 | } else { | 210 | } else { |
| 204 | rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]); | 211 | rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]); |
