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