]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/xt_usb/matrix.c
4218e39bb367066c9e6ac24f4f85e90a0c0c315d
[max/tmk_keyboard.git] / converter / xt_usb / matrix.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3 Copyright 2016 Ethan Apodaca <papodaca@gmail.com>
4
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.
9
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.
14
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/>.
17 */
18
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include "action.h"
22 #include "print.h"
23 #include "util.h"
24 #include "debug.h"
25 #include "xt.h"
26 #include "matrix.h"
27
28
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);
34 #endif
35
36 static uint8_t matrix[MATRIX_ROWS];
37 #define ROW(code)      (code>>3)
38 #define COL(code)      (code&0x07)
39
40 // matrix positions for exceptional keys
41 #define PRINT_SCREEN   (0x7C)
42 #define PAUSE          (0x7D)
43
44 static bool is_modified = false;
45
46
47 inline
48 uint8_t matrix_rows(void)
49 {
50     return MATRIX_ROWS;
51 }
52
53 inline
54 uint8_t matrix_cols(void)
55 {
56     return MATRIX_COLS;
57 }
58
59 void matrix_init(void)
60 {
61     debug_enable = true;
62     xt_host_init();
63
64     // initialize matrix state: all keys off
65     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
66
67     return;
68 }
69
70 static uint8_t move_codes(uint8_t code) {
71     switch(code) {
72         case 0x10:
73             code += 0x5E;
74             break;
75         case 0x19:
76             code += 0x41;
77             break;
78         case 0x1C:
79         case 0x1D:
80             code += 0x38;
81             break;
82         case 0x20:
83         case 0x21:
84         case 0x22:
85         case 0x24:
86             code += 0x40;
87             break;
88         case 0x2E:
89         case 0x30:
90         case 0x32:
91             code += 0x44;
92             break;
93         case 0x35:
94         case 0x38:
95             code += 0x21;
96             break;
97         case 0x47:
98         case 0x48:
99         case 0x49:
100         case 0x4B:
101         case 0x4D:
102         case 0x4F:
103         case 0x50:
104         case 0x51:
105         case 0x52:
106         case 0x53:
107             code += 0x28;
108             break;
109     }
110     return code;
111 }
112
113 uint8_t matrix_scan(void)
114 {
115
116     // scan code reading states
117     static enum {
118         INIT,
119         E0,
120         E0_2A,
121         E0_2A_E0,
122         E0_B7,
123         E0_B7_E0,
124
125         // print screen
126         E1,
127         E1_1D,
128         E1_1D_45,
129         E1_1D_45_E1,
130         E1_1D_45_E1_9D,
131         // pause
132     } state = INIT;
133
134
135     is_modified = false;
136
137     // 'pseudo break code' hack
138     if (matrix_is_on(ROW(PAUSE), COL(PAUSE))) {
139         matrix_break(PAUSE);
140     }
141
142     uint8_t code = xt_host_recv();
143     if (code) xprintf("%X\r\n", code);
144     switch (state) {
145         case INIT:
146             switch (code) {
147                 case 0xE0:
148                     state = E0;
149                     break;
150                 case 0xE1:
151                     state = E1;
152                     break;
153                 case 0x00:
154                     break;
155                 default:    // normal key make
156                     if (code < 0x80) {
157                         xprintf("make: %X\r\n", code);
158                         matrix_make(code);
159                     } else if (code > 0x80 && code < 0xFF) {
160                         xprintf("break %X\r\n", code);
161                         matrix_break(code - 0x80);
162                     } else {
163                         matrix_clear();
164                         clear_keyboard();
165                         xprintf("unexpected scan code at INIT: %02X\n", code);
166                     }
167                     state = INIT;
168             }
169             break;
170         case E0:    // E0-Prefixed
171             switch (code) { //move these codes to unused places on the matrix
172                 case 0x2A:
173                     state = E0_2A;
174                     break;
175                 case 0xB7:
176                     state = E0_B7;
177                     break;
178                 case 0x00:
179                     state = INIT;
180                     break;
181                 default:
182                     if (code < 0x80) {
183                         matrix_make(move_codes(code));
184                     } else if (code > 0x80 && code < 0xFF) {
185                         matrix_break(move_codes(code - 0x80));
186                     } else {
187                         matrix_clear();
188                         clear_keyboard();
189                         xprintf("unexpected scan code at E0: %02X\n", code);
190                     }
191                     state = INIT;
192             }
193             break;
194         case E0_2A:
195             switch (code) {
196                 case 0xE0:
197                     state = E0_2A_E0;
198                     break;
199                 default:
200                     state = INIT;
201             }
202             break;
203         case E0_2A_E0:
204             switch (code) {
205                 case 0x37:
206                     matrix_make(PRINT_SCREEN);
207                     break;
208                 default:
209                     state = INIT;
210             }
211             break;
212         case E0_B7:
213             switch (code) {
214                 case 0xE0:
215                     state = E0_B7;
216                     break;
217                 default:
218                     state = INIT;
219             }
220             break;
221         case E0_B7_E0:
222           switch (code) {
223               case 0xAA:
224                   matrix_break(PRINT_SCREEN);
225                   break;
226               default:
227                   state = INIT;
228           }
229           break;
230         case E1:
231             switch (code) {
232                 case 0x1D:
233                     state = E1_1D;
234                     break;
235                 default:
236                     state = INIT;
237             }
238             break;
239         case E1_1D:
240             switch (code) {
241                 case 0x45:
242                     state = E1_1D_45;
243                     break;
244                 default:
245                     state = INIT;
246             }
247             break;
248         case E1_1D_45:
249             switch (code) {
250                 case 0xE1:
251                     state = E1_1D_45_E1;
252                     break;
253                 default:
254                     state = INIT;
255             }
256             break;
257         case E1_1D_45_E1:
258             switch (code) {
259                 case 0x9D:
260                     state = E1_1D_45_E1_9D;
261                     break;
262                 default:
263                     state = INIT;
264             }
265             break;
266         case E1_1D_45_E1_9D:
267             switch (code) {
268                 case 0xC5:
269                     matrix_make(PAUSE);
270                     break;
271                 default:
272                     state = INIT;
273             }
274             break;
275         default:
276             state = INIT;
277     }
278
279     // TODO: request RESEND when error occurs?
280 /*
281     if (PS2_IS_FAILED(ps2_error)) {
282         uint8_t ret = ps2_host_send(PS2_RESEND);
283         xprintf("Resend: %02X\n", ret);
284     }
285 */
286     return 1;
287 }
288
289 bool matrix_is_modified(void)
290 {
291     return is_modified;
292 }
293
294 inline
295 bool matrix_has_ghost(void)
296 {
297 #ifdef MATRIX_HAS_GHOST
298     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
299         if (matrix_has_ghost_in_row(i))
300             return true;
301     }
302 #endif
303     return false;
304 }
305
306 inline
307 bool matrix_is_on(uint8_t row, uint8_t col)
308 {
309     return (matrix[row] & (1<<col));
310 }
311
312 inline
313 uint8_t matrix_get_row(uint8_t row)
314 {
315     return matrix[row];
316 }
317
318 void matrix_print(void)
319 {
320     print("\nr/c 01234567\n");
321     for (uint8_t row = 0; row < matrix_rows(); row++) {
322         phex(row); print(": ");
323         pbin_reverse(matrix_get_row(row));
324 #ifdef MATRIX_HAS_GHOST
325         if (matrix_has_ghost_in_row(row)) {
326             print(" <ghost");
327         }
328 #endif
329         print("\n");
330     }
331 }
332
333 uint8_t matrix_key_count(void)
334 {
335     uint8_t count = 0;
336     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
337         count += bitpop(matrix[i]);
338     }
339     return count;
340 }
341
342 #ifdef MATRIX_HAS_GHOST
343 inline
344 static bool matrix_has_ghost_in_row(uint8_t row)
345 {
346     // no ghost exists in case less than 2 keys on
347     if (((matrix[row] - 1) & matrix[row]) == 0)
348         return false;
349
350     // ghost exists in case same state as other row
351     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
352         if (i != row && (matrix[i] & matrix[row]) == matrix[row])
353             return true;
354     }
355     return false;
356 }
357 #endif
358
359
360 inline
361 static void matrix_make(uint8_t code)
362 {
363     if (!matrix_is_on(ROW(code), COL(code))) {
364         matrix[ROW(code)] |= 1<<COL(code);
365         is_modified = true;
366     }
367 }
368
369 inline
370 static void matrix_break(uint8_t code)
371 {
372     if (matrix_is_on(ROW(code), COL(code))) {
373         matrix[ROW(code)] &= ~(1<<COL(code));
374         is_modified = true;
375     }
376 }
377
378 inline
379 static void matrix_clear(void)
380 {
381     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
382 }