diff options
Diffstat (limited to 'keyboards/miniaxe/matrix.c')
-rw-r--r-- | keyboards/miniaxe/matrix.c | 596 |
1 files changed, 596 insertions, 0 deletions
diff --git a/keyboards/miniaxe/matrix.c b/keyboards/miniaxe/matrix.c new file mode 100644 index 000000000..55c458e1f --- /dev/null +++ b/keyboards/miniaxe/matrix.c | |||
@@ -0,0 +1,596 @@ | |||
1 | /* | ||
2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation, either version 2 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | |||
18 | /* | ||
19 | * scan matrix | ||
20 | */ | ||
21 | #include <stdint.h> | ||
22 | #include <stdbool.h> | ||
23 | #include <avr/io.h> | ||
24 | #include "wait.h" | ||
25 | #include "print.h" | ||
26 | #include "debug.h" | ||
27 | #include "util.h" | ||
28 | #include "matrix.h" | ||
29 | #include "split_util.h" | ||
30 | #include "pro_micro.h" | ||
31 | #include "config.h" | ||
32 | #include "timer.h" | ||
33 | #include "split_flags.h" | ||
34 | |||
35 | #ifdef RGBLIGHT_ENABLE | ||
36 | # include "rgblight.h" | ||
37 | #endif | ||
38 | #ifdef BACKLIGHT_ENABLE | ||
39 | # include "backlight.h" | ||
40 | extern backlight_config_t backlight_config; | ||
41 | #endif | ||
42 | |||
43 | #if defined(USE_I2C) || defined(EH) | ||
44 | # include "i2c.h" | ||
45 | #else // USE_SERIAL | ||
46 | # include "serial.h" | ||
47 | #endif | ||
48 | |||
49 | #ifndef DEBOUNCING_DELAY | ||
50 | # define DEBOUNCING_DELAY 5 | ||
51 | #endif | ||
52 | |||
53 | #if (DEBOUNCING_DELAY > 0) | ||
54 | static uint16_t debouncing_time; | ||
55 | static bool debouncing = false; | ||
56 | #endif | ||
57 | |||
58 | #if (MATRIX_COLS <= 8) | ||
59 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
60 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
61 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
62 | # define ROW_SHIFTER ((uint8_t)1) | ||
63 | #else | ||
64 | # error "Currently only supports 8 COLS" | ||
65 | #endif | ||
66 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
67 | |||
68 | #define ERROR_DISCONNECT_COUNT 5 | ||
69 | |||
70 | #define ROWS_PER_HAND (MATRIX_ROWS/2) | ||
71 | |||
72 | static uint8_t error_count = 0; | ||
73 | |||
74 | #if ((DIODE_DIRECTION == COL2ROW) || (DIODE_DIRECTION == ROW2COL)) | ||
75 | static uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
76 | static uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
77 | #elif (DIODE_DIRECTION == CUSTOM_MATRIX) | ||
78 | static uint8_t row_col_pins[MATRIX_ROWS][MATRIX_COLS] = MATRIX_ROW_COL_PINS; | ||
79 | #endif | ||
80 | |||
81 | /* matrix state(1:on, 0:off) */ | ||
82 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
83 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
84 | |||
85 | #if (DIODE_DIRECTION == COL2ROW) | ||
86 | static void init_cols(void); | ||
87 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); | ||
88 | static void unselect_rows(void); | ||
89 | static void select_row(uint8_t row); | ||
90 | static void unselect_row(uint8_t row); | ||
91 | #elif (DIODE_DIRECTION == ROW2COL) | ||
92 | static void init_rows(void); | ||
93 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); | ||
94 | static void unselect_cols(void); | ||
95 | static void unselect_col(uint8_t col); | ||
96 | static void select_col(uint8_t col); | ||
97 | #elif (DIODE_DIRECTION == CUSTOM_MATRIX) | ||
98 | static void init_cols_rows(void); | ||
99 | static bool read_cols(matrix_row_t current_matrix[], uint8_t current_row); | ||
100 | #endif | ||
101 | |||
102 | __attribute__ ((weak)) | ||
103 | void matrix_init_kb(void) { | ||
104 | matrix_init_user(); | ||
105 | } | ||
106 | |||
107 | __attribute__ ((weak)) | ||
108 | void matrix_scan_kb(void) { | ||
109 | matrix_scan_user(); | ||
110 | } | ||
111 | |||
112 | __attribute__ ((weak)) | ||
113 | void matrix_init_user(void) { | ||
114 | } | ||
115 | |||
116 | __attribute__ ((weak)) | ||
117 | void matrix_scan_user(void) { | ||
118 | } | ||
119 | |||
120 | __attribute__ ((weak)) | ||
121 | void matrix_slave_scan_user(void) { | ||
122 | } | ||
123 | |||
124 | inline | ||
125 | uint8_t matrix_rows(void) | ||
126 | { | ||
127 | return MATRIX_ROWS; | ||
128 | } | ||
129 | |||
130 | inline | ||
131 | uint8_t matrix_cols(void) | ||
132 | { | ||
133 | return MATRIX_COLS; | ||
134 | } | ||
135 | |||
136 | void matrix_init(void) | ||
137 | { | ||
138 | #ifdef DISABLE_JTAG | ||
139 | // JTAG disable for PORT F. write JTD bit twice within four cycles. | ||
140 | MCUCR |= (1<<JTD); | ||
141 | MCUCR |= (1<<JTD); | ||
142 | #endif | ||
143 | |||
144 | debug_enable = true; | ||
145 | debug_matrix = true; | ||
146 | debug_mouse = true; | ||
147 | |||
148 | // Set pinout for right half if pinout for that half is defined | ||
149 | if (!isLeftHand) { | ||
150 | #ifdef MATRIX_ROW_PINS_RIGHT | ||
151 | const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT; | ||
152 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) | ||
153 | row_pins[i] = row_pins_right[i]; | ||
154 | #endif | ||
155 | #ifdef MATRIX_COL_PINS_RIGHT | ||
156 | const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT; | ||
157 | for (uint8_t i = 0; i < MATRIX_COLS; i++) | ||
158 | col_pins[i] = col_pins_right[i]; | ||
159 | #endif | ||
160 | } | ||
161 | |||
162 | // initialize row and col | ||
163 | #if (DIODE_DIRECTION == COL2ROW) | ||
164 | unselect_rows(); | ||
165 | init_cols(); | ||
166 | #elif (DIODE_DIRECTION == ROW2COL) | ||
167 | unselect_cols(); | ||
168 | init_rows(); | ||
169 | #elif (DIODE_DIRECTION == CUSTOM_MATRIX) | ||
170 | init_cols_rows(); | ||
171 | #endif | ||
172 | |||
173 | // initialize matrix state: all keys off | ||
174 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
175 | matrix[i] = 0; | ||
176 | matrix_debouncing[i] = 0; | ||
177 | } | ||
178 | |||
179 | matrix_init_quantum(); | ||
180 | |||
181 | } | ||
182 | |||
183 | uint8_t _matrix_scan(void) | ||
184 | { | ||
185 | int offset = isLeftHand ? 0 : (ROWS_PER_HAND); | ||
186 | #if (DIODE_DIRECTION == COL2ROW) | ||
187 | // Set row, read cols | ||
188 | for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) { | ||
189 | # if (DEBOUNCING_DELAY > 0) | ||
190 | bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row); | ||
191 | |||
192 | if (matrix_changed) { | ||
193 | debouncing = true; | ||
194 | debouncing_time = timer_read(); | ||
195 | } | ||
196 | |||
197 | # else | ||
198 | read_cols_on_row(matrix+offset, current_row); | ||
199 | # endif | ||
200 | |||
201 | } | ||
202 | |||
203 | #elif (DIODE_DIRECTION == ROW2COL) | ||
204 | // Set col, read rows | ||
205 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
206 | # if (DEBOUNCING_DELAY > 0) | ||
207 | bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col); | ||
208 | if (matrix_changed) { | ||
209 | debouncing = true; | ||
210 | debouncing_time = timer_read(); | ||
211 | } | ||
212 | # else | ||
213 | read_rows_on_col(matrix+offset, current_col); | ||
214 | # endif | ||
215 | |||
216 | } | ||
217 | |||
218 | #elif (DIODE_DIRECTION == CUSTOM_MATRIX) | ||
219 | // Set row, read cols | ||
220 | for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) { | ||
221 | # if (DEBOUNCING_DELAY > 0) | ||
222 | bool matrix_changed = read_cols(matrix_debouncing+offset, current_row); | ||
223 | if (matrix_changed) { | ||
224 | debouncing = true; | ||
225 | debouncing_time = timer_read(); | ||
226 | } | ||
227 | # else | ||
228 | read_cols(matrix+offset, current_row); | ||
229 | # endif | ||
230 | } | ||
231 | #endif | ||
232 | |||
233 | # if (DEBOUNCING_DELAY > 0) | ||
234 | if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) { | ||
235 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
236 | matrix[i+offset] = matrix_debouncing[i+offset]; | ||
237 | } | ||
238 | debouncing = false; | ||
239 | } | ||
240 | # endif | ||
241 | |||
242 | return 1; | ||
243 | } | ||
244 | |||
245 | #if defined(USE_I2C) || defined(EH) | ||
246 | |||
247 | // Get rows from other half over i2c | ||
248 | int i2c_transaction(void) { | ||
249 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
250 | int err = 0; | ||
251 | |||
252 | // write backlight info | ||
253 | #ifdef BACKLIGHT_ENABLE | ||
254 | if (BACKLIT_DIRTY) { | ||
255 | err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE); | ||
256 | if (err) goto i2c_error; | ||
257 | |||
258 | // Backlight location | ||
259 | err = i2c_master_write(I2C_BACKLIT_START); | ||
260 | if (err) goto i2c_error; | ||
261 | |||
262 | // Write backlight | ||
263 | i2c_master_write(get_backlight_level()); | ||
264 | |||
265 | BACKLIT_DIRTY = false; | ||
266 | } | ||
267 | #endif | ||
268 | |||
269 | err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE); | ||
270 | if (err) goto i2c_error; | ||
271 | |||
272 | // start of matrix stored at I2C_KEYMAP_START | ||
273 | err = i2c_master_write(I2C_KEYMAP_START); | ||
274 | if (err) goto i2c_error; | ||
275 | |||
276 | // Start read | ||
277 | err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ); | ||
278 | if (err) goto i2c_error; | ||
279 | |||
280 | if (!err) { | ||
281 | int i; | ||
282 | for (i = 0; i < ROWS_PER_HAND-1; ++i) { | ||
283 | matrix[slaveOffset+i] = i2c_master_read(I2C_ACK); | ||
284 | } | ||
285 | matrix[slaveOffset+i] = i2c_master_read(I2C_NACK); | ||
286 | i2c_master_stop(); | ||
287 | } else { | ||
288 | i2c_error: // the cable is disconnceted, or something else went wrong | ||
289 | i2c_reset_state(); | ||
290 | return err; | ||
291 | } | ||
292 | |||
293 | #ifdef RGBLIGHT_ENABLE | ||
294 | if (RGB_DIRTY) { | ||
295 | err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE); | ||
296 | if (err) goto i2c_error; | ||
297 | |||
298 | // RGB Location | ||
299 | err = i2c_master_write(I2C_RGB_START); | ||
300 | if (err) goto i2c_error; | ||
301 | |||
302 | uint32_t dword = eeconfig_read_rgblight(); | ||
303 | |||
304 | // Write RGB | ||
305 | err = i2c_master_write_data(&dword, 4); | ||
306 | if (err) goto i2c_error; | ||
307 | |||
308 | RGB_DIRTY = false; | ||
309 | i2c_master_stop(); | ||
310 | } | ||
311 | #endif | ||
312 | |||
313 | return 0; | ||
314 | } | ||
315 | |||
316 | #else // USE_SERIAL | ||
317 | |||
318 | int serial_transaction(void) { | ||
319 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
320 | |||
321 | if (serial_update_buffers()) { | ||
322 | return 1; | ||
323 | } | ||
324 | |||
325 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
326 | matrix[slaveOffset+i] = serial_slave_buffer[i]; | ||
327 | } | ||
328 | |||
329 | #ifdef RGBLIGHT_ENABLE | ||
330 | // Code to send RGB over serial goes here (not implemented yet) | ||
331 | #endif | ||
332 | |||
333 | #ifdef BACKLIGHT_ENABLE | ||
334 | // Write backlight level for slave to read | ||
335 | serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0; | ||
336 | #endif | ||
337 | |||
338 | return 0; | ||
339 | } | ||
340 | #endif | ||
341 | |||
342 | uint8_t matrix_scan(void) | ||
343 | { | ||
344 | uint8_t ret = _matrix_scan(); | ||
345 | |||
346 | #if defined(USE_I2C) || defined(EH) | ||
347 | if( i2c_transaction() ) { | ||
348 | #else // USE_SERIAL | ||
349 | if( serial_transaction() ) { | ||
350 | #endif | ||
351 | |||
352 | error_count++; | ||
353 | |||
354 | if (error_count > ERROR_DISCONNECT_COUNT) { | ||
355 | // reset other half if disconnected | ||
356 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
357 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
358 | matrix[slaveOffset+i] = 0; | ||
359 | } | ||
360 | } | ||
361 | } else { | ||
362 | error_count = 0; | ||
363 | } | ||
364 | matrix_scan_quantum(); | ||
365 | return ret; | ||
366 | } | ||
367 | |||
368 | void matrix_slave_scan(void) { | ||
369 | _matrix_scan(); | ||
370 | |||
371 | int offset = (isLeftHand) ? 0 : ROWS_PER_HAND; | ||
372 | |||
373 | #if defined(USE_I2C) || defined(EH) | ||
374 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
375 | i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i]; | ||
376 | } | ||
377 | #else // USE_SERIAL | ||
378 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
379 | serial_slave_buffer[i] = matrix[offset+i]; | ||
380 | } | ||
381 | #endif | ||
382 | matrix_slave_scan_user(); | ||
383 | } | ||
384 | |||
385 | bool matrix_is_modified(void) | ||
386 | { | ||
387 | if (debouncing) return false; | ||
388 | return true; | ||
389 | } | ||
390 | |||
391 | inline | ||
392 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
393 | { | ||
394 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
395 | } | ||
396 | |||
397 | inline | ||
398 | matrix_row_t matrix_get_row(uint8_t row) | ||
399 | { | ||
400 | return matrix[row]; | ||
401 | } | ||
402 | |||
403 | void matrix_print(void) | ||
404 | { | ||
405 | print("\nr/c 0123456789ABCDEF\n"); | ||
406 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
407 | phex(row); print(": "); | ||
408 | pbin_reverse16(matrix_get_row(row)); | ||
409 | print("\n"); | ||
410 | } | ||
411 | } | ||
412 | |||
413 | uint8_t matrix_key_count(void) | ||
414 | { | ||
415 | uint8_t count = 0; | ||
416 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
417 | count += bitpop16(matrix[i]); | ||
418 | } | ||
419 | return count; | ||
420 | } | ||
421 | |||
422 | #if (DIODE_DIRECTION == COL2ROW) | ||
423 | |||
424 | static void init_cols(void) | ||
425 | { | ||
426 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
427 | uint8_t pin = col_pins[x]; | ||
428 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
429 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
430 | } | ||
431 | } | ||
432 | |||
433 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | ||
434 | { | ||
435 | // Store last value of row prior to reading | ||
436 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
437 | |||
438 | // Clear data in matrix row | ||
439 | current_matrix[current_row] = 0; | ||
440 | |||
441 | // Select row and wait for row selecton to stabilize | ||
442 | select_row(current_row); | ||
443 | wait_us(30); | ||
444 | |||
445 | // For each col... | ||
446 | for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
447 | |||
448 | // Select the col pin to read (active low) | ||
449 | uint8_t pin = col_pins[col_index]; | ||
450 | uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); | ||
451 | |||
452 | // Populate the matrix row with the state of the col pin | ||
453 | current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); | ||
454 | } | ||
455 | |||
456 | // Unselect row | ||
457 | unselect_row(current_row); | ||
458 | |||
459 | return (last_row_value != current_matrix[current_row]); | ||
460 | } | ||
461 | |||
462 | static void select_row(uint8_t row) | ||
463 | { | ||
464 | uint8_t pin = row_pins[row]; | ||
465 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | ||
466 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
467 | } | ||
468 | |||
469 | static void unselect_row(uint8_t row) | ||
470 | { | ||
471 | uint8_t pin = row_pins[row]; | ||
472 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
473 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
474 | } | ||
475 | |||
476 | static void unselect_rows(void) | ||
477 | { | ||
478 | for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { | ||
479 | uint8_t pin = row_pins[x]; | ||
480 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
481 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
482 | } | ||
483 | } | ||
484 | |||
485 | #elif (DIODE_DIRECTION == ROW2COL) | ||
486 | |||
487 | static void init_rows(void) | ||
488 | { | ||
489 | for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { | ||
490 | uint8_t pin = row_pins[x]; | ||
491 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
492 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
493 | } | ||
494 | } | ||
495 | |||
496 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | ||
497 | { | ||
498 | bool matrix_changed = false; | ||
499 | |||
500 | // Select col and wait for col selecton to stabilize | ||
501 | select_col(current_col); | ||
502 | wait_us(30); | ||
503 | |||
504 | // For each row... | ||
505 | for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) | ||
506 | { | ||
507 | |||
508 | // Store last value of row prior to reading | ||
509 | matrix_row_t last_row_value = current_matrix[row_index]; | ||
510 | |||
511 | // Check row pin state | ||
512 | if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) | ||
513 | { | ||
514 | // Pin LO, set col bit | ||
515 | current_matrix[row_index] |= (ROW_SHIFTER << current_col); | ||
516 | } | ||
517 | else | ||
518 | { | ||
519 | // Pin HI, clear col bit | ||
520 | current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); | ||
521 | } | ||
522 | |||
523 | // Determine if the matrix changed state | ||
524 | if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) | ||
525 | { | ||
526 | matrix_changed = true; | ||
527 | } | ||
528 | } | ||
529 | |||
530 | // Unselect col | ||
531 | unselect_col(current_col); | ||
532 | |||
533 | return matrix_changed; | ||
534 | } | ||
535 | |||
536 | static void select_col(uint8_t col) | ||
537 | { | ||
538 | uint8_t pin = col_pins[col]; | ||
539 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | ||
540 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
541 | } | ||
542 | |||
543 | static void unselect_col(uint8_t col) | ||
544 | { | ||
545 | uint8_t pin = col_pins[col]; | ||
546 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
547 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
548 | } | ||
549 | |||
550 | static void unselect_cols(void) | ||
551 | { | ||
552 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
553 | uint8_t pin = col_pins[x]; | ||
554 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
555 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
556 | } | ||
557 | } | ||
558 | |||
559 | #elif (DIODE_DIRECTION == CUSTOM_MATRIX) | ||
560 | |||
561 | static void init_cols_rows(void) | ||
562 | { | ||
563 | for(int row = 0; row < MATRIX_ROWS; row++) { | ||
564 | for(int col = 0; col < MATRIX_COLS; col++) { | ||
565 | uint8_t pin = row_col_pins[row][col]; | ||
566 | if(pin == NO_PIN) { | ||
567 | continue; | ||
568 | } | ||
569 | // DDxn set 0 for input | ||
570 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); | ||
571 | // PORTxn set 1 for input/pullup | ||
572 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); | ||
573 | } | ||
574 | } | ||
575 | } | ||
576 | |||
577 | static bool read_cols(matrix_row_t current_matrix[], uint8_t current_row) | ||
578 | { | ||
579 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
580 | current_matrix[current_row] = 0; | ||
581 | |||
582 | for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
583 | uint8_t pin = row_col_pins[current_row][col_index]; | ||
584 | if(pin == NO_PIN) { | ||
585 | current_matrix[current_row] |= 0; | ||
586 | } | ||
587 | else { | ||
588 | uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); | ||
589 | current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); | ||
590 | } | ||
591 | } | ||
592 | |||
593 | return (last_row_value != current_matrix[current_row]); | ||
594 | } | ||
595 | |||
596 | #endif | ||