]> git.gir.st - tmk_keyboard.git/blob - keyboard/kmac/matrix.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / keyboard / kmac / matrix.c
1 /*
2 Copyright 2013 Mathias Andersson <wraul@dbox.se>
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 #ifndef DEBOUNCE
29 # define DEBOUNCE 0
30 #endif
31 static uint8_t debouncing = DEBOUNCE;
32
33 /* matrix state(1:on, 0:off) */
34 static matrix_row_t matrix[MATRIX_ROWS];
35 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
36
37 static uint8_t read_rows(void);
38 static uint8_t read_caps(void);
39 static void init_rows(void);
40 static void unselect_cols(void);
41 static void select_col(uint8_t col);
42
43 inline
44 uint8_t matrix_rows(void)
45 {
46 return MATRIX_ROWS;
47 }
48
49 inline
50 uint8_t matrix_cols(void)
51 {
52 return MATRIX_COLS;
53 }
54
55 void matrix_init(void)
56 {
57 unselect_cols();
58 init_rows();
59 // initialize matrix state: all keys off
60 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
61 matrix[i] = 0;
62 matrix_debouncing[i] = 0;
63 }
64 }
65
66 uint8_t matrix_scan(void)
67 {
68 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
69 select_col(col);
70 _delay_us(3); // TODO: Determine the correct value needed here.
71 uint8_t rows = read_rows();
72 // Use the otherwise unused col: 0 row: 3 for caps lock.
73 if(col == 0) {
74 rows |= read_caps();
75 }
76 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
77 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
78 bool curr_bit = rows & (1<<row);
79 if (prev_bit != curr_bit) {
80 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
81 if (debouncing) {
82 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
83 }
84 debouncing = DEBOUNCE;
85 }
86 }
87 unselect_cols();
88 }
89
90 if (debouncing) {
91 if (--debouncing) {
92 _delay_ms(1);
93 } else {
94 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
95 matrix[i] = matrix_debouncing[i];
96 }
97 }
98 }
99
100 return 1;
101 }
102
103 bool matrix_is_modified(void)
104 {
105 if (debouncing) return false;
106 return true;
107 }
108
109 inline
110 bool matrix_is_on(uint8_t row, uint8_t col)
111 {
112 return (matrix[row] & ((matrix_row_t)1<<col));
113 }
114
115 inline
116 matrix_row_t matrix_get_row(uint8_t row)
117 {
118 return matrix[row];
119 }
120
121 void matrix_print(void)
122 {
123 print("\nr/c 0123456789ABCDEF\n");
124 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
125 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
126 }
127 }
128
129 uint8_t matrix_key_count(void)
130 {
131 uint8_t count = 0;
132 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
133 count += bitpop32(matrix[i]);
134 }
135 return count;
136 }
137
138 /* Row pin configuration
139 * row: 0 1 2 3 4 5
140 * pin: D0 D1 D2 D3 D5 B7
141 *
142 * Caps lock uses its own pin PE2
143 */
144 static void init_rows(void)
145 {
146 // Input (DDR:0, PORT:0)
147 DDRD &= ~0b00101111;
148 PORTD &= ~0b00101111;
149 DDRB &= ~(1<<7);
150 PORTB &= ~(1<<7);
151
152 // Input with pull-up (DDR:0, PORT:1)
153 DDRE &= ~(1<<2);
154 PORTE |= (1<<2);
155 }
156
157 static uint8_t read_rows(void)
158 {
159 return (PIND&(1<<0) ? (1<<0) : 0) |
160 (PIND&(1<<1) ? (1<<1) : 0) |
161 (PIND&(1<<2) ? (1<<2) : 0) |
162 (PIND&(1<<3) ? (1<<3) : 0) |
163 (PIND&(1<<5) ? (1<<4) : 0) |
164 (PINB&(1<<7) ? (1<<5) : 0);
165 }
166
167 static uint8_t read_caps(void)
168 {
169 return PINE&(1<<2) ? 0 : (1<<3);
170 }
171
172 /* Columns 0 - 15
173 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
174 * col / pin: PC6 PB6 PF0 PF1 PC7
175 * 0: 1 0 0 0 0
176 * 1: 1 0 1 0 0
177 * 2: 1 0 0 1 0
178 * 3: 1 0 1 1 0
179 * 4: 1 0 0 0 1
180 * 5: 1 0 1 0 1
181 * 6: 1 0 0 1 1
182 * 7: 1 0 1 1 1
183 * 8: 0 1 0 0 0
184 * 9: 0 1 1 0 0
185 * 10: 0 1 0 1 0
186 * 11: 0 1 1 1 0
187 * 12: 0 1 0 0 1
188 * 13: 0 1 1 0 1
189 * 14: 0 1 0 1 1
190 * 15: 0 1 1 1 1
191 *
192 * col: 16
193 * pin: PB5
194 */
195 static void unselect_cols(void)
196 {
197 DDRB |= (1<<5) | (1<<6);
198 PORTB &= ~((1<<5) | (1<<6));
199
200 DDRC |= (1<<6) | (1<<7);
201 PORTC &= ~((1<<6) | (1<<7));
202
203 DDRF |= (1<<0) | (1<<1);
204 PORTF &= ~((1<<0) | (1<<1));
205 }
206
207 static void select_col(uint8_t col)
208 {
209 // Output high (DDR:1, PORT:1) to select
210 switch (col) {
211 case 0:
212 PORTC |= (1<<6);
213 break;
214 case 1:
215 PORTC |= (1<<6);
216 PORTF |= (1<<0);
217 break;
218 case 2:
219 PORTC |= (1<<6);
220 PORTF |= (1<<1);
221 break;
222 case 3:
223 PORTC |= (1<<6);
224 PORTF |= (1<<0) | (1<<1);
225 break;
226 case 4:
227 PORTC |= (1<<6);
228 PORTC |= (1<<7);
229 break;
230 case 5:
231 PORTC |= (1<<6);
232 PORTF |= (1<<0);
233 PORTC |= (1<<7);
234 break;
235 case 6:
236 PORTC |= (1<<6);
237 PORTF |= (1<<1);
238 PORTC |= (1<<7);
239 break;
240 case 7:
241 PORTC |= (1<<6);
242 PORTF |= (1<<0) | (1<<1);
243 PORTC |= (1<<7);
244 break;
245 case 8:
246 PORTB |= (1<<6);
247 break;
248 case 9:
249 PORTB |= (1<<6);
250 PORTF |= (1<<0);
251 break;
252 case 10:
253 PORTB |= (1<<6);
254 PORTF |= (1<<1);
255 break;
256 case 11:
257 PORTB |= (1<<6);
258 PORTF |= (1<<0) | (1<<1);
259 break;
260 case 12:
261 PORTB |= (1<<6);
262 PORTC |= (1<<7);
263 break;
264 case 13:
265 PORTB |= (1<<6);
266 PORTF |= (1<<0);
267 PORTC |= (1<<7);
268 break;
269 case 14:
270 PORTB |= (1<<6);
271 PORTF |= (1<<1);
272 PORTC |= (1<<7);
273 break;
274 case 15:
275 PORTB |= (1<<6);
276 PORTF |= (1<<0) | (1<<1);
277 PORTC |= (1<<7);
278 break;
279 case 16:
280 PORTB |= (1<<5);
281 break;
282 }
283 }
Imprint / Impressum