]> git.gir.st - tmk_keyboard.git/blob - adb_usb/matrix.c
added copyright and license notice.
[tmk_keyboard.git] / 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 _matrix_is_modified = false;
41
42 // matrix state buffer(1:on, 0:off)
43 #if (MATRIX_COLS <= 8)
44 static uint8_t *matrix;
45 static uint8_t _matrix0[MATRIX_ROWS];
46 #else
47 static uint16_t *matrix;
48 static uint16_t _matrix0[MATRIX_ROWS];
49 #endif
50
51 #ifdef MATRIX_HAS_GHOST
52 static bool matrix_has_ghost_in_row(uint8_t row);
53 #endif
54 static void _register_key(uint8_t key);
55
56
57 inline
58 uint8_t matrix_rows(void)
59 {
60 return MATRIX_ROWS;
61 }
62
63 inline
64 uint8_t matrix_cols(void)
65 {
66 return MATRIX_COLS;
67 }
68
69 void matrix_init(void)
70 {
71 adb_host_init();
72
73 // initialize matrix state: all keys off
74 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
75 matrix = _matrix0;
76
77 print_enable = true;
78 debug_enable = true;
79 debug_matrix = true;
80 debug_keyboard = true;
81 debug_mouse = true;
82 print("debug enabled.\n");
83 return;
84 }
85
86 uint8_t matrix_scan(void)
87 {
88 uint16_t codes;
89 uint8_t key0, key1;
90
91 _matrix_is_modified = false;
92 codes = adb_host_kbd_recv();
93 key0 = codes>>8;
94 key1 = codes&0xFF;
95
96 if (codes == 0) { // no keys
97 return 0;
98 } else if (key0 == 0xFF && key1 != 0xFF) { // error
99 return codes&0xFF;
100 } else {
101 _matrix_is_modified = true;
102 _register_key(key0);
103 if (key1 != 0xFF) // key1 is 0xFF when no second key.
104 _register_key(key1);
105 }
106
107 if (debug_enable) {
108 print("adb_host_kbd_recv: "); phex16(codes); print("\n");
109 }
110 return 1;
111 }
112
113 bool matrix_is_modified(void)
114 {
115 return _matrix_is_modified;
116 }
117
118 inline
119 bool matrix_has_ghost(void)
120 {
121 #ifdef MATRIX_HAS_GHOST
122 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
123 if (matrix_has_ghost_in_row(i))
124 return true;
125 }
126 #endif
127 return false;
128 }
129
130 inline
131 bool matrix_is_on(uint8_t row, uint8_t col)
132 {
133 return (matrix[row] & (1<<col));
134 }
135
136 inline
137 #if (MATRIX_COLS <= 8)
138 uint8_t matrix_get_row(uint8_t row)
139 #else
140 uint16_t matrix_get_row(uint8_t row)
141 #endif
142 {
143 return matrix[row];
144 }
145
146 void matrix_print(void)
147 {
148 #if (MATRIX_COLS <= 8)
149 print("\nr/c 01234567\n");
150 #else
151 print("\nr/c 0123456789ABCDEF\n");
152 #endif
153 for (uint8_t row = 0; row < matrix_rows(); row++) {
154 phex(row); print(": ");
155 #if (MATRIX_COLS <= 8)
156 pbin_reverse(matrix_get_row(row));
157 #else
158 pbin_reverse16(matrix_get_row(row));
159 #endif
160 #ifdef MATRIX_HAS_GHOST
161 if (matrix_has_ghost_in_row(row)) {
162 print(" <ghost");
163 }
164 #endif
165 print("\n");
166 }
167 }
168
169 uint8_t matrix_key_count(void)
170 {
171 uint8_t count = 0;
172 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
173 #if (MATRIX_COLS <= 8)
174 count += bitpop(matrix[i]);
175 #else
176 count += bitpop16(matrix[i]);
177 #endif
178 }
179 return count;
180 }
181
182 #ifdef MATRIX_HAS_GHOST
183 inline
184 static bool matrix_has_ghost_in_row(uint8_t row)
185 {
186 // no ghost exists in case less than 2 keys on
187 if (((matrix[row] - 1) & matrix[row]) == 0)
188 return false;
189
190 // ghost exists in case same state as other row
191 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
192 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
193 return true;
194 }
195 return false;
196 }
197 #endif
198
199 inline
200 static void _register_key(uint8_t key)
201 {
202 uint8_t col, row;
203 col = key&0x07;
204 row = (key>>3)&0x0F;
205 if (key&0x80) {
206 matrix[row] &= ~(1<<col);
207 } else {
208 matrix[row] |= (1<<col);
209 }
210 }
Imprint / Impressum