7 #include <util/delay.h>
12 #include "usb_keyboard.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);
63 static void ps2_reset(void);
64 static void ps2_set_leds(uint8_t leds);
68 uint8_t matrix_rows(void)
74 uint8_t matrix_cols(void)
79 void matrix_init(void)
87 ps2_set_leds(1<<PS2_LED_NUM_LOCK);
89 ps2_set_leds(1<<PS2_LED_NUM_LOCK|1<<PS2_LED_CAPS_LOCK);
91 ps2_set_leds(1<<PS2_LED_NUM_LOCK|1<<PS2_LED_CAPS_LOCK|1<<PS2_LED_SCROLL_LOCK);
95 // initialize matrix state: all keys off
96 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
102 * PS/2 Scan Code Set 2: Exceptional Handling
104 * There are several keys to be handled exceptionally.
105 * The scan code for these keys are varied or prefix/postfix'd
106 * depending on modifier key state.
109 * http://www.microsoft.com/whdc/archive/scancode.mspx
110 * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
113 * Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left:
115 * modifiers | make | break
116 * ----------+---------------------------+----------------------
117 * Ohter | <make> | <break>
118 * LShift | E0 F0 12 <make> | <break> E0 12
119 * RShift | E0 F0 59 <make> | <break> E0 59
120 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
123 * modifiers | make | break
124 * ----------+---------------------------+----------------------
125 * Other | E0 12 <make> | <break> E0 F0 12
126 * Shift'd | <make> | <break>
128 * Handling: ignore these prefix/postfix codes
132 * modifiers | make | break
133 * ----------+---------------------------+----------------------
134 * Ohter | <make> | <break>
135 * LShift | E0 F0 12 <make> | <break> E0 12
136 * RShift | E0 F0 59 <make> | <break> E0 59
137 * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
139 * Handling: ignore these prefix/postfix codes
143 * With hoding down modifiers, the scan code is sent as following:
145 * modifiers | make | break
146 * ----------+--------------+-----------------------------------
147 * Other | E0 12 E0 7C | E0 F0 7C E0 F0 12
148 * Shift'd | E0 7C | E0 F0 7C
149 * Control'd | E0 7C | E0 F0 7C
152 * Handling: ignore prefix/postfix codes and treat both scan code
153 * E0 7C and 84 as PrintScreen.
156 * With hoding down modifiers, the scan code is sent as following:
158 * modifiers | make(no break code)
159 * ----------+--------------------------------------------------
160 * no mods | E1 14 77 E1 F0 14 F0 77
161 * Control'd | E0 7E E0 F0 7E
163 * Handling: treat these two code sequence as Pause
166 uint8_t matrix_scan(void)
174 // states for Pause/Break
181 E1_14_77_E1_F0_14_F0,
187 // Pause/Break off(PS/2 has no break for this key)
188 if (matrix_is_on(ROW(PAUSE), COL(PAUSE))) {
193 while ((code = ps2_host_recv())) {
194 debug_hex(code); debug(" ");
198 case 0xE0: // 2byte make
201 case 0xF0: // break code
204 case 0xE1: // Pause/Break
211 case 0x84: // PrintScreen
212 matrix_make(PRINT_SCREEN);
215 default: // normal key make
219 debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n");
226 case 0x12: // postfix/postfix code for exceptional keys
227 case 0x59: // postfix/postfix code for exceptional keys
231 case 0x7E: // former part of Control-Pause[E0 7E E0 F0 7E]
235 case 0xF0: // E0 break
240 matrix_make(code|0x80);
242 debug("unexpected scan code at E0: "); debug_hex(code); debug("\n");
254 matrix_break(PRINT_SCREEN);
261 debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
266 case E0_F0: // E0 break
268 case 0x12: // postfix/postfix code for exceptional keys
269 case 0x59: // postfix/postfix code for exceptional keys
270 case 0x7E: // latter part of Control-Pause[E0 7E E0 F0 7E]
276 matrix_break(code|0x80);
278 debug("unexpected scan code at E0_F0: "); debug_hex(code); debug("\n");
314 state = E1_14_77_E1_F0;
323 state = E1_14_77_E1_F0_14;
329 case E1_14_77_E1_F0_14:
332 state = E1_14_77_E1_F0_14_F0;
338 case E1_14_77_E1_F0_14_F0:
353 // handle LED indicators
354 static uint8_t prev_leds = 0;
355 if (prev_leds != usb_keyboard_leds) {
357 if (usb_keyboard_leds&(1<<USB_LED_SCROLL_LOCK))
358 leds |= (1<<PS2_LED_SCROLL_LOCK);
359 if (usb_keyboard_leds&(1<<USB_LED_NUM_LOCK))
360 leds |= (1<<PS2_LED_NUM_LOCK);
361 if (usb_keyboard_leds&(1<<USB_LED_CAPS_LOCK))
362 leds |= (1<<PS2_LED_CAPS_LOCK);
365 prev_leds = usb_keyboard_leds;
371 bool matrix_is_modified(void)
377 bool matrix_has_ghost(void)
379 #ifdef MATRIX_HAS_GHOST
380 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
381 if (matrix_has_ghost_in_row(i))
389 bool matrix_is_on(uint8_t row, uint8_t col)
391 return (matrix[row] & (1<<col));
395 #if (MATRIX_COLS <= 8)
396 uint8_t matrix_get_row(uint8_t row)
398 uint16_t matrix_get_row(uint8_t row)
404 void matrix_print(void)
406 #if (MATRIX_COLS <= 8)
407 print("\nr/c 01234567\n");
409 print("\nr/c 0123456789ABCDEF\n");
411 for (uint8_t row = 0; row < matrix_rows(); row++) {
412 phex(row); print(": ");
413 #if (MATRIX_COLS <= 8)
414 pbin_reverse(matrix_get_row(row));
416 pbin_reverse16(matrix_get_row(row));
418 #ifdef MATRIX_HAS_GHOST
419 if (matrix_has_ghost_in_row(row)) {
427 uint8_t matrix_key_count(void)
430 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
431 #if (MATRIX_COLS <= 8)
432 count += bitpop(matrix[i]);
434 count += bitpop16(matrix[i]);
440 #ifdef MATRIX_HAS_GHOST
442 static bool matrix_has_ghost_in_row(uint8_t row)
444 // no ghost exists in case less than 2 keys on
445 if (((matrix[row] - 1) & matrix[row]) == 0)
448 // ghost exists in case same state as other row
449 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
450 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
459 static void matrix_make(uint8_t code)
461 if (!matrix_is_on(ROW(code), COL(code))) {
462 matrix[ROW(code)] |= 1<<COL(code);
468 static void matrix_break(uint8_t code)
470 if (matrix_is_on(ROW(code), COL(code))) {
471 matrix[ROW(code)] &= ~(1<<COL(code));
476 static void ps2_reset(void)
479 ps2_host_recv(); // 0xFA
480 ps2_host_recv(); // 0xAA
484 static void ps2_set_leds(uint8_t leds)
487 ps2_host_recv(); // 0xFA
489 ps2_host_recv(); // 0xFA