aboutsummaryrefslogtreecommitdiff
path: root/keyboards/duck/tcv3/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/duck/tcv3/matrix.c')
-rw-r--r--keyboards/duck/tcv3/matrix.c280
1 files changed, 280 insertions, 0 deletions
diff --git a/keyboards/duck/tcv3/matrix.c b/keyboards/duck/tcv3/matrix.c
new file mode 100644
index 000000000..d2d90470a
--- /dev/null
+++ b/keyboards/duck/tcv3/matrix.c
@@ -0,0 +1,280 @@
1/*
2Copyright 2019 MechMerlin <[email protected]>
3This program is free software: you can redistribute it and/or modify
4it under the terms of the GNU General Public License as published by
5the Free Software Foundation, either version 2 of the License, or
6(at your option) any later version.
7
8This program is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License
14along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17#include <util/delay.h>
18#include <avr/io.h>
19#include <stdio.h>
20#include "matrix.h"
21#include "util.h"
22#include "print.h"
23#include "debug.h"
24
25static uint8_t debouncing = DEBOUNCE;
26
27/* matrix state(1:on, 0:off) */
28static matrix_row_t matrix[MATRIX_ROWS];
29static matrix_row_t matrix_debouncing[MATRIX_ROWS];
30
31static uint8_t read_rows(uint8_t col);
32static void init_rows(void);
33static void unselect_cols(void);
34static void select_col(uint8_t col);
35
36
37__attribute__ ((weak))
38void matrix_init_kb(void) {
39 matrix_init_user();
40}
41
42__attribute__ ((weak))
43void matrix_scan_kb(void) {
44 matrix_scan_user();
45}
46
47__attribute__ ((weak))
48void matrix_init_user(void) {
49}
50
51__attribute__ ((weak))
52void matrix_scan_user(void) {
53}
54
55void backlight_init_ports(void)
56{
57 // DDRD |= 0b11010000;
58 // PORTD &= ~0b01010000;
59 // PORTD |= 0b10000000;
60 // DDRB |= 0b00011111;
61 // PORTB &= ~0b00001110;
62 // PORTB |= 0b00010001;
63 // DDRE |= 0b01000000;
64 // PORTE &= ~0b01000000;
65}
66
67void matrix_init(void) {
68 backlight_init_ports();
69 unselect_cols();
70 init_rows();
71
72 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
73 matrix[i] = 0;
74 matrix_debouncing[i] = 0;
75 }
76
77 matrix_init_quantum();
78}
79
80uint8_t matrix_scan(void) {
81 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
82 select_col(col);
83 _delay_us(3);
84
85 uint8_t rows = read_rows(col);
86
87 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
88 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
89 bool curr_bit = rows & (1<<row);
90 if (prev_bit != curr_bit) {
91 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
92 if (debouncing) {
93 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
94 }
95 debouncing = DEBOUNCE;
96 }
97 }
98 unselect_cols();
99 }
100
101 if (debouncing) {
102 if (--debouncing) {
103 _delay_ms(1);
104 } else {
105 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
106 matrix[i] = matrix_debouncing[i];
107 }
108 }
109 }
110
111 matrix_scan_quantum();
112 return 1;
113}
114
115inline matrix_row_t matrix_get_row(uint8_t row) {
116 return matrix[row];
117}
118
119void matrix_print(void) {
120 print("\nr/c 0123456789ABCDEF\n");
121 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
122 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
123 }
124}
125/* Row pin configuration
126 * row: 0 1 2 3 4 5
127 * pin: PB7 PD0 PD1 PD2 PD3 PD5
128 *
129 * Reset Key uses its own pin PE2
130 */
131static void init_rows(void) {
132 DDRD &= ~0b00101111;
133 PORTD &= ~0b00101111;
134
135 DDRB &= ~0b10000000;
136 PORTB &= ~0b10000000;
137
138 DDRE &= ~0b00000100;
139 PORTE |= 0b00000100;
140}
141
142static uint8_t read_rows(uint8_t col) {
143 if (col == 19) {
144 return PINE&(1<<2) ? 0 : (1<<0);
145 } else {
146 return (PIND&(1<<0) ? (1<<1) : 0) |
147 (PIND&(1<<1) ? (1<<2) : 0) |
148 (PIND&(1<<2) ? (1<<3) : 0) |
149 (PIND&(1<<3) ? (1<<4) : 0) |
150 (PIND&(1<<5) ? (1<<5) : 0) |
151 (PINB&(1<<7) ? (1<<0) : 0);
152 }
153}
154
155
156/* Columns 0 - G
157 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
158 *
159 * atmega32u4 decoder pin
160 * C6 U1 E2
161 * B6 U2 E2
162 * F0 U1, U2 A0
163 * F1 U1, U2 A1
164 * C7 U1, U2 A2
165 *
166 * col 0: B4
167 * col 1: D7
168 * col I: B5
169 *
170 * col / pin: PC6 PB6 PF0 PF1 PC7 Decoder Pin
171 * 2: 1 0 0 0 0 U1 Y0
172 * 3: 1 0 1 0 0 U1 Y1
173 * 4: 1 0 0 1 0 U1 Y2
174 * 5: 1 0 1 1 0 U1 Y3
175 * 6: 1 0 0 0 1 U1 Y4
176 * 7: 1 0 1 0 1 U1 Y5
177 * 8: 1 0 0 1 1 U1 Y6
178 * 9: 1 0 1 1 1 U1 Y7
179 * A: 0 1 0 0 0 U2 Y0
180 * B: 0 1 1 0 0 U2 Y1
181 * C: 0 1 0 1 0 U2 Y2
182 * D: 0 1 1 1 0 U2 Y3
183 * E: 0 1 0 0 1 U2 Y4
184 * F: 0 1 1 0 1 U2 Y5
185 * G: 0 1 0 1 1 U2 Y6
186 * H: 0 1 1 1 1 U2 Y7
187 *
188 */
189static void unselect_cols(void) {
190 DDRB |= 0b01110000;
191 PORTB &= ~0b01110000;
192
193 DDRC |= 0b11000000;
194 PORTC &= ~0b11000000;
195
196 DDRD |= 0b10000000;
197 PORTD &= ~0b10000000;
198
199 DDRF |= 0b00000011;
200 PORTF &= ~0b00000011;
201}
202
203static void select_col(uint8_t col) {
204
205 switch (col) {
206 case 0:
207 PORTB |= 0b00010000;
208 break;
209 case 1:
210 PORTD |= 0b10000000;
211 break;
212 case 2: // U1 Y0
213 PORTC |= 0b01000000;
214 break;
215 case 3: // U1 Y1
216 PORTC |= 0b01000000;
217 PORTF |= 0b00000001;
218 break;
219 case 4: // U1 Y2
220 PORTC |= 0b01000000;
221 PORTF |= 0b00000010;
222 break;
223 case 5: // U1 Y3
224 PORTC |= 0b01000000;
225 PORTF |= 0b00000011;
226 break;
227 case 6: // U1 Y4
228 PORTC |= 0b11000000;
229 break;
230 case 7: // U1 Y5
231 PORTC |= 0b11000000;
232 PORTF |= 0b00000001;
233 break;
234 case 8: // U1 Y6
235 PORTC |= 0b11000000;
236 PORTF |= 0b00000010;
237 break;
238 case 9: // U1 Y7
239 PORTC |= 0b11000000;
240 PORTF |= 0b00000011;
241 break;
242 case 10: // U2 Y0
243 PORTB |= 0b01000000;
244 break;
245 case 11: // U2 Y1
246 PORTB |= 0b01000000;
247 PORTF |= 0b00000001;
248 break;
249 case 12: // U2 Y2
250 PORTB |= 0b01000000;
251 PORTF |= 0b00000010;
252 break;
253 case 13: // U2 Y3
254 PORTB |= 0b01000000;
255 PORTF |= 0b00000011;
256 break;
257 case 14: // U2 Y4
258 PORTB |= 0b01000000;
259 PORTC |= 0b10000000;
260 break;
261 case 15: // U2 Y5
262 PORTB |= 0b01000000;
263 PORTC |= 0b10000000;
264 PORTF |= 0b00000001;
265 break;
266 case 16: // U2 Y6
267 PORTB |= 0b01000000;
268 PORTC |= 0b10000000;
269 PORTF |= 0b00000010;
270 break;
271 case 17: // U2 Y7
272 PORTB |= 0b01000000;
273 PORTC |= 0b10000000;
274 PORTF |= 0b00000011;
275 break;
276 case 18:
277 PORTB |= 0b00100000;
278 break;
279 }
280} \ No newline at end of file