]> git.donarmstrong.com Git - tmk_firmware.git/blob - ps2_usb/matrix.c
fixed: not to send key if fn_keycode is modifier
[tmk_firmware.git] / ps2_usb / matrix.c
1 /*
2  * scan matrix
3  */
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <avr/io.h>
7 #include <util/delay.h>
8 #include "print.h"
9 #include "util.h"
10 #include "debug.h"
11 #include "ps2.h"
12 #include "usb_keyboard.h"
13 #include "matrix_skel.h"
14
15
16 #if (MATRIX_COLS > 16)
17 #   error "MATRIX_COLS must not exceed 16"
18 #endif
19 #if (MATRIX_ROWS > 255)
20 #   error "MATRIX_ROWS must not exceed 255"
21 #endif
22
23
24 /*
25  * Matrix usage:
26  * "PS/2 Scan Codes Set 2" is assigned to 256(32x8)cells matrix.
27  * Hmm, It is very sparse and not efficient :(
28  *
29  *      8bit
30  *    ---------
31  *  0|         |
32  *  :|   XX    | 00-7F for normal codes(without E0-prefix)
33  *  f|_________|
34  * 10|         |
35  *  :|  E0 XX  | 80-FF for E0-prefix codes(use (XX|0x80) as code)
36  * 1f|         |
37  *    ---------
38  * exceptions:
39  * 83:    F8[0x83](normal codes but > 0x7F)
40  * FC:    PrintScreen[E0 7C or 84]
41  * FE:    Puause
42  */
43 #define F8             (0x83)
44 #define PRINT_SCREEN   (0xFC)
45 #define PAUSE          (0xFE)
46 #define ROW(code)      (code>>3)
47 #define COL(code)      (code&0x07)
48
49 static bool is_modified = false;
50
51 // matrix state buffer(1:on, 0:off)
52 #if (MATRIX_COLS <= 8)
53 static uint8_t matrix[MATRIX_ROWS];
54 #else
55 static uint16_t matrix[MATRIX_ROWS];
56 #endif
57
58 #ifdef MATRIX_HAS_GHOST
59 static bool matrix_has_ghost_in_row(uint8_t row);
60 #endif
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);
65
66
67 inline
68 uint8_t matrix_rows(void)
69 {
70     return MATRIX_ROWS;
71 }
72
73 inline
74 uint8_t matrix_cols(void)
75 {
76     return MATRIX_COLS;
77 }
78
79 void matrix_init(void)
80 {
81     print_enable = true;
82     ps2_host_init();
83
84     ps2_reset();
85
86     // flush LEDs
87     ps2_set_leds(1<<PS2_LED_NUM_LOCK);
88     _delay_ms(100);
89     ps2_set_leds(1<<PS2_LED_NUM_LOCK|1<<PS2_LED_CAPS_LOCK);
90     _delay_ms(100);
91     ps2_set_leds(1<<PS2_LED_NUM_LOCK|1<<PS2_LED_CAPS_LOCK|1<<PS2_LED_SCROLL_LOCK);
92     _delay_ms(300);
93     ps2_set_leds(0x00);
94     
95     // initialize matrix state: all keys off
96     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
97
98     return;
99 }
100
101 /*
102  * PS/2 Scan Code Set 2: Exceptional Handling
103  *
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.
107  *
108  * References:
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
111  *
112  *
113  * Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left:
114  *     Num Lock: off
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
121  *
122  *     Num Lock: on
123  *     modifiers | make                      | break
124  *     ----------+---------------------------+----------------------
125  *     Other     | E0 12              <make> | <break>  E0 F0 12
126  *     Shift'd   |                    <make> | <break>
127  *
128  *     Handling: ignore these prefix/postfix codes
129  *
130  *
131  * Keypad-/:
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
138  *
139  *     Handling: ignore these prefix/postfix codes
140  *
141  *
142  * PrintScreen:
143  *     With hoding down modifiers, the scan code is sent as following:
144  *
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
150  *     Alt'd     |           84 | F0 84
151  *
152  *     Handling: ignore prefix/postfix codes and treat both scan code
153  *               E0 7C and 84 as PrintScreen.
154  *
155  * Pause:
156  *     With hoding down modifiers, the scan code is sent as following:
157  *
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
162  *
163  *     Handling: treat these two code sequence as Pause
164  *
165  */
166 uint8_t matrix_scan(void)
167 {
168
169     static enum {
170         INIT,
171         F0,
172         E0,
173         E0_F0,
174         // states for Pause/Break
175         E1,
176         E1_14,
177         E1_14_77,
178         E1_14_77_E1,
179         E1_14_77_E1_F0,
180         E1_14_77_E1_F0_14,
181         E1_14_77_E1_F0_14_F0,
182     } state = INIT;
183
184
185     is_modified = false;
186
187     // Pause/Break off(PS/2 has no break for this key)
188     if (matrix_is_on(ROW(PAUSE), COL(PAUSE))) {
189         matrix_break(PAUSE);
190     }
191
192     uint8_t code;
193     while ((code = ps2_host_recv())) {
194 debug_hex(code); debug(" ");
195         switch (state) {
196             case INIT:
197                 switch (code) {
198                     case 0xE0:  // 2byte make
199                         state = E0;
200                         break;
201                     case 0xF0:  // break code
202                         state = F0;
203                         break;
204                     case 0xE1:  // Pause/Break
205                         state = E1;
206                         break;
207                     case 0x83:  // F8
208                         matrix_make(F8);
209                         state = INIT;
210                         break;
211                     case 0x84:  // PrintScreen
212                         matrix_make(PRINT_SCREEN);
213                         state = INIT;
214                         break;
215                     default:    // normal key make
216                         if (code < 0x80) {
217                             matrix_make(code);
218                         } else {
219                             debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n");
220                         }
221                         state = INIT;
222                 }
223                 break;
224             case E0:
225                 switch (code) {
226                     case 0x12:  // postfix/postfix code for exceptional keys
227                     case 0x59:  // postfix/postfix code for exceptional keys
228                         // ignore
229                         state = INIT;
230                         break;
231                     case 0x7E:  // former part of Control-Pause[E0 7E  E0 F0 7E]
232                         matrix_make(PAUSE);
233                         state = INIT;
234                         break;
235                     case 0xF0:  // E0 break
236                         state = E0_F0;
237                         break;
238                     default:    // E0 make
239                         if (code < 0x80) {
240                             matrix_make(code|0x80);
241                         } else {
242                             debug("unexpected scan code at E0: "); debug_hex(code); debug("\n");
243                         }
244                         state = INIT;
245                 }
246                 break;
247             case F0:
248                 switch (code) {
249                     case 0x83:
250                         matrix_break(F8);
251                         state = INIT;
252                         break;
253                     case 0x84:
254                         matrix_break(PRINT_SCREEN);
255                         state = INIT;
256                         break;
257                     default:
258                     if (code < 0x80) {
259                         matrix_break(code);
260                     } else {
261                         debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
262                     }
263                     state = INIT;
264                 }
265                 break;
266             case E0_F0: // E0 break
267                 switch (code) {
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]
271                         // ignore
272                         state = INIT;
273                         break;
274                     default:
275                         if (code < 0x80) {
276                             matrix_break(code|0x80);
277                         } else {
278                             debug("unexpected scan code at E0_F0: "); debug_hex(code); debug("\n");
279                         }
280                         state = INIT;
281                 }
282                 break;
283             /* Pause */
284             case E1:
285                 switch (code) {
286                     case 0x14:
287                         state = E1_14;
288                         break;
289                     default:
290                         state = INIT;
291                 }
292                 break;
293             case E1_14:
294                 switch (code) {
295                     case 0x77:
296                         state = E1_14_77;
297                         break;
298                     default:
299                         state = INIT;
300                 }
301                 break;
302             case E1_14_77:
303                 switch (code) {
304                     case 0xE1:
305                         state = E1_14_77_E1;
306                         break;
307                     default:
308                         state = INIT;
309                 }
310                 break;
311             case E1_14_77_E1:
312                 switch (code) {
313                     case 0xF0:
314                         state = E1_14_77_E1_F0;
315                         break;
316                     default:
317                         state = INIT;
318                 }
319                 break;
320             case E1_14_77_E1_F0:
321                 switch (code) {
322                     case 0x14:
323                         state = E1_14_77_E1_F0_14;
324                         break;
325                     default:
326                         state = INIT;
327                 }
328                 break;
329             case E1_14_77_E1_F0_14:
330                 switch (code) {
331                     case 0xF0:
332                         state = E1_14_77_E1_F0_14_F0;
333                         break;
334                     default:
335                         state = INIT;
336                 }
337                 break;
338             case E1_14_77_E1_F0_14_F0:
339                 switch (code) {
340                     case 0x77:
341                         matrix_make(PAUSE);
342                         state = INIT;
343                         break;
344                     default:
345                         state = INIT;
346                 }
347                 break;
348             default:
349                 state = INIT;
350         }
351     }
352
353     // handle LED indicators
354     static uint8_t prev_leds = 0;
355     if (prev_leds != usb_keyboard_leds) {
356         uint8_t leds = 0;
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);
363
364         ps2_set_leds(leds);
365         prev_leds = usb_keyboard_leds;
366     }
367
368     return 1;
369 }
370
371 bool matrix_is_modified(void)
372 {
373     return is_modified;
374 }
375
376 inline
377 bool matrix_has_ghost(void)
378 {
379 #ifdef MATRIX_HAS_GHOST
380     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
381         if (matrix_has_ghost_in_row(i))
382             return true;
383     }
384 #endif
385     return false;
386 }
387
388 inline
389 bool matrix_is_on(uint8_t row, uint8_t col)
390 {
391     return (matrix[row] & (1<<col));
392 }
393
394 inline
395 #if (MATRIX_COLS <= 8)
396 uint8_t matrix_get_row(uint8_t row)
397 #else
398 uint16_t matrix_get_row(uint8_t row)
399 #endif
400 {
401     return matrix[row];
402 }
403
404 void matrix_print(void)
405 {
406 #if (MATRIX_COLS <= 8)
407     print("\nr/c 01234567\n");
408 #else
409     print("\nr/c 0123456789ABCDEF\n");
410 #endif
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));
415 #else
416         pbin_reverse16(matrix_get_row(row));
417 #endif
418 #ifdef MATRIX_HAS_GHOST
419         if (matrix_has_ghost_in_row(row)) {
420             print(" <ghost");
421         }
422 #endif
423         print("\n");
424     }
425 }
426
427 uint8_t matrix_key_count(void)
428 {
429     uint8_t count = 0;
430     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
431 #if (MATRIX_COLS <= 8)
432         count += bitpop(matrix[i]);
433 #else
434         count += bitpop16(matrix[i]);
435 #endif
436     }
437     return count;
438 }
439
440 #ifdef MATRIX_HAS_GHOST
441 inline
442 static bool matrix_has_ghost_in_row(uint8_t row)
443 {
444     // no ghost exists in case less than 2 keys on
445     if (((matrix[row] - 1) & matrix[row]) == 0)
446         return false;
447
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])
451             return true;
452     }
453     return false;
454 }
455 #endif
456
457
458 inline
459 static void matrix_make(uint8_t code)
460 {
461     if (!matrix_is_on(ROW(code), COL(code))) {
462         matrix[ROW(code)] |= 1<<COL(code);
463         is_modified = true;
464     }
465 }
466
467 inline
468 static void matrix_break(uint8_t code)
469 {
470     if (matrix_is_on(ROW(code), COL(code))) {
471         matrix[ROW(code)] &= ~(1<<COL(code));
472         is_modified = true;
473     }
474 }
475
476 static void ps2_reset(void)
477 {
478     ps2_host_send(0xFF);
479     ps2_host_recv(); // 0xFA
480     ps2_host_recv(); // 0xAA
481     _delay_ms(1000);
482 }
483
484 static void ps2_set_leds(uint8_t leds)
485 {
486         ps2_host_send(0xED);
487         ps2_host_recv();        // 0xFA
488         ps2_host_send(leds);
489         ps2_host_recv();        // 0xFA
490 }