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)
86 ps2_set_leds(1<<PS2_LED_NUM_LOCK);
88 ps2_set_leds(1<<PS2_LED_NUM_LOCK|1<<PS2_LED_CAPS_LOCK);
90 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 code = ps2_host_recv();
194 if (code == 0x00) return 0;
195 //while ((code = ps2_host_recv())) {
196 //phex(code); print(" ");
200 case 0xE0: // 2byte make
203 case 0xF0: // break code
206 case 0xE1: // Pause/Break
213 case 0x84: // PrintScreen
214 matrix_make(PRINT_SCREEN);
217 default: // normal key make
221 debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n");
228 case 0x12: // postfix/postfix code for exceptional keys
229 case 0x59: // postfix/postfix code for exceptional keys
233 case 0x7E: // former part of Control-Pause[E0 7E E0 F0 7E]
237 case 0xF0: // E0 break
242 matrix_make(code|0x80);
244 debug("unexpected scan code at E0: "); debug_hex(code); debug("\n");
256 matrix_break(PRINT_SCREEN);
263 debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
268 case E0_F0: // E0 break
270 case 0x12: // postfix/postfix code for exceptional keys
271 case 0x59: // postfix/postfix code for exceptional keys
272 case 0x7E: // latter part of Control-Pause[E0 7E E0 F0 7E]
278 matrix_break(code|0x80);
280 debug("unexpected scan code at E0_F0: "); debug_hex(code); debug("\n");
316 state = E1_14_77_E1_F0;
325 state = E1_14_77_E1_F0_14;
331 case E1_14_77_E1_F0_14:
334 state = E1_14_77_E1_F0_14_F0;
340 case E1_14_77_E1_F0_14_F0:
356 // handle LED indicators
358 static uint8_t prev_leds = 0;
359 if (prev_leds != usb_keyboard_leds) {
361 if (usb_keyboard_leds&(1<<USB_LED_SCROLL_LOCK))
362 leds |= (1<<PS2_LED_SCROLL_LOCK);
363 if (usb_keyboard_leds&(1<<USB_LED_NUM_LOCK))
364 leds |= (1<<PS2_LED_NUM_LOCK);
365 if (usb_keyboard_leds&(1<<USB_LED_CAPS_LOCK))
366 leds |= (1<<PS2_LED_CAPS_LOCK);
369 prev_leds = usb_keyboard_leds;
376 bool matrix_is_modified(void)
382 bool matrix_has_ghost(void)
384 #ifdef MATRIX_HAS_GHOST
385 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
386 if (matrix_has_ghost_in_row(i))
394 bool matrix_is_on(uint8_t row, uint8_t col)
396 return (matrix[row] & (1<<col));
400 #if (MATRIX_COLS <= 8)
401 uint8_t matrix_get_row(uint8_t row)
403 uint16_t matrix_get_row(uint8_t row)
409 void matrix_print(void)
411 #if (MATRIX_COLS <= 8)
412 print("\nr/c 01234567\n");
414 print("\nr/c 0123456789ABCDEF\n");
416 for (uint8_t row = 0; row < matrix_rows(); row++) {
417 phex(row); print(": ");
418 #if (MATRIX_COLS <= 8)
419 pbin_reverse(matrix_get_row(row));
421 pbin_reverse16(matrix_get_row(row));
423 #ifdef MATRIX_HAS_GHOST
424 if (matrix_has_ghost_in_row(row)) {
432 uint8_t matrix_key_count(void)
435 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
436 #if (MATRIX_COLS <= 8)
437 count += bitpop(matrix[i]);
439 count += bitpop16(matrix[i]);
445 #ifdef MATRIX_HAS_GHOST
447 static bool matrix_has_ghost_in_row(uint8_t row)
449 // no ghost exists in case less than 2 keys on
450 if (((matrix[row] - 1) & matrix[row]) == 0)
453 // ghost exists in case same state as other row
454 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
455 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
464 static void matrix_make(uint8_t code)
466 if (!matrix_is_on(ROW(code), COL(code))) {
467 matrix[ROW(code)] |= 1<<COL(code);
469 //print("matrix_make: "); phex(code); print("\n");
474 static void matrix_break(uint8_t code)
476 if (matrix_is_on(ROW(code), COL(code))) {
477 matrix[ROW(code)] &= ~(1<<COL(code));
479 //print("matrix_break: "); phex(code); print("\n");
483 static void ps2_reset(void)
486 if (ps2_host_recv() != 0xFA) return;
488 if (ps2_host_recv() != 0xAA) return;
491 static void ps2_set_leds(uint8_t leds)
494 if (ps2_host_recv() != 0xFA) return; // 0xFA
496 if (ps2_host_recv() != 0xFA) return; // 0xFA