diff options
Diffstat (limited to 'keyboards/duck/eagle_viper/v2/matrix.c')
-rw-r--r-- | keyboards/duck/eagle_viper/v2/matrix.c | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/keyboards/duck/eagle_viper/v2/matrix.c b/keyboards/duck/eagle_viper/v2/matrix.c new file mode 100644 index 000000000..0964493ac --- /dev/null +++ b/keyboards/duck/eagle_viper/v2/matrix.c | |||
@@ -0,0 +1,258 @@ | |||
1 | /* | ||
2 | Copyright 2017 MechMerlin <[email protected]> | ||
3 | This program is free software: you can redistribute it and/or modify | ||
4 | it under the terms of the GNU General Public License as published by | ||
5 | the Free Software Foundation, either version 2 of the License, or | ||
6 | (at your option) any later version. | ||
7 | |||
8 | This program is distributed in the hope that it will be useful, | ||
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License | ||
14 | along 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 | |||
25 | static uint8_t debouncing = DEBOUNCE; | ||
26 | |||
27 | /* matrix state(1:on, 0:off) */ | ||
28 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
29 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
30 | |||
31 | static uint8_t read_rows(uint8_t col); | ||
32 | static void init_rows(void); | ||
33 | static void unselect_cols(void); | ||
34 | static void select_col(uint8_t col); | ||
35 | |||
36 | |||
37 | __attribute__ ((weak)) | ||
38 | void matrix_init_kb(void) { | ||
39 | matrix_init_user(); | ||
40 | } | ||
41 | |||
42 | __attribute__ ((weak)) | ||
43 | void matrix_scan_kb(void) { | ||
44 | matrix_scan_user(); | ||
45 | } | ||
46 | |||
47 | __attribute__ ((weak)) | ||
48 | void matrix_init_user(void) { | ||
49 | } | ||
50 | |||
51 | __attribute__ ((weak)) | ||
52 | void matrix_scan_user(void) { | ||
53 | } | ||
54 | |||
55 | void 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 | |||
67 | void 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 | |||
80 | uint8_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 | |||
115 | inline matrix_row_t matrix_get_row(uint8_t row) { | ||
116 | return matrix[row]; | ||
117 | } | ||
118 | |||
119 | void 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 | |||
126 | /* Row pin configuration - diode connected | ||
127 | * row: 0 1 2 3 4 | ||
128 | * pin: PD0 PD1 PD2 PD3 PD5 | ||
129 | * | ||
130 | * Caps Lock uses its own pin PE2 on the column pin, row pin is grounded | ||
131 | */ | ||
132 | static void init_rows(void) { | ||
133 | DDRD &= ~0b00101111; | ||
134 | PORTD &= ~0b00101111; | ||
135 | |||
136 | DDRE &= ~0b00000100; | ||
137 | PORTE |= 0b00000100; | ||
138 | } | ||
139 | |||
140 | static uint8_t read_rows(uint8_t col) { | ||
141 | |||
142 | return (PIND&(1<<0) ? (1<<0) : 0) | | ||
143 | (PIND&(1<<1) ? (1<<1) : 0) | | ||
144 | (PIND&(1<<2) ? (1<<2) : 0) | | ||
145 | (PIND&(1<<3) ? (1<<3) : 0) | | ||
146 | (PIND&(1<<5) ? (1<<4) : 0) | | ||
147 | (col==0 ? ((PINE&(1<<2) ? 0 : (1<<2))) : 0); | ||
148 | |||
149 | } | ||
150 | |||
151 | uint8_t read_fwkey(void) | ||
152 | { | ||
153 | return PINE&(1<<2) ? 0 : (1<<2); | ||
154 | } | ||
155 | |||
156 | /* Columns 0 - 15 | ||
157 | * | ||
158 | * atmega32u4 decoder pin | ||
159 | * PC6 U1 E3 | ||
160 | * PB6 U2 E3 | ||
161 | * PF0 U1, U2 A0 | ||
162 | * PF1 U1, U2 A1 | ||
163 | * PC7 U1, U2 A2 | ||
164 | * | ||
165 | * These columns uses two 74HC237D 3 to 8 bit demultiplexers. | ||
166 | * col / pin: PC6 PB6 PF0 PF1 PC7 Decoder Pin | ||
167 | * 0: 1 0 0 0 0 U1 Y0 | ||
168 | * 1: 1 0 1 0 0 U1 Y1 | ||
169 | * 2: 1 0 0 1 0 U1 Y2 | ||
170 | * 3: 1 0 1 1 0 U1 Y3 | ||
171 | * 4: 1 0 0 0 1 U1 Y4 | ||
172 | * 5: 1 0 1 0 1 U1 Y5 | ||
173 | * 6: 1 0 0 1 1 U1 Y6 | ||
174 | * 7: 1 0 1 1 1 U1 Y7 | ||
175 | * 8: 0 1 0 0 0 U2 Y0 | ||
176 | * 9: 0 1 1 0 0 U2 Y1 | ||
177 | * 10: 0 1 0 1 0 U2 Y2 | ||
178 | * 11: 0 1 1 1 0 U2 Y3 | ||
179 | * 12: 0 1 0 0 1 U2 Y4 | ||
180 | * 13: 0 1 1 0 1 U2 Y5 | ||
181 | * 14: 0 1 0 1 1 U2 Y6 | ||
182 | * | ||
183 | */ | ||
184 | static void unselect_cols(void) { | ||
185 | DDRB |= 0b01000000; | ||
186 | PORTB &= ~0b01000000; | ||
187 | |||
188 | DDRC |= 0b11000000; | ||
189 | PORTC &= ~0b11000000; | ||
190 | |||
191 | DDRF |= 0b00000011; | ||
192 | PORTF &= ~0b00000011; | ||
193 | } | ||
194 | |||
195 | static void select_col(uint8_t col) { | ||
196 | |||
197 | switch (col) { | ||
198 | case 0: | ||
199 | PORTC |= 0b01000000; | ||
200 | break; | ||
201 | case 1: | ||
202 | PORTC |= 0b01000000; | ||
203 | PORTF |= 0b00000001; | ||
204 | break; | ||
205 | case 2: | ||
206 | PORTC |= 0b01000000; | ||
207 | PORTF |= 0b00000010; | ||
208 | break; | ||
209 | case 3: | ||
210 | PORTC |= 0b01000000; | ||
211 | PORTF |= 0b00000011; | ||
212 | break; | ||
213 | case 4: | ||
214 | PORTC |= 0b11000000; | ||
215 | break; | ||
216 | case 5: | ||
217 | PORTC |= 0b11000000; | ||
218 | PORTF |= 0b00000001; | ||
219 | break; | ||
220 | case 6: | ||
221 | PORTC |= 0b11000000; | ||
222 | PORTF |= 0b00000010; | ||
223 | break; | ||
224 | case 7: | ||
225 | PORTC |= 0b11000000; | ||
226 | PORTF |= 0b00000011; | ||
227 | break; | ||
228 | case 8: | ||
229 | PORTB |= 0b01000000; | ||
230 | break; | ||
231 | case 9: | ||
232 | PORTB |= 0b01000000; | ||
233 | PORTF |= 0b00000001; | ||
234 | break; | ||
235 | case 10: | ||
236 | PORTB |= 0b01000000; | ||
237 | PORTF |= 0b00000010; | ||
238 | break; | ||
239 | case 11: | ||
240 | PORTB |= 0b01000000; | ||
241 | PORTF |= 0b00000011; | ||
242 | break; | ||
243 | case 12: | ||
244 | PORTB |= 0b01000000; | ||
245 | PORTC |= 0b10000000; | ||
246 | break; | ||
247 | case 13: | ||
248 | PORTB |= 0b01000000; | ||
249 | PORTF |= 0b00000001; | ||
250 | PORTC |= 0b10000000; | ||
251 | break; | ||
252 | case 14: | ||
253 | PORTB |= 0b01000000; | ||
254 | PORTF |= 0b00000010; | ||
255 | PORTC |= 0b10000000; | ||
256 | break; | ||
257 | } | ||
258 | } | ||