]>
Commit | Line | Data |
---|---|---|
0dde25e8 | 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 | ||
56e098d7 | 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" | |
c958b2d1 | 29 | #include "matrix.h" |
7780fd1c MA |
30 | #include "report.h" |
31 | #include "host.h" | |
af667a05 | 32 | #include "led.h" |
56e098d7 | 33 | |
34 | ||
56e098d7 | 35 | |
36 | ||
b653b622 | 37 | static bool has_media_keys = false; |
49f44a2a | 38 | static bool is_iso_layout = false; |
7780fd1c | 39 | static report_mouse_t mouse_report = {}; |
56e098d7 | 40 | |
41 | // matrix state buffer(1:on, 0:off) | |
1ad31539 | 42 | static matrix_row_t matrix[MATRIX_ROWS]; |
56e098d7 | 43 | |
d8ce19ab | 44 | static void register_key(uint8_t key); |
56e098d7 | 45 | |
46 | ||
56e098d7 | 47 | void matrix_init(void) |
48 | { | |
b653b622 | 49 | // LED on |
50 | DDRD |= (1<<6); PORTD |= (1<<6); | |
51 | ||
56e098d7 | 52 | adb_host_init(); |
c18c52f5 | 53 | // wait for keyboard to boot up and receive command |
b653b622 | 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 | } | |
49f44a2a | 65 | |
4d9e66ba | 66 | // Determine ISO keyboard by handler id |
49f44a2a | 67 | // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815 |
cccd4186 | 68 | uint8_t handler_id = (uint8_t) adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_3); |
4d9e66ba | 69 | switch (handler_id) { |
49f44a2a | 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 | ||
b653b622 | 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 | ||
c18c52f5 | 86 | // Enable keyboard left/right modifier distinction |
b653b622 | 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 | } | |
56e098d7 | 101 | |
102 | // initialize matrix state: all keys off | |
d8ce19ab | 103 | for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00; |
56e098d7 | 104 | |
af667a05 | 105 | led_set(host_keyboard_leds()); |
106 | ||
56e098d7 | 107 | debug_enable = true; |
c531a185 | 108 | //debug_matrix = true; |
109 | //debug_keyboard = true; | |
110 | //debug_mouse = true; | |
56e098d7 | 111 | print("debug enabled.\n"); |
c219570e | 112 | |
b653b622 | 113 | // LED off |
c219570e | 114 | DDRD |= (1<<6); PORTD &= ~(1<<6); |
56e098d7 | 115 | return; |
116 | } | |
117 | ||
7780fd1c MA |
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; | |
49f44a2a | 129 | static int8_t mouseacc; |
7780fd1c MA |
130 | _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller |
131 | codes = adb_host_mouse_recv(); | |
49f44a2a | 132 | // If nothing received reset mouse acceleration, and quit. |
7780fd1c MA |
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; | |
49f44a2a | 142 | // lower seven bits are movement, as signed int_7. |
143 | // low byte is X-axis, high byte is Y. | |
7780fd1c MA |
144 | y = (codes>>8 & 0x3F); |
145 | x = (codes>>0 & 0x3F); | |
146 | // bit seven and fifteen is negative | |
49f44a2a | 147 | // usb does not use int_8, but int_7 (measuring distance) with sign-bit. |
7780fd1c MA |
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; | |
49f44a2a | 155 | // Cap our two bytes per axis to one byte. |
7780fd1c MA |
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 | } | |
49f44a2a | 168 | // Send result by usb. |
7780fd1c MA |
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 | ||
56e098d7 | 176 | uint8_t matrix_scan(void) |
177 | { | |
263c4626 SG |
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; | |
56e098d7 | 185 | uint16_t codes; |
186 | uint8_t key0, key1; | |
187 | ||
263c4626 SG |
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 | |
b653b622 | 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 | } | |
263c4626 | 238 | } |
56e098d7 | 239 | key0 = codes>>8; |
240 | key1 = codes&0xFF; | |
56e098d7 | 241 | |
d8ce19ab | 242 | if (debug_matrix && codes) { |
76033dcd | 243 | print("adb_host_kbd_recv: "); phex16(codes); print("\n"); |
244 | } | |
245 | ||
56e098d7 | 246 | if (codes == 0) { // no keys |
247 | return 0; | |
d8ce19ab | 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 | |
c531a185 | 253 | xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes); |
4fc0efd3 | 254 | // something wrong or plug-in |
255 | matrix_init(); | |
d8ce19ab | 256 | return key1; |
56e098d7 | 257 | } else { |
49f44a2a | 258 | /* Swap codes for ISO keyboard |
4d9e66ba | 259 | * https://github.com/tmk/tmk_keyboard/issues/35 |
49f44a2a | 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) { | |
4d9e66ba | 292 | if ((key0 & 0x7F) == 0x32) { |
293 | key0 = (key0 & 0x80) | 0x0A; | |
294 | } else if ((key0 & 0x7F) == 0x0A) { | |
295 | key0 = (key0 & 0x80) | 0x32; | |
49f44a2a | 296 | } |
297 | } | |
d8ce19ab | 298 | register_key(key0); |
56e098d7 | 299 | if (key1 != 0xFF) // key1 is 0xFF when no second key. |
263c4626 | 300 | extra_key = key1<<8 | 0xFF; // process in a separate call |
56e098d7 | 301 | } |
302 | ||
303 | return 1; | |
304 | } | |
305 | ||
56e098d7 | 306 | inline |
1ad31539 | 307 | matrix_row_t matrix_get_row(uint8_t row) |
56e098d7 | 308 | { |
309 | return matrix[row]; | |
310 | } | |
311 | ||
56e098d7 | 312 | inline |
d8ce19ab | 313 | static void register_key(uint8_t key) |
56e098d7 | 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 | } |