]> git.gir.st - tmk_keyboard.git/blob - converter/adb_usb/matrix.c
adb_usb: reinitialize for plug-in after startup
[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 handle id
78 // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815
79 uint16_t handle_id = adb_host_talk(ADB_ADDR_KEYBOARD, 3);
80 switch (handle_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 handle_id2 = adb_host_talk(ADB_ADDR_KEYBOARD, 3);
112 xprintf("handle_id: %02X -> %02X\n", handle_id&0xff, handle_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 *
218 * ANSI
219 * ,----------- ----------.
220 * | *a| 1| 2 =|Backspa|
221 * |----------- ----------|
222 * |Tab | Q| | ]| *c|
223 * |----------- ----------|
224 * |CapsLo| A| '|Return |
225 * |----------- ----------|
226 * |Shift | Shift |
227 * `----------- ----------'
228 *
229 * ISO
230 * ,----------- ----------.
231 * | *a| 1| 2 =|Backspa|
232 * |----------- ----------|
233 * |Tab | Q| | ]|Retur|
234 * |----------- -----` |
235 * |CapsLo| A| '| *c| |
236 * |----------- ----------|
237 * |Shif| *b| Shift |
238 * `----------- ----------'
239 *
240 * ADB scan code USB usage
241 * ------------- ---------
242 * Key ANSI ISO ANSI ISO
243 * ---------------------------------------------
244 * *a 0x32 0x0A 0x35 0x35
245 * *b ---- 0x32 ---- 0x64
246 * *c 0x2A 0x2A 0x31 0x31(or 0x32)
247 */
248 if (is_iso_layout) {
249 if (key0 == 0x32) {
250 key0 = 0x0A;
251 } else if (key0 == 0x0A) {
252 key0 = 0x32;
253 }
254 }
255 register_key(key0);
256 if (key1 != 0xFF) // key1 is 0xFF when no second key.
257 extra_key = key1<<8 | 0xFF; // process in a separate call
258 }
259
260 return 1;
261 }
262
263 bool matrix_is_modified(void)
264 {
265 return is_modified;
266 }
267
268 inline
269 bool matrix_has_ghost(void)
270 {
271 #ifdef MATRIX_HAS_GHOST
272 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
273 if (matrix_has_ghost_in_row(i))
274 return true;
275 }
276 #endif
277 return false;
278 }
279
280 inline
281 bool matrix_is_on(uint8_t row, uint8_t col)
282 {
283 return (matrix[row] & (1<<col));
284 }
285
286 inline
287 #if (MATRIX_COLS <= 8)
288 uint8_t matrix_get_row(uint8_t row)
289 #else
290 uint16_t matrix_get_row(uint8_t row)
291 #endif
292 {
293 return matrix[row];
294 }
295
296 void matrix_print(void)
297 {
298 if (!debug_matrix) return;
299 #if (MATRIX_COLS <= 8)
300 print("r/c 01234567\n");
301 #else
302 print("r/c 0123456789ABCDEF\n");
303 #endif
304 for (uint8_t row = 0; row < matrix_rows(); row++) {
305 phex(row); print(": ");
306 #if (MATRIX_COLS <= 8)
307 pbin_reverse(matrix_get_row(row));
308 #else
309 pbin_reverse16(matrix_get_row(row));
310 #endif
311 #ifdef MATRIX_HAS_GHOST
312 if (matrix_has_ghost_in_row(row)) {
313 print(" <ghost");
314 }
315 #endif
316 print("\n");
317 }
318 }
319
320 uint8_t matrix_key_count(void)
321 {
322 uint8_t count = 0;
323 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
324 #if (MATRIX_COLS <= 8)
325 count += bitpop(matrix[i]);
326 #else
327 count += bitpop16(matrix[i]);
328 #endif
329 }
330 return count;
331 }
332
333 #ifdef MATRIX_HAS_GHOST
334 inline
335 static bool matrix_has_ghost_in_row(uint8_t row)
336 {
337 // no ghost exists in case less than 2 keys on
338 if (((matrix[row] - 1) & matrix[row]) == 0)
339 return false;
340
341 // ghost exists in case same state as other row
342 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
343 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
344 return true;
345 }
346 return false;
347 }
348 #endif
349
350 inline
351 static void register_key(uint8_t key)
352 {
353 uint8_t col, row;
354 col = key&0x07;
355 row = (key>>3)&0x0F;
356 if (key&0x80) {
357 matrix[row] &= ~(1<<col);
358 } else {
359 matrix[row] |= (1<<col);
360 }
361 is_modified = true;
362 }
Imprint / Impressum