diff options
Diffstat (limited to 'keyboards/dp60/matrix.c')
-rw-r--r-- | keyboards/dp60/matrix.c | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/keyboards/dp60/matrix.c b/keyboards/dp60/matrix.c new file mode 100644 index 000000000..a9974757d --- /dev/null +++ b/keyboards/dp60/matrix.c | |||
@@ -0,0 +1,260 @@ | |||
1 | /** | ||
2 | * matrix.c | ||
3 | * | ||
4 | Copyright 2020 astro <[email protected]> | ||
5 | This program is free software: you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation, either version 2 of the License, or | ||
8 | (at your option) any later version. | ||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
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 | #include "quantum.h" | ||
17 | |||
18 | static uint8_t debouncing = DEBOUNCE; | ||
19 | |||
20 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
21 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
22 | |||
23 | static uint8_t read_rows(void); | ||
24 | static void init_rows(void); | ||
25 | static void init_cols(void); | ||
26 | static void unselect_cols(void); | ||
27 | static void select_col(uint8_t col); | ||
28 | |||
29 | |||
30 | __attribute__ ((weak)) | ||
31 | void matrix_init_kb(void) | ||
32 | { | ||
33 | matrix_init_user(); | ||
34 | } | ||
35 | |||
36 | __attribute__ ((weak)) | ||
37 | void matrix_scan_kb(void) | ||
38 | { | ||
39 | matrix_scan_user(); | ||
40 | } | ||
41 | |||
42 | __attribute__ ((weak)) | ||
43 | void matrix_init_user(void) {} | ||
44 | |||
45 | __attribute__ ((weak)) | ||
46 | void matrix_scan_user(void) {} | ||
47 | |||
48 | void matrix_init(void) | ||
49 | { | ||
50 | //setPinOutput(F0); | ||
51 | //writePinHigh(F0); | ||
52 | setPinOutput(B4); | ||
53 | writePinLow(B4); | ||
54 | |||
55 | init_cols(); | ||
56 | init_rows(); | ||
57 | |||
58 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
59 | matrix[i] = 0; | ||
60 | matrix_debouncing[i] = 0; | ||
61 | } | ||
62 | |||
63 | matrix_init_quantum(); | ||
64 | } | ||
65 | |||
66 | uint8_t matrix_scan(void) | ||
67 | { | ||
68 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | ||
69 | select_col(col); | ||
70 | _delay_us(3); | ||
71 | |||
72 | uint8_t rows = read_rows(); | ||
73 | |||
74 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
75 | bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col); | ||
76 | bool curr_bit = rows & (1<<row); | ||
77 | if (prev_bit != curr_bit) { | ||
78 | matrix_debouncing[row] ^= ((matrix_row_t)1<<col); | ||
79 | debouncing = DEBOUNCE; | ||
80 | } | ||
81 | } | ||
82 | unselect_cols(); | ||
83 | } | ||
84 | |||
85 | if (debouncing) { | ||
86 | if (--debouncing) { | ||
87 | _delay_ms(1); | ||
88 | } else { | ||
89 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
90 | matrix[i] = matrix_debouncing[i]; | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | |||
95 | matrix_scan_quantum(); | ||
96 | return 1; | ||
97 | } | ||
98 | |||
99 | inline matrix_row_t matrix_get_row(uint8_t row) | ||
100 | { | ||
101 | return matrix[row]; | ||
102 | } | ||
103 | |||
104 | void matrix_print(void) | ||
105 | { | ||
106 | print("\nr/c 0123456789ABCDEF\n"); | ||
107 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
108 | xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row))); | ||
109 | } | ||
110 | } | ||
111 | |||
112 | /* | ||
113 | * Row pin configuration | ||
114 | * row: 0 1 2 3 4 | ||
115 | * pin: PE6 PF6 PF7 PB7 PD4 | ||
116 | */ | ||
117 | static void init_rows(void) | ||
118 | { | ||
119 | setPinInputHigh(E6); | ||
120 | setPinInputHigh(F6); | ||
121 | setPinInputHigh(F7); | ||
122 | setPinInputHigh(B7); | ||
123 | setPinInputHigh(D4); | ||
124 | } | ||
125 | |||
126 | static uint8_t read_rows() | ||
127 | { | ||
128 | return ((readPin(E6) ? 0 : (1 << 0)) | | ||
129 | (readPin(F6) ? 0 : (1 << 1)) | | ||
130 | (readPin(F7) ? 0 : (1 << 2)) | | ||
131 | (readPin(B7) ? 0 : (1 << 3)) | | ||
132 | (readPin(D4) ? 0 : (1 << 4))); | ||
133 | } | ||
134 | |||
135 | /* | ||
136 | * Columns 0 - 13 | ||
137 | * These columns uses two 74LVC138 3 to 8 bit demultiplexers. | ||
138 | * EN Pin, PF5, PD6 | ||
139 | * | ||
140 | * col / pin: PF0 PF1 PF4 | ||
141 | * 0: 0 0 0 | ||
142 | * 1: 1 0 0 | ||
143 | * 2: 0 1 0 | ||
144 | * 3: 1 1 0 | ||
145 | * 4: 0 0 1 | ||
146 | * 5: 1 0 1 | ||
147 | * 6: 0 1 1 | ||
148 | * PD2 PD3 PD5 | ||
149 | * 7: 0 0 0 | ||
150 | * 8: 1 0 0 | ||
151 | * 9: 0 1 0 | ||
152 | * 10: 1 1 0 | ||
153 | * 11: 0 0 1 | ||
154 | * 12: 1 0 1 | ||
155 | * 13: 0 1 1 | ||
156 | * | ||
157 | */ | ||
158 | static void init_cols(void) | ||
159 | { | ||
160 | setPinOutput(F0); | ||
161 | setPinOutput(F1); | ||
162 | setPinOutput(F4); | ||
163 | setPinOutput(F5); | ||
164 | |||
165 | setPinOutput(D2); | ||
166 | setPinOutput(D3); | ||
167 | setPinOutput(D5); | ||
168 | setPinOutput(D6); | ||
169 | |||
170 | unselect_cols(); | ||
171 | } | ||
172 | |||
173 | static void unselect_cols(void) | ||
174 | { | ||
175 | writePinHigh(F0); | ||
176 | writePinHigh(F1); | ||
177 | writePinHigh(F4); | ||
178 | writePinHigh(F5); | ||
179 | |||
180 | writePinHigh(D2); | ||
181 | writePinHigh(D3); | ||
182 | writePinHigh(D5); | ||
183 | writePinHigh(D6); | ||
184 | } | ||
185 | |||
186 | static void select_col(uint8_t col) { | ||
187 | |||
188 | switch (col) { | ||
189 | case 0: | ||
190 | writePinLow(F0); | ||
191 | writePinLow(F1); | ||
192 | writePinLow(F4); | ||
193 | break; | ||
194 | case 1: | ||
195 | writePinHigh(F0); | ||
196 | writePinLow(F1); | ||
197 | writePinLow(F4); | ||
198 | break; | ||
199 | case 2: | ||
200 | writePinLow(F0); | ||
201 | writePinHigh(F1); | ||
202 | writePinLow(F4); | ||
203 | break; | ||
204 | case 3: | ||
205 | writePinHigh(F0); | ||
206 | writePinHigh(F1); | ||
207 | writePinLow(F4); | ||
208 | break; | ||
209 | case 4: | ||
210 | writePinLow(F0); | ||
211 | writePinLow(F1); | ||
212 | writePinHigh(F4); | ||
213 | break; | ||
214 | case 5: | ||
215 | writePinHigh(F0); | ||
216 | writePinLow(F1); | ||
217 | writePinHigh(F4); | ||
218 | break; | ||
219 | case 6: | ||
220 | writePinLow(F0); | ||
221 | writePinHigh(F1); | ||
222 | writePinHigh(F4); | ||
223 | break; | ||
224 | case 7: | ||
225 | writePinLow(D2); | ||
226 | writePinLow(D3); | ||
227 | writePinLow(D5); | ||
228 | break; | ||
229 | case 8: | ||
230 | writePinHigh(D2); | ||
231 | writePinLow(D3); | ||
232 | writePinLow(D5); | ||
233 | break; | ||
234 | case 9: | ||
235 | writePinLow(D2); | ||
236 | writePinHigh(D3); | ||
237 | writePinLow(D5); | ||
238 | break; | ||
239 | case 10: | ||
240 | writePinHigh(D2); | ||
241 | writePinHigh(D3); | ||
242 | writePinLow(D5); | ||
243 | break; | ||
244 | case 11: | ||
245 | writePinLow(D2); | ||
246 | writePinLow(D3); | ||
247 | writePinHigh(D5); | ||
248 | break; | ||
249 | case 12: | ||
250 | writePinHigh(D2); | ||
251 | writePinLow(D3); | ||
252 | writePinHigh(D5); | ||
253 | break; | ||
254 | case 13: | ||
255 | writePinLow(D2); | ||
256 | writePinHigh(D3); | ||
257 | writePinHigh(D5); | ||
258 | break; | ||
259 | } | ||
260 | } | ||