]> git.gir.st - tmk_keyboard.git/blob - converter/adb_usb/matrix.c
Merge pull request #292 from obones/command_warning
[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_modified = false;
43 static report_mouse_t mouse_report = {};
44
45 // matrix state buffer(1:on, 0:off)
46 #if (MATRIX_COLS <= 8)
47 static uint8_t matrix[MATRIX_ROWS];
48 #else
49 static uint16_t matrix[MATRIX_ROWS];
50 #endif
51
52 #ifdef MATRIX_HAS_GHOST
53 static bool matrix_has_ghost_in_row(uint8_t row);
54 #endif
55 static void register_key(uint8_t key);
56
57
58 inline
59 uint8_t matrix_rows(void)
60 {
61 return MATRIX_ROWS;
62 }
63
64 inline
65 uint8_t matrix_cols(void)
66 {
67 return MATRIX_COLS;
68 }
69
70 void matrix_init(void)
71 {
72 adb_host_init();
73 // wait for keyboard to boot up and receive command
74 _delay_ms(1000);
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);
80
81 // initialize matrix state: all keys off
82 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
83
84 debug_enable = true;
85 //debug_matrix = true;
86 //debug_keyboard = true;
87 //debug_mouse = true;
88 print("debug enabled.\n");
89
90 // LED flash
91 DDRD |= (1<<6); PORTD |= (1<<6);
92 _delay_ms(500);
93 DDRD |= (1<<6); PORTD &= ~(1<<6);
94
95 return;
96 }
97
98 #ifdef ADB_MOUSE_ENABLE
99
100 #ifdef MAX
101 #undef MAX
102 #endif
103 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
104
105 void adb_mouse_task(void)
106 {
107 uint16_t codes;
108 int16_t x, y;
109 static int8_t mouseacc;
110 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
111 codes = adb_host_mouse_recv();
112 // If nothing received reset mouse acceleration, and quit.
113 if (!codes) {
114 mouseacc = 1;
115 return;
116 };
117 // Bit sixteen is button.
118 if (~codes & (1 << 15))
119 mouse_report.buttons |= MOUSE_BTN1;
120 if (codes & (1 << 15))
121 mouse_report.buttons &= ~MOUSE_BTN1;
122 // lower seven bits are movement, as signed int_7.
123 // low byte is X-axis, high byte is Y.
124 y = (codes>>8 & 0x3F);
125 x = (codes>>0 & 0x3F);
126 // bit seven and fifteen is negative
127 // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
128 if (codes & (1 << 6))
129 x = (x-0x40);
130 if (codes & (1 << 14))
131 y = (y-0x40);
132 // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
133 x *= mouseacc;
134 y *= mouseacc;
135 // Cap our two bytes per axis to one byte.
136 // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
137 // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
138 mouse_report.x = -MAX(-MAX(x, -127), -127);
139 mouse_report.y = -MAX(-MAX(y, -127), -127);
140 if (debug_mouse) {
141 print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
142 print("adb_mouse raw: [");
143 phex(mouseacc); print(" ");
144 phex(mouse_report.buttons); print("|");
145 print_decs(mouse_report.x); print(" ");
146 print_decs(mouse_report.y); print("]\n");
147 }
148 // Send result by usb.
149 host_mouse_send(&mouse_report);
150 // increase acceleration of mouse
151 mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
152 return;
153 }
154 #endif
155
156 uint8_t matrix_scan(void)
157 {
158 /* extra_key is volatile and more convoluted than necessary because gcc refused
159 to generate valid code otherwise. Making extra_key uint8_t and constructing codes
160 here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
161 extra_key from memory, and leave garbage in the high byte of codes. I tried
162 dozens of code variations and it kept generating broken assembly output. So
163 beware if attempting to make extra_key code more logical and efficient. */
164 static volatile uint16_t extra_key = 0xFFFF;
165 uint16_t codes;
166 uint8_t key0, key1;
167
168 is_modified = false;
169
170 codes = extra_key;
171 extra_key = 0xFFFF;
172
173 if ( codes == 0xFFFF )
174 {
175 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
176 codes = adb_host_kbd_recv();
177 }
178 key0 = codes>>8;
179 key1 = codes&0xFF;
180
181 if (debug_matrix && codes) {
182 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
183 }
184
185 if (codes == 0) { // no keys
186 return 0;
187 } else if (codes == 0x7F7F) { // power key press
188 register_key(0x7F);
189 } else if (codes == 0xFFFF) { // power key release
190 register_key(0xFF);
191 } else if (key0 == 0xFF) { // error
192 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
193 return key1;
194 } else {
195 register_key(key0);
196 if (key1 != 0xFF) // key1 is 0xFF when no second key.
197 extra_key = key1<<8 | 0xFF; // process in a separate call
198 }
199
200 return 1;
201 }
202
203 bool matrix_is_modified(void)
204 {
205 return is_modified;
206 }
207
208 inline
209 bool matrix_has_ghost(void)
210 {
211 #ifdef MATRIX_HAS_GHOST
212 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
213 if (matrix_has_ghost_in_row(i))
214 return true;
215 }
216 #endif
217 return false;
218 }
219
220 inline
221 bool matrix_is_on(uint8_t row, uint8_t col)
222 {
223 return (matrix[row] & (1<<col));
224 }
225
226 inline
227 #if (MATRIX_COLS <= 8)
228 uint8_t matrix_get_row(uint8_t row)
229 #else
230 uint16_t matrix_get_row(uint8_t row)
231 #endif
232 {
233 return matrix[row];
234 }
235
236 void matrix_print(void)
237 {
238 if (!debug_matrix) return;
239 #if (MATRIX_COLS <= 8)
240 print("r/c 01234567\n");
241 #else
242 print("r/c 0123456789ABCDEF\n");
243 #endif
244 for (uint8_t row = 0; row < matrix_rows(); row++) {
245 phex(row); print(": ");
246 #if (MATRIX_COLS <= 8)
247 pbin_reverse(matrix_get_row(row));
248 #else
249 pbin_reverse16(matrix_get_row(row));
250 #endif
251 #ifdef MATRIX_HAS_GHOST
252 if (matrix_has_ghost_in_row(row)) {
253 print(" <ghost");
254 }
255 #endif
256 print("\n");
257 }
258 }
259
260 uint8_t matrix_key_count(void)
261 {
262 uint8_t count = 0;
263 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
264 #if (MATRIX_COLS <= 8)
265 count += bitpop(matrix[i]);
266 #else
267 count += bitpop16(matrix[i]);
268 #endif
269 }
270 return count;
271 }
272
273 #ifdef MATRIX_HAS_GHOST
274 inline
275 static bool matrix_has_ghost_in_row(uint8_t row)
276 {
277 // no ghost exists in case less than 2 keys on
278 if (((matrix[row] - 1) & matrix[row]) == 0)
279 return false;
280
281 // ghost exists in case same state as other row
282 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
283 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
284 return true;
285 }
286 return false;
287 }
288 #endif
289
290 inline
291 static void register_key(uint8_t key)
292 {
293 uint8_t col, row;
294 col = key&0x07;
295 row = (key>>3)&0x0F;
296 if (key&0x80) {
297 matrix[row] &= ~(1<<col);
298 } else {
299 matrix[row] |= (1<<col);
300 }
301 is_modified = true;
302 }
Imprint / Impressum