]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/eagle_viper/v2/matrix.c
Duck Eagle/Viper V2 support (#2216)
[qmk_firmware.git] / keyboards / eagle_viper / v2 / matrix.c
1 /*
2 Copyright 2017 MechMerlin <mechmerlin@gmail.com>
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 2 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along 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
25 static uint8_t debouncing = DEBOUNCING_DELAY;
26
27 /* matrix state(1:on, 0:off) */
28 static matrix_row_t matrix[MATRIX_ROWS];
29 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
30
31 static uint8_t read_rows(uint8_t col);
32 static void init_rows(void);
33 static void unselect_cols(void);
34 static void select_col(uint8_t col);
35
36 __attribute__ ((weak))
37 void matrix_init_quantum(void) {
38     matrix_init_kb();
39 }
40
41 __attribute__ ((weak))
42 void matrix_scan_quantum(void) {
43     matrix_scan_kb();
44 }
45
46 __attribute__ ((weak))
47 void matrix_init_kb(void) {
48     matrix_init_user();
49 }
50
51 __attribute__ ((weak))
52 void matrix_scan_kb(void) {
53     matrix_scan_user();
54 }
55
56 __attribute__ ((weak))
57 void matrix_init_user(void) {
58 }
59
60 __attribute__ ((weak))
61 void matrix_scan_user(void) {
62 }
63
64 void backlight_init_ports(void)
65 {
66   DDRD  |=  0b11010000;
67   PORTD &= ~0b01010000;
68   PORTD |=  0b10000000;
69   DDRB  |=  0b00011111;
70   PORTB &= ~0b00001110;
71   PORTB |=  0b00010001;
72   DDRE  |=  0b01000000;
73   PORTE &= ~0b01000000;
74 }
75
76 void matrix_init(void) {
77   backlight_init_ports();
78   unselect_cols();
79   init_rows();
80
81   for (uint8_t i=0; i < MATRIX_ROWS; i++)  {
82     matrix[i] = 0;
83     matrix_debouncing[i] = 0;
84   }
85
86   matrix_init_quantum();
87 }
88
89 uint8_t matrix_scan(void) {
90   for (uint8_t col = 0; col < MATRIX_COLS; col++) {
91     select_col(col);
92     _delay_us(3);
93
94     uint8_t rows = read_rows(col);
95
96     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
97       bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
98       bool curr_bit = rows & (1<<row);
99       if (prev_bit != curr_bit) {
100         matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
101         debouncing = DEBOUNCING_DELAY;
102       }
103     }
104     unselect_cols();
105   }
106
107   if (debouncing) {
108     if (--debouncing) {
109       _delay_ms(1);
110     } else {
111       for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
112         matrix[i] = matrix_debouncing[i];
113       }
114     }
115   }
116
117   matrix_scan_quantum();
118   return 1;
119 }
120
121 inline matrix_row_t matrix_get_row(uint8_t row) {
122   return matrix[row];
123 }
124
125 void matrix_print(void) {
126   print("\nr/c 0123456789ABCDEF\n");
127   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
128     xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
129   }
130 }
131
132 /* Row pin configuration
133  * row: 0    1    2    3    4    5
134  * pin: PB7  PD0  PD1  PD2  PD3  PD5
135  *
136  * Esc uses its own pin PE2
137  */
138 static void init_rows(void) {
139     DDRD  &= ~0b00101111;
140     PORTD &= ~0b00101111;
141
142     DDRB  &= ~0b10000000;
143     PORTB &= ~0b10000000;
144
145     DDRE  &= ~0b00000100;
146     PORTE |=  0b00000100;
147 }
148
149 static uint8_t read_rows(uint8_t col) {
150
151     return (PIND&(1<<0) ? (1<<0) : 0) |
152             (PIND&(1<<1) ? (1<<1) : 0) |
153             (PIND&(1<<2) ? (1<<2) : 0) |
154             (PIND&(1<<3) ? (1<<3) : 0) |
155             (PIND&(1<<5) ? (1<<4) : 0) |
156             (PINB&(1<<7) ? (1<<5) : 0) |
157             (col==0 ? ((PINE&(1<<2) ? 0 : (1<<2))) : 0);
158     
159 }
160
161 uint8_t read_fwkey(void)
162 {
163   return PINE&(1<<2) ? 0 : (1<<2);
164 }
165
166 /* Columns 0 - 15
167  * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
168  * col / pin:    PC6  PB6  PF0  PF1  PC7
169  * 0:             1    0    0    0    0
170  * 1:             1    0    1    0    0
171  * 2:             1    0    0    1    0
172  * 3:             1    0    1    1    0
173  * 4:             1    0    0    0    1
174  * 5:             1    0    1    0    1
175  * 6:             1    0    0    1    1
176  * 7:             1    0    1    1    1
177  * 8:             0    1    0    0    0
178  * 9:             0    1    1    0    0
179  * 10:            0    1    0    1    0
180  * 11:            0    1    1    1    0
181  * 12:            0    1    0    0    1
182  * 13:            0    1    1    0    1
183  * 14:            0    1    0    1    1
184  * 15:            0    1    1    1    1
185  *
186  */
187 static void unselect_cols(void) {
188   DDRB  |=  0b01000000;
189   PORTB &= ~0b01000000;
190
191   DDRC  |=  0b11000000;
192   PORTC &= ~0b11000000;
193
194   DDRF  |=  0b00000011;
195   PORTF &= ~0b00000011;
196 }
197
198 static void select_col(uint8_t col) {
199  
200    switch (col) {
201         case 0:
202             PORTC |= 0b01000000;
203             break;
204         case 1:
205             PORTC |= 0b01000000;
206             PORTF |= 0b00000001;
207             break;
208         case 2:
209             PORTC |= 0b01000000;
210             PORTF |= 0b00000010;
211             break;
212         case 3:
213             PORTC |= 0b01000000;
214             PORTF |= 0b00000011;
215             break;
216         case 4:
217             PORTC |= 0b11000000;
218             break;
219         case 5:
220             PORTC |= 0b11000000;
221             PORTF |= 0b00000001;
222             break;
223         case 6:
224             PORTC |= 0b11000000;
225             PORTF |= 0b00000010;
226             break;
227         case 7:
228             PORTC |= 0b11000000;
229             PORTF |= 0b00000011;
230             break;
231         case 8:
232             PORTB |= 0b01000000;
233             break;
234         case 9:
235             PORTB |= 0b01000000;
236             PORTF |= 0b00000001;
237             break;
238         case 10:
239             PORTB |= 0b01000000;
240             PORTF |= 0b00000010;
241             break;
242         case 11:
243             PORTB |= 0b01000000;
244             PORTF |= 0b00000011;
245             break;
246         case 12:
247             PORTB |= 0b01000000;
248             PORTC |= 0b10000000;
249             break;
250         case 13:
251             PORTB |= 0b01000000;
252             PORTF |= 0b00000001;
253             PORTC |= 0b10000000;
254             break;
255         case 14:
256             PORTB |= 0b01000000;
257             PORTF |= 0b00000010;
258             PORTC |= 0b10000000;
259             break;
260         case 15:
261             PORTB |= 0b01000000;
262             PORTF |= 0b00000011;
263             PORTC |= 0b10000000;
264             break;
265     }
266 }