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