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();
152 default: // normal key make
153 if (code < 0x80 && code != 0x00) {
154 xprintf("make: %X\r\n", code);
156 } else if (code > 0x80 && code < 0xFF && code != 0x00) {
157 xprintf("break %X\r\n", code);
158 matrix_break(code - 0x80);
163 case E0: // E0-Prefixed
164 switch (code) { //move these codes to unused places on the matrix
172 if (code < 0x80 && code != 0x00) {
173 matrix_make(move_codes(code));
174 } else if (code > 0x80 && code < 0xFF && code != 0x00) {
175 matrix_break(move_codes(code - 0x80));
188 matrix_make(PRINT_SCREEN);
200 matrix_break(PRINT_SCREEN);
224 state = E1_1D_45_E1_9D;
240 bool matrix_is_modified(void)
246 bool matrix_has_ghost(void)
248 #ifdef MATRIX_HAS_GHOST
249 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
250 if (matrix_has_ghost_in_row(i))
258 bool matrix_is_on(uint8_t row, uint8_t col)
260 return (matrix[row] & (1<<col));
264 uint8_t matrix_get_row(uint8_t row)
269 void matrix_print(void)
271 print("\nr/c 01234567\n");
272 for (uint8_t row = 0; row < matrix_rows(); row++) {
273 phex(row); print(": ");
274 pbin_reverse(matrix_get_row(row));
275 #ifdef MATRIX_HAS_GHOST
276 if (matrix_has_ghost_in_row(row)) {
284 uint8_t matrix_key_count(void)
287 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
288 count += bitpop(matrix[i]);
293 #ifdef MATRIX_HAS_GHOST
295 static bool matrix_has_ghost_in_row(uint8_t row)
297 // no ghost exists in case less than 2 keys on
298 if (((matrix[row] - 1) & matrix[row]) == 0)
301 // ghost exists in case same state as other row
302 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
303 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
312 static void matrix_make(uint8_t code)
314 if (!matrix_is_on(ROW(code), COL(code))) {
315 matrix[ROW(code)] |= 1<<COL(code);
321 static void matrix_break(uint8_t code)
323 if (matrix_is_on(ROW(code), COL(code))) {
324 matrix[ROW(code)] &= ~(1<<COL(code));
330 static void matrix_clear(void)
332 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;