@@ -43,16 +43,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
43
43
# define ROW_SHIFTER ((uint32_t)1)
44
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
46
#ifdef MATRIX_MASKED
57
47
extern const matrix_row_t matrix_mask [];
58
48
#endif
@@ -70,6 +60,9 @@ static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
70
60
/* matrix state(1:on, 0:off) */
71
61
static matrix_row_t matrix [MATRIX_ROWS ];
72
62
63
+ static matrix_row_t matrix_raw [MATRIX_ROWS ];
64
+
65
+
73
66
#if DIODE_DIRECTION == COL2ROW
74
67
static matrix_row_t matrix_debouncing [MATRIX_ROWS ];
75
68
#else // ROW2COL
@@ -79,13 +72,13 @@ static matrix_row_t matrix[MATRIX_ROWS];
79
72
80
73
#if (DIODE_DIRECTION == COL2ROW )
81
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
76
static void unselect_rows (void );
84
77
static void select_row (uint8_t row );
85
78
static void unselect_row (uint8_t row );
86
79
#else // ROW2COL
87
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
82
static void unselect_cols (void );
90
83
static void unselect_col (uint8_t col );
91
84
static void select_col (uint8_t col );
@@ -169,6 +162,7 @@ void matrix_init(void) {
169
162
// initialize matrix state: all keys off
170
163
for (uint8_t i = 0 ; i < MATRIX_ROWS ; i ++ ) {
171
164
matrix [i ] = 0 ;
165
+ matrix_raw [i ] = 0 ;
172
166
matrix_debouncing [i ] = 0 ;
173
167
}
174
168
@@ -178,6 +172,7 @@ void matrix_init(void) {
178
172
179
173
// initialize matrix state: all keys off
180
174
for (uint8_t i = 0 ; i < MATRIX_ROWS ; i ++ ) {
175
+ matrix_raw [i ] = 0 ;
181
176
matrix [i ] = 0 ;
182
177
}
183
178
@@ -196,67 +191,73 @@ uint8_t matrix_scan(void)
196
191
#if (DIODE_DIRECTION == COL2ROW )
197
192
198
193
// 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 );
212
196
}
213
197
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
+ // }
223
220
224
221
#else // ROW2COL
225
222
226
223
// 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 );
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
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
+ // }
260
261
261
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 ++ ) {
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 );
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
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 ++ ) {
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 );
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
400
static void select_col (uint8_t col )
0 commit comments