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.c272
1 files changed, 272 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..3c3240b9f
--- /dev/null
+++ b/keyboards/duck/orion/v3/matrix.c
@@ -0,0 +1,272 @@
1/*
2Copyright 2019 MechMerlin <mechmerlin@gmail.com>
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
126/* Row pin configuration - diode connected
127 * row: 0 1 2 3 4 5
128 * pin: PD0 PD1 PD2 PD3 PD5 PB7
129 *
130 * Backspace uses its own pin PE2 on the column pin, row pin is grounded
131 */
132static void init_rows(void) {
133 DDRD &= ~0b00101111;
134 PORTD &= ~0b00101111;
135
136 DDRB &= ~0b10000000;
137 PORTB &= ~0b10000000;
138
139 DDRE &= ~0b00000100;
140 PORTE |= 0b00000100;
141}
142
143static uint8_t read_rows(uint8_t col) {
144
145 return (PIND&(1<<0) ? (1<<0) : 0) |
146 (PIND&(1<<1) ? (1<<1) : 0) |
147 (PIND&(1<<2) ? (1<<2) : 0) |
148 (PIND&(1<<3) ? (1<<3) : 0) |
149 (PIND&(1<<5) ? (1<<4) : 0) |
150 (PINB&(1<<7) ? (1<<5) : 0) |
151 (col== 17 ? ((PINE&(1<<2) ? 0 : (1<<1))) : 0);
152
153}
154
155uint8_t read_fwkey(void)
156{
157 return PINE&(1<<2) ? 0 : (1<<2);
158}
159
160/* Columns 0 - 15
161 *
162 * atmega32u4 decoder pin
163 * PC6 U1 E3
164 * PB6 U2 E3
165 * PF0 U1, U2 A0
166 * PF1 U1, U2 A1
167 * PC7 U1, U2 A2
168 *
169 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
170 * col / pin: PC6 PB6 PF0 PF1 PC7 Decoder Pin
171 * 0: 1 0 0 0 0 U1 Y0
172 * 1: 1 0 1 0 0 U1 Y1
173 * 2: 1 0 0 1 0 U1 Y2
174 * 3: 1 0 1 1 0 U1 Y3
175 * 4: 1 0 0 0 1 U1 Y4
176 * 5: 1 0 1 0 1 U1 Y5
177 * 6: 1 0 0 1 1 U1 Y6
178 * 7: 1 0 1 1 1 U1 Y7
179 *
180 * 8: 0 1 0 0 0 U2 Y0
181 * 9: 0 1 1 0 0 U2 Y1
182 * 10: 0 1 0 1 0 U2 Y2
183 * 11: 0 1 1 1 0 U2 Y3
184 * 12: 0 1 0 0 1 U2 Y4
185 * 13: 0 1 1 0 1 U2 Y5
186 * 14: 0 1 0 1 1 U2 Y6
187 * 15: 0 1 1 1 1 U2 Y7
188 *
189 */
190static void unselect_cols(void) {
191 DDRB |= 0b01100000;
192 PORTB &= ~0b01100000;
193
194 DDRC |= 0b11000000;
195 PORTC &= ~0b11000000;
196
197 DDRF |= 0b00000011;
198 PORTF &= ~0b00000011;
199}
200
201static void select_col(uint8_t col) {
202
203 switch (col) {
204 case 0:
205 PORTC |= 0b01000000;
206 break;
207 case 1:
208 PORTC |= 0b01000000;
209 PORTF |= 0b00000001;
210 break;
211 case 2:
212 PORTC |= 0b01000000;
213 PORTF |= 0b00000010;
214 break;
215 case 3:
216 PORTC |= 0b01000000;
217 PORTF |= 0b00000011;
218 break;
219 case 4:
220 PORTC |= 0b11000000;
221 break;
222 case 5:
223 PORTC |= 0b11000000;
224 PORTF |= 0b00000001;
225 break;
226 case 6:
227 PORTC |= 0b11000000;
228 PORTF |= 0b00000010;
229 break;
230 case 7:
231 PORTC |= 0b11000000;
232 PORTF |= 0b00000011;
233 break;
234 case 8:
235 PORTB |= 0b01000000;
236 break;
237 case 9:
238 PORTB |= 0b01000000;
239 PORTF |= 0b00000001;
240 break;
241 case 10:
242 PORTB |= 0b01000000;
243 PORTF |= 0b00000010;
244 break;
245 case 11:
246 PORTB |= 0b01000000;
247 PORTF |= 0b00000011;
248 break;
249 case 12:
250 PORTB |= 0b01000000;
251 PORTC |= 0b10000000;
252 break;
253 case 13:
254 PORTB |= 0b01000000;
255 PORTF |= 0b00000001;
256 PORTC |= 0b10000000;
257 break;
258 case 14:
259 PORTB |= 0b01000000;
260 PORTF |= 0b00000010;
261 PORTC |= 0b10000000;
262 break;
263 case 15:
264 PORTB |= 0b01000000;
265 PORTF |= 0b00000011;
266 PORTC |= 0b10000000;
267 break;
268 case 16:
269 PORTB |= 0b00100000;
270 break;
271 }
272}