diff options
Diffstat (limited to 'quantum/split_common')
-rw-r--r-- | quantum/split_common/matrix.c | 41 | ||||
-rw-r--r-- | quantum/split_common/split_util.c | 65 |
2 files changed, 69 insertions, 37 deletions
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index 95ee2433a..5bad9db08 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c @@ -61,17 +61,22 @@ static void init_pins(void) { } static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { - matrix_row_t last_row_value = current_matrix[current_row]; - current_matrix[current_row] = 0; + // Start with a clear matrix row + matrix_row_t current_row_value = 0; for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { pin_t pin = direct_pins[current_row][col_index]; if (pin != NO_PIN) { - current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); + current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); } } - return (last_row_value != current_matrix[current_row]); + // If the row has changed, store the row and return the changed flag. + if (current_matrix[current_row] != current_row_value) { + current_matrix[current_row] = current_row_value; + return true; + } + return false; } #elif defined(DIODE_DIRECTION) @@ -98,11 +103,8 @@ static void init_pins(void) { } static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { - // Store last value of row prior to reading - matrix_row_t last_row_value = current_matrix[current_row]; - - // Clear data in matrix row - current_matrix[current_row] = 0; + // Start with a clear matrix row + matrix_row_t current_row_value = 0; // Select row and wait for row selecton to stabilize select_row(current_row); @@ -114,13 +116,18 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) uint8_t pin_state = readPin(col_pins[col_index]); // Populate the matrix row with the state of the col pin - current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); + current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); } // Unselect row unselect_row(current_row); - return (last_row_value != current_matrix[current_row]); + // If the row has changed, store the row and return the changed flag. + if (current_matrix[current_row] != current_row_value) { + current_matrix[current_row] = current_row_value; + return true; + } + return false; } # elif (DIODE_DIRECTION == ROW2COL) @@ -155,20 +162,22 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // For each row... for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { // Store last value of row prior to reading - matrix_row_t last_row_value = current_matrix[row_index]; + matrix_row_t last_row_value = current_matrix[row_index]; + matrix_row_t current_row_value = last_row_value; // Check row pin state if (readPin(row_pins[row_index]) == 0) { // Pin LO, set col bit - current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); + current_row_value |= (MATRIX_ROW_SHIFTER << current_col); } else { // Pin HI, clear col bit - current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); + current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col); } // Determine if the matrix changed state - if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { - matrix_changed = true; + if ((last_row_value != current_row_value)) { + matrix_changed |= true; + current_matrix[row_index] = current_row_value; } } diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c index fb6a3b85a..dfd06f5f9 100644 --- a/quantum/split_common/split_util.c +++ b/quantum/split_common/split_util.c @@ -6,6 +6,14 @@ #include "transport.h" #include "quantum.h" +#ifdef PROTOCOL_LUFA +# include <LUFA/Drivers/USB/USB.h> +#endif + +#ifdef PROTOCOL_VUSB +# include "usbdrv.h" +#endif + #ifdef EE_HANDS # include "eeconfig.h" #endif @@ -22,30 +30,54 @@ # define SPLIT_USB_TIMEOUT_POLL 10 #endif +#ifdef PROTOCOL_CHIBIOS +# define SPLIT_USB_DETECT // Force this on for now +#endif + volatile bool isLeftHand = true; -bool waitForUsb(void) { +#if defined(SPLIT_USB_DETECT) +# if defined(PROTOCOL_LUFA) +static inline bool usbHasActiveConnection(void) { return USB_Device_IsAddressSet(); } +static inline void usbDisable(void) { USB_Disable(); } +# elif defined(PROTOCOL_CHIBIOS) +static inline bool usbHasActiveConnection(void) { return usbGetDriverStateI(&USBD1) == USB_ACTIVE; } +static inline void usbDisable(void) { usbStop(&USBD1); } +# elif defined(PROTOCOL_VUSB) +static inline bool usbHasActiveConnection(void) { + usbPoll(); + return usbConfiguration; +} +static inline void usbDisable(void) { usbDeviceDisconnect(); } +# else +static inline bool usbHasActiveConnection(void) { return true; } +static inline void usbDisable(void) {} +# endif + +bool usbIsActive(void) { for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) { // This will return true if a USB connection has been established -#if defined(__AVR__) - if (UDADDR & _BV(ADDEN)) { -#else - if (usbGetDriverStateI(&USBD1) == USB_ACTIVE) { -#endif + if (usbHasActiveConnection()) { return true; } wait_ms(SPLIT_USB_TIMEOUT_POLL); } // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow -#if defined(__AVR__) - (USBCON &= ~(_BV(USBE) | _BV(OTGPADE))); -#else - usbStop(&USBD1); -#endif + usbDisable(); return false; } +#elif defined(PROTOCOL_LUFA) +static inline bool usbIsActive(void) { + USB_OTGPAD_On(); // enables VBUS pad + wait_us(5); + + return USB_VBUS_GetStatus(); // checks state of VBUS +} +#else +static inline bool usbIsActive(void) { return true; } +#endif __attribute__((weak)) bool is_keyboard_left(void) { #if defined(SPLIT_HAND_PIN) @@ -66,16 +98,7 @@ __attribute__((weak)) bool is_keyboard_master(void) { // only check once, as this is called often if (usbstate == UNKNOWN) { -#if defined(SPLIT_USB_DETECT) || defined(PROTOCOL_CHIBIOS) - usbstate = waitForUsb() ? MASTER : SLAVE; -#elif defined(__AVR__) - USBCON |= (1 << OTGPADE); // enables VBUS pad - wait_us(5); - - usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS -#else - usbstate = MASTER; -#endif + usbstate = usbIsActive() ? MASTER : SLAVE; } return (usbstate == MASTER); |