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>
28 #define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
29 #define print_matrix_header() print("\nr/c 01234567\n")
30 #define matrix_bitpop(i) bitpop(matrix[i])
31 #define ROW_SHIFTER ((uint8_t)1)
34 static void matrix_make(uint8_t code);
35 static void matrix_break(uint8_t code);
40 * 'Scan Code Set 3' is assigned into 17x8 cell matrix.
50 static uint8_t matrix[MATRIX_ROWS];
51 #define ROW(code) (code>>3)
52 #define COL(code) (code&0x07)
55 __attribute__ ((weak))
56 void matrix_init_user(void) {
59 void matrix_init(void)
62 //debug_matrix = true;
63 //debug_keyboard = true;
64 //debug_mouse = false;
68 // initialize matrix state: all keys off
69 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
75 uint8_t matrix_scan(void)
78 // scan code reading states
90 if ((code = ps2_host_recv())) {
91 debug("r"); debug_hex(code); debug(" ");
97 if (ps2_host_send(0xFF) == 0xFA) {
98 debug("[ack]\nRESET_RESPONSE: ");
99 state = RESET_RESPONSE;
104 debug("[ok]\nKBD_ID: ");
107 debug("err\nRESET: ");
111 // after reset receive keyboard ID(2 bytes)
125 if (ps2_host_send(0xF8) == 0xFA) {
126 debug("[ack]\nREADY\n");
138 default: // normal key make
142 debug("unexpected scan code at READY: "); debug_hex(code); debug("\n");
148 case F0: // Break code
156 debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
167 uint8_t matrix_get_row(uint8_t row)
173 static void matrix_make(uint8_t code)
175 if (!matrix_is_on(ROW(code), COL(code))) {
176 matrix[ROW(code)] |= 1<<COL(code);
181 static void matrix_break(uint8_t code)
183 if (matrix_is_on(ROW(code), COL(code))) {
184 matrix[ROW(code)] &= ~(1<<COL(code));
188 bool matrix_is_on(uint8_t row, uint8_t col)
190 return (matrix_get_row(row) & (1<<col));
193 void matrix_print(void)
195 #if (MATRIX_COLS <= 8)
196 print("r/c 01234567\n");
197 #elif (MATRIX_COLS <= 16)
198 print("r/c 0123456789ABCDEF\n");
199 #elif (MATRIX_COLS <= 32)
200 print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
203 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
205 #if (MATRIX_COLS <= 8)
206 xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
207 #elif (MATRIX_COLS <= 16)
208 xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
209 #elif (MATRIX_COLS <= 32)
210 xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
212 #ifdef MATRIX_HAS_GHOST
213 matrix_has_ghost_in_row(row) ? " <ghost" : ""
221 #ifdef MATRIX_HAS_GHOST
222 __attribute__ ((weak))
223 bool matrix_has_ghost_in_row(uint8_t row)
225 matrix_row_t matrix_row = matrix_get_row(row);
226 // No ghost exists when less than 2 keys are down on the row
227 if (((matrix_row - 1) & matrix_row) == 0)
230 // Ghost occurs when the row shares column line with other row
231 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
232 if (i != row && (matrix_get_row(i) & matrix_row))