[Keyboard] Small Refactor of Duck boards (#5521)
[jackhill/qmk/firmware.git] / keyboards / duck / octagon / v1 / 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 DDRB |= 0b00011111; // PB0 (caps), PB1 (alpha), PB2 (extra), PB3 (modnum), PB4 (caps)
58 DDRD |= 0b11010000; // PD4, (rgb blue), PD6 (rgb red), PD7 (rgb green)
59 DDRE |= 0b01000000; // PE6 (frow)
60 }
61
62 void matrix_init(void) {
63 backlight_init_ports();
64 unselect_cols();
65 init_rows();
66
67 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
68 matrix[i] = 0;
69 matrix_debouncing[i] = 0;
70 }
71
72 matrix_init_quantum();
73 }
74
75 uint8_t matrix_scan(void) {
76 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
77 select_col(col);
78 _delay_us(3);
79
80 uint8_t rows = read_rows(col);
81
82 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
83 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
84 bool curr_bit = rows & (1<<row);
85 if (prev_bit != curr_bit) {
86 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
87 if (debouncing) {
88 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
89 }
90 debouncing = DEBOUNCING_DELAY;
91 }
92 }
93 unselect_cols();
94 }
95
96 if (debouncing) {
97 if (--debouncing) {
98 _delay_ms(1);
99 } else {
100 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101 matrix[i] = matrix_debouncing[i];
102 }
103 }
104 }
105
106 matrix_scan_quantum();
107 return 1;
108 }
109
110 inline matrix_row_t matrix_get_row(uint8_t row) {
111 return matrix[row];
112 }
113
114 void matrix_print(void) {
115 print("\nr/c 0123456789ABCDEF\n");
116 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
117 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
118 }
119 }
120
121 /* Row pin configuration
122 * row: 0 1 2 3 4 5
123 * pin: PB7 PD0 PD1 PD2 PD3 PD5
124 *
125 * Esc uses its own pin PE2
126 */
127 static void init_rows(void) {
128 DDRD &= ~0b00101111; // PD0, PD1, PD2, PD3, PD5 input
129 PORTD &= ~0b00101111; // PD0, PD1, PD2, PD3, PD5 low
130
131 DDRB &= ~0b10000000; // PB7 input
132 PORTB &= ~0b10000000; // PB7 low
133
134 DDRE &= ~0b00000100; // PE2 input
135 PORTE |= 0b00000100; // PE2 high
136 }
137
138 static uint8_t read_rows(uint8_t col) {
139 if (col == 14) {
140 return PINE&(1<<2) ? 0 : (1<<0); // PE2 (Row 0)
141 } else {
142 return (PIND&(1<<0) ? (1<<0) : 0) | // PD0 (Row 0)
143 (PIND&(1<<1) ? (1<<1) : 0) | // PD1 (Row 1)
144 (PIND&(1<<2) ? (1<<2) : 0) | // PD2 (Row 2)
145 (PIND&(1<<3) ? (1<<3) : 0) | // PD3 (Row 3)
146 (PIND&(1<<5) ? (1<<4) : 0) | // PD5 (Row 4)
147 (PINB&(1<<7) ? (1<<5) : 0); // PB7 (Row 5)
148 }
149 }
150
151 uint8_t read_fwkey(void)
152 {
153 return PINE&(1<<2) ? 0 : (1<<0);
154 }
155
156 static void unselect_cols(void) {
157 DDRB |= 0b01000000; // PB6 (U2) output
158 PORTB &= ~0b01000000; // PB6 (U2) low
159
160 DDRC |= 0b11000000; // PC6 (U1), PC7 (A2) output
161 PORTC &= ~0b11000000; // PC6 (U1), PC7 (A2) low
162
163 DDRF |= 0b00000011; // PF0 (A0), PF1 (A1) output
164 PORTF &= ~0b00000011; // PF0 (A0), PF1 (A1) low
165 }
166
167 static void select_col(uint8_t col) {
168
169 switch (col) {
170 case 0:
171 PORTC |= 0b01000000; // PC6 high
172 break;
173 case 1:
174 PORTC |= 0b01000000; // PC6 high
175 PORTF |= 0b00000001; // PF0 high
176 break;
177 case 2:
178 PORTC |= 0b01000000; // PC6 high
179 PORTF |= 0b00000010; // PF1 high
180 break;
181 case 3:
182 PORTC |= 0b01000000; // PC6 high
183 PORTF |= 0b00000011; // PF0, PF1 high
184 break;
185 case 4:
186 PORTC |= 0b11000000; // PC6, PC7 high
187 break;
188 case 5:
189 PORTC |= 0b11000000; // PC6, PC7 high
190 PORTF |= 0b00000001; // PF0 high
191 break;
192 case 6:
193 PORTC |= 0b11000000; // PC6, PC7 high
194 PORTF |= 0b00000010; // PF1 high
195 break;
196 case 7:
197 PORTC |= 0b11000000; // PC6, PC7 high
198 PORTF |= 0b00000011; // PF0, PF1 high
199 break;
200 case 8:
201 PORTB |= 0b01000000; // PB6 high
202 break;
203 case 9:
204 PORTB |= 0b01000000; // PB6 high
205 PORTF |= 0b00000001; // PF0 high
206 break;
207 case 10:
208 PORTB |= 0b01000000; // PB6 high
209 PORTF |= 0b00000010; // PF1 high
210 break;
211 case 11:
212 PORTB |= 0b01000000; // PB6 high
213 PORTF |= 0b00000011; // PF0, PF1 high
214 break;
215 case 12:
216 PORTB |= 0b01000000; // PB6 high
217 PORTC |= 0b10000000; // PC7 high
218 break;
219 case 13:
220 PORTB |= 0b01000000; // PB6 high
221 PORTF |= 0b00000001; // PF0 high
222 PORTC |= 0b10000000; // PC7 high
223 break;
224 case 15:
225 PORTB |= 0b01000000; // PB6 high
226 PORTF |= 0b00000010; // PF1 high
227 PORTC |= 0b10000000; // PC7 high
228 break;
229 }
230 }