Skip to content

Commit 4c69608

Browse files
committed
Refactoring Matrix scanning
1 parent 508eddf commit 4c69608

File tree

2 files changed

+95
-88
lines changed

2 files changed

+95
-88
lines changed

quantum/matrix.c

+95-78
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
4343
# define ROW_SHIFTER ((uint32_t)1)
4444
#endif
4545

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-
5646
#ifdef MATRIX_MASKED
5747
extern const matrix_row_t matrix_mask[];
5848
#endif
@@ -70,6 +60,9 @@ static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
7060
/* matrix state(1:on, 0:off) */
7161
static matrix_row_t matrix[MATRIX_ROWS];
7262

63+
static matrix_row_t matrix_raw[MATRIX_ROWS];
64+
65+
7366
#if DIODE_DIRECTION == COL2ROW
7467
static matrix_row_t matrix_debouncing[MATRIX_ROWS];
7568
#else // ROW2COL
@@ -79,13 +72,13 @@ static matrix_row_t matrix[MATRIX_ROWS];
7972

8073
#if (DIODE_DIRECTION == COL2ROW)
8174
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)
8376
static void unselect_rows(void);
8477
static void select_row(uint8_t row);
8578
static void unselect_row(uint8_t row);
8679
#else // ROW2COL
8780
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)
8982
static void unselect_cols(void);
9083
static void unselect_col(uint8_t col);
9184
static void select_col(uint8_t col);
@@ -169,6 +162,7 @@ void matrix_init(void) {
169162
// initialize matrix state: all keys off
170163
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
171164
matrix[i] = 0;
165+
matrix_raw[i] = 0;
172166
matrix_debouncing[i] = 0;
173167
}
174168

@@ -178,6 +172,7 @@ void matrix_init(void) {
178172

179173
// initialize matrix state: all keys off
180174
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
175+
matrix_raw[i] = 0;
181176
matrix[i] = 0;
182177
}
183178

@@ -196,67 +191,73 @@ uint8_t matrix_scan(void)
196191
#if (DIODE_DIRECTION == COL2ROW)
197192

198193
// Set row, read cols
199-
200-
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
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);
194+
for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
195+
read_cols_on_row(matrix, current_row);
212196
}
213197

214-
if (debouncing) {
215-
if (--debouncing) {
216-
wait_ms(1);
217-
} else {
218-
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
219-
matrix[i] = matrix_debouncing[i];
220-
}
221-
}
222-
}
198+
// select_row(i);
199+
// wait_us(30); // without this wait read unstable value.
200+
// matrix_row_t current_row = read_cols();
201+
// if (matrix_debouncing[i] != current_row) {
202+
// matrix_debouncing[i] = current_row;
203+
// if (debouncing) {
204+
// debug("bounce!: "); debug_hex(debouncing); debug("\n");
205+
// }
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+
// }
223220

224221
#else // ROW2COL
225222

226223
// Set col, read rows
227-
228-
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
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);
224+
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
225+
read_rows_on_col(matrix, current_col);
240226
}
241227

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-
}
251228

252-
// Untranspose matrix
253-
for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
254-
matrix_row_t row = 0;
255-
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
256-
row |= ((matrix_transposed[x] & (1<<y)) >> y) << x;
257-
}
258-
matrix[y] = row;
259-
}
229+
// for (uint8_t i = 0; i < MATRIX_COLS; i++) {
230+
// select_col(i);
231+
// wait_us(30); // without this wait read unstable value.
232+
// matrix_col_t current_col = read_rows();
233+
// if (matrix_transposed_debouncing[i] != current_col) {
234+
// matrix_transposed_debouncing[i] = current_col;
235+
// if (debouncing) {
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+
// }
260261

261262
#endif
262263

@@ -322,16 +323,25 @@ static void init_cols(void)
322323
}
323324
}
324325

325-
static matrix_row_t read_cols(void)
326+
static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
326327
{
327-
matrix_row_t result = 0;
328+
// Clear data in matrix row
329+
current_matrix[current_row] = 0;
328330

329-
for(uint8_t x = 0; x < MATRIX_COLS; x++) {
330-
uint8_t pin = col_pins[x];
331-
result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (ROW_SHIFTER << x);
332-
}
331+
// Select row and wait for row selecton to stabilize
332+
select_row(current_row);
333+
wait_us(30);
333334

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+
}
335345
}
336346

337347
static void select_row(uint8_t row)
@@ -368,16 +378,23 @@ static void init_rows(void)
368378
}
369379
}
370380

371-
static matrix_col_t read_rows(void)
381+
static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
372382
{
373-
matrix_col_t result = 0;
374383

375-
for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
376-
uint8_t pin = row_pins[x];
377-
result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (COL_SHIFTER << x);
378-
}
384+
// Select col and wait for col selecton to stabilize
385+
select_col(current_col);
386+
wait_us(30);
379387

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+
}
381398
}
382399

383400
static void select_col(uint8_t col)

tmk_core/common/matrix.h

-10
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ typedef uint32_t matrix_row_t;
3131
#error "MATRIX_COLS: invalid value"
3232
#endif
3333

34-
#if (MATRIX_ROWS <= 8)
35-
typedef uint8_t matrix_col_t;
36-
#elif (MATRIX_ROWS <= 16)
37-
typedef uint16_t matrix_col_t;
38-
#elif (MATRIX_ROWS <= 32)
39-
typedef uint32_t matrix_col_t;
40-
#else
41-
#error "MATRIX_COLS: invalid value"
42-
#endif
43-
4434
#define MATRIX_IS_ON(row, col) (matrix_get_row(row) && (1<<col))
4535

4636

0 commit comments

Comments
 (0)