]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - ps2_vusb/matrix.c
added initial support of mousekeys to ps2_vusb
[max/tmk_keyboard.git] / ps2_vusb / 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     ps2_host_init();
82     _delay_ms(1000);
83
84     // flush LEDs
85 /*
86     ps2_set_leds(1<<PS2_LED_NUM_LOCK);
87     _delay_ms(100);
88     ps2_set_leds(1<<PS2_LED_NUM_LOCK|1<<PS2_LED_CAPS_LOCK);
89     _delay_ms(100);
90     ps2_set_leds(1<<PS2_LED_NUM_LOCK|1<<PS2_LED_CAPS_LOCK|1<<PS2_LED_SCROLL_LOCK);
91     _delay_ms(300);
92     ps2_set_leds(0x00);
93 */
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     code = ps2_host_recv();
194     if (code == 0x00) return 0;
195     //while ((code = ps2_host_recv())) {
196 //phex(code); print(" ");
197         switch (state) {
198             case INIT:
199                 switch (code) {
200                     case 0xE0:  // 2byte make
201                         state = E0;
202                         break;
203                     case 0xF0:  // break code
204                         state = F0;
205                         break;
206                     case 0xE1:  // Pause/Break
207                         state = E1;
208                         break;
209                     case 0x83:  // F8
210                         matrix_make(F8);
211                         state = INIT;
212                         break;
213                     case 0x84:  // PrintScreen
214                         matrix_make(PRINT_SCREEN);
215                         state = INIT;
216                         break;
217                     default:    // normal key make
218                         if (code < 0x80) {
219                             matrix_make(code);
220                         } else {
221                             debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n");
222                         }
223                         state = INIT;
224                 }
225                 break;
226             case E0:
227                 switch (code) {
228                     case 0x12:  // postfix/postfix code for exceptional keys
229                     case 0x59:  // postfix/postfix code for exceptional keys
230                         // ignore
231                         state = INIT;
232                         break;
233                     case 0x7E:  // former part of Control-Pause[E0 7E  E0 F0 7E]
234                         matrix_make(PAUSE);
235                         state = INIT;
236                         break;
237                     case 0xF0:  // E0 break
238                         state = E0_F0;
239                         break;
240                     default:    // E0 make
241                         if (code < 0x80) {
242                             matrix_make(code|0x80);
243                         } else {
244                             debug("unexpected scan code at E0: "); debug_hex(code); debug("\n");
245                         }
246                         state = INIT;
247                 }
248                 break;
249             case F0:
250                 switch (code) {
251                     case 0x83:
252                         matrix_break(F8);
253                         state = INIT;
254                         break;
255                     case 0x84:
256                         matrix_break(PRINT_SCREEN);
257                         state = INIT;
258                         break;
259                     default:
260                     if (code < 0x80) {
261                         matrix_break(code);
262                     } else {
263                         debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
264                     }
265                     state = INIT;
266                 }
267                 break;
268             case E0_F0: // E0 break
269                 switch (code) {
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]
273                         // ignore
274                         state = INIT;
275                         break;
276                     default:
277                         if (code < 0x80) {
278                             matrix_break(code|0x80);
279                         } else {
280                             debug("unexpected scan code at E0_F0: "); debug_hex(code); debug("\n");
281                         }
282                         state = INIT;
283                 }
284                 break;
285             /* Pause */
286             case E1:
287                 switch (code) {
288                     case 0x14:
289                         state = E1_14;
290                         break;
291                     default:
292                         state = INIT;
293                 }
294                 break;
295             case E1_14:
296                 switch (code) {
297                     case 0x77:
298                         state = E1_14_77;
299                         break;
300                     default:
301                         state = INIT;
302                 }
303                 break;
304             case E1_14_77:
305                 switch (code) {
306                     case 0xE1:
307                         state = E1_14_77_E1;
308                         break;
309                     default:
310                         state = INIT;
311                 }
312                 break;
313             case E1_14_77_E1:
314                 switch (code) {
315                     case 0xF0:
316                         state = E1_14_77_E1_F0;
317                         break;
318                     default:
319                         state = INIT;
320                 }
321                 break;
322             case E1_14_77_E1_F0:
323                 switch (code) {
324                     case 0x14:
325                         state = E1_14_77_E1_F0_14;
326                         break;
327                     default:
328                         state = INIT;
329                 }
330                 break;
331             case E1_14_77_E1_F0_14:
332                 switch (code) {
333                     case 0xF0:
334                         state = E1_14_77_E1_F0_14_F0;
335                         break;
336                     default:
337                         state = INIT;
338                 }
339                 break;
340             case E1_14_77_E1_F0_14_F0:
341                 switch (code) {
342                     case 0x77:
343                         matrix_make(PAUSE);
344                         state = INIT;
345                         break;
346                     default:
347                         state = INIT;
348                 }
349                 break;
350             default:
351                 state = INIT;
352         }
353     //}
354 //print("|");
355
356     // handle LED indicators
357 /*
358     static uint8_t prev_leds = 0;
359     if (prev_leds != usb_keyboard_leds) {
360         uint8_t leds = 0;
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);
367
368         ps2_set_leds(leds);
369         prev_leds = usb_keyboard_leds;
370     }
371 */
372
373     return 1;
374 }
375
376 bool matrix_is_modified(void)
377 {
378     return is_modified;
379 }
380
381 inline
382 bool matrix_has_ghost(void)
383 {
384 #ifdef MATRIX_HAS_GHOST
385     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
386         if (matrix_has_ghost_in_row(i))
387             return true;
388     }
389 #endif
390     return false;
391 }
392
393 inline
394 bool matrix_is_on(uint8_t row, uint8_t col)
395 {
396     return (matrix[row] & (1<<col));
397 }
398
399 inline
400 #if (MATRIX_COLS <= 8)
401 uint8_t matrix_get_row(uint8_t row)
402 #else
403 uint16_t matrix_get_row(uint8_t row)
404 #endif
405 {
406     return matrix[row];
407 }
408
409 void matrix_print(void)
410 {
411 #if (MATRIX_COLS <= 8)
412     print("\nr/c 01234567\n");
413 #else
414     print("\nr/c 0123456789ABCDEF\n");
415 #endif
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));
420 #else
421         pbin_reverse16(matrix_get_row(row));
422 #endif
423 #ifdef MATRIX_HAS_GHOST
424         if (matrix_has_ghost_in_row(row)) {
425             print(" <ghost");
426         }
427 #endif
428         print("\n");
429     }
430 }
431
432 uint8_t matrix_key_count(void)
433 {
434     uint8_t count = 0;
435     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
436 #if (MATRIX_COLS <= 8)
437         count += bitpop(matrix[i]);
438 #else
439         count += bitpop16(matrix[i]);
440 #endif
441     }
442     return count;
443 }
444
445 #ifdef MATRIX_HAS_GHOST
446 inline
447 static bool matrix_has_ghost_in_row(uint8_t row)
448 {
449     // no ghost exists in case less than 2 keys on
450     if (((matrix[row] - 1) & matrix[row]) == 0)
451         return false;
452
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])
456             return true;
457     }
458     return false;
459 }
460 #endif
461
462
463 inline
464 static void matrix_make(uint8_t code)
465 {
466     if (!matrix_is_on(ROW(code), COL(code))) {
467         matrix[ROW(code)] |= 1<<COL(code);
468         is_modified = true;
469         //print("matrix_make: "); phex(code); print("\n");
470     }
471 }
472
473 inline
474 static void matrix_break(uint8_t code)
475 {
476     if (matrix_is_on(ROW(code), COL(code))) {
477         matrix[ROW(code)] &= ~(1<<COL(code));
478         is_modified = true;
479         //print("matrix_break: "); phex(code); print("\n");
480     }
481 }
482
483 static void ps2_reset(void)
484 {
485     ps2_host_send(0xFF);
486     if (ps2_host_recv() != 0xFA) return;
487     _delay_ms(1000);
488     if (ps2_host_recv() != 0xAA) return;
489 }
490
491 static void ps2_set_leds(uint8_t leds)
492 {
493         ps2_host_send(0xED);
494         if (ps2_host_recv() != 0xFA) return;        // 0xFA
495         ps2_host_send(leds);
496         if (ps2_host_recv() != 0xFA) return;        // 0xFA
497 }