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>
34 #if (MATRIX_COLS > 16)
35 # error "MATRIX_COLS must not exceed 16"
37 #if (MATRIX_ROWS > 255)
38 # error "MATRIX_ROWS must not exceed 255"
42 static bool is_iso_layout
= false;
43 static bool is_modified
= false;
44 static report_mouse_t mouse_report
= {};
46 // matrix state buffer(1:on, 0:off)
47 #if (MATRIX_COLS <= 8)
48 static uint8_t matrix
[MATRIX_ROWS
];
50 static uint16_t matrix
[MATRIX_ROWS
];
53 #ifdef MATRIX_HAS_GHOST
54 static bool matrix_has_ghost_in_row(uint8_t row
);
56 static void register_key(uint8_t key
);
60 uint8_t matrix_rows(void)
66 uint8_t matrix_cols(void)
71 void matrix_init(void)
74 // wait for keyboard to boot up and receive command
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);
81 case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
82 case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
87 is_iso_layout
= false;
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);
97 // initialize matrix state: all keys off
98 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) matrix
[i
] = 0x00;
101 //debug_matrix = true;
102 //debug_keyboard = true;
103 //debug_mouse = true;
104 print("debug enabled.\n");
107 DDRD
|= (1<<6); PORTD
|= (1<<6);
109 DDRD
|= (1<<6); PORTD
&= ~(1<<6);
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));
117 #ifdef ADB_MOUSE_ENABLE
122 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
124 void adb_mouse_task(void)
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.
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))
149 if (codes
& (1 << 14))
151 // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
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);
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");
167 // Send result by usb.
168 host_mouse_send(&mouse_report
);
169 // increase acceleration of mouse
170 mouseacc
+= ( mouseacc
< ADB_MOUSE_MAXACC
? 1 : 0 );
175 uint8_t matrix_scan(void)
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;
192 if ( codes
== 0xFFFF )
194 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
195 codes
= adb_host_kbd_recv();
200 if (debug_matrix
&& codes
) {
201 print("adb_host_kbd_recv: "); phex16(codes
); print("\n");
204 if (codes
== 0) { // no keys
206 } else if (codes
== 0x7F7F) { // power key press
208 } else if (codes
== 0xFFFF) { // power key release
210 } else if (key0
== 0xFF) { // error
211 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes
);
212 // something wrong or plug-in
216 /* Swap codes for ISO keyboard
217 * https://github.com/tmk/tmk_keyboard/issues/35
220 * ,----------- ----------.
221 * | *a| 1| 2 =|Backspa|
222 * |----------- ----------|
224 * |----------- ----------|
225 * |CapsLo| A| '|Return |
226 * |----------- ----------|
228 * `----------- ----------'
231 * ,----------- ----------.
232 * | *a| 1| 2 =|Backspa|
233 * |----------- ----------|
234 * |Tab | Q| | ]|Retur|
235 * |----------- -----` |
236 * |CapsLo| A| '| *c| |
237 * |----------- ----------|
239 * `----------- ----------'
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)
250 if ((key0
& 0x7F) == 0x32) {
251 key0
= (key0
& 0x80) | 0x0A;
252 } else if ((key0
& 0x7F) == 0x0A) {
253 key0
= (key0
& 0x80) | 0x32;
257 if (key1
!= 0xFF) // key1 is 0xFF when no second key.
258 extra_key
= key1
<<8 | 0xFF; // process in a separate call
264 bool matrix_is_modified(void)
270 bool matrix_has_ghost(void)
272 #ifdef MATRIX_HAS_GHOST
273 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
274 if (matrix_has_ghost_in_row(i
))
282 bool matrix_is_on(uint8_t row
, uint8_t col
)
284 return (matrix
[row
] & (1<<col
));
288 #if (MATRIX_COLS <= 8)
289 uint8_t matrix_get_row(uint8_t row
)
291 uint16_t matrix_get_row(uint8_t row
)
297 void matrix_print(void)
299 if (!debug_matrix
) return;
300 #if (MATRIX_COLS <= 8)
301 print("r/c 01234567\n");
303 print("r/c 0123456789ABCDEF\n");
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
));
310 pbin_reverse16(matrix_get_row(row
));
312 #ifdef MATRIX_HAS_GHOST
313 if (matrix_has_ghost_in_row(row
)) {
321 uint8_t matrix_key_count(void)
324 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
325 #if (MATRIX_COLS <= 8)
326 count
+= bitpop(matrix
[i
]);
328 count
+= bitpop16(matrix
[i
]);
334 #ifdef MATRIX_HAS_GHOST
336 static bool matrix_has_ghost_in_row(uint8_t row
)
338 // no ghost exists in case less than 2 keys on
339 if (((matrix
[row
] - 1) & matrix
[row
]) == 0)
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
])
352 static void register_key(uint8_t key
)
358 matrix
[row
] &= ~(1<<col
);
360 matrix
[row
] |= (1<<col
);