]> git.gir.st - tmk_keyboard.git/blob - converter/adb_usb/matrix.c
Merge pull request #80 from gblargg/master
[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
31
32 #if (MATRIX_COLS > 16)
33 # error "MATRIX_COLS must not exceed 16"
34 #endif
35 #if (MATRIX_ROWS > 255)
36 # error "MATRIX_ROWS must not exceed 255"
37 #endif
38
39
40 static bool is_modified = false;
41
42 // matrix state buffer(1:on, 0:off)
43 #if (MATRIX_COLS <= 8)
44 static uint8_t matrix[MATRIX_ROWS];
45 #else
46 static uint16_t matrix[MATRIX_ROWS];
47 #endif
48
49 #ifdef MATRIX_HAS_GHOST
50 static bool matrix_has_ghost_in_row(uint8_t row);
51 #endif
52 static void register_key(uint8_t key);
53
54
55 inline
56 uint8_t matrix_rows(void)
57 {
58 return MATRIX_ROWS;
59 }
60
61 inline
62 uint8_t matrix_cols(void)
63 {
64 return MATRIX_COLS;
65 }
66
67 void matrix_init(void)
68 {
69 adb_host_init();
70 // wait for keyboard to boot up and receive command
71 _delay_ms(1000);
72 // Enable keyboard left/right modifier distinction
73 // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11)
74 // upper byte: reserved bits 0000, device address 0010
75 // lower byte: device handler 00000011
76 adb_host_listen(0x2B,0x02,0x03);
77
78 // initialize matrix state: all keys off
79 for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
80
81 debug_enable = true;
82 //debug_matrix = true;
83 //debug_keyboard = true;
84 //debug_mouse = true;
85 print("debug enabled.\n");
86 return;
87 }
88
89 uint8_t matrix_scan(void)
90 {
91 /* extra_key is volatile and more convoluted than necessary because gcc refused
92 to generate valid code otherwise. Making extra_key uint8_t and constructing codes
93 here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
94 extra_key from memory, and leave garbage in the high byte of codes. I tried
95 dozens of code variations and it kept generating broken assembly output. So
96 beware if attempting to make extra_key code more logical and efficient. */
97 static volatile uint16_t extra_key = 0xFFFF;
98 uint16_t codes;
99 uint8_t key0, key1;
100
101 is_modified = false;
102
103 codes = extra_key;
104 extra_key = 0xFFFF;
105
106 if ( codes == 0xFFFF )
107 {
108 _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
109 codes = adb_host_kbd_recv();
110 }
111 key0 = codes>>8;
112 key1 = codes&0xFF;
113
114 if (debug_matrix && codes) {
115 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
116 }
117
118 if (codes == 0) { // no keys
119 return 0;
120 } else if (codes == 0x7F7F) { // power key press
121 register_key(0x7F);
122 } else if (codes == 0xFFFF) { // power key release
123 register_key(0xFF);
124 } else if (key0 == 0xFF) { // error
125 xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
126 return key1;
127 } else {
128 register_key(key0);
129 if (key1 != 0xFF) // key1 is 0xFF when no second key.
130 extra_key = key1<<8 | 0xFF; // process in a separate call
131 }
132
133 return 1;
134 }
135
136 bool matrix_is_modified(void)
137 {
138 return is_modified;
139 }
140
141 inline
142 bool matrix_has_ghost(void)
143 {
144 #ifdef MATRIX_HAS_GHOST
145 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
146 if (matrix_has_ghost_in_row(i))
147 return true;
148 }
149 #endif
150 return false;
151 }
152
153 inline
154 bool matrix_is_on(uint8_t row, uint8_t col)
155 {
156 return (matrix[row] & (1<<col));
157 }
158
159 inline
160 #if (MATRIX_COLS <= 8)
161 uint8_t matrix_get_row(uint8_t row)
162 #else
163 uint16_t matrix_get_row(uint8_t row)
164 #endif
165 {
166 return matrix[row];
167 }
168
169 void matrix_print(void)
170 {
171 if (!debug_matrix) return;
172 #if (MATRIX_COLS <= 8)
173 print("r/c 01234567\n");
174 #else
175 print("r/c 0123456789ABCDEF\n");
176 #endif
177 for (uint8_t row = 0; row < matrix_rows(); row++) {
178 phex(row); print(": ");
179 #if (MATRIX_COLS <= 8)
180 pbin_reverse(matrix_get_row(row));
181 #else
182 pbin_reverse16(matrix_get_row(row));
183 #endif
184 #ifdef MATRIX_HAS_GHOST
185 if (matrix_has_ghost_in_row(row)) {
186 print(" <ghost");
187 }
188 #endif
189 print("\n");
190 }
191 }
192
193 uint8_t matrix_key_count(void)
194 {
195 uint8_t count = 0;
196 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
197 #if (MATRIX_COLS <= 8)
198 count += bitpop(matrix[i]);
199 #else
200 count += bitpop16(matrix[i]);
201 #endif
202 }
203 return count;
204 }
205
206 #ifdef MATRIX_HAS_GHOST
207 inline
208 static bool matrix_has_ghost_in_row(uint8_t row)
209 {
210 // no ghost exists in case less than 2 keys on
211 if (((matrix[row] - 1) & matrix[row]) == 0)
212 return false;
213
214 // ghost exists in case same state as other row
215 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
216 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
217 return true;
218 }
219 return false;
220 }
221 #endif
222
223 inline
224 static void register_key(uint8_t key)
225 {
226 uint8_t col, row;
227 col = key&0x07;
228 row = (key>>3)&0x0F;
229 if (key&0x80) {
230 matrix[row] &= ~(1<<col);
231 } else {
232 matrix[row] |= (1<<col);
233 }
234 is_modified = true;
235 }
Imprint / Impressum