aboutsummaryrefslogtreecommitdiff
path: root/keyboards/duck/octagon/v2/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/duck/octagon/v2/matrix.c')
-rw-r--r--keyboards/duck/octagon/v2/matrix.c261
1 files changed, 261 insertions, 0 deletions
diff --git a/keyboards/duck/octagon/v2/matrix.c b/keyboards/duck/octagon/v2/matrix.c
new file mode 100644
index 000000000..25d1e45b0
--- /dev/null
+++ b/keyboards/duck/octagon/v2/matrix.c
@@ -0,0 +1,261 @@
1/*
2Copyright 2017 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
126/* Row pin configuration
127 * row: 0 1 2 3 4 5
128 * pin: PB7 PD0 PD1 PD2 PD3 PD5
129 *
130 * Esc uses its own pin PE2
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 if (col == 16) {
145 return PINE&(1<<2) ? 0 : (1<<0);
146 } else {
147 return (PIND&(1<<0) ? (1<<0) : 0) |
148 (PIND&(1<<1) ? (1<<1) : 0) |
149 (PIND&(1<<2) ? (1<<2) : 0) |
150 (PIND&(1<<3) ? (1<<3) : 0) |
151 (PIND&(1<<5) ? (1<<4) : 0) |
152 (PINB&(1<<7) ? (1<<5) : 0);
153 }
154}
155
156uint8_t read_fwkey(void)
157{
158 return PINE&(1<<2) ? 0 : (1<<0);
159}
160
161/* Columns 0 - 15
162 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
163 * col / pin: PC6 PB6 PF0 PF1 PC7
164 * 0: 1 0 0 0 0
165 * 1: 1 0 1 0 0
166 * 2: 1 0 0 1 0
167 * 3: 1 0 1 1 0
168 * 4: 1 0 0 0 1
169 * 5: 1 0 1 0 1
170 * 6: 1 0 0 1 1
171 * 7: 1 0 1 1 1
172 * 8: 0 1 0 0 0
173 * 9: 0 1 1 0 0
174 * 10: 0 1 0 1 0
175 * 11: 0 1 1 1 0
176 * 12: 0 1 0 0 1
177 * 13: 0 1 1 0 1
178 * 14: 0 1 0 1 1
179 * 15: 0 1 1 1 1
180 *
181 */
182static void unselect_cols(void) {
183 DDRB |= 0b01000000;
184 PORTB &= ~0b01000000;
185
186 DDRC |= 0b11000000;
187 PORTC &= ~0b11000000;
188
189 DDRF |= 0b00000011;
190 PORTF &= ~0b00000011;
191}
192
193static void select_col(uint8_t col) {
194
195 switch (col) {
196 case 0:
197 PORTC |= 0b01000000;
198 break;
199 case 1:
200 PORTC |= 0b01000000;
201 PORTF |= 0b00000001;
202 break;
203 case 2:
204 PORTC |= 0b01000000;
205 PORTF |= 0b00000010;
206 break;
207 case 3:
208 PORTC |= 0b01000000;
209 PORTF |= 0b00000011;
210 break;
211 case 4:
212 PORTC |= 0b11000000;
213 break;
214 case 5:
215 PORTC |= 0b11000000;
216 PORTF |= 0b00000001;
217 break;
218 case 6:
219 PORTC |= 0b11000000;
220 PORTF |= 0b00000010;
221 break;
222 case 7:
223 PORTC |= 0b11000000;
224 PORTF |= 0b00000011;
225 break;
226 case 8:
227 PORTB |= 0b01000000;
228 break;
229 case 9:
230 PORTB |= 0b01000000;
231 PORTF |= 0b00000001;
232 break;
233 case 10:
234 PORTB |= 0b01000000;
235 PORTF |= 0b00000010;
236 break;
237 case 11:
238 PORTB |= 0b01000000;
239 PORTF |= 0b00000011;
240 break;
241 case 12:
242 PORTB |= 0b01000000;
243 PORTC |= 0b10000000;
244 break;
245 case 13:
246 PORTB |= 0b01000000;
247 PORTF |= 0b00000001;
248 PORTC |= 0b10000000;
249 break;
250 case 14:
251 PORTB |= 0b01000000;
252 PORTF |= 0b00000010;
253 PORTC |= 0b10000000;
254 break;
255 case 15:
256 PORTB |= 0b01000000;
257 PORTF |= 0b00000011;
258 PORTC |= 0b10000000;
259 break;
260 }
261}