aa0c38c68d56067094e1122364d8efdac334f8ba
[tmk_keyboard.git] / converter / ps2_usb / matrix.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "action.h"
23 #include "print.h"
24 #include "util.h"
25 #include "debug.h"
26 #include "ps2.h"
27 #include "matrix.h"
28
29
30 static void matrix_make(uint8_t code);
31 static void matrix_break(uint8_t code);
32 #ifdef MATRIX_HAS_GHOST
33 static bool matrix_has_ghost_in_row(uint8_t row);
34 #endif
35
36
37 /*
38 * Matrix Array usage:
39 * 'Scan Code Set 2' is assigned into 256(32x8)cell matrix.
40 * Hmm, it is very sparse and not efficient :(
41 *
42 * Notes:
43 * Both 'Hanguel/English'(F1) and 'Hanja'(F2) collide with 'Delete'(E0 71) and 'Down'(E0 72).
44 * These two Korean keys need exceptional handling and are not supported for now. Sorry.
45 *
46 * 8bit wide
47 * +---------+
48 * 0| |
49 * :| XX | 00-7F for normal codes(without E0-prefix)
50 * f|_________|
51 * 10| |
52 * :| E0 YY | 80-FF for E0-prefixed codes
53 * 1f| | (<YY>|0x80) is used as matrix position.
54 * +---------+
55 *
56 * Exceptions:
57 * 0x83: F7(0x83) This is a normal code but beyond 0x7F.
58 * 0xFC: PrintScreen
59 * 0xFE: Pause
60 */
61 static uint8_t matrix[MATRIX_ROWS];
62 #define ROW(code) (code>>3)
63 #define COL(code) (code&0x07)
64
65 // matrix positions for exceptional keys
66 #define F7 (0x83)
67 #define PRINT_SCREEN (0xFC)
68 #define PAUSE (0xFE)
69
70 static bool is_modified = false;
71
72
73 inline
74 uint8_t matrix_rows(void)
75 {
76 return MATRIX_ROWS;
77 }
78
79 inline
80 uint8_t matrix_cols(void)
81 {
82 return MATRIX_COLS;
83 }
84
85 void matrix_init(void)
86 {
87 ps2_host_init();
88
89 // initialize matrix state: all keys off
90 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
91
92 return;
93 }
94
95 /*
96 * PS/2 Scan Code Set 2: Exceptional Handling
97 *
98 * There are several keys to be handled exceptionally.
99 * The scan code for these keys are varied or prefix/postfix'd
100 * depending on modifier key state.
101 *
102 * Keyboard Scan Code Specification:
103 * http://www.microsoft.com/whdc/archive/scancode.mspx
104 * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
105 *
106 *
107 * 1) Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left
108 * a) when Num Lock is off
109 * modifiers | make | break
110 * ----------+---------------------------+----------------------
111 * Ohter | <make> | <break>
112 * LShift | E0 F0 12 <make> | <break> E0 12
113 * RShift | E0 F0 59 <make> | <break> E0 59
114 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
115 *
116 * b) when Num Lock is on
117 * modifiers | make | break
118 * ----------+---------------------------+----------------------
119 * Other | E0 12 <make> | <break> E0 F0 12
120 * Shift'd | <make> | <break>
121 *
122 * Handling: These prefix/postfix codes are ignored.
123 *
124 *
125 * 2) Keypad /
126 * modifiers | make | break
127 * ----------+---------------------------+----------------------
128 * Ohter | <make> | <break>
129 * LShift | E0 F0 12 <make> | <break> E0 12
130 * RShift | E0 F0 59 <make> | <break> E0 59
131 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
132 *
133 * Handling: These prefix/postfix codes are ignored.
134 *
135 *
136 * 3) PrintScreen
137 * modifiers | make | break
138 * ----------+--------------+-----------------------------------
139 * Other | E0 12 E0 7C | E0 F0 7C E0 F0 12
140 * Shift'd | E0 7C | E0 F0 7C
141 * Control'd | E0 7C | E0 F0 7C
142 * Alt'd | 84 | F0 84
143 *
144 * Handling: These prefix/postfix codes are ignored, and both scan codes
145 * 'E0 7C' and 84 are seen as PrintScreen.
146 *
147 * 4) Pause
148 * modifiers | make(no break code)
149 * ----------+--------------------------------------------------
150 * Other | E1 14 77 E1 F0 14 F0 77
151 * Control'd | E0 7E E0 F0 7E
152 *
153 * Handling: Both code sequences are treated as a whole.
154 * And we need a ad hoc 'pseudo break code' hack to get the key off
155 * because it has no break code.
156 *
157 */
158 uint8_t matrix_scan(void)
159 {
160
161 // scan code reading states
162 static enum {
163 INIT,
164 F0,
165 E0,
166 E0_F0,
167 // Pause
168 E1,
169 E1_14,
170 E1_14_77,
171 E1_14_77_E1,
172 E1_14_77_E1_F0,
173 E1_14_77_E1_F0_14,
174 E1_14_77_E1_F0_14_F0,
175 // Control'd Pause
176 E0_7E,
177 E0_7E_E0,
178 E0_7E_E0_F0,
179 } state = INIT;
180
181
182 is_modified = false;
183
184 // 'pseudo break code' hack
185 if (matrix_is_on(ROW(PAUSE), COL(PAUSE))) {
186 matrix_break(PAUSE);
187 }
188
189 uint8_t code = ps2_host_recv();
190 if (!ps2_error) {
191 switch (state) {
192 case INIT:
193 switch (code) {
194 case 0xE0:
195 state = E0;
196 break;
197 case 0xF0:
198 state = F0;
199 break;
200 case 0xE1:
201 state = E1;
202 break;
203 case 0x83: // F7
204 matrix_make(F7);
205 state = INIT;
206 break;
207 case 0x84: // Alt'd PrintScreen
208 matrix_make(PRINT_SCREEN);
209 state = INIT;
210 break;
211 case 0x00: // Overrun [3]p.25
212 print("Overrun\n");
213 clear_keyboard();
214 state = INIT;
215 break;
216 default: // normal key make
217 if (code < 0x80) {
218 matrix_make(code);
219 } else {
220 xprintf("unexpected scan code at INIT: %02X\n", code);
221 clear_keyboard();
222 }
223 state = INIT;
224 }
225 break;
226 case E0: // E0-Prefixed
227 switch (code) {
228 case 0x12: // to be ignored
229 case 0x59: // to be ignored
230 state = INIT;
231 break;
232 case 0x7E: // Control'd Pause
233 state = E0_7E;
234 break;
235 case 0xF0:
236 state = E0_F0;
237 break;
238 default:
239 if (code < 0x80) {
240 matrix_make(code|0x80);
241 } else {
242 xprintf("unexpected scan code at E0: %02X\n", code);
243 clear_keyboard();
244 }
245 state = INIT;
246 }
247 break;
248 case F0: // Break code
249 switch (code) {
250 case 0x83: // F7
251 matrix_break(F7);
252 state = INIT;
253 break;
254 case 0x84: // Alt'd PrintScreen
255 matrix_break(PRINT_SCREEN);
256 state = INIT;
257 break;
258 default:
259 if (code < 0x80) {
260 matrix_break(code);
261 } else {
262 xprintf("unexpected scan code at F0: %02X\n", code);
263 clear_keyboard();
264 }
265 state = INIT;
266 }
267 break;
268 case E0_F0: // Break code of E0-prefixed
269 switch (code) {
270 case 0x12: // to be ignored
271 case 0x59: // to be ignored
272 state = INIT;
273 break;
274 default:
275 if (code < 0x80) {
276 matrix_break(code|0x80);
277 } else {
278 xprintf("unexpected scan code at E0_F0: %02X\n", code);
279 clear_keyboard();
280 }
281 state = INIT;
282 }
283 break;
284 // following are states of Pause
285 case E1:
286 switch (code) {
287 case 0x14:
288 state = E1_14;
289 break;
290 default:
291 state = INIT;
292 }
293 break;
294 case E1_14:
295 switch (code) {
296 case 0x77:
297 state = E1_14_77;
298 break;
299 default:
300 state = INIT;
301 }
302 break;
303 case E1_14_77:
304 switch (code) {
305 case 0xE1:
306 state = E1_14_77_E1;
307 break;
308 default:
309 state = INIT;
310 }
311 break;
312 case E1_14_77_E1:
313 switch (code) {
314 case 0xF0:
315 state = E1_14_77_E1_F0;
316 break;
317 default:
318 state = INIT;
319 }
320 break;
321 case E1_14_77_E1_F0:
322 switch (code) {
323 case 0x14:
324 state = E1_14_77_E1_F0_14;
325 break;
326 default:
327 state = INIT;
328 }
329 break;
330 case E1_14_77_E1_F0_14:
331 switch (code) {
332 case 0xF0:
333 state = E1_14_77_E1_F0_14_F0;
334 break;
335 default:
336 state = INIT;
337 }
338 break;
339 case E1_14_77_E1_F0_14_F0:
340 switch (code) {
341 case 0x77:
342 matrix_make(PAUSE);
343 state = INIT;
344 break;
345 default:
346 state = INIT;
347 }
348 break;
349 // Following are states of Control'd Pause
350 case E0_7E:
351 if (code == 0xE0)
352 state = E0_7E_E0;
353 else
354 state = INIT;
355 break;
356 case E0_7E_E0:
357 if (code == 0xF0)
358 state = E0_7E_E0_F0;
359 else
360 state = INIT;
361 break;
362 case E0_7E_E0_F0:
363 if (code == 0x7E)
364 matrix_make(PAUSE);
365 state = INIT;
366 break;
367 default:
368 state = INIT;
369 }
370 }
371
372 if (ps2_error > PS2_ERR_STARTBIT3) {
373 uint8_t ret = ps2_host_send(PS2_RESEND);
374 xprintf("Resend: %02X\n", ret);
375 }
376 return 1;
377 }
378
379 bool matrix_is_modified(void)
380 {
381 return is_modified;
382 }
383
384 inline
385 bool matrix_has_ghost(void)
386 {
387 #ifdef MATRIX_HAS_GHOST
388 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
389 if (matrix_has_ghost_in_row(i))
390 return true;
391 }
392 #endif
393 return false;
394 }
395
396 inline
397 bool matrix_is_on(uint8_t row, uint8_t col)
398 {
399 return (matrix[row] & (1<<col));
400 }
401
402 inline
403 uint8_t matrix_get_row(uint8_t row)
404 {
405 return matrix[row];
406 }
407
408 void matrix_print(void)
409 {
410 print("\nr/c 01234567\n");
411 for (uint8_t row = 0; row < matrix_rows(); row++) {
412 phex(row); print(": ");
413 pbin_reverse(matrix_get_row(row));
414 #ifdef MATRIX_HAS_GHOST
415 if (matrix_has_ghost_in_row(row)) {
416 print(" <ghost");
417 }
418 #endif
419 print("\n");
420 }
421 }
422
423 uint8_t matrix_key_count(void)
424 {
425 uint8_t count = 0;
426 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
427 count += bitpop(matrix[i]);
428 }
429 return count;
430 }
431
432 #ifdef MATRIX_HAS_GHOST
433 inline
434 static bool matrix_has_ghost_in_row(uint8_t row)
435 {
436 // no ghost exists in case less than 2 keys on
437 if (((matrix[row] - 1) & matrix[row]) == 0)
438 return false;
439
440 // ghost exists in case same state as other row
441 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
442 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
443 return true;
444 }
445 return false;
446 }
447 #endif
448
449
450 inline
451 static void matrix_make(uint8_t code)
452 {
453 if (!matrix_is_on(ROW(code), COL(code))) {
454 matrix[ROW(code)] |= 1<<COL(code);
455 is_modified = true;
456 }
457 }
458
459 inline
460 static void matrix_break(uint8_t code)
461 {
462 if (matrix_is_on(ROW(code), COL(code))) {
463 matrix[ROW(code)] &= ~(1<<COL(code));
464 is_modified = true;
465 }
466 }
Imprint / Impressum