diff options
author | IBNobody <protospherex@gmail.com> | 2016-10-28 16:24:20 -0500 |
---|---|---|
committer | IBNobody <protospherex@gmail.com> | 2016-10-28 16:24:20 -0500 |
commit | 4c6960835c0a6e29670dabdc27117d7d3c7f99f5 (patch) | |
tree | 6b559d1aac3476257ca7d7db4b701f14514a268d /quantum/matrix.c | |
parent | 508eddf8ba8548d3f71e1c09a404839beb49f45c (diff) | |
download | qmk_firmware-4c6960835c0a6e29670dabdc27117d7d3c7f99f5.tar.gz qmk_firmware-4c6960835c0a6e29670dabdc27117d7d3c7f99f5.zip |
Refactoring Matrix scanning
Diffstat (limited to 'quantum/matrix.c')
-rw-r--r-- | quantum/matrix.c | 173 |
1 files changed, 95 insertions, 78 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c index a7dab0987..1bacea1be 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c | |||
@@ -43,16 +43,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
43 | # define ROW_SHIFTER ((uint32_t)1) | 43 | # define ROW_SHIFTER ((uint32_t)1) |
44 | #endif | 44 | #endif |
45 | 45 | ||
46 | #if (MATRIX_ROWS <= 8) | ||
47 | # define COL_SHIFTER ((uint8_t)1) | ||
48 | #elif (MATRIX_ROWS <= 16) | ||
49 | # define COL_SHIFTER ((uint16_t)1) | ||
50 | #elif (MATRIX_ROWS <= 32) | ||
51 | # define COL_SHIFTER ((uint32_t)1) | ||
52 | #endif | ||
53 | |||
54 | |||
55 | |||
56 | #ifdef MATRIX_MASKED | 46 | #ifdef MATRIX_MASKED |
57 | extern const matrix_row_t matrix_mask[]; | 47 | extern const matrix_row_t matrix_mask[]; |
58 | #endif | 48 | #endif |
@@ -70,6 +60,9 @@ static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | |||
70 | /* matrix state(1:on, 0:off) */ | 60 | /* matrix state(1:on, 0:off) */ |
71 | static matrix_row_t matrix[MATRIX_ROWS]; | 61 | static matrix_row_t matrix[MATRIX_ROWS]; |
72 | 62 | ||
63 | static matrix_row_t matrix_raw[MATRIX_ROWS]; | ||
64 | |||
65 | |||
73 | #if DIODE_DIRECTION == COL2ROW | 66 | #if DIODE_DIRECTION == COL2ROW |
74 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | 67 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; |
75 | #else // ROW2COL | 68 | #else // ROW2COL |
@@ -79,13 +72,13 @@ static matrix_row_t matrix[MATRIX_ROWS]; | |||
79 | 72 | ||
80 | #if (DIODE_DIRECTION == COL2ROW) | 73 | #if (DIODE_DIRECTION == COL2ROW) |
81 | static void init_cols(void); | 74 | static void init_cols(void); |
82 | static matrix_row_t read_cols(void); | 75 | static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) |
83 | static void unselect_rows(void); | 76 | static void unselect_rows(void); |
84 | static void select_row(uint8_t row); | 77 | static void select_row(uint8_t row); |
85 | static void unselect_row(uint8_t row); | 78 | static void unselect_row(uint8_t row); |
86 | #else // ROW2COL | 79 | #else // ROW2COL |
87 | static void init_rows(void); | 80 | static void init_rows(void); |
88 | static matrix_col_t read_rows(void); | 81 | static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) |
89 | static void unselect_cols(void); | 82 | static void unselect_cols(void); |
90 | static void unselect_col(uint8_t col); | 83 | static void unselect_col(uint8_t col); |
91 | static void select_col(uint8_t col); | 84 | static void select_col(uint8_t col); |
@@ -169,6 +162,7 @@ void matrix_init(void) { | |||
169 | // initialize matrix state: all keys off | 162 | // initialize matrix state: all keys off |
170 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | 163 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { |
171 | matrix[i] = 0; | 164 | matrix[i] = 0; |
165 | matrix_raw[i] = 0; | ||
172 | matrix_debouncing[i] = 0; | 166 | matrix_debouncing[i] = 0; |
173 | } | 167 | } |
174 | 168 | ||
@@ -178,6 +172,7 @@ void matrix_init(void) { | |||
178 | 172 | ||
179 | // initialize matrix state: all keys off | 173 | // initialize matrix state: all keys off |
180 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | 174 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { |
175 | matrix_raw[i] = 0; | ||
181 | matrix[i] = 0; | 176 | matrix[i] = 0; |
182 | } | 177 | } |
183 | 178 | ||
@@ -196,67 +191,73 @@ uint8_t matrix_scan(void) | |||
196 | #if (DIODE_DIRECTION == COL2ROW) | 191 | #if (DIODE_DIRECTION == COL2ROW) |
197 | 192 | ||
198 | // Set row, read cols | 193 | // Set row, read cols |
199 | 194 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | |
200 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | 195 | read_cols_on_row(matrix, current_row); |
201 | select_row(i); | ||
202 | wait_us(30); // without this wait read unstable value. | ||
203 | matrix_row_t current_row = read_cols(); | ||
204 | if (matrix_debouncing[i] != current_row) { | ||
205 | matrix_debouncing[i] = current_row; | ||
206 | if (debouncing) { | ||
207 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | ||
208 | } | ||
209 | debouncing = DEBOUNCING_DELAY; | ||
210 | } | ||
211 | unselect_row(i); | ||
212 | } | 196 | } |
213 | 197 | ||
214 | if (debouncing) { | 198 | // select_row(i); |
215 | if (--debouncing) { | 199 | // wait_us(30); // without this wait read unstable value. |
216 | wait_ms(1); | 200 | // matrix_row_t current_row = read_cols(); |
217 | } else { | 201 | // if (matrix_debouncing[i] != current_row) { |
218 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | 202 | // matrix_debouncing[i] = current_row; |
219 | matrix[i] = matrix_debouncing[i]; | 203 | // if (debouncing) { |
220 | } | 204 | // debug("bounce!: "); debug_hex(debouncing); debug("\n"); |
221 | } | 205 | // } |
222 | } | 206 | // debouncing = DEBOUNCING_DELAY; |
207 | // } | ||
208 | // unselect_row(i); | ||
209 | // } | ||
210 | |||
211 | // if (debouncing) { | ||
212 | // if (--debouncing) { | ||
213 | // wait_ms(1); | ||
214 | // } else { | ||
215 | // for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
216 | // matrix[i] = matrix_debouncing[i]; | ||
217 | // } | ||
218 | // } | ||
219 | // } | ||
223 | 220 | ||
224 | #else // ROW2COL | 221 | #else // ROW2COL |
225 | 222 | ||
226 | // Set col, read rows | 223 | // Set col, read rows |
227 | 224 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | |
228 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { | 225 | read_rows_on_col(matrix, current_col); |
229 | select_col(i); | ||
230 | wait_us(30); // without this wait read unstable value. | ||
231 | matrix_col_t current_col = read_rows(); | ||
232 | if (matrix_transposed_debouncing[i] != current_col) { | ||
233 | matrix_transposed_debouncing[i] = current_col; | ||
234 | if (debouncing) { | ||
235 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | ||
236 | } | ||
237 | debouncing = DEBOUNCING_DELAY; | ||
238 | } | ||
239 | unselect_col(i); | ||
240 | } | 226 | } |
241 | 227 | ||
242 | if (debouncing) { | ||
243 | if (--debouncing) { | ||
244 | wait_ms(1); | ||
245 | } else { | ||
246 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { | ||
247 | matrix_transposed[i] = matrix_transposed_debouncing[i]; | ||
248 | } | ||
249 | } | ||
250 | } | ||
251 | 228 | ||
252 | // Untranspose matrix | 229 | // for (uint8_t i = 0; i < MATRIX_COLS; i++) { |
253 | for (uint8_t y = 0; y < MATRIX_ROWS; y++) { | 230 | // select_col(i); |
254 | matrix_row_t row = 0; | 231 | // wait_us(30); // without this wait read unstable value. |
255 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | 232 | // matrix_col_t current_col = read_rows(); |
256 | row |= ((matrix_transposed[x] & (1<<y)) >> y) << x; | 233 | // if (matrix_transposed_debouncing[i] != current_col) { |
257 | } | 234 | // matrix_transposed_debouncing[i] = current_col; |
258 | matrix[y] = row; | 235 | // if (debouncing) { |
259 | } | 236 | // debug("bounce!: "); debug_hex(debouncing); debug("\n"); |
237 | // } | ||
238 | // debouncing = DEBOUNCING_DELAY; | ||
239 | // } | ||
240 | // unselect_col(i); | ||
241 | // } | ||
242 | |||
243 | // if (debouncing) { | ||
244 | // if (--debouncing) { | ||
245 | // wait_ms(1); | ||
246 | // } else { | ||
247 | // for (uint8_t i = 0; i < MATRIX_COLS; i++) { | ||
248 | // matrix_transposed[i] = matrix_transposed_debouncing[i]; | ||
249 | // } | ||
250 | // } | ||
251 | // } | ||
252 | |||
253 | // // Untranspose matrix | ||
254 | // for (uint8_t y = 0; y < MATRIX_ROWS; y++) { | ||
255 | // matrix_row_t row = 0; | ||
256 | // for (uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
257 | // row |= ((matrix_transposed[x] & (1<<y)) >> y) << x; | ||
258 | // } | ||
259 | // matrix[y] = row; | ||
260 | // } | ||
260 | 261 | ||
261 | #endif | 262 | #endif |
262 | 263 | ||
@@ -322,16 +323,25 @@ static void init_cols(void) | |||
322 | } | 323 | } |
323 | } | 324 | } |
324 | 325 | ||
325 | static matrix_row_t read_cols(void) | 326 | static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) |
326 | { | 327 | { |
327 | matrix_row_t result = 0; | 328 | // Clear data in matrix row |
329 | current_matrix[current_row] = 0; | ||
328 | 330 | ||
329 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | 331 | // Select row and wait for row selecton to stabilize |
330 | uint8_t pin = col_pins[x]; | 332 | select_row(current_row); |
331 | result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (ROW_SHIFTER << x); | 333 | wait_us(30); |
332 | } | ||
333 | 334 | ||
334 | return result; | 335 | // For each col... |
336 | for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
337 | |||
338 | // Select the col pin to read (active low) | ||
339 | uint8_t pin = col_pins[col_index]; | ||
340 | uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); | ||
341 | |||
342 | // Populate the matrix row with the state of the col pin | ||
343 | current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); | ||
344 | } | ||
335 | } | 345 | } |
336 | 346 | ||
337 | static void select_row(uint8_t row) | 347 | static void select_row(uint8_t row) |
@@ -368,16 +378,23 @@ static void init_rows(void) | |||
368 | } | 378 | } |
369 | } | 379 | } |
370 | 380 | ||
371 | static matrix_col_t read_rows(void) | 381 | static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) |
372 | { | 382 | { |
373 | matrix_col_t result = 0; | ||
374 | 383 | ||
375 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | 384 | // Select col and wait for col selecton to stabilize |
376 | uint8_t pin = row_pins[x]; | 385 | select_col(current_col); |
377 | result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (COL_SHIFTER << x); | 386 | wait_us(30); |
378 | } | ||
379 | 387 | ||
380 | return result; | 388 | // For each row... |
389 | for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { | ||
390 | |||
391 | // Select the row pin to read (active low) | ||
392 | uint8_t pin = row_pins[row_index]; | ||
393 | uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); | ||
394 | |||
395 | // Populate the matrix row with the state of the col pin | ||
396 | current_matrix[row_index] &= pin_state ? ~(ROW_SHIFTER << current_col) : 0; | ||
397 | } | ||
381 | } | 398 | } |
382 | 399 | ||
383 | static void select_col(uint8_t col) | 400 | static void select_col(uint8_t col) |