]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/eagle_viper/v2/matrix.c
7003a7ae00dfd6e88d87cfbd3437559cf826f53a
[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
37 __attribute__ ((weak))
38 void matrix_init_kb(void) {
39     matrix_init_user();
40 }
41
42 __attribute__ ((weak))
43 void matrix_scan_kb(void) {
44     matrix_scan_user();
45 }
46
47 __attribute__ ((weak))
48 void matrix_init_user(void) {
49 }
50
51 __attribute__ ((weak))
52 void matrix_scan_user(void) {
53 }
54
55 void 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
67 void 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
80 uint8_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
112 inline matrix_row_t matrix_get_row(uint8_t row) {
113   return matrix[row];
114 }
115
116 void 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  */
129 static 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
140 static 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
152 uint8_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  */
178 static 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
189 static 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 }