adb_usb: Fix swap ISO codes bug
[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
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
49f44a2a 42static bool is_iso_layout = false;
d8ce19ab 43static bool is_modified = false;
7780fd1c 44static report_mouse_t mouse_report = {};
56e098d7 45
46// matrix state buffer(1:on, 0:off)
47#if (MATRIX_COLS <= 8)
d8ce19ab 48static uint8_t matrix[MATRIX_ROWS];
56e098d7 49#else
d8ce19ab 50static uint16_t matrix[MATRIX_ROWS];
56e098d7 51#endif
52
53#ifdef MATRIX_HAS_GHOST
54static bool matrix_has_ghost_in_row(uint8_t row);
55#endif
d8ce19ab 56static void register_key(uint8_t key);
56e098d7 57
58
59inline
60uint8_t matrix_rows(void)
61{
62 return MATRIX_ROWS;
63}
64
65inline
66uint8_t matrix_cols(void)
67{
68 return MATRIX_COLS;
69}
70
71void matrix_init(void)
72{
73 adb_host_init();
c18c52f5 74 // wait for keyboard to boot up and receive command
75 _delay_ms(1000);
49f44a2a 76
4d9e66ba 77 // Determine ISO keyboard by handler id
49f44a2a 78 // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815
4d9e66ba 79 uint16_t handler_id = adb_host_talk(ADB_ADDR_KEYBOARD, 3);
80 switch (handler_id) {
49f44a2a 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
c18c52f5 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);
56e098d7 96
97 // initialize matrix state: all keys off
d8ce19ab 98 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
56e098d7 99
56e098d7 100 debug_enable = true;
c531a185 101 //debug_matrix = true;
102 //debug_keyboard = true;
103 //debug_mouse = true;
56e098d7 104 print("debug enabled.\n");
c219570e 105
106 // LED flash
107 DDRD |= (1<<6); PORTD |= (1<<6);
108 _delay_ms(500);
109 DDRD |= (1<<6); PORTD &= ~(1<<6);
110
4d9e66ba 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));
49f44a2a 113
56e098d7 114 return;
115}
116
7780fd1c
MA
117#ifdef ADB_MOUSE_ENABLE
118
119#ifdef MAX
120#undef MAX
121#endif
122#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
123
124void adb_mouse_task(void)
125{
126 uint16_t codes;
127 int16_t x, y;
49f44a2a 128 static int8_t mouseacc;
7780fd1c
MA
129 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
130 codes = adb_host_mouse_recv();
49f44a2a 131 // If nothing received reset mouse acceleration, and quit.
7780fd1c
MA
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;
49f44a2a 141 // lower seven bits are movement, as signed int_7.
142 // low byte is X-axis, high byte is Y.
7780fd1c
MA
143 y = (codes>>8 & 0x3F);
144 x = (codes>>0 & 0x3F);
145 // bit seven and fifteen is negative
49f44a2a 146 // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
7780fd1c
MA
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;
49f44a2a 154 // Cap our two bytes per axis to one byte.
7780fd1c
MA
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 }
49f44a2a 167 // Send result by usb.
7780fd1c
MA
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
56e098d7 175uint8_t matrix_scan(void)
176{
263c4626
SG
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;
56e098d7 184 uint16_t codes;
185 uint8_t key0, key1;
186
d8ce19ab 187 is_modified = false;
263c4626
SG
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 }
56e098d7 197 key0 = codes>>8;
198 key1 = codes&0xFF;
56e098d7 199
d8ce19ab 200 if (debug_matrix && codes) {
76033dcd 201 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
202 }
203
56e098d7 204 if (codes == 0) { // no keys
205 return 0;
d8ce19ab 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
c531a185 211 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
4fc0efd3 212 // something wrong or plug-in
213 matrix_init();
d8ce19ab 214 return key1;
56e098d7 215 } else {
49f44a2a 216 /* Swap codes for ISO keyboard
4d9e66ba 217 * https://github.com/tmk/tmk_keyboard/issues/35
49f44a2a 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) {
4d9e66ba 250 if ((key0 & 0x7F) == 0x32) {
251 key0 = (key0 & 0x80) | 0x0A;
252 } else if ((key0 & 0x7F) == 0x0A) {
253 key0 = (key0 & 0x80) | 0x32;
49f44a2a 254 }
255 }
d8ce19ab 256 register_key(key0);
56e098d7 257 if (key1 != 0xFF) // key1 is 0xFF when no second key.
263c4626 258 extra_key = key1<<8 | 0xFF; // process in a separate call
56e098d7 259 }
260
261 return 1;
262}
263
264bool matrix_is_modified(void)
265{
d8ce19ab 266 return is_modified;
56e098d7 267}
268
269inline
270bool 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
281inline
282bool matrix_is_on(uint8_t row, uint8_t col)
283{
284 return (matrix[row] & (1<<col));
285}
286
287inline
288#if (MATRIX_COLS <= 8)
289uint8_t matrix_get_row(uint8_t row)
290#else
291uint16_t matrix_get_row(uint8_t row)
292#endif
293{
294 return matrix[row];
295}
296
297void matrix_print(void)
298{
d8ce19ab 299 if (!debug_matrix) return;
56e098d7 300#if (MATRIX_COLS <= 8)
76033dcd 301 print("r/c 01234567\n");
56e098d7 302#else
76033dcd 303 print("r/c 0123456789ABCDEF\n");
56e098d7 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
321uint8_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
335inline
336static 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
351inline
d8ce19ab 352static void register_key(uint8_t key)
56e098d7 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 }
d8ce19ab 362 is_modified = true;
56e098d7 363}
Imprint / Impressum