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>
36 static bool has_media_keys
= false;
37 static bool is_iso_layout
= false;
38 static report_mouse_t mouse_report
= {};
40 // matrix state buffer(1:on, 0:off)
41 static matrix_row_t matrix
[MATRIX_ROWS
];
43 static void register_key(uint8_t key
);
46 void matrix_init(void)
49 DDRD
|= (1<<6); PORTD
|= (1<<6);
52 // wait for keyboard to boot up and receive command
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
);
60 xprintf("Scan: addr:%d, reg3:%04X\n", addr
, reg3
);
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
);
69 case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
70 case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
75 is_iso_layout
= false;
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));
82 xprintf("Found: media keys\n");
85 // Enable keyboard left/right modifier distinction
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
);
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
);
96 xprintf("Scan: addr:%d, reg3:%04X\n", addr
, reg3
);
101 // initialize matrix state: all keys off
102 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
105 //debug_matrix = true;
106 //debug_keyboard = true;
107 //debug_mouse = true;
108 print("debug enabled.\n");
111 DDRD
|= (1<<6); PORTD
&= ~(1<<6);
115 #ifdef ADB_MOUSE_ENABLE
120 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
122 void adb_mouse_task(void)
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.
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))
147 if (codes
& (1 << 14))
149 // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
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);
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");
165 // Send result by usb.
166 host_mouse_send(&mouse_report
);
167 // increase acceleration of mouse
168 mouseacc
+= ( mouseacc
< ADB_MOUSE_MAXACC
? 1 : 0 );
173 uint8_t matrix_scan(void)
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;
188 if ( codes
== 0xFFFF )
190 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
191 codes
= adb_host_kbd_recv(ADB_ADDR_KEYBOARD
);
193 // Adjustable keybaord media keys
194 if (codes
== 0 && has_media_keys
&&
195 (codes
= adb_host_kbd_recv(ADB_ADDR_APPLIANCE
))) {
197 switch (codes
& 0x7f ) {
199 codes
= (codes
& ~0x007f) | 0x42;
202 codes
= (codes
& ~0x007f) | 0x4a;
204 case 0x02: // Volume down
205 codes
= (codes
& ~0x007f) | 0x49;
207 case 0x03: // Volume Up
208 codes
= (codes
& ~0x007f) | 0x48;
210 case 0x7F: // no code
213 xprintf("ERROR: media key1\n");
217 switch ((codes
>> 8) & 0x7f ) {
219 codes
= (codes
& ~0x7f00) | (0x42 << 8);
222 codes
= (codes
& ~0x7f00) | (0x4a << 8);
224 case 0x02: // Volume down
225 codes
= (codes
& ~0x7f00) | (0x49 << 8);
227 case 0x03: // Volume Up
228 codes
= (codes
& ~0x7f00) | (0x48 << 8);
231 xprintf("ERROR: media key0\n");
239 if (debug_matrix
&& codes
) {
240 print("adb_host_kbd_recv: "); phex16(codes
); print("\n");
243 if (codes
== 0) { // no keys
245 } else if (codes
== 0x7F7F) { // power key press
247 } else if (codes
== 0xFFFF) { // power key release
249 } else if (key0
== 0xFF) { // error
250 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes
);
251 // something wrong or plug-in
255 /* Swap codes for ISO keyboard
256 * https://github.com/tmk/tmk_keyboard/issues/35
259 * ,----------- ----------.
260 * | *a| 1| 2 =|Backspa|
261 * |----------- ----------|
263 * |----------- ----------|
264 * |CapsLo| A| '|Return |
265 * |----------- ----------|
267 * `----------- ----------'
270 * ,----------- ----------.
271 * | *a| 1| 2 =|Backspa|
272 * |----------- ----------|
273 * |Tab | Q| | ]|Retur|
274 * |----------- -----` |
275 * |CapsLo| A| '| *c| |
276 * |----------- ----------|
278 * `----------- ----------'
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)
289 if ((key0
& 0x7F) == 0x32) {
290 key0
= (key0
& 0x80) | 0x0A;
291 } else if ((key0
& 0x7F) == 0x0A) {
292 key0
= (key0
& 0x80) | 0x32;
296 if (key1
!= 0xFF) // key1 is 0xFF when no second key.
297 extra_key
= key1
<<8 | 0xFF; // process in a separate call
304 matrix_row_t
matrix_get_row(uint8_t row
)
310 static void register_key(uint8_t key
)
316 matrix
[row
] &= ~(1<<col
);
318 matrix
[row
] |= (1<<col
);