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)
71 //debug_matrix = true;
72 //debug_keyboard = true;
73 //debug_mouse = false;
76 // Make and Break code without Typematic
77 while (ps2_host_send(0xF8) != 0xFA) {
78 debug("send F8: failed\n");
81 debug("send F8: OK\n");
84 // initialize matrix state: all keys off
85 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
90 uint8_t matrix_scan(void)
93 // scan code reading states
103 while ((code = ps2_host_recv())) {
112 default: // normal key make
116 debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n");
122 case F0: // Break code
128 debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
139 bool matrix_is_modified(void)
145 bool matrix_has_ghost(void)
147 #ifdef MATRIX_HAS_GHOST
148 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
149 if (matrix_has_ghost_in_row(i))
157 bool matrix_is_on(uint8_t row, uint8_t col)
159 return (matrix[row] & (1<<col));
163 uint8_t matrix_get_row(uint8_t row)
168 void matrix_print(void)
170 print("\nr/c 01234567\n");
171 for (uint8_t row = 0; row < matrix_rows(); row++) {
172 phex(row); print(": ");
173 pbin_reverse(matrix_get_row(row));
174 #ifdef MATRIX_HAS_GHOST
175 if (matrix_has_ghost_in_row(row)) {
183 uint8_t matrix_key_count(void)
186 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
187 count += bitpop(matrix[i]);
192 #ifdef MATRIX_HAS_GHOST
194 static bool matrix_has_ghost_in_row(uint8_t row)
196 // no ghost exists in case less than 2 keys on
197 if (((matrix[row] - 1) & matrix[row]) == 0)
200 // ghost exists in case same state as other row
201 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
202 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
211 static void matrix_make(uint8_t code)
213 if (!matrix_is_on(ROW(code), COL(code))) {
214 matrix[ROW(code)] |= 1<<COL(code);
220 static void matrix_break(uint8_t code)
222 if (matrix_is_on(ROW(code), COL(code))) {
223 matrix[ROW(code)] &= ~(1<<COL(code));