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_modified = false;
43 static report_mouse_t mouse_report = {};
45 // matrix state buffer(1:on, 0:off)
46 #if (MATRIX_COLS <= 8)
47 static uint8_t matrix[MATRIX_ROWS];
49 static uint16_t matrix[MATRIX_ROWS];
52 #ifdef MATRIX_HAS_GHOST
53 static bool matrix_has_ghost_in_row(uint8_t row);
55 static void register_key(uint8_t key);
59 uint8_t matrix_rows(void)
65 uint8_t matrix_cols(void)
70 void matrix_init(void)
73 // wait for keyboard to boot up and receive command
75 // Enable keyboard left/right modifier distinction
76 // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11)
77 // upper byte: reserved bits 0000, device address 0010
78 // lower byte: device handler 00000011
79 adb_host_listen(0x2B,0x02,0x03);
81 // initialize matrix state: all keys off
82 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
85 //debug_matrix = true;
86 //debug_keyboard = true;
88 print("debug enabled.\n");
92 #ifdef ADB_MOUSE_ENABLE
97 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
99 void adb_mouse_task(void)
103 static int8_t mouseacc;
104 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
105 codes = adb_host_mouse_recv();
106 // If nothing received reset mouse acceleration, and quit.
111 // Bit sixteen is button.
112 if (~codes & (1 << 15))
113 mouse_report.buttons |= MOUSE_BTN1;
114 if (codes & (1 << 15))
115 mouse_report.buttons &= ~MOUSE_BTN1;
116 // lower seven bits are movement, as signed int_7.
117 // low byte is X-axis, high byte is Y.
118 y = (codes>>8 & 0x3F);
119 x = (codes>>0 & 0x3F);
120 // bit seven and fifteen is negative
121 // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
122 if (codes & (1 << 6))
124 if (codes & (1 << 14))
126 // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
129 // Cap our two bytes per axis to one byte.
130 // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
131 // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
132 mouse_report.x = -MAX(-MAX(x, -127), -127);
133 mouse_report.y = -MAX(-MAX(y, -127), -127);
135 print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
136 print("adb_mouse raw: [");
137 phex(mouseacc); print(" ");
138 phex(mouse_report.buttons); print("|");
139 print_decs(mouse_report.x); print(" ");
140 print_decs(mouse_report.y); print("]\n");
142 // Send result by usb.
143 host_mouse_send(&mouse_report);
144 // increase acceleration of mouse
145 mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
150 uint8_t matrix_scan(void)
152 /* extra_key is volatile and more convoluted than necessary because gcc refused
153 to generate valid code otherwise. Making extra_key uint8_t and constructing codes
154 here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
155 extra_key from memory, and leave garbage in the high byte of codes. I tried
156 dozens of code variations and it kept generating broken assembly output. So
157 beware if attempting to make extra_key code more logical and efficient. */
158 static volatile uint16_t extra_key = 0xFFFF;
167 if ( codes == 0xFFFF )
169 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
170 codes = adb_host_kbd_recv();
175 if (debug_matrix && codes) {
176 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
179 if (codes == 0) { // no keys
181 } else if (codes == 0x7F7F) { // power key press
183 } else if (codes == 0xFFFF) { // power key release
185 } else if (key0 == 0xFF) { // error
186 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
190 if (key1 != 0xFF) // key1 is 0xFF when no second key.
191 extra_key = key1<<8 | 0xFF; // process in a separate call
197 bool matrix_is_modified(void)
203 bool matrix_has_ghost(void)
205 #ifdef MATRIX_HAS_GHOST
206 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
207 if (matrix_has_ghost_in_row(i))
215 bool matrix_is_on(uint8_t row, uint8_t col)
217 return (matrix[row] & (1<<col));
221 #if (MATRIX_COLS <= 8)
222 uint8_t matrix_get_row(uint8_t row)
224 uint16_t matrix_get_row(uint8_t row)
230 void matrix_print(void)
232 if (!debug_matrix) return;
233 #if (MATRIX_COLS <= 8)
234 print("r/c 01234567\n");
236 print("r/c 0123456789ABCDEF\n");
238 for (uint8_t row = 0; row < matrix_rows(); row++) {
239 phex(row); print(": ");
240 #if (MATRIX_COLS <= 8)
241 pbin_reverse(matrix_get_row(row));
243 pbin_reverse16(matrix_get_row(row));
245 #ifdef MATRIX_HAS_GHOST
246 if (matrix_has_ghost_in_row(row)) {
254 uint8_t matrix_key_count(void)
257 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
258 #if (MATRIX_COLS <= 8)
259 count += bitpop(matrix[i]);
261 count += bitpop16(matrix[i]);
267 #ifdef MATRIX_HAS_GHOST
269 static bool matrix_has_ghost_in_row(uint8_t row)
271 // no ghost exists in case less than 2 keys on
272 if (((matrix[row] - 1) & matrix[row]) == 0)
275 // ghost exists in case same state as other row
276 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
277 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
285 static void register_key(uint8_t key)
291 matrix[row] &= ~(1<<col);
293 matrix[row] |= (1<<col);