aboutsummaryrefslogtreecommitdiff
path: root/keyboards/mechlovin/olly/bb/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/mechlovin/olly/bb/matrix.c')
-rw-r--r--keyboards/mechlovin/olly/bb/matrix.c362
1 files changed, 362 insertions, 0 deletions
diff --git a/keyboards/mechlovin/olly/bb/matrix.c b/keyboards/mechlovin/olly/bb/matrix.c
new file mode 100644
index 000000000..d745f7d2c
--- /dev/null
+++ b/keyboards/mechlovin/olly/bb/matrix.c
@@ -0,0 +1,362 @@
1/*
2Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
3Copyright 2019 Evy Dekkers
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include "matrix.h"
20
21static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
22
23/* Cols 0 - 16
24 * These columns use two 74HC138 3 to 8 bit demultiplexer. B4, C7 is the enable pin, must be set high (1) to use it.
25 *
26 * col / pin: PA0 PA1 PA2 PB4 PC7 PC2 PC3 PC5
27 * 0: 0 ── 0 ── 0 1 ── 0 0 0 0
28 * ────────────────────────────────────────────
29 * 1: 0 ── 0 ── 1 1 ── 0 0 0 0
30 * ────────────────────────────────────────────
31 * 2: 0 ── 1 ── 0 1 ── 0 0 0 0
32 * ────────────────────────────────────────────
33 * 3: 0 ── 1 ── 1 1 ── 0 0 0 0
34 * ────────────────────────────────────────────
35 * 4: 1 ── 0 ── 0 1 ── 0 0 0 0
36 * ────────────────────────────────────────────
37 * 5: 1 ── 0 ── 1 1 ── 0 0 0 0
38 * ────────────────────────────────────────────
39 * 6: 1 ── 1 ── 0 1 ── 0 0 0 0
40 * ────────────────────────────────────────────
41 * 7: 1 ── 1 ── 1 1 ── 0 0 0 0
42 * ────────────────────────────────────────────
43 * 8: 1 ── 1 ── 1 0 ── 1 0 0 0
44 * ────────────────────────────────────────────
45 * 9: 0 ── 0 ── 0 0 ── 1 0 0 0
46 * ────────────────────────────────────────────
47 *10: 0 ── 0 ── 1 0 ── 1 0 0 0
48 * ────────────────────────────────────────────
49 *11: 0 ── 1 ── 0 0 ── 1 0 0 0
50 * ────────────────────────────────────────────
51 *12: 0 ── 1 ── 1 0 ── 1 0 0 0
52 * ────────────────────────────────────────────
53 *13: 1 ── 0 ── 0 0 ── 1 0 0 0
54 * ────────────────────────────────────────────
55 *14: 1 ── 0 ── 1 0 ── 1 0 0 0
56 * ────────────────────────────────────────────
57 *15: 1 ── 1 ── 0 0 ── 1 0 0 0
58 * ────────────────────────────────────────────
59 *16: 0 ── 0 ── 0 0 ── 0 1 0 0
60 * ────────────────────────────────────────────
61 *17: 0 ── 0 ── 0 0 ── 0 0 1 0
62 * ────────────────────────────────────────────
63 *18: 0 ── 0 ── 0 0 ── 0 0 0 1
64 *
65 */
66static void select_col(uint8_t col) {
67 switch (col) {
68 case 0:
69 writePinLow(A0);
70 writePinLow(A1);
71 writePinLow(A2);
72 writePinHigh(B4);
73 break;
74 case 1:
75 writePinLow(A0);
76 writePinLow(A1);
77 writePinHigh(A2);
78 writePinHigh(B4);
79 break;
80 case 2:
81 writePinLow(A0);
82 writePinHigh(A1);
83 writePinLow(A2);
84 writePinHigh(B4);
85 break;
86 case 3:
87 writePinLow(A0);
88 writePinHigh(A1);
89 writePinHigh(A2);
90 writePinHigh(B4);
91 break;
92 case 4:
93 writePinHigh(A0);
94 writePinLow(A1);
95 writePinLow(A2);
96 writePinHigh(B4);
97 break;
98 case 5:
99 writePinHigh(A0);
100 writePinLow(A1);
101 writePinHigh(A2);
102 writePinHigh(B4);
103 break;
104 case 6:
105 writePinHigh(A0);
106 writePinHigh(A1);
107 writePinLow(A2);
108 writePinHigh(B4);
109 break;
110 case 7:
111 writePinHigh(A0);
112 writePinHigh(A1);
113 writePinHigh(A2);
114 writePinHigh(B4);
115 break;
116 case 8:
117 writePinHigh(A0);
118 writePinHigh(A1);
119 writePinHigh(A2);
120 writePinHigh(C7);
121 break;
122 case 9:
123 writePinLow(A0);
124 writePinLow(A1);
125 writePinLow(A2);
126 writePinHigh(C7);
127 break;
128 case 10:
129 writePinLow(A0);
130 writePinLow(A1);
131 writePinHigh(A2);
132 writePinHigh(C7);
133 break;
134 case 11:
135 writePinLow(A0);
136 writePinHigh(A1);
137 writePinLow(A2);
138 writePinHigh(C7);
139 break;
140 case 12:
141 writePinLow(A0);
142 writePinHigh(A1);
143 writePinHigh(A2);
144 writePinHigh(C7);
145 break;
146 case 13:
147 writePinHigh(A0);
148 writePinLow(A1);
149 writePinLow(A2);
150 writePinHigh(C7);
151 break;
152 case 14:
153 writePinHigh(A0);
154 writePinLow(A1);
155 writePinHigh(A2);
156 writePinHigh(C7);
157 break;
158 case 15:
159 writePinHigh(A0);
160 writePinHigh(A1);
161 writePinLow(A2);
162 writePinHigh(C7);
163 break;
164 case 16:
165 writePinLow(C2);
166 break;
167 case 17:
168 writePinLow(C3);
169 break;
170 case 18:
171 writePinLow(C5);
172 break;
173 }
174}
175
176static void unselect_col(uint8_t col) {
177 switch (col) {
178 case 0:
179 writePinHigh(A0);
180 writePinHigh(A1);
181 writePinHigh(A2);
182 writePinLow(B4);
183 break;
184 case 1:
185 writePinHigh(A0);
186 writePinHigh(A1);
187 writePinLow(A2);
188 writePinLow(B4);
189 break;
190 case 2:
191 writePinHigh(A0);
192 writePinLow(A1);
193 writePinHigh(A2);
194 writePinLow(B4);
195 break;
196 case 3:
197 writePinHigh(A0);
198 writePinLow(A1);
199 writePinLow(A2);
200 writePinLow(B4);
201 break;
202 case 4:
203 writePinLow(A0);
204 writePinHigh(A1);
205 writePinHigh(A2);
206 writePinLow(B4);
207 break;
208 case 5:
209 writePinLow(A0);
210 writePinHigh(A1);
211 writePinLow(A2);
212 writePinLow(B4);
213 break;
214 case 6:
215 writePinLow(A0);
216 writePinLow(A1);
217 writePinHigh(A2);
218 writePinLow(B4);
219 break;
220 case 7:
221 writePinLow(A0);
222 writePinLow(A1);
223 writePinLow(A2);
224 writePinLow(B4);
225 break;
226 case 8:
227 writePinLow(A0);
228 writePinLow(A1);
229 writePinLow(A2);
230 writePinLow(C7);
231 break;
232 case 9:
233 writePinHigh(A0);
234 writePinHigh(A1);
235 writePinHigh(A2);
236 writePinLow(C7);
237 break;
238 case 10:
239 writePinHigh(A0);
240 writePinHigh(A1);
241 writePinLow(A2);
242 writePinLow(C7);
243 break;
244 case 11:
245 writePinHigh(A0);
246 writePinLow(A1);
247 writePinHigh(A2);
248 writePinLow(C7);
249 break;
250 case 12:
251 writePinHigh(A0);
252 writePinLow(A1);
253 writePinLow(A2);
254 writePinLow(C7);
255 break;
256 case 13:
257 writePinLow(A0);
258 writePinHigh(A1);
259 writePinHigh(A2);
260 writePinLow(C7);
261 break;
262 case 14:
263 writePinLow(A0);
264 writePinHigh(A1);
265 writePinLow(A2);
266 writePinLow(C7);
267 break;
268 case 15:
269 writePinLow(A0);
270 writePinLow(A1);
271 writePinHigh(A2);
272 writePinLow(C7);
273 break;
274 case 16:
275 writePinHigh(C2);
276 break;
277 case 17:
278 writePinHigh(C3);
279 break;
280 case 18:
281 writePinHigh(C5);
282 break;
283 }
284}
285
286static void unselect_cols(void) {
287 //Native
288 writePinHigh(C2);
289 writePinHigh(C3);
290 writePinHigh(C5);
291
292 //Demultiplexer
293 writePinLow(B4);
294 writePinLow(C7);
295 writePinHigh(A0);
296 writePinHigh(A1);
297 writePinHigh(A2);
298}
299
300static void init_pins(void) {
301 unselect_cols();
302 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
303 setPinInputHigh(row_pins[x]);
304 }
305 setPinOutput(A0);
306 setPinOutput(A1);
307 setPinOutput(A2);
308 setPinOutput(B4);
309 setPinOutput(C7);
310 setPinOutput(C2);
311 setPinOutput(C3);
312 setPinOutput(C5);
313}
314
315static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
316 bool matrix_changed = false;
317
318 // Select col and wait for col selecton to stabilize
319 select_col(current_col);
320 matrix_io_delay();
321
322 // For each row...
323 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
324 // Store last value of row prior to reading
325 matrix_row_t last_row_value = current_matrix[row_index];
326
327 // Check row pin state
328 if (readPin(row_pins[row_index]) == 0) {
329 // Pin LO, set col bit
330 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
331 } else {
332 // Pin HI, clear col bit
333 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
334 }
335
336 // Determine if the matrix changed state
337 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
338 matrix_changed = true;
339 }
340 }
341
342 // Unselect col
343 unselect_col(current_col);
344
345 return matrix_changed;
346}
347
348void matrix_init_custom(void) {
349 // initialize key pins
350 init_pins();
351}
352
353bool matrix_scan_custom(matrix_row_t current_matrix[]) {
354 bool changed = false;
355
356 // Set col, read rows
357 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
358 changed |= read_rows_on_col(current_matrix, current_col);
359 }
360
361 return changed;
362}