]> git.gir.st - tmk_keyboard.git/blob - keyboard/lightsaber/matrix.c
e3eca84df8d057764309905ba8087ee46f51bc9f
[tmk_keyboard.git] / keyboard / lightsaber / matrix.c
1 /*
2 Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
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_fwkey(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-17
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: 12 row: 3 for firmware.
73 if(col == 12) {
74 rows |= read_fwkey();
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: PD0 PD1 PD2 PD3 PD5 PB7
141 *
142 * Firmware 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_fwkey(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 * col: 17
196 * pin: PD4
197 */
198 static void unselect_cols(void)
199 {
200 DDRB |= (1<<5) | (1<<6);
201 PORTB &= ~((1<<5) | (1<<6));
202
203 DDRC |= (1<<6) | (1<<7);
204 PORTC &= ~((1<<6) | (1<<7));
205
206 DDRD |= (1<<4);
207 PORTD &= ~(1<<4);
208
209 DDRF |= (1<<0) | (1<<1);
210 PORTF &= ~((1<<0) | (1<<1));
211 }
212
213 static void select_col(uint8_t col)
214 {
215 // Output high (DDR:1, PORT:1) to select
216 switch (col) {
217 case 0:
218 PORTC |= (1<<6);
219 break;
220 case 1:
221 PORTC |= (1<<6);
222 PORTF |= (1<<0);
223 break;
224 case 2:
225 PORTC |= (1<<6);
226 PORTF |= (1<<1);
227 break;
228 case 3:
229 PORTC |= (1<<6);
230 PORTF |= (1<<0) | (1<<1);
231 break;
232 case 4:
233 PORTC |= (1<<6);
234 PORTC |= (1<<7);
235 break;
236 case 5:
237 PORTC |= (1<<6);
238 PORTF |= (1<<0);
239 PORTC |= (1<<7);
240 break;
241 case 6:
242 PORTC |= (1<<6);
243 PORTF |= (1<<1);
244 PORTC |= (1<<7);
245 break;
246 case 7:
247 PORTC |= (1<<6);
248 PORTF |= (1<<0) | (1<<1);
249 PORTC |= (1<<7);
250 break;
251 case 8:
252 PORTB |= (1<<6);
253 break;
254 case 9:
255 PORTB |= (1<<6);
256 PORTF |= (1<<0);
257 break;
258 case 10:
259 PORTB |= (1<<6);
260 PORTF |= (1<<1);
261 break;
262 case 11:
263 PORTB |= (1<<6);
264 PORTF |= (1<<0) | (1<<1);
265 break;
266 case 12:
267 PORTB |= (1<<6);
268 PORTC |= (1<<7);
269 break;
270 case 13:
271 PORTB |= (1<<6);
272 PORTF |= (1<<0);
273 PORTC |= (1<<7);
274 break;
275 case 14:
276 PORTB |= (1<<6);
277 PORTF |= (1<<1);
278 PORTC |= (1<<7);
279 break;
280 case 15:
281 PORTB |= (1<<6);
282 PORTF |= (1<<0) | (1<<1);
283 PORTC |= (1<<7);
284 break;
285 case 16:
286 PORTB |= (1<<5);
287 break;
288 case 17:
289 PORTD |= (1<<4);
290 break;
291 }
292 }
Imprint / Impressum