]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/adb_usb/matrix.c
xt_usb: Fix scan code print for debug
[max/tmk_keyboard.git] / converter / adb_usb / matrix.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <util/delay.h>
25 #include "print.h"
26 #include "util.h"
27 #include "debug.h"
28 #include "adb.h"
29 #include "matrix.h"
30 #include "report.h"
31 #include "host.h"
32 #include "led.h"
33 #include "timer.h"
34
35
36
37
38 static bool has_media_keys = false;
39 static bool is_iso_layout = false;
40
41 // matrix state buffer(1:on, 0:off)
42 static matrix_row_t matrix[MATRIX_ROWS];
43
44 static void register_key(uint8_t key);
45
46
47 void matrix_init(void)
48 {
49     // LED on
50     DDRD |= (1<<6); PORTD |= (1<<6);
51
52     adb_host_init();
53     // wait for keyboard to boot up and receive command
54     _delay_ms(2000);
55
56     // device scan
57     xprintf("Before init:\n");
58     for (uint8_t addr = 1; addr < 16; addr++) {
59         uint16_t reg3 = adb_host_talk(addr, ADB_REG_3);
60         if (reg3) {
61             xprintf("Scan: addr:%d, reg3:%04X\n", addr, reg3);
62         }
63         _delay_ms(20);
64     }
65
66     // Determine ISO keyboard by handler id
67     // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815
68     uint8_t handler_id = (uint8_t) adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_3);
69     switch (handler_id) {
70     case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
71     case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
72     case 0xC4: case 0xC7:
73         is_iso_layout = true;
74         break;
75     default:
76         is_iso_layout = false;
77         break;
78     }
79     xprintf("hadler_id: %02X, is_iso_layout: %s\n", handler_id, (is_iso_layout ? "yes" : "no"));
80
81     // Adjustable keyboard media keys: address=0x07 and handlerID=0x02
82     has_media_keys = (0x02 == (adb_host_talk(ADB_ADDR_APPLIANCE, ADB_REG_3) & 0xff));
83     if (has_media_keys) {
84         xprintf("Found: media keys\n");
85     }
86
87     // Enable keyboard left/right modifier distinction
88     // Listen Register3
89     //  upper byte: reserved bits 0000, keyboard address 0010
90     //  lower byte: device handler 00000011
91     adb_host_listen(ADB_ADDR_KEYBOARD, ADB_REG_3, ADB_ADDR_KEYBOARD, ADB_HANDLER_EXTENDED_PROTOCOL);
92
93     // device scan
94     xprintf("After init:\n");
95     for (uint8_t addr = 1; addr < 16; addr++) {
96         uint16_t reg3 = adb_host_talk(addr, ADB_REG_3);
97         if (reg3) {
98             xprintf("Scan: addr:%d, reg3:%04X\n", addr, reg3);
99         }
100         _delay_ms(20);
101     }
102
103     // initialize matrix state: all keys off
104     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
105
106     led_set(host_keyboard_leds());
107
108     debug_enable = true;
109     //debug_matrix = true;
110     //debug_keyboard = true;
111     //debug_mouse = true;
112     print("debug enabled.\n");
113
114     // LED off
115     DDRD |= (1<<6); PORTD &= ~(1<<6);
116     return;
117 }
118
119 #ifdef ADB_MOUSE_ENABLE
120
121 #ifdef MAX
122 #undef MAX
123 #endif
124 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
125
126 static report_mouse_t mouse_report = {};
127
128 void adb_mouse_task(void)
129 {
130     uint16_t codes;
131     int16_t x, y;
132     static int8_t mouseacc;
133
134     /* tick of last polling */
135     static uint16_t tick_ms;
136
137     // polling with 12ms interval
138     if (timer_elapsed(tick_ms) < 12) return;
139     tick_ms = timer_read();
140
141     codes = adb_host_mouse_recv();
142     // If nothing received reset mouse acceleration, and quit.
143     if (!codes) {
144         mouseacc = 1;
145         return;
146     };
147     // Bit sixteen is button.
148     if (~codes & (1 << 15))
149         mouse_report.buttons |= MOUSE_BTN1;
150     if (codes & (1 << 15))
151         mouse_report.buttons &= ~MOUSE_BTN1;
152     // lower seven bits are movement, as signed int_7.
153     // low byte is X-axis, high byte is Y.
154     y = (codes>>8 & 0x3F);
155     x = (codes>>0 & 0x3F);
156     // bit seven and fifteen is negative
157     // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
158     if (codes & (1 << 6))
159           x = (x-0x40);
160     if (codes & (1 << 14))
161          y = (y-0x40);
162     // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
163     x *= mouseacc;
164     y *= mouseacc;
165     // Cap our two bytes per axis to one byte.
166     // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
167          // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
168     mouse_report.x = -MAX(-MAX(x, -127), -127);
169     mouse_report.y = -MAX(-MAX(y, -127), -127);
170     if (debug_mouse) {
171             print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
172             print("adb_mouse raw: [");
173             phex(mouseacc); print(" ");
174             phex(mouse_report.buttons); print("|");
175             print_decs(mouse_report.x); print(" ");
176             print_decs(mouse_report.y); print("]\n");
177     }
178     // Send result by usb.
179     host_mouse_send(&mouse_report);
180     // increase acceleration of mouse
181     mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
182     return;
183 }
184 #endif
185
186 uint8_t matrix_scan(void)
187 {
188     /* extra_key is volatile and more convoluted than necessary because gcc refused
189     to generate valid code otherwise. Making extra_key uint8_t and constructing codes
190     here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
191     extra_key from memory, and leave garbage in the high byte of codes. I tried
192     dozens of code variations and it kept generating broken assembly output. So
193     beware if attempting to make extra_key code more logical and efficient. */
194     static volatile uint16_t extra_key = 0xFFFF;
195     uint16_t codes;
196     uint8_t key0, key1;
197
198     /* tick of last polling */
199     static uint16_t tick_ms;
200
201     codes = extra_key;
202     extra_key = 0xFFFF;
203
204     if ( codes == 0xFFFF )
205     {
206         // polling with 12ms interval
207         if (timer_elapsed(tick_ms) < 12) return 0;
208         tick_ms = timer_read();
209
210         codes = adb_host_kbd_recv(ADB_ADDR_KEYBOARD);
211
212         // Adjustable keybaord media keys
213         if (codes == 0 && has_media_keys &&
214                 (codes = adb_host_kbd_recv(ADB_ADDR_APPLIANCE))) {
215             // key1
216             switch (codes & 0x7f ) {
217             case 0x00:  // Mic
218                 codes = (codes & ~0x007f) | 0x42;
219                 break;
220             case 0x01:  // Mute
221                 codes = (codes & ~0x007f) | 0x4a;
222                 break;
223             case 0x02:  // Volume down
224                 codes = (codes & ~0x007f) | 0x49;
225                 break;
226             case 0x03:  // Volume Up
227                 codes = (codes & ~0x007f) | 0x48;
228                 break;
229             case 0x7F:  // no code
230                 break;
231             default:
232                 xprintf("ERROR: media key1\n");
233                 return 0x11;
234             }
235             // key0
236             switch ((codes >> 8) & 0x7f ) {
237             case 0x00:  // Mic
238                 codes = (codes & ~0x7f00) | (0x42 << 8);
239                 break;
240             case 0x01:  // Mute
241                 codes = (codes & ~0x7f00) | (0x4a << 8);
242                 break;
243             case 0x02:  // Volume down
244                 codes = (codes & ~0x7f00) | (0x49 << 8);
245                 break;
246             case 0x03:  // Volume Up
247                 codes = (codes & ~0x7f00) | (0x48 << 8);
248                 break;
249             default:
250                 xprintf("ERROR: media key0\n");
251                 return 0x10;
252             }
253         }
254     }
255     key0 = codes>>8;
256     key1 = codes&0xFF;
257
258     if (debug_matrix && codes) {
259         print("adb_host_kbd_recv: "); phex16(codes); print("\n");
260     }
261
262     if (codes == 0) {                           // no keys
263         return 0;
264     } else if (codes == 0x7F7F) {   // power key press
265         register_key(0x7F);
266     } else if (codes == 0xFFFF) {   // power key release
267         register_key(0xFF);
268     } else if (key0 == 0xFF) {      // error
269         xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
270         // something wrong or plug-in
271         matrix_init();
272         return key1;
273     } else {
274         /* Swap codes for ISO keyboard
275          * https://github.com/tmk/tmk_keyboard/issues/35
276          *
277          * ANSI
278          * ,-----------    ----------.
279          * | *a|  1|  2     =|Backspa|
280          * |-----------    ----------|
281          * |Tab  |  Q|     |  ]|   *c|
282          * |-----------    ----------|
283          * |CapsLo|  A|    '|Return  |
284          * |-----------    ----------|
285          * |Shift   |      Shift     |
286          * `-----------    ----------'
287          *
288          * ISO
289          * ,-----------    ----------.
290          * | *a|  1|  2     =|Backspa|
291          * |-----------    ----------|
292          * |Tab  |  Q|     |  ]|Retur|
293          * |-----------    -----`    |
294          * |CapsLo|  A|    '| *c|    |
295          * |-----------    ----------|
296          * |Shif| *b|      Shift     |
297          * `-----------    ----------'
298          *
299          *         ADB scan code   USB usage
300          *         -------------   ---------
301          * Key     ANSI    ISO     ANSI    ISO
302          * ---------------------------------------------
303          * *a      0x32    0x0A    0x35    0x35
304          * *b      ----    0x32    ----    0x64
305          * *c      0x2A    0x2A    0x31    0x31(or 0x32)
306          */
307         if (is_iso_layout) {
308             if ((key0 & 0x7F) == 0x32) {
309                 key0 = (key0 & 0x80) | 0x0A;
310             } else if ((key0 & 0x7F) == 0x0A) {
311                 key0 = (key0 & 0x80) | 0x32;
312             }
313         }
314         register_key(key0);
315         if (key1 != 0xFF)       // key1 is 0xFF when no second key.
316             extra_key = key1<<8 | 0xFF; // process in a separate call
317     }
318
319     return 1;
320 }
321
322 inline
323 matrix_row_t matrix_get_row(uint8_t row)
324 {
325     return matrix[row];
326 }
327
328 inline
329 static void register_key(uint8_t key)
330 {
331     uint8_t col, row;
332     col = key&0x07;
333     row = (key>>3)&0x0F;
334     if (key&0x80) {
335         matrix[row] &= ~(1<<col);
336     } else {
337         matrix[row] |=  (1<<col);
338     }
339 }