[Keyboard] Small Refactor of Duck boards (#5521)
[jackhill/qmk/firmware.git] / keyboards / duck / eagle_viper / v2 / matrix.c
CommitLineData
cbc5de67
M
1/*
2Copyright 2017 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 = DEBOUNCING_DELAY;
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
cbc5de67
M
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);
cb2f2fd2
M
92 if (debouncing) {
93 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
94 }
cbc5de67
M
95 debouncing = DEBOUNCING_DELAY;
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
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==0 ? ((PINE&(1<<2) ? 0 : (1<<2))) : 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 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
162 * col / pin: PC6 PB6 PF0 PF1 PC7
163 * 0: 1 0 0 0 0
164 * 1: 1 0 1 0 0
165 * 2: 1 0 0 1 0
166 * 3: 1 0 1 1 0
167 * 4: 1 0 0 0 1
168 * 5: 1 0 1 0 1
169 * 6: 1 0 0 1 1
170 * 7: 1 0 1 1 1
171 * 8: 0 1 0 0 0
172 * 9: 0 1 1 0 0
173 * 10: 0 1 0 1 0
174 * 11: 0 1 1 1 0
175 * 12: 0 1 0 0 1
176 * 13: 0 1 1 0 1
177 * 14: 0 1 0 1 1
178 * 15: 0 1 1 1 1
179 *
180 */
181static void unselect_cols(void) {
182 DDRB |= 0b01000000;
183 PORTB &= ~0b01000000;
184
185 DDRC |= 0b11000000;
186 PORTC &= ~0b11000000;
187
188 DDRF |= 0b00000011;
189 PORTF &= ~0b00000011;
190}
191
192static void select_col(uint8_t col) {
193
194 switch (col) {
195 case 0:
196 PORTC |= 0b01000000;
197 break;
198 case 1:
199 PORTC |= 0b01000000;
200 PORTF |= 0b00000001;
201 break;
202 case 2:
203 PORTC |= 0b01000000;
204 PORTF |= 0b00000010;
205 break;
206 case 3:
207 PORTC |= 0b01000000;
208 PORTF |= 0b00000011;
209 break;
210 case 4:
211 PORTC |= 0b11000000;
212 break;
213 case 5:
214 PORTC |= 0b11000000;
215 PORTF |= 0b00000001;
216 break;
217 case 6:
218 PORTC |= 0b11000000;
219 PORTF |= 0b00000010;
220 break;
221 case 7:
222 PORTC |= 0b11000000;
223 PORTF |= 0b00000011;
224 break;
225 case 8:
226 PORTB |= 0b01000000;
227 break;
228 case 9:
229 PORTB |= 0b01000000;
230 PORTF |= 0b00000001;
231 break;
232 case 10:
233 PORTB |= 0b01000000;
234 PORTF |= 0b00000010;
235 break;
236 case 11:
237 PORTB |= 0b01000000;
238 PORTF |= 0b00000011;
239 break;
240 case 12:
241 PORTB |= 0b01000000;
242 PORTC |= 0b10000000;
243 break;
244 case 13:
245 PORTB |= 0b01000000;
246 PORTF |= 0b00000001;
247 PORTC |= 0b10000000;
248 break;
249 case 14:
250 PORTB |= 0b01000000;
251 PORTF |= 0b00000010;
252 PORTC |= 0b10000000;
253 break;
254 case 15:
255 PORTB |= 0b01000000;
256 PORTF |= 0b00000011;
257 PORTC |= 0b10000000;
258 break;
259 }
260}