aboutsummaryrefslogtreecommitdiff
path: root/keyboards/duck/orion/v3/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/duck/orion/v3/matrix.c')
-rw-r--r--keyboards/duck/orion/v3/matrix.c266
1 files changed, 266 insertions, 0 deletions
diff --git a/keyboards/duck/orion/v3/matrix.c b/keyboards/duck/orion/v3/matrix.c
new file mode 100644
index 000000000..84673bd44
--- /dev/null
+++ b/keyboards/duck/orion/v3/matrix.c
@@ -0,0 +1,266 @@
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 "quantum.h"
18
19static uint8_t debouncing = DEBOUNCE;
20
21/* matrix state(1:on, 0:off) */
22static matrix_row_t matrix[MATRIX_ROWS];
23static matrix_row_t matrix_debouncing[MATRIX_ROWS];
24
25static uint8_t read_rows(uint8_t col);
26static void init_rows(void);
27static void unselect_cols(void);
28static void select_col(uint8_t col);
29
30
31__attribute__ ((weak))
32void matrix_init_kb(void) {
33 matrix_init_user();
34}
35
36__attribute__ ((weak))
37void matrix_scan_kb(void) {
38 matrix_scan_user();
39}
40
41__attribute__ ((weak))
42void matrix_init_user(void) {
43}
44
45__attribute__ ((weak))
46void matrix_scan_user(void) {
47}
48
49void indicator_init_ports(void) {
50
51 // Num LED
52 setPinOutput(B4);
53
54 // Caps Lock
55 setPinOutput(B0);
56
57 // Scroll Lock
58 setPinOutput(D7);
59}
60
61void matrix_init(void) {
62 indicator_init_ports();
63 unselect_cols();
64 init_rows();
65
66 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
67 matrix[i] = 0;
68 matrix_debouncing[i] = 0;
69 }
70
71 matrix_init_quantum();
72}
73
74uint8_t matrix_scan(void) {
75 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
76 select_col(col);
77 _delay_us(3);
78
79 uint8_t rows = read_rows(col);
80
81 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
82 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
83 bool curr_bit = rows & (1<<row);
84 if (prev_bit != curr_bit) {
85 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
86 if (debouncing) {
87 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
88 }
89 debouncing = DEBOUNCE;
90 }
91 }
92 unselect_cols();
93 }
94
95 if (debouncing) {
96 if (--debouncing) {
97 _delay_ms(1);
98 } else {
99 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
100 matrix[i] = matrix_debouncing[i];
101 }
102 }
103 }
104
105 matrix_scan_quantum();
106 return 1;
107}
108
109inline matrix_row_t matrix_get_row(uint8_t row) {
110 return matrix[row];
111}
112
113void matrix_print(void) {
114 print("\nr/c 0123456789ABCDEF\n");
115 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
116 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
117 }
118}
119
120/* Row pin configuration - diode connected
121 * row: 0 1 2 3 4 5
122 * pin: PD0 PD1 PD2 PD3 PD5 PB7
123 *
124 * Backspace uses its own pin PE2 on the column pin, row pin is grounded
125 */
126static void init_rows(void) {
127 DDRD &= ~0b00101111;
128 PORTD &= ~0b00101111;
129
130 DDRB &= ~0b10000000;
131 PORTB &= ~0b10000000;
132
133 DDRE &= ~0b00000100;
134 PORTE |= 0b00000100;
135}
136
137static uint8_t read_rows(uint8_t col) {
138
139 return (PIND&(1<<0) ? (1<<0) : 0) |
140 (PIND&(1<<1) ? (1<<1) : 0) |
141 (PIND&(1<<2) ? (1<<2) : 0) |
142 (PIND&(1<<3) ? (1<<3) : 0) |
143 (PIND&(1<<5) ? (1<<4) : 0) |
144 (PINB&(1<<7) ? (1<<5) : 0) |
145 (col== 17 ? ((PINE&(1<<2) ? 0 : (1<<1))) : 0);
146
147}
148
149uint8_t read_fwkey(void)
150{
151 return PINE&(1<<2) ? 0 : (1<<2);
152}
153
154/* Columns 0 - 15
155 *
156 * atmega32u4 decoder pin
157 * PC6 U1 E3
158 * PB6 U2 E3
159 * PF0 U1, U2 A0
160 * PF1 U1, U2 A1
161 * PC7 U1, U2 A2
162 *
163 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
164 * col / pin: PC6 PB6 PF0 PF1 PC7 Decoder Pin
165 * 0: 1 0 0 0 0 U1 Y0
166 * 1: 1 0 1 0 0 U1 Y1
167 * 2: 1 0 0 1 0 U1 Y2
168 * 3: 1 0 1 1 0 U1 Y3
169 * 4: 1 0 0 0 1 U1 Y4
170 * 5: 1 0 1 0 1 U1 Y5
171 * 6: 1 0 0 1 1 U1 Y6
172 * 7: 1 0 1 1 1 U1 Y7
173 *
174 * 8: 0 1 0 0 0 U2 Y0
175 * 9: 0 1 1 0 0 U2 Y1
176 * 10: 0 1 0 1 0 U2 Y2
177 * 11: 0 1 1 1 0 U2 Y3
178 * 12: 0 1 0 0 1 U2 Y4
179 * 13: 0 1 1 0 1 U2 Y5
180 * 14: 0 1 0 1 1 U2 Y6
181 * 15: 0 1 1 1 1 U2 Y7
182 *
183 */
184static void unselect_cols(void) {
185 DDRB |= 0b01100000;
186 PORTB &= ~0b01100000;
187
188 DDRC |= 0b11000000;
189 PORTC &= ~0b11000000;
190
191 DDRF |= 0b00000011;
192 PORTF &= ~0b00000011;
193}
194
195static 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 case 15:
258 PORTB |= 0b01000000;
259 PORTF |= 0b00000011;
260 PORTC |= 0b10000000;
261 break;
262 case 16:
263 PORTB |= 0b00100000;
264 break;
265 }
266}