]>
git.gir.st - tmk_keyboard.git/blob - converter/xt_usb/matrix.c
de9706a6ace4274c2d7700d94377661b927ab64c
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 #ifdef MATRIX_HAS_GHOST
32 static bool matrix_has_ghost_in_row(uint8_t row
);
35 static uint8_t matrix
[MATRIX_ROWS
];
36 #define ROW(code) (code>>3)
37 #define COL(code) (code&0x07)
39 // matrix positions for exceptional keys
40 #define PRINT_SCREEN (0x7C)
43 static bool is_modified
= false;
47 uint8_t matrix_rows(void)
53 uint8_t matrix_cols(void)
58 void matrix_init(void)
63 // initialize matrix state: all keys off
64 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
69 static uint8_t move_codes(uint8_t code
) {
112 uint8_t matrix_scan(void)
115 // scan code reading states
136 // 'pseudo break code' hack
137 if (matrix_is_on(ROW(PAUSE
), COL(PAUSE
))) {
141 uint8_t code
= xt_host_recv();
151 default: // normal key make
152 if (code
< 0x80 && code
!= 0x00) {
153 xprintf("make: %X\r\n", code
);
155 } else if (code
> 0x80 && code
< 0xFF && code
!= 0x00) {
156 xprintf("break %X\r\n", code
);
157 matrix_break(code
- 0x80);
162 case E0
: // E0-Prefixed
163 switch (code
) { //move these codes to unused places on the matrix
171 if (code
< 0x80 && code
!= 0x00) {
172 matrix_make(move_codes(code
));
173 } else if (code
> 0x80 && code
< 0xFF && code
!= 0x00) {
174 matrix_break(move_codes(code
- 0x80));
187 matrix_make(PRINT_SCREEN
);
199 matrix_break(PRINT_SCREEN
);
223 state
= E1_1D_45_E1_9D
;
239 bool matrix_is_modified(void)
245 bool matrix_has_ghost(void)
247 #ifdef MATRIX_HAS_GHOST
248 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
249 if (matrix_has_ghost_in_row(i
))
257 bool matrix_is_on(uint8_t row
, uint8_t col
)
259 return (matrix
[row
] & (1<<col
));
263 uint8_t matrix_get_row(uint8_t row
)
268 void matrix_print(void)
270 print("\nr/c 01234567\n");
271 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
272 phex(row
); print(": ");
273 pbin_reverse(matrix_get_row(row
));
274 #ifdef MATRIX_HAS_GHOST
275 if (matrix_has_ghost_in_row(row
)) {
283 uint8_t matrix_key_count(void)
286 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
287 count
+= bitpop(matrix
[i
]);
292 #ifdef MATRIX_HAS_GHOST
294 static bool matrix_has_ghost_in_row(uint8_t row
)
296 // no ghost exists in case less than 2 keys on
297 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
300 // ghost exists in case same state as other row
301 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
302 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
311 static void matrix_make(uint8_t code
)
313 if (!matrix_is_on(ROW(code
), COL(code
))) {
314 matrix
[ROW(code
)] |= 1<<COL(code
);
320 static void matrix_break(uint8_t code
)
322 if (matrix_is_on(ROW(code
), COL(code
))) {
323 matrix
[ROW(code
)] &= ~(1<<COL(code
));
328 void matrix_clear(void)
330 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;