~ruther/qmk_firmware

79094d3f72709be3c55bd8f22cc0a7e98f4b6564 — Dasky 1 year, 6 months ago cf7d343
Modify split config is_keyboard_master/left checks. (#21875)

* rework split configuration

* Apply suggestions from code review

Co-authored-by: Joel Challis <git@zvecr.com>

* lint

* missed return

---------

Co-authored-by: Joel Challis <git@zvecr.com>
1 files changed, 40 insertions(+), 33 deletions(-)

M quantum/split_common/split_util.c
M quantum/split_common/split_util.c => quantum/split_common/split_util.c +40 -33
@@ 56,6 56,11 @@ static uint8_t connection_errors = 0;

volatile bool isLeftHand = true;

static struct {
    bool master;
    bool left;
} split_config;

#if defined(SPLIT_USB_DETECT)
_Static_assert((SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL) <= UINT16_MAX, "Please lower SPLIT_USB_TIMEOUT and/or increase SPLIT_USB_TIMEOUT_POLL.");
static bool usbIsActive(void) {


@@ 131,8 136,10 @@ static uint8_t peek_matrix_intersection(pin_t out_pin, pin_t in_pin) {
}
#endif

__attribute__((weak)) bool is_keyboard_left(void) {
__attribute__((weak)) bool is_keyboard_left_impl(void) {
#if defined(SPLIT_HAND_PIN)
    setPinInput(SPLIT_HAND_PIN);
    wait_us(100);
    // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
#    ifdef SPLIT_HAND_PIN_LOW_IS_LEFT
    return !readPin(SPLIT_HAND_PIN);


@@ 146,36 153,6 @@ __attribute__((weak)) bool is_keyboard_left(void) {
    return !peek_matrix_intersection(SPLIT_HAND_MATRIX_GRID);
#    endif
#elif defined(EE_HANDS)
    return eeconfig_read_handedness();
#elif defined(MASTER_RIGHT)
    return !is_keyboard_master();
#endif

    return is_keyboard_master();
}

__attribute__((weak)) bool is_keyboard_master(void) {
    static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;

    // only check once, as this is called often
    if (usbstate == UNKNOWN) {
        usbstate = usbIsActive() ? MASTER : SLAVE;

        // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
        if (usbstate == SLAVE) {
            usb_disconnect();
        }
    }

    return (usbstate == MASTER);
}

// this code runs before the keyboard is fully initialized
void split_pre_init(void) {
#if defined(SPLIT_HAND_PIN)
    setPinInput(SPLIT_HAND_PIN);
    wait_us(100);
#elif defined(EE_HANDS)
    if (!eeconfig_is_enabled()) {
        eeconfig_init();
    }


@@ 193,12 170,42 @@ void split_pre_init(void) {
        eeconfig_update_handedness(should_be_left);
    }
#    endif // defined(INIT_EE_HANDS_LEFT) || defined(INIT_EE_HANDS_RIGHT)
    return eeconfig_read_handedness();
#elif defined(MASTER_RIGHT)
    return !is_keyboard_master();
#else
    return is_keyboard_master();
#endif
    isLeftHand = is_keyboard_left();
}

__attribute__((weak)) bool is_keyboard_master_impl(void) {
    bool is_master = usbIsActive();

    // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
    if (!is_master) {
        usb_disconnect();
    }
    return is_master;
}

__attribute__((weak)) bool is_keyboard_left(void) {
    return split_config.left;
}

__attribute__((weak)) bool is_keyboard_master(void) {
    return split_config.master;
}

// this code runs before the keyboard is fully initialized
void split_pre_init(void) {
    split_config.master = is_keyboard_master_impl();
    split_config.left   = is_keyboard_left_impl();

    isLeftHand = is_keyboard_left(); // TODO: Remove isLeftHand

#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
    uint8_t num_rgb_leds_split[2] = RGBLED_SPLIT;
    if (isLeftHand) {
    if (is_keyboard_left()) {
        rgblight_set_clipping_range(0, num_rgb_leds_split[0]);
    } else {
        rgblight_set_clipping_range(num_rgb_leds_split[0], num_rgb_leds_split[1]);

Do not follow this link