Fix matrix.c to use new default impl.
[tmk_keyboard.git] / converter / adb_usb / matrix.c
CommitLineData
0dde25e8 1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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"
56e098d7 32
33
56e098d7 34
35
b653b622 36static bool has_media_keys = false;
49f44a2a 37static bool is_iso_layout = false;
7780fd1c 38static report_mouse_t mouse_report = {};
56e098d7 39
40// matrix state buffer(1:on, 0:off)
1ad31539 41static matrix_row_t matrix[MATRIX_ROWS];
56e098d7 42
d8ce19ab 43static void register_key(uint8_t key);
56e098d7 44
45
56e098d7 46void matrix_init(void)
47{
b653b622 48 // LED on
49 DDRD |= (1<<6); PORTD |= (1<<6);
50
56e098d7 51 adb_host_init();
c18c52f5 52 // wait for keyboard to boot up and receive command
b653b622 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 }
49f44a2a 64
4d9e66ba 65 // Determine ISO keyboard by handler id
49f44a2a 66 // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815
b653b622 67 uint16_t handler_id = adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_3);
4d9e66ba 68 switch (handler_id) {
49f44a2a 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
b653b622 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
c18c52f5 85 // Enable keyboard left/right modifier distinction
b653b622 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 }
56e098d7 100
101 // initialize matrix state: all keys off
d8ce19ab 102 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
56e098d7 103
56e098d7 104 debug_enable = true;
c531a185 105 //debug_matrix = true;
106 //debug_keyboard = true;
107 //debug_mouse = true;
56e098d7 108 print("debug enabled.\n");
c219570e 109
b653b622 110 // LED off
c219570e 111 DDRD |= (1<<6); PORTD &= ~(1<<6);
56e098d7 112 return;
113}
114
7780fd1c
MA
115#ifdef ADB_MOUSE_ENABLE
116
117#ifdef MAX
118#undef MAX
119#endif
120#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
121
122void adb_mouse_task(void)
123{
124 uint16_t codes;
125 int16_t x, y;
49f44a2a 126 static int8_t mouseacc;
7780fd1c
MA
127 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
128 codes = adb_host_mouse_recv();
49f44a2a 129 // If nothing received reset mouse acceleration, and quit.
7780fd1c
MA
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;
49f44a2a 139 // lower seven bits are movement, as signed int_7.
140 // low byte is X-axis, high byte is Y.
7780fd1c
MA
141 y = (codes>>8 & 0x3F);
142 x = (codes>>0 & 0x3F);
143 // bit seven and fifteen is negative
49f44a2a 144 // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
7780fd1c
MA
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;
49f44a2a 152 // Cap our two bytes per axis to one byte.
7780fd1c
MA
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 }
49f44a2a 165 // Send result by usb.
7780fd1c
MA
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
56e098d7 173uint8_t matrix_scan(void)
174{
263c4626
SG
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;
56e098d7 182 uint16_t codes;
183 uint8_t key0, key1;
184
263c4626
SG
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
b653b622 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 }
263c4626 235 }
56e098d7 236 key0 = codes>>8;
237 key1 = codes&0xFF;
56e098d7 238
d8ce19ab 239 if (debug_matrix && codes) {
76033dcd 240 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
241 }
242
56e098d7 243 if (codes == 0) { // no keys
244 return 0;
d8ce19ab 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
c531a185 250 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
4fc0efd3 251 // something wrong or plug-in
252 matrix_init();
d8ce19ab 253 return key1;
56e098d7 254 } else {
49f44a2a 255 /* Swap codes for ISO keyboard
4d9e66ba 256 * https://github.com/tmk/tmk_keyboard/issues/35
49f44a2a 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) {
4d9e66ba 289 if ((key0 & 0x7F) == 0x32) {
290 key0 = (key0 & 0x80) | 0x0A;
291 } else if ((key0 & 0x7F) == 0x0A) {
292 key0 = (key0 & 0x80) | 0x32;
49f44a2a 293 }
294 }
d8ce19ab 295 register_key(key0);
56e098d7 296 if (key1 != 0xFF) // key1 is 0xFF when no second key.
263c4626 297 extra_key = key1<<8 | 0xFF; // process in a separate call
56e098d7 298 }
299
300 return 1;
301}
302
56e098d7 303inline
1ad31539 304matrix_row_t matrix_get_row(uint8_t row)
56e098d7 305{
306 return matrix[row];
307}
308
56e098d7 309inline
d8ce19ab 310static void register_key(uint8_t key)
56e098d7 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}
Imprint / Impressum