diff options
Diffstat (limited to 'quantum/matrix.c')
-rw-r--r-- | quantum/matrix.c | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c index 67d8af6ee..9e998508a 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c | |||
@@ -48,17 +48,22 @@ static void init_pins(void) { | |||
48 | } | 48 | } |
49 | 49 | ||
50 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | 50 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { |
51 | matrix_row_t last_row_value = current_matrix[current_row]; | 51 | // Start with a clear matrix row |
52 | current_matrix[current_row] = 0; | 52 | matrix_row_t current_row_value = 0; |
53 | 53 | ||
54 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | 54 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { |
55 | pin_t pin = direct_pins[current_row][col_index]; | 55 | pin_t pin = direct_pins[current_row][col_index]; |
56 | if (pin != NO_PIN) { | 56 | if (pin != NO_PIN) { |
57 | current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); | 57 | current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); |
58 | } | 58 | } |
59 | } | 59 | } |
60 | 60 | ||
61 | return (last_row_value != current_matrix[current_row]); | 61 | // If the row has changed, store the row and return the changed flag. |
62 | if (current_matrix[current_row] != current_row_value) { | ||
63 | current_matrix[current_row] = current_row_value; | ||
64 | return true; | ||
65 | } | ||
66 | return false; | ||
62 | } | 67 | } |
63 | 68 | ||
64 | #elif defined(DIODE_DIRECTION) | 69 | #elif defined(DIODE_DIRECTION) |
@@ -84,12 +89,9 @@ static void init_pins(void) { | |||
84 | } | 89 | } |
85 | } | 90 | } |
86 | 91 | ||
87 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | 92 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { |
88 | // Store last value of row prior to reading | 93 | // Start with a clear matrix row |
89 | matrix_row_t last_row_value = current_matrix[current_row]; | 94 | matrix_row_t current_row_value = 0; |
90 | |||
91 | // Clear data in matrix row | ||
92 | current_matrix[current_row] = 0; | ||
93 | 95 | ||
94 | // Select row and wait for row selecton to stabilize | 96 | // Select row and wait for row selecton to stabilize |
95 | select_row(current_row); | 97 | select_row(current_row); |
@@ -101,13 +103,18 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
101 | uint8_t pin_state = readPin(col_pins[col_index]); | 103 | uint8_t pin_state = readPin(col_pins[col_index]); |
102 | 104 | ||
103 | // Populate the matrix row with the state of the col pin | 105 | // Populate the matrix row with the state of the col pin |
104 | current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); | 106 | current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); |
105 | } | 107 | } |
106 | 108 | ||
107 | // Unselect row | 109 | // Unselect row |
108 | unselect_row(current_row); | 110 | unselect_row(current_row); |
109 | 111 | ||
110 | return (last_row_value != current_matrix[current_row]); | 112 | // If the row has changed, store the row and return the changed flag. |
113 | if (current_matrix[current_row] != current_row_value) { | ||
114 | current_matrix[current_row] = current_row_value; | ||
115 | return true; | ||
116 | } | ||
117 | return false; | ||
111 | } | 118 | } |
112 | 119 | ||
113 | # elif (DIODE_DIRECTION == ROW2COL) | 120 | # elif (DIODE_DIRECTION == ROW2COL) |
@@ -143,19 +150,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | |||
143 | for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { | 150 | for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { |
144 | // Store last value of row prior to reading | 151 | // Store last value of row prior to reading |
145 | matrix_row_t last_row_value = current_matrix[row_index]; | 152 | matrix_row_t last_row_value = current_matrix[row_index]; |
153 | matrix_row_t current_row_value = last_row_value; | ||
146 | 154 | ||
147 | // Check row pin state | 155 | // Check row pin state |
148 | if (readPin(row_pins[row_index]) == 0) { | 156 | if (readPin(row_pins[row_index]) == 0) { |
149 | // Pin LO, set col bit | 157 | // Pin LO, set col bit |
150 | current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); | 158 | current_row_value |= (MATRIX_ROW_SHIFTER << current_col); |
151 | } else { | 159 | } else { |
152 | // Pin HI, clear col bit | 160 | // Pin HI, clear col bit |
153 | current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); | 161 | current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col); |
154 | } | 162 | } |
155 | 163 | ||
156 | // Determine if the matrix changed state | 164 | // Determine if the matrix changed state |
157 | if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { | 165 | if ((last_row_value != current_row_value)) { |
158 | matrix_changed = true; | 166 | matrix_changed |= true; |
167 | current_matrix[row_index] = current_row_value; | ||
159 | } | 168 | } |
160 | } | 169 | } |
161 | 170 | ||