|
1 | | -#include "quantum.h" |
| 1 | +// Copyright 2024 splitkb.com (support@splitkb.com) |
| 2 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + |
2 | 4 | #include "split_util.h" |
| 5 | +#include "atomic_util.h" |
3 | 6 |
|
4 | 7 | #ifdef SPLIT_KEYBOARD |
5 | 8 | # define ROWS_PER_HAND (MATRIX_ROWS / 2) |
6 | 9 | #else |
7 | 10 | # define ROWS_PER_HAND (MATRIX_ROWS) |
8 | 11 | #endif |
9 | 12 |
|
10 | | -#if defined(MATRIX_ROW_PINS_RIGHT) || defined(MATRIX_COL_PINS_RIGHT) |
11 | | - #define SPLIT_MUTABLE |
| 13 | +#ifdef MATRIX_ROW_PINS_RIGHT |
| 14 | +# define SPLIT_MUTABLE_ROW |
| 15 | +#else |
| 16 | +# define SPLIT_MUTABLE_ROW const |
| 17 | +#endif |
| 18 | +#ifdef MATRIX_COL_PINS_RIGHT |
| 19 | +# define SPLIT_MUTABLE_COL |
12 | 20 | #else |
13 | | - #define SPLIT_MUTABLE const |
| 21 | +# define SPLIT_MUTABLE_COL const |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef MATRIX_INPUT_PRESSED_STATE |
| 25 | +# define MATRIX_INPUT_PRESSED_STATE 0 |
14 | 26 | #endif |
15 | 27 |
|
16 | | -static SPLIT_MUTABLE pin_t row_pins[ROWS_PER_HAND] = MATRIX_ROW_PINS; |
17 | | -static SPLIT_MUTABLE pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; |
| 28 | +#ifdef DIRECT_PINS |
| 29 | +static SPLIT_MUTABLE pin_t direct_pins[ROWS_PER_HAND][MATRIX_COLS] = DIRECT_PINS; |
| 30 | +#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) |
| 31 | +# ifdef MATRIX_ROW_PINS |
| 32 | +static SPLIT_MUTABLE_ROW pin_t row_pins[ROWS_PER_HAND] = MATRIX_ROW_PINS; |
| 33 | +# endif // MATRIX_ROW_PINS |
| 34 | +# ifdef MATRIX_COL_PINS |
| 35 | +static SPLIT_MUTABLE_COL pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; |
| 36 | +# endif // MATRIX_COL_PINS |
| 37 | +#endif |
| 38 | + |
| 39 | +void matrix_init_kb(void) { |
18 | 40 |
|
19 | | -void matrix_init_user(void) { |
20 | 41 | gpio_set_pin_input_high(HLC_ENCODER_BUTTON); |
21 | 42 |
|
| 43 | + // Also need to define here otherwise right half is swapped |
22 | 44 | if (!isLeftHand) { |
23 | | -#ifdef MATRIX_ROW_PINS_RIGHT |
24 | | - const pin_t row_pins_right[ROWS_PER_HAND] = MATRIX_ROW_PINS_RIGHT; |
25 | | - for (uint8_t i = 0; i < ROWS_PER_HAND; i++) row_pins[i] = row_pins_right[i]; |
26 | | -#endif |
27 | | -#ifdef MATRIX_COL_PINS_RIGHT |
28 | | - const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT; |
29 | | - for (uint8_t i = 0; i < MATRIX_COLS; i++) col_pins[i] = col_pins_right[i]; |
30 | | -#endif |
| 45 | + # ifdef MATRIX_ROW_PINS_RIGHT |
| 46 | + const pin_t row_pins_right[ROWS_PER_HAND] = MATRIX_ROW_PINS_RIGHT; |
| 47 | + for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { |
| 48 | + row_pins[i] = row_pins_right[i]; |
| 49 | + } |
| 50 | + # endif |
| 51 | + # ifdef MATRIX_COL_PINS_RIGHT |
| 52 | + const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT; |
| 53 | + for (uint8_t i = 0; i < MATRIX_COLS; i++) { |
| 54 | + col_pins[i] = col_pins_right[i]; |
| 55 | + } |
| 56 | + # endif |
31 | 57 | } |
32 | 58 | } |
33 | 59 |
|
34 | | -static inline void select_row(uint8_t row) { |
35 | | - if (row_pins[row] != NO_PIN) { |
36 | | - gpio_set_pin_output(row_pins[row]); |
37 | | - gpio_write_pin_low(row_pins[row]); |
| 60 | +static inline void setPinOutput_writeLow(pin_t pin) { |
| 61 | + ATOMIC_BLOCK_FORCEON { |
| 62 | + gpio_set_pin_output(pin); |
| 63 | + gpio_write_pin_low(pin); |
38 | 64 | } |
39 | 65 | } |
40 | 66 |
|
41 | | -static inline void unselect_row(uint8_t row) { |
42 | | - if (row_pins[row] != NO_PIN) { |
43 | | - gpio_set_pin_input_high(row_pins[row]); |
| 67 | +static inline void setPinOutput_writeHigh(pin_t pin) { |
| 68 | + ATOMIC_BLOCK_FORCEON { |
| 69 | + gpio_set_pin_output(pin); |
| 70 | + gpio_write_pin_high(pin); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +static inline void setPinInputHigh_atomic(pin_t pin) { |
| 75 | + ATOMIC_BLOCK_FORCEON { |
| 76 | + gpio_set_pin_input_high(pin); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +static inline uint8_t readMatrixPin(pin_t pin) { |
| 81 | + if (pin != NO_PIN) { |
| 82 | + return (gpio_read_pin(pin) == MATRIX_INPUT_PRESSED_STATE) ? 0 : 1; |
| 83 | + } else { |
| 84 | + return 1; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// THIS FUNCTION IS CHANGED, removed NO_PIN check |
| 89 | +static bool select_row(uint8_t row) { |
| 90 | + pin_t pin = row_pins[row]; |
| 91 | + setPinOutput_writeLow(pin); |
| 92 | + return true; |
| 93 | +} |
| 94 | + |
| 95 | +static void unselect_row(uint8_t row) { |
| 96 | + pin_t pin = row_pins[row]; |
| 97 | + if (pin != NO_PIN) { |
| 98 | +# ifdef MATRIX_UNSELECT_DRIVE_HIGH |
| 99 | + setPinOutput_writeHigh(pin); |
| 100 | +# else |
| 101 | + setPinInputHigh_atomic(pin); |
| 102 | +# endif |
44 | 103 | } |
45 | 104 | } |
46 | 105 |
|
47 | 106 | void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { |
| 107 | + // Start with a clear matrix row |
48 | 108 | matrix_row_t current_row_value = 0; |
49 | 109 |
|
50 | | - // Custom handling for encoder button on the last row |
| 110 | + if (!select_row(current_row)) { // Select row |
| 111 | + return; // skip NO_PIN row |
| 112 | + } |
| 113 | + matrix_output_select_delay(); |
| 114 | + |
| 115 | + // ↓↓↓ THIS HAS BEEN ADDED/CHANGED |
51 | 116 | if (current_row == (ROWS_PER_HAND - 1)) { |
52 | | - if (!gpio_read_pin(HLC_ENCODER_BUTTON)) { |
53 | | - current_row_value |= (1 << 0); |
| 117 | + current_row_value |= ((!gpio_read_pin(HLC_ENCODER_BUTTON)) & 1) << 0; |
| 118 | + } else { |
| 119 | + // For each col... |
| 120 | + matrix_row_t row_shifter = MATRIX_ROW_SHIFTER; |
| 121 | + for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) { |
| 122 | + uint8_t pin_state = readMatrixPin(col_pins[col_index]); |
| 123 | + |
| 124 | + // Populate the matrix row with the state of the col pin |
| 125 | + current_row_value |= pin_state ? 0 : row_shifter; |
54 | 126 | } |
55 | 127 | } |
56 | | - // Standard column reading for other rows |
57 | | - else { |
58 | | - select_row(current_row); |
59 | | - matrix_output_select_delay(); |
60 | | - |
61 | | - for (uint8_t col = 0; col < MATRIX_COLS; col++) { |
62 | | - if (col_pins[col] != NO_PIN && !gpio_read_pin(col_pins[col])) { |
63 | | - current_row_value |= (1 << col); |
64 | | - } |
65 | | - } |
| 128 | + // ↑↑↑ THIS HAS BEEN ADDED/CHANGED |
66 | 129 |
|
67 | | - unselect_row(current_row); |
68 | | - matrix_output_unselect_delay(current_row, current_row_value != 0); |
69 | | - } |
| 130 | + // Unselect row |
| 131 | + unselect_row(current_row); |
| 132 | + matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for all Col signals to go HIGH |
70 | 133 |
|
| 134 | + // Update the matrix |
71 | 135 | current_matrix[current_row] = current_row_value; |
72 | 136 | } |
0 commit comments