]> git.gir.st - tmk_keyboard.git/blob - keyboard/hbkb/matrix.c
Fix ghost block and remove matrix_has_ghost()
[tmk_keyboard.git] / keyboard / hbkb / matrix.c
1 /*
2 Copyright 2012 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 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26
27
28 /*
29 * Happy Buckling Keyboard(IBM Model M mod)
30 *
31 * Pin usage:
32 * COL: PD0-7
33 * ROW: PB0-7, PF4-7
34 */
35
36 #if (MATRIX_COLS > 16)
37 # error "MATRIX_COLS must not exceed 16"
38 #endif
39 #if (MATRIX_ROWS > 255)
40 # error "MATRIX_ROWS must not exceed 255"
41 #endif
42
43
44 #ifndef DEBOUNCE
45 # define DEBOUNCE 0
46 #endif
47 static uint8_t debouncing = DEBOUNCE;
48
49 // matrix state buffer(1:on, 0:off)
50 #if (MATRIX_COLS <= 8)
51 static uint8_t *matrix;
52 static uint8_t *matrix_prev;
53 static uint8_t _matrix0[MATRIX_ROWS];
54 static uint8_t _matrix1[MATRIX_ROWS];
55 #else
56 static uint16_t *matrix;
57 static uint16_t *matrix_prev;
58 static uint16_t _matrix0[MATRIX_ROWS];
59 static uint16_t _matrix1[MATRIX_ROWS];
60 #endif
61
62 #ifdef MATRIX_HAS_GHOST
63 static bool matrix_has_ghost_in_row(uint8_t row);
64 #endif
65 static uint8_t read_col(void);
66 static void unselect_rows(void);
67 static void select_row(uint8_t row);
68
69
70 inline
71 uint8_t matrix_rows(void)
72 {
73 return MATRIX_ROWS;
74 }
75
76 inline
77 uint8_t matrix_cols(void)
78 {
79 return MATRIX_COLS;
80 }
81
82 void matrix_init(void)
83 {
84 print_enable = true;
85 debug_enable = true;
86 debug_matrix = true;
87 debug_keyboard = false;
88 debug_mouse = false;
89 print("debug enabled.\n");
90
91 // JTAG disable for PORT F. write JTD bit twice within four cycles.
92 MCUCR |= (1<<JTD);
93 MCUCR |= (1<<JTD);
94
95 // initialize rows
96 unselect_rows();
97
98 // initialize columns to input with pull-up(DDR:0, PORT:1)
99 DDRD = 0x00;
100 PORTD = 0xFF;
101
102 // initialize matrix state: all keys off
103 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
104 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
105 matrix = _matrix0;
106 matrix_prev = _matrix1;
107 }
108
109 uint8_t matrix_scan(void)
110 {
111 if (!debouncing) {
112 uint8_t *tmp = matrix_prev;
113 matrix_prev = matrix;
114 matrix = tmp;
115 }
116
117 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
118 unselect_rows();
119 select_row(i);
120 _delay_us(30); // without this wait read unstable value.
121 if (matrix[i] != (uint8_t)~read_col()) {
122 matrix[i] = (uint8_t)~read_col();
123 if (debouncing) {
124 debug("bounce!: "); debug_hex(debouncing); print("\n");
125 }
126 _delay_ms(1); // TODO: work around. HAHAHAHAHAAHA
127 debouncing = DEBOUNCE;
128 }
129 }
130 unselect_rows();
131
132 if (debouncing) {
133 debouncing--;
134 }
135
136 return 1;
137 }
138
139 bool matrix_is_modified(void)
140 {
141 if (debouncing) return false;
142 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
143 if (matrix[i] != matrix_prev[i]) {
144 return true;
145 }
146 }
147 return false;
148 }
149
150 inline
151 bool matrix_is_on(uint8_t row, uint8_t col)
152 {
153 return (matrix[row] & (1<<col));
154 }
155
156 inline
157 #if (MATRIX_COLS <= 8)
158 uint8_t matrix_get_row(uint8_t row)
159 #else
160 uint16_t matrix_get_row(uint8_t row)
161 #endif
162 {
163 return matrix[row];
164 }
165
166 void matrix_print(void)
167 {
168 print("\nr/c 01234567\n");
169 for (uint8_t row = 0; row < matrix_rows(); row++) {
170 phex(row); print(": ");
171 #if (MATRIX_COLS <= 8)
172 pbin_reverse(matrix_get_row(row));
173 #else
174 pbin_reverse16(matrix_get_row(row));
175 #endif
176 #ifdef MATRIX_HAS_GHOST
177 if (matrix_has_ghost_in_row(row)) {
178 print(" <ghost");
179 }
180 #endif
181 print("\n");
182 }
183 }
184
185 uint8_t matrix_key_count(void)
186 {
187 uint8_t count = 0;
188 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
189 #if (MATRIX_COLS <= 8)
190 count += bitpop(matrix[i]);
191 #else
192 count += bitpop16(matrix[i]);
193 #endif
194 }
195 return count;
196 }
197
198 #ifdef MATRIX_HAS_GHOST
199 inline
200 static bool matrix_has_ghost_in_row(uint8_t row)
201 {
202 // no ghost exists in case less than 2 keys on
203 if (((matrix[row] - 1) & matrix[row]) == 0)
204 return false;
205
206 // ghost exists in case same state as other row
207 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
208 //if (i != row && (matrix[i] & matrix[row]) == matrix[row])
209 if (i != row && (matrix[i] & matrix[row]))
210 return true;
211 }
212 return false;
213 }
214 #endif
215
216 inline
217 static uint8_t read_col(void)
218 {
219 return PIND;
220 }
221
222 inline
223 static void unselect_rows(void)
224 {
225 // Hi-Z(DDR:0, PORT:0) to unselect
226 DDRB &= ~0b11111111;
227 PORTB &= ~0b11111111;
228 DDRF &= ~0b11110000;
229 PORTF &= ~0b11110000;
230 }
231
232 inline
233 static void select_row(uint8_t row)
234 {
235 // Output low(DDR:1, PORT:0) to select
236 switch (row) {
237 case 0:
238 case 1:
239 case 2:
240 case 3:
241 case 4:
242 case 5:
243 case 6:
244 case 7:
245 DDRB |= (1<<row);
246 PORTB &= ~(1<<row);
247 break;
248 case 8:
249 DDRF |= (1<<4);
250 PORTF &= ~(1<<4);
251 break;
252 case 9:
253 case 10:
254 case 11:
255 DDRF |= (1<<(row-4));
256 PORTF &= ~(1<<(row-4));
257 break;
258 }
259 }
Imprint / Impressum