2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
24 #include <util/delay.h>
38 static bool has_media_keys = false;
39 static bool is_iso_layout = false;
41 // matrix state buffer(1:on, 0:off)
42 static matrix_row_t matrix[MATRIX_ROWS];
44 static void register_key(uint8_t key);
47 void matrix_init(void)
50 DDRD |= (1<<6); PORTD |= (1<<6);
53 // wait for keyboard to boot up and receive command
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);
61 xprintf("Scan: addr:%d, reg3:%04X\n", addr, reg3);
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);
70 case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
71 case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
76 is_iso_layout = false;
79 xprintf("hadler_id: %02X, is_iso_layout: %s\n", handler_id, (is_iso_layout ? "yes" : "no"));
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));
84 xprintf("Found: media keys\n");
87 // Enable keyboard left/right modifier distinction
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);
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);
98 xprintf("Scan: addr:%d, reg3:%04X\n", addr, reg3);
103 // initialize matrix state: all keys off
104 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
106 led_set(host_keyboard_leds());
109 //debug_matrix = true;
110 //debug_keyboard = true;
111 //debug_mouse = true;
112 print("debug enabled.\n");
115 DDRD |= (1<<6); PORTD &= ~(1<<6);
119 #ifdef ADB_MOUSE_ENABLE
124 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
126 static report_mouse_t mouse_report = {};
128 void adb_mouse_task(void)
132 static int8_t mouseacc;
134 /* tick of last polling */
135 static uint16_t tick_ms;
137 // polling with 12ms interval
138 if (timer_elapsed(tick_ms) < 12) return;
139 tick_ms = timer_read();
141 codes = adb_host_mouse_recv();
142 // If nothing received reset mouse acceleration, and quit.
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))
160 if (codes & (1 << 14))
162 // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
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);
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");
178 // Send result by usb.
179 host_mouse_send(&mouse_report);
180 // increase acceleration of mouse
181 mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
186 uint8_t matrix_scan(void)
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;
198 /* tick of last polling */
199 static uint16_t tick_ms;
204 if ( codes == 0xFFFF )
206 // polling with 12ms interval
207 if (timer_elapsed(tick_ms) < 12) return 0;
208 tick_ms = timer_read();
210 codes = adb_host_kbd_recv(ADB_ADDR_KEYBOARD);
212 // Adjustable keybaord media keys
213 if (codes == 0 && has_media_keys &&
214 (codes = adb_host_kbd_recv(ADB_ADDR_APPLIANCE))) {
216 switch (codes & 0x7f ) {
218 codes = (codes & ~0x007f) | 0x42;
221 codes = (codes & ~0x007f) | 0x4a;
223 case 0x02: // Volume down
224 codes = (codes & ~0x007f) | 0x49;
226 case 0x03: // Volume Up
227 codes = (codes & ~0x007f) | 0x48;
229 case 0x7F: // no code
232 xprintf("ERROR: media key1\n");
236 switch ((codes >> 8) & 0x7f ) {
238 codes = (codes & ~0x7f00) | (0x42 << 8);
241 codes = (codes & ~0x7f00) | (0x4a << 8);
243 case 0x02: // Volume down
244 codes = (codes & ~0x7f00) | (0x49 << 8);
246 case 0x03: // Volume Up
247 codes = (codes & ~0x7f00) | (0x48 << 8);
250 xprintf("ERROR: media key0\n");
258 if (debug_matrix && codes) {
259 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
262 if (codes == 0) { // no keys
264 } else if (codes == 0x7F7F) { // power key press
266 } else if (codes == 0xFFFF) { // power key release
268 } else if (key0 == 0xFF) { // error
269 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
270 // something wrong or plug-in
274 /* Swap codes for ISO keyboard
275 * https://github.com/tmk/tmk_keyboard/issues/35
278 * ,----------- ----------.
279 * | *a| 1| 2 =|Backspa|
280 * |----------- ----------|
282 * |----------- ----------|
283 * |CapsLo| A| '|Return |
284 * |----------- ----------|
286 * `----------- ----------'
289 * ,----------- ----------.
290 * | *a| 1| 2 =|Backspa|
291 * |----------- ----------|
292 * |Tab | Q| | ]|Retur|
293 * |----------- -----` |
294 * |CapsLo| A| '| *c| |
295 * |----------- ----------|
297 * `----------- ----------'
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)
308 if ((key0 & 0x7F) == 0x32) {
309 key0 = (key0 & 0x80) | 0x0A;
310 } else if ((key0 & 0x7F) == 0x0A) {
311 key0 = (key0 & 0x80) | 0x32;
315 if (key1 != 0xFF) // key1 is 0xFF when no second key.
316 extra_key = key1<<8 | 0xFF; // process in a separate call
323 matrix_row_t matrix_get_row(uint8_t row)
329 static void register_key(uint8_t key)
335 matrix[row] &= ~(1<<col);
337 matrix[row] |= (1<<col);