aboutsummaryrefslogtreecommitdiff
path: root/keyboards/duck/eagle_viper/v2/matrix.c
diff options
context:
space:
mode:
authorMechMerlin <30334081+mechmerlin@users.noreply.github.com>2018-08-28 16:52:30 -0700
committerDrashna Jaelre <drashna@live.com>2018-08-28 16:52:30 -0700
commitb1935c5e0dcd1adf32df652c65deffad35cd7405 (patch)
treee3f814bae67a65d33bccf1ce187bf4b4104edf1f /keyboards/duck/eagle_viper/v2/matrix.c
parent3916b06168f39233f77a252fbf5f9cfdae3df4fe (diff)
downloadqmk_firmware-b1935c5e0dcd1adf32df652c65deffad35cd7405.tar.gz
qmk_firmware-b1935c5e0dcd1adf32df652c65deffad35cd7405.zip
Putting my ducks in a row: Eagle/Viper V2 into Duck directory (#3766)
* mv eagle_viper into duck * delete files * remove eagle_viper .c and .h
Diffstat (limited to 'keyboards/duck/eagle_viper/v2/matrix.c')
-rw-r--r--keyboards/duck/eagle_viper/v2/matrix.c257
1 files changed, 257 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..7003a7ae0
--- /dev/null
+++ b/keyboards/duck/eagle_viper/v2/matrix.c
@@ -0,0 +1,257 @@
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
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 debouncing = DEBOUNCING_DELAY;
93 }
94 }
95 unselect_cols();
96 }
97
98 if (debouncing) {
99 if (--debouncing) {
100 _delay_ms(1);
101 } else {
102 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
103 matrix[i] = matrix_debouncing[i];
104 }
105 }
106 }
107
108 matrix_scan_quantum();
109 return 1;
110}
111
112inline matrix_row_t matrix_get_row(uint8_t row) {
113 return matrix[row];
114}
115
116void matrix_print(void) {
117 print("\nr/c 0123456789ABCDEF\n");
118 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
119 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
120 }
121}
122
123/* Row pin configuration
124 * row: 0 1 2 3 4 5
125 * pin: PB7 PD0 PD1 PD2 PD3 PD5
126 *
127 * Esc uses its own pin PE2
128 */
129static void init_rows(void) {
130 DDRD &= ~0b00101111;
131 PORTD &= ~0b00101111;
132
133 DDRB &= ~0b10000000;
134 PORTB &= ~0b10000000;
135
136 DDRE &= ~0b00000100;
137 PORTE |= 0b00000100;
138}
139
140static 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 (PINB&(1<<7) ? (1<<5) : 0) |
148 (col==0 ? ((PINE&(1<<2) ? 0 : (1<<2))) : 0);
149
150}
151
152uint8_t read_fwkey(void)
153{
154 return PINE&(1<<2) ? 0 : (1<<2);
155}
156
157/* Columns 0 - 15
158 * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
159 * col / pin: PC6 PB6 PF0 PF1 PC7
160 * 0: 1 0 0 0 0
161 * 1: 1 0 1 0 0
162 * 2: 1 0 0 1 0
163 * 3: 1 0 1 1 0
164 * 4: 1 0 0 0 1
165 * 5: 1 0 1 0 1
166 * 6: 1 0 0 1 1
167 * 7: 1 0 1 1 1
168 * 8: 0 1 0 0 0
169 * 9: 0 1 1 0 0
170 * 10: 0 1 0 1 0
171 * 11: 0 1 1 1 0
172 * 12: 0 1 0 0 1
173 * 13: 0 1 1 0 1
174 * 14: 0 1 0 1 1
175 * 15: 0 1 1 1 1
176 *
177 */
178static void unselect_cols(void) {
179 DDRB |= 0b01000000;
180 PORTB &= ~0b01000000;
181
182 DDRC |= 0b11000000;
183 PORTC &= ~0b11000000;
184
185 DDRF |= 0b00000011;
186 PORTF &= ~0b00000011;
187}
188
189static void select_col(uint8_t col) {
190
191 switch (col) {
192 case 0:
193 PORTC |= 0b01000000;
194 break;
195 case 1:
196 PORTC |= 0b01000000;
197 PORTF |= 0b00000001;
198 break;
199 case 2:
200 PORTC |= 0b01000000;
201 PORTF |= 0b00000010;
202 break;
203 case 3:
204 PORTC |= 0b01000000;
205 PORTF |= 0b00000011;
206 break;
207 case 4:
208 PORTC |= 0b11000000;
209 break;
210 case 5:
211 PORTC |= 0b11000000;
212 PORTF |= 0b00000001;
213 break;
214 case 6:
215 PORTC |= 0b11000000;
216 PORTF |= 0b00000010;
217 break;
218 case 7:
219 PORTC |= 0b11000000;
220 PORTF |= 0b00000011;
221 break;
222 case 8:
223 PORTB |= 0b01000000;
224 break;
225 case 9:
226 PORTB |= 0b01000000;
227 PORTF |= 0b00000001;
228 break;
229 case 10:
230 PORTB |= 0b01000000;
231 PORTF |= 0b00000010;
232 break;
233 case 11:
234 PORTB |= 0b01000000;
235 PORTF |= 0b00000011;
236 break;
237 case 12:
238 PORTB |= 0b01000000;
239 PORTC |= 0b10000000;
240 break;
241 case 13:
242 PORTB |= 0b01000000;
243 PORTF |= 0b00000001;
244 PORTC |= 0b10000000;
245 break;
246 case 14:
247 PORTB |= 0b01000000;
248 PORTF |= 0b00000010;
249 PORTC |= 0b10000000;
250 break;
251 case 15:
252 PORTB |= 0b01000000;
253 PORTF |= 0b00000011;
254 PORTC |= 0b10000000;
255 break;
256 }
257}