]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - converter/adb_usb/matrix.c
adb_usb: Fix #518 Lag on typing
[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
80     // Adjustable keyboard media keys: address=0x07 and handlerID=0x02
81     has_media_keys = (0x02 == (adb_host_talk(ADB_ADDR_APPLIANCE, ADB_REG_3) & 0xff));
82     if (has_media_keys) {
83         xprintf("Found: media keys\n");
84     }
85
86     // Enable keyboard left/right modifier distinction
87     // Listen Register3
88     //  upper byte: reserved bits 0000, keyboard address 0010
89     //  lower byte: device handler 00000011
90     adb_host_listen(ADB_ADDR_KEYBOARD, ADB_REG_3, ADB_ADDR_KEYBOARD, ADB_HANDLER_EXTENDED_PROTOCOL);
91
92     // device scan
93     xprintf("After init:\n");
94     for (uint8_t addr = 1; addr < 16; addr++) {
95         uint16_t reg3 = adb_host_talk(addr, ADB_REG_3);
96         if (reg3) {
97             xprintf("Scan: addr:%d, reg3:%04X\n", addr, reg3);
98         }
99         _delay_ms(20);
100     }
101
102     // initialize matrix state: all keys off
103     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
104
105     led_set(host_keyboard_leds());
106
107     debug_enable = true;
108     //debug_matrix = true;
109     //debug_keyboard = true;
110     //debug_mouse = true;
111     print("debug enabled.\n");
112
113     // LED off
114     DDRD |= (1<<6); PORTD &= ~(1<<6);
115     return;
116 }
117
118 #ifdef ADB_MOUSE_ENABLE
119
120 #ifdef MAX
121 #undef MAX
122 #endif
123 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
124
125 static report_mouse_t mouse_report = {};
126
127 void adb_mouse_task(void)
128 {
129     uint16_t codes;
130     int16_t x, y;
131     static int8_t mouseacc;
132
133     /* tick of last polling */
134     static uint16_t tick_ms;
135
136     // polling with 12ms interval
137     if (timer_elapsed(tick_ms) < 12) return;
138     tick_ms = timer_read();
139
140     codes = adb_host_mouse_recv();
141     // If nothing received reset mouse acceleration, and quit.
142     if (!codes) {
143         mouseacc = 1;
144         return;
145     };
146     // Bit sixteen is button.
147     if (~codes & (1 << 15))
148         mouse_report.buttons |= MOUSE_BTN1;
149     if (codes & (1 << 15))
150         mouse_report.buttons &= ~MOUSE_BTN1;
151     // lower seven bits are movement, as signed int_7.
152     // low byte is X-axis, high byte is Y.
153     y = (codes>>8 & 0x3F);
154     x = (codes>>0 & 0x3F);
155     // bit seven and fifteen is negative
156     // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
157     if (codes & (1 << 6))
158           x = (x-0x40);
159     if (codes & (1 << 14))
160          y = (y-0x40);
161     // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
162     x *= mouseacc;
163     y *= mouseacc;
164     // Cap our two bytes per axis to one byte.
165     // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
166          // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
167     mouse_report.x = -MAX(-MAX(x, -127), -127);
168     mouse_report.y = -MAX(-MAX(y, -127), -127);
169     if (debug_mouse) {
170             print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
171             print("adb_mouse raw: [");
172             phex(mouseacc); print(" ");
173             phex(mouse_report.buttons); print("|");
174             print_decs(mouse_report.x); print(" ");
175             print_decs(mouse_report.y); print("]\n");
176     }
177     // Send result by usb.
178     host_mouse_send(&mouse_report);
179     // increase acceleration of mouse
180     mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
181     return;
182 }
183 #endif
184
185 uint8_t matrix_scan(void)
186 {
187     /* extra_key is volatile and more convoluted than necessary because gcc refused
188     to generate valid code otherwise. Making extra_key uint8_t and constructing codes
189     here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
190     extra_key from memory, and leave garbage in the high byte of codes. I tried
191     dozens of code variations and it kept generating broken assembly output. So
192     beware if attempting to make extra_key code more logical and efficient. */
193     static volatile uint16_t extra_key = 0xFFFF;
194     uint16_t codes;
195     uint8_t key0, key1;
196
197     /* tick of last polling */
198     static uint16_t tick_ms;
199
200     codes = extra_key;
201     extra_key = 0xFFFF;
202
203     if ( codes == 0xFFFF )
204     {
205         // polling with 12ms interval
206         if (timer_elapsed(tick_ms) < 12) return 0;
207         tick_ms = timer_read();
208
209         codes = adb_host_kbd_recv(ADB_ADDR_KEYBOARD);
210
211         // Adjustable keybaord media keys
212         if (codes == 0 && has_media_keys &&
213                 (codes = adb_host_kbd_recv(ADB_ADDR_APPLIANCE))) {
214             // key1
215             switch (codes & 0x7f ) {
216             case 0x00:  // Mic
217                 codes = (codes & ~0x007f) | 0x42;
218                 break;
219             case 0x01:  // Mute
220                 codes = (codes & ~0x007f) | 0x4a;
221                 break;
222             case 0x02:  // Volume down
223                 codes = (codes & ~0x007f) | 0x49;
224                 break;
225             case 0x03:  // Volume Up
226                 codes = (codes & ~0x007f) | 0x48;
227                 break;
228             case 0x7F:  // no code
229                 break;
230             default:
231                 xprintf("ERROR: media key1\n");
232                 return 0x11;
233             }
234             // key0
235             switch ((codes >> 8) & 0x7f ) {
236             case 0x00:  // Mic
237                 codes = (codes & ~0x7f00) | (0x42 << 8);
238                 break;
239             case 0x01:  // Mute
240                 codes = (codes & ~0x7f00) | (0x4a << 8);
241                 break;
242             case 0x02:  // Volume down
243                 codes = (codes & ~0x7f00) | (0x49 << 8);
244                 break;
245             case 0x03:  // Volume Up
246                 codes = (codes & ~0x7f00) | (0x48 << 8);
247                 break;
248             default:
249                 xprintf("ERROR: media key0\n");
250                 return 0x10;
251             }
252         }
253     }
254     key0 = codes>>8;
255     key1 = codes&0xFF;
256
257     if (debug_matrix && codes) {
258         print("adb_host_kbd_recv: "); phex16(codes); print("\n");
259     }
260
261     if (codes == 0) {                           // no keys
262         return 0;
263     } else if (codes == 0x7F7F) {   // power key press
264         register_key(0x7F);
265     } else if (codes == 0xFFFF) {   // power key release
266         register_key(0xFF);
267     } else if (key0 == 0xFF) {      // error
268         xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
269         // something wrong or plug-in
270         matrix_init();
271         return key1;
272     } else {
273         /* Swap codes for ISO keyboard
274          * https://github.com/tmk/tmk_keyboard/issues/35
275          *
276          * ANSI
277          * ,-----------    ----------.
278          * | *a|  1|  2     =|Backspa|
279          * |-----------    ----------|
280          * |Tab  |  Q|     |  ]|   *c|
281          * |-----------    ----------|
282          * |CapsLo|  A|    '|Return  |
283          * |-----------    ----------|
284          * |Shift   |      Shift     |
285          * `-----------    ----------'
286          *
287          * ISO
288          * ,-----------    ----------.
289          * | *a|  1|  2     =|Backspa|
290          * |-----------    ----------|
291          * |Tab  |  Q|     |  ]|Retur|
292          * |-----------    -----`    |
293          * |CapsLo|  A|    '| *c|    |
294          * |-----------    ----------|
295          * |Shif| *b|      Shift     |
296          * `-----------    ----------'
297          *
298          *         ADB scan code   USB usage
299          *         -------------   ---------
300          * Key     ANSI    ISO     ANSI    ISO
301          * ---------------------------------------------
302          * *a      0x32    0x0A    0x35    0x35
303          * *b      ----    0x32    ----    0x64
304          * *c      0x2A    0x2A    0x31    0x31(or 0x32)
305          */
306         if (is_iso_layout) {
307             if ((key0 & 0x7F) == 0x32) {
308                 key0 = (key0 & 0x80) | 0x0A;
309             } else if ((key0 & 0x7F) == 0x0A) {
310                 key0 = (key0 & 0x80) | 0x32;
311             }
312         }
313         register_key(key0);
314         if (key1 != 0xFF)       // key1 is 0xFF when no second key.
315             extra_key = key1<<8 | 0xFF; // process in a separate call
316     }
317
318     return 1;
319 }
320
321 inline
322 matrix_row_t matrix_get_row(uint8_t row)
323 {
324     return matrix[row];
325 }
326
327 inline
328 static void register_key(uint8_t key)
329 {
330     uint8_t col, row;
331     col = key&0x07;
332     row = (key>>3)&0x0F;
333     if (key&0x80) {
334         matrix[row] &= ~(1<<col);
335     } else {
336         matrix[row] |=  (1<<col);
337     }
338 }