]>
git.gir.st - tmk_keyboard.git/blob - converter/xt_usb/matrix.c
4218e39bb367066c9e6ac24f4f85e90a0c0c315d
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3 Copyright 2016 Ethan Apodaca <papodaca@gmail.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 static void matrix_make(uint8_t code
);
30 static void matrix_break(uint8_t code
);
31 static void matrix_clear(void);
32 #ifdef MATRIX_HAS_GHOST
33 static bool matrix_has_ghost_in_row(uint8_t row
);
36 static uint8_t matrix
[MATRIX_ROWS
];
37 #define ROW(code) (code>>3)
38 #define COL(code) (code&0x07)
40 // matrix positions for exceptional keys
41 #define PRINT_SCREEN (0x7C)
44 static bool is_modified
= false;
48 uint8_t matrix_rows(void)
54 uint8_t matrix_cols(void)
59 void matrix_init(void)
64 // initialize matrix state: all keys off
65 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
70 static uint8_t move_codes(uint8_t code
) {
113 uint8_t matrix_scan(void)
116 // scan code reading states
137 // 'pseudo break code' hack
138 if (matrix_is_on(ROW(PAUSE
), COL(PAUSE
))) {
142 uint8_t code
= xt_host_recv();
143 if (code
) xprintf("%X\r\n", code
);
155 default: // normal key make
157 xprintf("make: %X\r\n", code
);
159 } else if (code
> 0x80 && code
< 0xFF) {
160 xprintf("break %X\r\n", code
);
161 matrix_break(code
- 0x80);
165 xprintf("unexpected scan code at INIT: %02X\n", code
);
170 case E0
: // E0-Prefixed
171 switch (code
) { //move these codes to unused places on the matrix
183 matrix_make(move_codes(code
));
184 } else if (code
> 0x80 && code
< 0xFF) {
185 matrix_break(move_codes(code
- 0x80));
189 xprintf("unexpected scan code at E0: %02X\n", code
);
206 matrix_make(PRINT_SCREEN
);
224 matrix_break(PRINT_SCREEN
);
260 state
= E1_1D_45_E1_9D
;
279 // TODO: request RESEND when error occurs?
281 if (PS2_IS_FAILED(ps2_error)) {
282 uint8_t ret = ps2_host_send(PS2_RESEND);
283 xprintf("Resend: %02X\n", ret);
289 bool matrix_is_modified(void)
295 bool matrix_has_ghost(void)
297 #ifdef MATRIX_HAS_GHOST
298 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
299 if (matrix_has_ghost_in_row(i
))
307 bool matrix_is_on(uint8_t row
, uint8_t col
)
309 return (matrix
[row
] & (1<<col
));
313 uint8_t matrix_get_row(uint8_t row
)
318 void matrix_print(void)
320 print("\nr/c 01234567\n");
321 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
322 phex(row
); print(": ");
323 pbin_reverse(matrix_get_row(row
));
324 #ifdef MATRIX_HAS_GHOST
325 if (matrix_has_ghost_in_row(row
)) {
333 uint8_t matrix_key_count(void)
336 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
337 count
+= bitpop(matrix
[i
]);
342 #ifdef MATRIX_HAS_GHOST
344 static bool matrix_has_ghost_in_row(uint8_t row
)
346 // no ghost exists in case less than 2 keys on
347 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
350 // ghost exists in case same state as other row
351 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
352 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
361 static void matrix_make(uint8_t code
)
363 if (!matrix_is_on(ROW(code
), COL(code
))) {
364 matrix
[ROW(code
)] |= 1<<COL(code
);
370 static void matrix_break(uint8_t code
)
372 if (matrix_is_on(ROW(code
), COL(code
))) {
373 matrix
[ROW(code
)] &= ~(1<<COL(code
));
379 static void matrix_clear(void)
381 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;