]>
git.gir.st - tmk_keyboard.git/blob - 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/>.
24 #include <util/delay.h>
32 #if (MATRIX_COLS > 16)
33 # error "MATRIX_COLS must not exceed 16"
35 #if (MATRIX_ROWS > 255)
36 # error "MATRIX_ROWS must not exceed 255"
42 * "PS/2 Scan Codes Set 2" is assigned to 256(32x8)cells matrix.
43 * Hmm, It is very sparse and not efficient :(
48 * :| XX | 00-7F for normal codes(without E0-prefix)
51 * :| E0 XX | 80-FF for E0-prefix codes(use (XX|0x80) as code)
55 * 83: F8[0x83](normal codes but > 0x7F)
56 * FC: PrintScreen[E0 7C or 84]
60 #define PRINT_SCREEN (0xFC)
62 #define ROW(code) (code>>3)
63 #define COL(code) (code&0x07)
65 static bool is_modified
= false;
67 // matrix state buffer(1:on, 0:off)
68 #if (MATRIX_COLS <= 8)
69 static uint8_t matrix
[MATRIX_ROWS
];
71 static uint16_t matrix
[MATRIX_ROWS
];
74 #ifdef MATRIX_HAS_GHOST
75 static bool matrix_has_ghost_in_row(uint8_t row
);
77 static void matrix_make(uint8_t code
);
78 static void matrix_break(uint8_t code
);
82 uint8_t matrix_rows(void)
88 uint8_t matrix_cols(void)
93 void matrix_init(void)
97 // initialize matrix state: all keys off
98 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
104 * PS/2 Scan Code Set 2: Exceptional Handling
106 * There are several keys to be handled exceptionally.
107 * The scan code for these keys are varied or prefix/postfix'd
108 * depending on modifier key state.
111 * http://www.microsoft.com/whdc/archive/scancode.mspx
112 * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
115 * Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left:
117 * modifiers | make | break
118 * ----------+---------------------------+----------------------
119 * Ohter | <make> | <break>
120 * LShift | E0 F0 12 <make> | <break> E0 12
121 * RShift | E0 F0 59 <make> | <break> E0 59
122 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
125 * modifiers | make | break
126 * ----------+---------------------------+----------------------
127 * Other | E0 12 <make> | <break> E0 F0 12
128 * Shift'd | <make> | <break>
130 * Handling: ignore these prefix/postfix codes
134 * modifiers | make | break
135 * ----------+---------------------------+----------------------
136 * Ohter | <make> | <break>
137 * LShift | E0 F0 12 <make> | <break> E0 12
138 * RShift | E0 F0 59 <make> | <break> E0 59
139 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
141 * Handling: ignore these prefix/postfix codes
145 * With hoding down modifiers, the scan code is sent as following:
147 * modifiers | make | break
148 * ----------+--------------+-----------------------------------
149 * Other | E0 12 E0 7C | E0 F0 7C E0 F0 12
150 * Shift'd | E0 7C | E0 F0 7C
151 * Control'd | E0 7C | E0 F0 7C
154 * Handling: ignore prefix/postfix codes and treat both scan code
155 * E0 7C and 84 as PrintScreen.
158 * With hoding down modifiers, the scan code is sent as following:
160 * modifiers | make(no break code)
161 * ----------+--------------------------------------------------
162 * no mods | E1 14 77 E1 F0 14 F0 77
163 * Control'd | E0 7E E0 F0 7E
165 * Handling: treat these two code sequence as Pause
168 uint8_t matrix_scan(void)
176 // states for Pause/Break
183 E1_14_77_E1_F0_14_F0
,
189 // Pause/Break off(PS/2 has no break for this key)
190 if (matrix_is_on(ROW(PAUSE
), COL(PAUSE
))) {
195 while ((code
= ps2_host_recv())) {
199 case 0xE0: // 2byte make
202 case 0xF0: // break code
205 case 0xE1: // Pause/Break
212 case 0x84: // PrintScreen
213 matrix_make(PRINT_SCREEN
);
216 default: // normal key make
220 debug("unexpected scan code at INIT: "); debug_hex(code
); debug("\n");
227 case 0x12: // postfix/postfix code for exceptional keys
228 case 0x59: // postfix/postfix code for exceptional keys
232 case 0x7E: // former part of Control-Pause[E0 7E E0 F0 7E]
236 case 0xF0: // E0 break
241 matrix_make(code
|0x80);
243 debug("unexpected scan code at E0: "); debug_hex(code
); debug("\n");
255 matrix_break(PRINT_SCREEN
);
262 debug("unexpected scan code at F0: "); debug_hex(code
); debug("\n");
267 case E0_F0
: // E0 break
269 case 0x12: // postfix/postfix code for exceptional keys
270 case 0x59: // postfix/postfix code for exceptional keys
271 case 0x7E: // latter part of Control-Pause[E0 7E E0 F0 7E]
277 matrix_break(code
|0x80);
279 debug("unexpected scan code at E0_F0: "); debug_hex(code
); debug("\n");
315 state
= E1_14_77_E1_F0
;
324 state
= E1_14_77_E1_F0_14
;
330 case E1_14_77_E1_F0_14
:
333 state
= E1_14_77_E1_F0_14_F0
;
339 case E1_14_77_E1_F0_14_F0
:
356 bool matrix_is_modified(void)
362 bool matrix_has_ghost(void)
364 #ifdef MATRIX_HAS_GHOST
365 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
366 if (matrix_has_ghost_in_row(i
))
374 bool matrix_is_on(uint8_t row
, uint8_t col
)
376 return (matrix
[row
] & (1<<col
));
380 #if (MATRIX_COLS <= 8)
381 uint8_t matrix_get_row(uint8_t row
)
383 uint16_t matrix_get_row(uint8_t row
)
389 void matrix_print(void)
391 #if (MATRIX_COLS <= 8)
392 print("\nr/c 01234567\n");
394 print("\nr/c 0123456789ABCDEF\n");
396 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
397 phex(row
); print(": ");
398 #if (MATRIX_COLS <= 8)
399 pbin_reverse(matrix_get_row(row
));
401 pbin_reverse16(matrix_get_row(row
));
403 #ifdef MATRIX_HAS_GHOST
404 if (matrix_has_ghost_in_row(row
)) {
412 uint8_t matrix_key_count(void)
415 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
416 #if (MATRIX_COLS <= 8)
417 count
+= bitpop(matrix
[i
]);
419 count
+= bitpop16(matrix
[i
]);
425 #ifdef MATRIX_HAS_GHOST
427 static bool matrix_has_ghost_in_row(uint8_t row
)
429 // no ghost exists in case less than 2 keys on
430 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
433 // ghost exists in case same state as other row
434 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
435 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
444 static void matrix_make(uint8_t code
)
446 if (!matrix_is_on(ROW(code
), COL(code
))) {
447 matrix
[ROW(code
)] |= 1<<COL(code
);
453 static void matrix_break(uint8_t code
)
455 if (matrix_is_on(ROW(code
), COL(code
))) {
456 matrix
[ROW(code
)] &= ~(1<<COL(code
));