]>
git.gir.st - tmk_keyboard.git/blob - ps2_usb/matrix.c
7 #include <util/delay.h>
15 #if (MATRIX_COLS > 16)
16 # error "MATRIX_COLS must not exceed 16"
18 #if (MATRIX_ROWS > 255)
19 # error "MATRIX_ROWS must not exceed 255"
25 * "PS/2 Scan Codes Set 2" is assigned to 256(32x8)cells matrix.
26 * Hmm, It is very sparse and not efficient :(
31 * :| XX | 00-7F for normal codes(without E0-prefix)
34 * :| E0 XX | 80-FF for E0-prefix codes(use (XX|0x80) as code)
38 * 83: F8[0x83](normal codes but > 0x7F)
39 * FC: PrintScreen[E0 7C or 84]
43 #define PRINT_SCREEN (0xFC)
45 #define ROW(code) (code>>3)
46 #define COL(code) (code&0x07)
48 static bool is_modified
= false;
50 // matrix state buffer(1:on, 0:off)
51 #if (MATRIX_COLS <= 8)
52 static uint8_t matrix
[MATRIX_ROWS
];
54 static uint16_t matrix
[MATRIX_ROWS
];
57 #ifdef MATRIX_HAS_GHOST
58 static bool matrix_has_ghost_in_row(uint8_t row
);
60 static void matrix_make(uint8_t code
);
61 static void matrix_break(uint8_t code
);
65 uint8_t matrix_rows(void)
71 uint8_t matrix_cols(void)
76 void matrix_init(void)
80 // initialize matrix state: all keys off
81 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
87 * PS/2 Scan Code Set 2: Exceptional Handling
89 * There are several keys to be handled exceptionally.
90 * The scan code for these keys are varied or prefix/postfix'd
91 * depending on modifier key state.
94 * http://www.microsoft.com/whdc/archive/scancode.mspx
95 * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
98 * Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left:
100 * modifiers | make | break
101 * ----------+---------------------------+----------------------
102 * Ohter | <make> | <break>
103 * LShift | E0 F0 12 <make> | <break> E0 12
104 * RShift | E0 F0 59 <make> | <break> E0 59
105 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
108 * modifiers | make | break
109 * ----------+---------------------------+----------------------
110 * Other | E0 12 <make> | <break> E0 F0 12
111 * Shift'd | <make> | <break>
113 * Handling: ignore these prefix/postfix codes
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
124 * Handling: ignore these prefix/postfix codes
128 * With hoding down modifiers, the scan code is sent as following:
130 * modifiers | make | break
131 * ----------+--------------+-----------------------------------
132 * Other | E0 12 E0 7C | E0 F0 7C E0 F0 12
133 * Shift'd | E0 7C | E0 F0 7C
134 * Control'd | E0 7C | E0 F0 7C
137 * Handling: ignore prefix/postfix codes and treat both scan code
138 * E0 7C and 84 as PrintScreen.
141 * With hoding down modifiers, the scan code is sent as following:
143 * modifiers | make(no break code)
144 * ----------+--------------------------------------------------
145 * no mods | E1 14 77 E1 F0 14 F0 77
146 * Control'd | E0 7E E0 F0 7E
148 * Handling: treat these two code sequence as Pause
151 uint8_t matrix_scan(void)
159 // states for Pause/Break
166 E1_14_77_E1_F0_14_F0
,
172 // Pause/Break off(PS/2 has no break for this key)
173 if (matrix_is_on(ROW(PAUSE
), COL(PAUSE
))) {
178 while ((code
= ps2_host_recv())) {
182 case 0xE0: // 2byte make
185 case 0xF0: // break code
188 case 0xE1: // Pause/Break
195 case 0x84: // PrintScreen
196 matrix_make(PRINT_SCREEN
);
199 default: // normal key make
203 debug("unexpected scan code at INIT: "); debug_hex(code
); debug("\n");
210 case 0x12: // postfix/postfix code for exceptional keys
211 case 0x59: // postfix/postfix code for exceptional keys
215 case 0x7E: // former part of Control-Pause[E0 7E E0 F0 7E]
219 case 0xF0: // E0 break
224 matrix_make(code
|0x80);
226 debug("unexpected scan code at E0: "); debug_hex(code
); debug("\n");
238 matrix_break(PRINT_SCREEN
);
245 debug("unexpected scan code at F0: "); debug_hex(code
); debug("\n");
250 case E0_F0
: // E0 break
252 case 0x12: // postfix/postfix code for exceptional keys
253 case 0x59: // postfix/postfix code for exceptional keys
254 case 0x7E: // latter part of Control-Pause[E0 7E E0 F0 7E]
260 matrix_break(code
|0x80);
262 debug("unexpected scan code at E0_F0: "); debug_hex(code
); debug("\n");
298 state
= E1_14_77_E1_F0
;
307 state
= E1_14_77_E1_F0_14
;
313 case E1_14_77_E1_F0_14
:
316 state
= E1_14_77_E1_F0_14_F0
;
322 case E1_14_77_E1_F0_14_F0
:
339 bool matrix_is_modified(void)
345 bool matrix_has_ghost(void)
347 #ifdef MATRIX_HAS_GHOST
348 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
349 if (matrix_has_ghost_in_row(i
))
357 bool matrix_is_on(uint8_t row
, uint8_t col
)
359 return (matrix
[row
] & (1<<col
));
363 #if (MATRIX_COLS <= 8)
364 uint8_t matrix_get_row(uint8_t row
)
366 uint16_t matrix_get_row(uint8_t row
)
372 void matrix_print(void)
374 #if (MATRIX_COLS <= 8)
375 print("\nr/c 01234567\n");
377 print("\nr/c 0123456789ABCDEF\n");
379 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
380 phex(row
); print(": ");
381 #if (MATRIX_COLS <= 8)
382 pbin_reverse(matrix_get_row(row
));
384 pbin_reverse16(matrix_get_row(row
));
386 #ifdef MATRIX_HAS_GHOST
387 if (matrix_has_ghost_in_row(row
)) {
395 uint8_t matrix_key_count(void)
398 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
399 #if (MATRIX_COLS <= 8)
400 count
+= bitpop(matrix
[i
]);
402 count
+= bitpop16(matrix
[i
]);
408 #ifdef MATRIX_HAS_GHOST
410 static bool matrix_has_ghost_in_row(uint8_t row
)
412 // no ghost exists in case less than 2 keys on
413 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
416 // ghost exists in case same state as other row
417 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
418 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
427 static void matrix_make(uint8_t code
)
429 if (!matrix_is_on(ROW(code
), COL(code
))) {
430 matrix
[ROW(code
)] |= 1<<COL(code
);
436 static void matrix_break(uint8_t code
)
438 if (matrix_is_on(ROW(code
), COL(code
))) {
439 matrix
[ROW(code
)] &= ~(1<<COL(code
));