]>
git.gir.st - tmk_keyboard.git/blob - converter/ps2_usb/matrix.c
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/>.
30 static void matrix_make(uint8_t code
);
31 static void matrix_break(uint8_t code
);
36 * 'Scan Code Set 2' is assigned into 256(32x8)cell matrix.
37 * Hmm, it is very sparse and not efficient :(
40 * Both 'Hanguel/English'(F1) and 'Hanja'(F2) collide with 'Delete'(E0 71) and 'Down'(E0 72).
41 * These two Korean keys need exceptional handling and are not supported for now. Sorry.
46 * :| XX | 00-7F for normal codes(without E0-prefix)
49 * :| E0 YY | 80-FF for E0-prefixed codes
50 * 1f| | (<YY>|0x80) is used as matrix position.
54 * 0x83: F7(0x83) This is a normal code but beyond 0x7F.
58 static uint8_t matrix
[MATRIX_ROWS
];
59 #define ROW(code) (code>>3)
60 #define COL(code) (code&0x07)
62 // matrix positions for exceptional keys
64 #define PRINT_SCREEN (0xFC)
67 static bool is_modified
= false;
70 void matrix_init(void)
75 // initialize matrix state: all keys off
76 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
82 * PS/2 Scan Code Set 2: Exceptional Handling
84 * There are several keys to be handled exceptionally.
85 * The scan code for these keys are varied or prefix/postfix'd
86 * depending on modifier key state.
88 * Keyboard Scan Code Specification:
89 * http://www.microsoft.com/whdc/archive/scancode.mspx
90 * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
93 * 1) Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left
94 * a) when Num Lock is off
95 * modifiers | make | break
96 * ----------+---------------------------+----------------------
97 * Ohter | <make> | <break>
98 * LShift | E0 F0 12 <make> | <break> E0 12
99 * RShift | E0 F0 59 <make> | <break> E0 59
100 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
102 * b) when Num Lock is on
103 * modifiers | make | break
104 * ----------+---------------------------+----------------------
105 * Other | E0 12 <make> | <break> E0 F0 12
106 * Shift'd | <make> | <break>
108 * Handling: These prefix/postfix codes are ignored.
112 * modifiers | make | break
113 * ----------+---------------------------+----------------------
114 * Ohter | <make> | <break>
115 * LShift | E0 F0 12 <make> | <break> E0 12
116 * RShift | E0 F0 59 <make> | <break> E0 59
117 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
119 * Handling: These prefix/postfix codes are ignored.
123 * modifiers | make | break
124 * ----------+--------------+-----------------------------------
125 * Other | E0 12 E0 7C | E0 F0 7C E0 F0 12
126 * Shift'd | E0 7C | E0 F0 7C
127 * Control'd | E0 7C | E0 F0 7C
130 * Handling: These prefix/postfix codes are ignored, and both scan codes
131 * 'E0 7C' and 84 are seen as PrintScreen.
134 * modifiers | make(no break code)
135 * ----------+--------------------------------------------------
136 * Other | E1 14 77 E1 F0 14 F0 77
137 * Control'd | E0 7E E0 F0 7E
139 * Handling: Both code sequences are treated as a whole.
140 * And we need a ad hoc 'pseudo break code' hack to get the key off
141 * because it has no break code.
144 uint8_t matrix_scan(void)
147 // scan code reading states
160 E1_14_77_E1_F0_14_F0
,
170 // 'pseudo break code' hack
171 if (matrix_is_on(ROW(PAUSE
), COL(PAUSE
))) {
175 uint8_t code
= ps2_host_recv();
176 if (code
) xprintf("%i\r\n", code
);
194 case 0x84: // Alt'd PrintScreen
195 matrix_make(PRINT_SCREEN
);
198 case 0x00: // Overrun [3]p.25
204 case 0xAA: // Self-test passed
205 case 0xFC: // Self-test failed
206 printf("BAT %s\n", (code
== 0xAA) ? "OK" : "NG");
207 led_set(host_keyboard_leds());
210 default: // normal key make
216 xprintf("unexpected scan code at INIT: %02X\n", code
);
221 case E0
: // E0-Prefixed
223 case 0x12: // to be ignored
224 case 0x59: // to be ignored
227 case 0x7E: // Control'd Pause
235 matrix_make(code
|0x80);
239 xprintf("unexpected scan code at E0: %02X\n", code
);
244 case F0
: // Break code
250 case 0x84: // Alt'd PrintScreen
251 matrix_break(PRINT_SCREEN
);
257 xprintf("unexpected scan code at F0: F0(clear and cont.)\n");
265 xprintf("unexpected scan code at F0: %02X\n", code
);
270 case E0_F0
: // Break code of E0-prefixed
272 case 0x12: // to be ignored
273 case 0x59: // to be ignored
278 matrix_break(code
|0x80);
282 xprintf("unexpected scan code at E0_F0: %02X\n", code
);
287 // following are states of Pause
318 state
= E1_14_77_E1_F0
;
327 state
= E1_14_77_E1_F0_14
;
333 case E1_14_77_E1_F0_14
:
336 state
= E1_14_77_E1_F0_14_F0
;
342 case E1_14_77_E1_F0_14_F0
:
352 // Following are states of Control'd Pause
375 // TODO: request RESEND when error occurs?
377 if (PS2_IS_FAILED(ps2_error)) {
378 uint8_t ret = ps2_host_send(PS2_RESEND);
379 xprintf("Resend: %02X\n", ret);
386 bool matrix_is_on(uint8_t row
, uint8_t col
)
388 return (matrix
[row
] & (1<<col
));
392 uint8_t matrix_get_row(uint8_t row
)
397 uint8_t matrix_key_count(void)
400 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
401 count
+= bitpop(matrix
[i
]);
408 static void matrix_make(uint8_t code
)
410 if (!matrix_is_on(ROW(code
), COL(code
))) {
411 matrix
[ROW(code
)] |= 1<<COL(code
);
417 static void matrix_break(uint8_t code
)
419 if (matrix_is_on(ROW(code
), COL(code
))) {
420 matrix
[ROW(code
)] &= ~(1<<COL(code
));
425 void matrix_clear(void)
427 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;