]> git.gir.st - tmk_keyboard.git/blob - converter/adb_usb/matrix.c
onekey: Add desc of V-USB osc calribration
[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
34
35
36
37 static bool has_media_keys = false;
38 static bool is_iso_layout = false;
39 static report_mouse_t mouse_report = {};
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 void adb_mouse_task(void)
126 {
127 uint16_t codes;
128 int16_t x, y;
129 static int8_t mouseacc;
130 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
131 codes = adb_host_mouse_recv();
132 // If nothing received reset mouse acceleration, and quit.
133 if (!codes) {
134 mouseacc = 1;
135 return;
136 };
137 // Bit sixteen is button.
138 if (~codes & (1 << 15))
139 mouse_report.buttons |= MOUSE_BTN1;
140 if (codes & (1 << 15))
141 mouse_report.buttons &= ~MOUSE_BTN1;
142 // lower seven bits are movement, as signed int_7.
143 // low byte is X-axis, high byte is Y.
144 y = (codes>>8 & 0x3F);
145 x = (codes>>0 & 0x3F);
146 // bit seven and fifteen is negative
147 // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
148 if (codes & (1 << 6))
149 x = (x-0x40);
150 if (codes & (1 << 14))
151 y = (y-0x40);
152 // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
153 x *= mouseacc;
154 y *= mouseacc;
155 // Cap our two bytes per axis to one byte.
156 // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
157 // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
158 mouse_report.x = -MAX(-MAX(x, -127), -127);
159 mouse_report.y = -MAX(-MAX(y, -127), -127);
160 if (debug_mouse) {
161 print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
162 print("adb_mouse raw: [");
163 phex(mouseacc); print(" ");
164 phex(mouse_report.buttons); print("|");
165 print_decs(mouse_report.x); print(" ");
166 print_decs(mouse_report.y); print("]\n");
167 }
168 // Send result by usb.
169 host_mouse_send(&mouse_report);
170 // increase acceleration of mouse
171 mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
172 return;
173 }
174 #endif
175
176 uint8_t matrix_scan(void)
177 {
178 /* extra_key is volatile and more convoluted than necessary because gcc refused
179 to generate valid code otherwise. Making extra_key uint8_t and constructing codes
180 here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
181 extra_key from memory, and leave garbage in the high byte of codes. I tried
182 dozens of code variations and it kept generating broken assembly output. So
183 beware if attempting to make extra_key code more logical and efficient. */
184 static volatile uint16_t extra_key = 0xFFFF;
185 uint16_t codes;
186 uint8_t key0, key1;
187
188 codes = extra_key;
189 extra_key = 0xFFFF;
190
191 if ( codes == 0xFFFF )
192 {
193 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
194 codes = adb_host_kbd_recv(ADB_ADDR_KEYBOARD);
195
196 // Adjustable keybaord media keys
197 if (codes == 0 && has_media_keys &&
198 (codes = adb_host_kbd_recv(ADB_ADDR_APPLIANCE))) {
199 // key1
200 switch (codes & 0x7f ) {
201 case 0x00: // Mic
202 codes = (codes & ~0x007f) | 0x42;
203 break;
204 case 0x01: // Mute
205 codes = (codes & ~0x007f) | 0x4a;
206 break;
207 case 0x02: // Volume down
208 codes = (codes & ~0x007f) | 0x49;
209 break;
210 case 0x03: // Volume Up
211 codes = (codes & ~0x007f) | 0x48;
212 break;
213 case 0x7F: // no code
214 break;
215 default:
216 xprintf("ERROR: media key1\n");
217 return 0x11;
218 }
219 // key0
220 switch ((codes >> 8) & 0x7f ) {
221 case 0x00: // Mic
222 codes = (codes & ~0x7f00) | (0x42 << 8);
223 break;
224 case 0x01: // Mute
225 codes = (codes & ~0x7f00) | (0x4a << 8);
226 break;
227 case 0x02: // Volume down
228 codes = (codes & ~0x7f00) | (0x49 << 8);
229 break;
230 case 0x03: // Volume Up
231 codes = (codes & ~0x7f00) | (0x48 << 8);
232 break;
233 default:
234 xprintf("ERROR: media key0\n");
235 return 0x10;
236 }
237 }
238 }
239 key0 = codes>>8;
240 key1 = codes&0xFF;
241
242 if (debug_matrix && codes) {
243 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
244 }
245
246 if (codes == 0) { // no keys
247 return 0;
248 } else if (codes == 0x7F7F) { // power key press
249 register_key(0x7F);
250 } else if (codes == 0xFFFF) { // power key release
251 register_key(0xFF);
252 } else if (key0 == 0xFF) { // error
253 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
254 // something wrong or plug-in
255 matrix_init();
256 return key1;
257 } else {
258 /* Swap codes for ISO keyboard
259 * https://github.com/tmk/tmk_keyboard/issues/35
260 *
261 * ANSI
262 * ,----------- ----------.
263 * | *a| 1| 2 =|Backspa|
264 * |----------- ----------|
265 * |Tab | Q| | ]| *c|
266 * |----------- ----------|
267 * |CapsLo| A| '|Return |
268 * |----------- ----------|
269 * |Shift | Shift |
270 * `----------- ----------'
271 *
272 * ISO
273 * ,----------- ----------.
274 * | *a| 1| 2 =|Backspa|
275 * |----------- ----------|
276 * |Tab | Q| | ]|Retur|
277 * |----------- -----` |
278 * |CapsLo| A| '| *c| |
279 * |----------- ----------|
280 * |Shif| *b| Shift |
281 * `----------- ----------'
282 *
283 * ADB scan code USB usage
284 * ------------- ---------
285 * Key ANSI ISO ANSI ISO
286 * ---------------------------------------------
287 * *a 0x32 0x0A 0x35 0x35
288 * *b ---- 0x32 ---- 0x64
289 * *c 0x2A 0x2A 0x31 0x31(or 0x32)
290 */
291 if (is_iso_layout) {
292 if ((key0 & 0x7F) == 0x32) {
293 key0 = (key0 & 0x80) | 0x0A;
294 } else if ((key0 & 0x7F) == 0x0A) {
295 key0 = (key0 & 0x80) | 0x32;
296 }
297 }
298 register_key(key0);
299 if (key1 != 0xFF) // key1 is 0xFF when no second key.
300 extra_key = key1<<8 | 0xFF; // process in a separate call
301 }
302
303 return 1;
304 }
305
306 inline
307 matrix_row_t matrix_get_row(uint8_t row)
308 {
309 return matrix[row];
310 }
311
312 inline
313 static void register_key(uint8_t key)
314 {
315 uint8_t col, row;
316 col = key&0x07;
317 row = (key>>3)&0x0F;
318 if (key&0x80) {
319 matrix[row] &= ~(1<<col);
320 } else {
321 matrix[row] |= (1<<col);
322 }
323 }
Imprint / Impressum