]>
git.gir.st - tmk_keyboard.git/blob - converter/terminal_usb/matrix.c
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
21 #include <util/delay.h>
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
);
38 * 'Scan Code Set 3' is assigned into 17x8 cell matrix.
48 static uint8_t matrix
[MATRIX_ROWS
];
49 #define ROW(code) (code>>3)
50 #define COL(code) (code&0x07)
52 static bool is_modified
= false;
56 uint8_t matrix_rows(void)
62 uint8_t matrix_cols(void)
67 void matrix_init(void)
70 //debug_matrix = true;
71 //debug_keyboard = true;
72 //debug_mouse = false;
76 // initialize matrix state: all keys off
77 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
82 uint8_t matrix_scan(void)
85 // scan code reading states
99 if ((code
= ps2_host_recv())) {
100 debug("r"); debug_hex(code
); debug(" ");
106 if (ps2_host_send(0xFF) == 0xFA) {
107 debug("[ack]\nRESET_RESPONSE: ");
108 state
= RESET_RESPONSE
;
113 debug("[ok]\nKBD_ID: ");
116 debug("err\nRESET: ");
120 // after reset receive keyboad ID(2 bytes)
134 if (ps2_host_send(0xF8) == 0xFA) {
135 debug("[ack]\nREADY\n");
147 default: // normal key make
151 debug("unexpected scan code at READY: "); debug_hex(code
); debug("\n");
157 case F0
: // Break code
165 debug("unexpected scan code at F0: "); debug_hex(code
); debug("\n");
175 bool matrix_is_modified(void)
181 bool matrix_has_ghost(void)
183 #ifdef MATRIX_HAS_GHOST
184 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
185 if (matrix_has_ghost_in_row(i
))
193 bool matrix_is_on(uint8_t row
, uint8_t col
)
195 return (matrix
[row
] & (1<<col
));
199 uint8_t matrix_get_row(uint8_t row
)
204 void matrix_print(void)
206 print("\nr/c 01234567\n");
207 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
208 phex(row
); print(": ");
209 pbin_reverse(matrix_get_row(row
));
210 #ifdef MATRIX_HAS_GHOST
211 if (matrix_has_ghost_in_row(row
)) {
219 uint8_t matrix_key_count(void)
222 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
223 count
+= bitpop(matrix
[i
]);
228 #ifdef MATRIX_HAS_GHOST
230 static bool matrix_has_ghost_in_row(uint8_t row
)
232 // no ghost exists in case less than 2 keys on
233 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
236 // ghost exists in case same state as other row
237 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
238 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
247 static void matrix_make(uint8_t code
)
249 if (!matrix_is_on(ROW(code
), COL(code
))) {
250 matrix
[ROW(code
)] |= 1<<COL(code
);
256 static void matrix_break(uint8_t code
)
258 if (matrix_is_on(ROW(code
), COL(code
))) {
259 matrix
[ROW(code
)] &= ~(1<<COL(code
));