]>
git.gir.st - tmk_keyboard.git/blob - ps2_vusb/matrix.c
7 #include <util/delay.h>
13 #include "matrix_skel.h"
16 #if (MATRIX_COLS > 16)
17 # error "MATRIX_COLS must not exceed 16"
19 #if (MATRIX_ROWS > 255)
20 # error "MATRIX_ROWS must not exceed 255"
26 * "PS/2 Scan Codes Set 2" is assigned to 256(32x8)cells matrix.
27 * Hmm, It is very sparse and not efficient :(
32 * :| XX | 00-7F for normal codes(without E0-prefix)
35 * :| E0 XX | 80-FF for E0-prefix codes(use (XX|0x80) as code)
39 * 83: F8[0x83](normal codes but > 0x7F)
40 * FC: PrintScreen[E0 7C or 84]
44 #define PRINT_SCREEN (0xFC)
46 #define ROW(code) (code>>3)
47 #define COL(code) (code&0x07)
49 static bool is_modified
= false;
51 // matrix state buffer(1:on, 0:off)
52 #if (MATRIX_COLS <= 8)
53 static uint8_t matrix
[MATRIX_ROWS
];
55 static uint16_t matrix
[MATRIX_ROWS
];
58 #ifdef MATRIX_HAS_GHOST
59 static bool matrix_has_ghost_in_row(uint8_t row
);
61 static void matrix_make(uint8_t code
);
62 static void matrix_break(uint8_t code
);
66 uint8_t matrix_rows(void)
72 uint8_t matrix_cols(void)
77 void matrix_init(void)
81 // initialize matrix state: all keys off
82 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
88 * PS/2 Scan Code Set 2: Exceptional Handling
90 * There are several keys to be handled exceptionally.
91 * The scan code for these keys are varied or prefix/postfix'd
92 * depending on modifier key state.
95 * http://www.microsoft.com/whdc/archive/scancode.mspx
96 * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
99 * Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left:
101 * modifiers | make | break
102 * ----------+---------------------------+----------------------
103 * Ohter | <make> | <break>
104 * LShift | E0 F0 12 <make> | <break> E0 12
105 * RShift | E0 F0 59 <make> | <break> E0 59
106 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
109 * modifiers | make | break
110 * ----------+---------------------------+----------------------
111 * Other | E0 12 <make> | <break> E0 F0 12
112 * Shift'd | <make> | <break>
114 * Handling: ignore these prefix/postfix codes
118 * modifiers | make | break
119 * ----------+---------------------------+----------------------
120 * Ohter | <make> | <break>
121 * LShift | E0 F0 12 <make> | <break> E0 12
122 * RShift | E0 F0 59 <make> | <break> E0 59
123 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
125 * Handling: ignore these prefix/postfix codes
129 * With hoding down modifiers, the scan code is sent as following:
131 * modifiers | make | break
132 * ----------+--------------+-----------------------------------
133 * Other | E0 12 E0 7C | E0 F0 7C E0 F0 12
134 * Shift'd | E0 7C | E0 F0 7C
135 * Control'd | E0 7C | E0 F0 7C
138 * Handling: ignore prefix/postfix codes and treat both scan code
139 * E0 7C and 84 as PrintScreen.
142 * With hoding down modifiers, the scan code is sent as following:
144 * modifiers | make(no break code)
145 * ----------+--------------------------------------------------
146 * no mods | E1 14 77 E1 F0 14 F0 77
147 * Control'd | E0 7E E0 F0 7E
149 * Handling: treat these two code sequence as Pause
152 uint8_t matrix_scan(void)
160 // states for Pause/Break
167 E1_14_77_E1_F0_14_F0
,
173 // Pause/Break off(PS/2 has no break for this key)
174 if (matrix_is_on(ROW(PAUSE
), COL(PAUSE
))) {
179 while ((code
= ps2_host_recv())) {
183 case 0xE0: // 2byte make
186 case 0xF0: // break code
189 case 0xE1: // Pause/Break
196 case 0x84: // PrintScreen
197 matrix_make(PRINT_SCREEN
);
200 default: // normal key make
204 debug("unexpected scan code at INIT: "); debug_hex(code
); debug("\n");
211 case 0x12: // postfix/postfix code for exceptional keys
212 case 0x59: // postfix/postfix code for exceptional keys
216 case 0x7E: // former part of Control-Pause[E0 7E E0 F0 7E]
220 case 0xF0: // E0 break
225 matrix_make(code
|0x80);
227 debug("unexpected scan code at E0: "); debug_hex(code
); debug("\n");
239 matrix_break(PRINT_SCREEN
);
246 debug("unexpected scan code at F0: "); debug_hex(code
); debug("\n");
251 case E0_F0
: // E0 break
253 case 0x12: // postfix/postfix code for exceptional keys
254 case 0x59: // postfix/postfix code for exceptional keys
255 case 0x7E: // latter part of Control-Pause[E0 7E E0 F0 7E]
261 matrix_break(code
|0x80);
263 debug("unexpected scan code at E0_F0: "); debug_hex(code
); debug("\n");
299 state
= E1_14_77_E1_F0
;
308 state
= E1_14_77_E1_F0_14
;
314 case E1_14_77_E1_F0_14
:
317 state
= E1_14_77_E1_F0_14_F0
;
323 case E1_14_77_E1_F0_14_F0
:
340 bool matrix_is_modified(void)
346 bool matrix_has_ghost(void)
348 #ifdef MATRIX_HAS_GHOST
349 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
350 if (matrix_has_ghost_in_row(i
))
358 bool matrix_is_on(uint8_t row
, uint8_t col
)
360 return (matrix
[row
] & (1<<col
));
364 #if (MATRIX_COLS <= 8)
365 uint8_t matrix_get_row(uint8_t row
)
367 uint16_t matrix_get_row(uint8_t row
)
373 void matrix_print(void)
375 #if (MATRIX_COLS <= 8)
376 print("\nr/c 01234567\n");
378 print("\nr/c 0123456789ABCDEF\n");
380 for (uint8_t row
= 0; row
< matrix_rows(); row
++) {
381 phex(row
); print(": ");
382 #if (MATRIX_COLS <= 8)
383 pbin_reverse(matrix_get_row(row
));
385 pbin_reverse16(matrix_get_row(row
));
387 #ifdef MATRIX_HAS_GHOST
388 if (matrix_has_ghost_in_row(row
)) {
396 uint8_t matrix_key_count(void)
399 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
400 #if (MATRIX_COLS <= 8)
401 count
+= bitpop(matrix
[i
]);
403 count
+= bitpop16(matrix
[i
]);
409 #ifdef MATRIX_HAS_GHOST
411 static bool matrix_has_ghost_in_row(uint8_t row
)
413 // no ghost exists in case less than 2 keys on
414 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
417 // ghost exists in case same state as other row
418 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
419 if (i
!= row
&& (matrix
[i
] & matrix
[row
]) == matrix
[row
])
428 static void matrix_make(uint8_t code
)
430 if (!matrix_is_on(ROW(code
), COL(code
))) {
431 matrix
[ROW(code
)] |= 1<<COL(code
);
433 //print("matrix_make: "); phex(code); print("\n");
438 static void matrix_break(uint8_t code
)
440 if (matrix_is_on(ROW(code
), COL(code
))) {
441 matrix
[ROW(code
)] &= ~(1<<COL(code
));
443 //print("matrix_break: "); phex(code); print("\n");