aboutsummaryrefslogtreecommitdiff
path: root/keyboards/lz/erghost/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/lz/erghost/matrix.c')
-rw-r--r--keyboards/lz/erghost/matrix.c344
1 files changed, 344 insertions, 0 deletions
diff --git a/keyboards/lz/erghost/matrix.c b/keyboards/lz/erghost/matrix.c
new file mode 100644
index 000000000..6b7d091cb
--- /dev/null
+++ b/keyboards/lz/erghost/matrix.c
@@ -0,0 +1,344 @@
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#include "gpio.h"
21
22static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
23
24/* Cols 0 - 16
25 * These columns use two 74HC138 3 to 8 bit demultiplexer. B0, F1 is the enable pin, must be set high (1) to use it.
26 *
27 * col / pin: PB5 PB7 PF0 PB0 PF1 PE6
28 * 0: 0 ── 0 ── 0 1 ── 0 0
29 * ────────────────────────────────────────────
30 * 1: 0 ── 0 ── 1 1 ── 0 0
31 * ────────────────────────────────────────────
32 * 2: 0 ── 1 ── 0 1 ── 0 0
33 * ────────────────────────────────────────────
34 * 3: 0 ── 1 ── 1 1 ── 0 0
35 * ────────────────────────────────────────────
36 * 4: 1 ── 0 ── 0 1 ── 0 0
37 * ────────────────────────────────────────────
38 * 5: 1 ── 0 ── 1 1 ── 0 0
39 * ────────────────────────────────────────────
40 * 6: 1 ── 1 ── 0 1 ── 0 0
41 * ────────────────────────────────────────────
42 * 7: 1 ── 1 ── 1 1 ── 0 0
43 * ────────────────────────────────────────────
44 * 8: 0 ── 0 ── 0 0 ── 1 0
45 * ────────────────────────────────────────────
46 * 9: 0 ── 0 ── 1 0 ── 1 0
47 * ────────────────────────────────────────────
48 *10: 0 ── 1 ── 0 0 ── 1 0
49 * ────────────────────────────────────────────
50 *11: 0 ── 1 ── 1 0 ── 1 0
51 * ────────────────────────────────────────────
52 *12: 1 ── 0 ── 0 0 ── 1 0
53 * ────────────────────────────────────────────
54 *13: 1 ── 0 ── 1 0 ── 1 0
55 * ────────────────────────────────────────────
56 *14: 1 ── 1 ── 1 0 ── 1 0
57 * ────────────────────────────────────────────
58 *15: 1 ── 1 ── 0 0 ── 1 0
59 * ────────────────────────────────────────────
60 *16: 0 ── 0 ── 0 0 ── 0 1
61 *
62 */
63static void select_col(uint8_t col) {
64 switch (col) {
65 case 0:
66 writePinLow(B5);
67 writePinLow(B7);
68 writePinLow(F0);
69 writePinHigh(B0);
70 break;
71 case 1:
72 writePinLow(B5);
73 writePinLow(B7);
74 writePinHigh(F0);
75 writePinHigh(B0);
76 break;
77 case 2:
78 writePinLow(B5);
79 writePinHigh(B7);
80 writePinLow(F0);
81 writePinHigh(B0);
82 break;
83 case 3:
84 writePinLow(B5);
85 writePinHigh(B7);
86 writePinHigh(F0);
87 writePinHigh(B0);
88 break;
89 case 4:
90 writePinHigh(B5);
91 writePinLow(B7);
92 writePinLow(F0);
93 writePinHigh(B0);
94 break;
95 case 5:
96 writePinHigh(B5);
97 writePinLow(B7);
98 writePinHigh(F0);
99 writePinHigh(B0);
100 break;
101 case 6:
102 writePinHigh(B5);
103 writePinHigh(B7);
104 writePinLow(F0);
105 writePinHigh(B0);
106 break;
107 case 7:
108 writePinHigh(B5);
109 writePinHigh(B7);
110 writePinHigh(F0);
111 writePinHigh(B0);
112 break;
113 case 8:
114 writePinLow(B5);
115 writePinLow(B7);
116 writePinLow(F0);
117 writePinHigh(F1);
118 break;
119 case 9:
120 writePinLow(B5);
121 writePinLow(B7);
122 writePinHigh(F0);
123 writePinHigh(F1);
124 break;
125 case 10:
126 writePinLow(B5);
127 writePinHigh(B7);
128 writePinLow(F0);
129 writePinHigh(F1);
130 break;
131 case 11:
132 writePinLow(B5);
133 writePinHigh(B7);
134 writePinHigh(F0);
135 writePinHigh(F1);
136 break;
137 case 12:
138 writePinHigh(B5);
139 writePinLow(B7);
140 writePinLow(F0);
141 writePinHigh(F1);
142 break;
143 case 13:
144 writePinHigh(B5);
145 writePinLow(B7);
146 writePinHigh(F0);
147 writePinHigh(F1);
148 break;
149 case 14:
150 writePinHigh(B5);
151 writePinHigh(B7);
152 writePinHigh(F0);
153 writePinHigh(F1);
154 break;
155 case 15:
156 writePinHigh(B5);
157 writePinHigh(B7);
158 writePinLow(F0);
159 writePinHigh(F1);
160 break;
161 case 16:
162 writePinLow(E6);
163 break;
164 }
165}
166
167static void unselect_col(uint8_t col) {
168 switch (col) {
169 case 0:
170 writePinHigh(B5);
171 writePinHigh(B7);
172 writePinHigh(F0);
173 writePinLow(B0);
174 break;
175 case 1:
176 writePinHigh(B5);
177 writePinHigh(B7);
178 writePinLow(F0);
179 writePinLow(B0);
180 break;
181 case 2:
182 writePinHigh(B5);
183 writePinLow(B7);
184 writePinHigh(F0);
185 writePinLow(B0);
186 break;
187 case 3:
188 writePinHigh(B5);
189 writePinLow(B7);
190 writePinLow(F0);
191 writePinLow(B0);
192 break;
193 case 4:
194 writePinLow(B5);
195 writePinHigh(B7);
196 writePinHigh(F0);
197 writePinLow(B0);
198 break;
199 case 5:
200 writePinLow(B5);
201 writePinHigh(B7);
202 writePinLow(F0);
203 writePinLow(B0);
204 break;
205 case 6:
206 writePinLow(B5);
207 writePinLow(B7);
208 writePinHigh(F0);
209 writePinLow(B0);
210 break;
211 case 7:
212 writePinLow(B5);
213 writePinLow(B7);
214 writePinLow(F0);
215 writePinLow(B0);
216 break;
217 case 8:
218 writePinHigh(B5);
219 writePinHigh(B7);
220 writePinHigh(F0);
221 writePinLow(F1);
222 break;
223 case 9:
224 writePinHigh(B5);
225 writePinHigh(B7);
226 writePinLow(F0);
227 writePinLow(F1);
228 break;
229 case 10:
230 writePinHigh(B5);
231 writePinLow(B7);
232 writePinHigh(F0);
233 writePinLow(F1);
234 break;
235 case 11:
236 writePinHigh(B5);
237 writePinLow(B7);
238 writePinLow(F0);
239 writePinLow(F1);
240 break;
241 case 12:
242 writePinLow(B5);
243 writePinHigh(B7);
244 writePinHigh(F0);
245 writePinLow(F1);
246 break;
247 case 13:
248 writePinLow(B5);
249 writePinHigh(B7);
250 writePinLow(F0);
251 writePinLow(F1);
252 break;
253 case 14:
254 writePinLow(B5);
255 writePinLow(B7);
256 writePinLow(F0);
257 writePinLow(F1);
258 break;
259 case 15:
260 writePinLow(B5);
261 writePinLow(B7);
262 writePinHigh(F0);
263 writePinLow(F1);
264 break;
265 case 16:
266 writePinHigh(E6);
267 break;
268 }
269}
270
271static void unselect_cols(void) {
272 //Native
273 writePinHigh(E6);
274
275 //Demultiplexer
276 writePinLow(B0);
277 writePinLow(F1);
278 writePinHigh(B5);
279 writePinHigh(B7);
280 writePinHigh(F0);
281}
282
283static void init_pins(void) {
284 unselect_cols();
285 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
286 setPinInputHigh(row_pins[x]);
287 }
288 setPinOutput(B5);
289 setPinOutput(B7);
290 setPinOutput(F0);
291 setPinOutput(B0);
292 setPinOutput(F1);
293 setPinOutput(E6);
294}
295
296static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
297 bool matrix_changed = false;
298
299 // Select col and wait for col selecton to stabilize
300 select_col(current_col);
301 matrix_io_delay();
302
303 // For each row...
304 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
305 // Store last value of row prior to reading
306 matrix_row_t last_row_value = current_matrix[row_index];
307
308 // Check row pin state
309 if (readPin(row_pins[row_index]) == 0) {
310 // Pin LO, set col bit
311 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
312 } else {
313 // Pin HI, clear col bit
314 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
315 }
316
317 // Determine if the matrix changed state
318 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
319 matrix_changed = true;
320 }
321 }
322
323 // Unselect col
324 unselect_col(current_col);
325
326 return matrix_changed;
327}
328
329void matrix_init_custom(void) {
330 // initialize key pins
331 init_pins();
332}
333
334bool matrix_scan_custom(matrix_row_t current_matrix[]) {
335 bool changed = false;
336
337 // Set col, read rows
338 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
339 changed |= read_rows_on_col(current_matrix, current_col);
340 }
341
342 return changed;
343}
344