aboutsummaryrefslogtreecommitdiff
path: root/quantum/split_common
diff options
context:
space:
mode:
authorDrashna Jaelre <drashna@live.com>2021-07-11 14:31:35 -0700
committerGitHub <noreply@github.com>2021-07-11 14:31:35 -0700
commitccc0b23a75f30f0ee9e1c6440dd3d56c99d38ea2 (patch)
tree26143876908e9c0b1b279a7da61be0f90cd00de4 /quantum/split_common
parent0b06452d00dcd967bfcaffe89e88a4758c878e74 (diff)
downloadqmk_firmware-ccc0b23a75f30f0ee9e1c6440dd3d56c99d38ea2.tar.gz
qmk_firmware-ccc0b23a75f30f0ee9e1c6440dd3d56c99d38ea2.zip
Unify matrix for split common and regular matrix (#13330)
Diffstat (limited to 'quantum/split_common')
-rw-r--r--quantum/split_common/matrix.c340
1 files changed, 0 insertions, 340 deletions
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c
deleted file mode 100644
index d8e078e9b..000000000
--- a/quantum/split_common/matrix.c
+++ /dev/null
@@ -1,340 +0,0 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include <stdint.h>
18#include <stdbool.h>
19#include <string.h>
20#include "util.h"
21#include "matrix.h"
22#include "debounce.h"
23#include "quantum.h"
24#include "split_util.h"
25#include "config.h"
26#include "transactions.h"
27
28#ifndef ERROR_DISCONNECT_COUNT
29# define ERROR_DISCONNECT_COUNT 5
30#endif // ERROR_DISCONNECT_COUNT
31
32#define ROWS_PER_HAND (MATRIX_ROWS / 2)
33
34#ifdef DIRECT_PINS
35static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
36#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
37# ifdef MATRIX_ROW_PINS
38static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
39# endif // MATRIX_ROW_PINS
40# ifdef MATRIX_COL_PINS
41static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
42# endif // MATRIX_COL_PINS
43#endif
44
45/* matrix state(1:on, 0:off) */
46extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
47extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
48
49// row offsets for each hand
50uint8_t thisHand, thatHand;
51
52// user-defined overridable functions
53__attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); }
54__attribute__((weak)) void matrix_slave_scan_user(void) {}
55__attribute__((weak)) void matrix_init_pins(void);
56__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
57__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
58
59static inline void setPinOutput_writeLow(pin_t pin) {
60 ATOMIC_BLOCK_FORCEON {
61 setPinOutput(pin);
62 writePinLow(pin);
63 }
64}
65
66static inline void setPinInputHigh_atomic(pin_t pin) {
67 ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
68}
69
70static inline uint8_t readMatrixPin(pin_t pin) {
71 if (pin != NO_PIN) {
72 return readPin(pin);
73 } else {
74 return 1;
75 }
76}
77
78// matrix code
79
80#ifdef DIRECT_PINS
81
82__attribute__((weak)) void matrix_init_pins(void) {
83 for (int row = 0; row < MATRIX_ROWS; row++) {
84 for (int col = 0; col < MATRIX_COLS; col++) {
85 pin_t pin = direct_pins[row][col];
86 if (pin != NO_PIN) {
87 setPinInputHigh(pin);
88 }
89 }
90 }
91}
92
93__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
94 // Start with a clear matrix row
95 matrix_row_t current_row_value = 0;
96
97 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
98 pin_t pin = direct_pins[current_row][col_index];
99 if (pin != NO_PIN) {
100 current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
101 }
102 }
103
104 // Update the matrix
105 current_matrix[current_row] = current_row_value;
106}
107
108#elif defined(DIODE_DIRECTION)
109# if defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
110# if (DIODE_DIRECTION == COL2ROW)
111
112static bool select_row(uint8_t row) {
113 pin_t pin = row_pins[row];
114 if (pin != NO_PIN) {
115 setPinOutput_writeLow(pin);
116 return true;
117 }
118 return false;
119}
120
121static void unselect_row(uint8_t row) {
122 pin_t pin = row_pins[row];
123 if (pin != NO_PIN) {
124 setPinInputHigh_atomic(pin);
125 }
126}
127
128static void unselect_rows(void) {
129 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
130 unselect_row(x);
131 }
132}
133
134__attribute__((weak)) void matrix_init_pins(void) {
135 unselect_rows();
136 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
137 if (col_pins[x] != NO_PIN) {
138 setPinInputHigh_atomic(col_pins[x]);
139 }
140 }
141}
142
143__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
144 // Start with a clear matrix row
145 matrix_row_t current_row_value = 0;
146
147 if (!select_row(current_row)) { // Select row
148 return; // skip NO_PIN row
149 }
150 matrix_output_select_delay();
151
152 // For each col...
153 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
154 uint8_t pin_state = readMatrixPin(col_pins[col_index]);
155
156 // Populate the matrix row with the state of the col pin
157 current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
158 }
159
160 // Unselect row
161 unselect_row(current_row);
162 matrix_output_unselect_delay(); // wait for all Col signals to go HIGH
163
164 // Update the matrix
165 current_matrix[current_row] = current_row_value;
166}
167
168# elif (DIODE_DIRECTION == ROW2COL)
169
170static bool select_col(uint8_t col) {
171 pin_t pin = col_pins[col];
172 if (pin != NO_PIN) {
173 setPinOutput_writeLow(pin);
174 return true;
175 }
176 return false;
177}
178
179static void unselect_col(uint8_t col) {
180 pin_t pin = col_pins[col];
181 if (pin != NO_PIN) {
182 setPinInputHigh_atomic(pin);
183 }
184}
185
186static void unselect_cols(void) {
187 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
188 unselect_col(x);
189 }
190}
191
192__attribute__((weak)) void matrix_init_pins(void) {
193 unselect_cols();
194 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
195 if (row_pins[x] != NO_PIN) {
196 setPinInputHigh_atomic(row_pins[x]);
197 }
198 }
199}
200
201__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
202 // Select col
203 if (!select_col(current_col)) { // select col
204 return; // skip NO_PIN col
205 }
206 matrix_output_select_delay();
207
208 // For each row...
209 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
210 // Check row pin state
211 if (readMatrixPin(row_pins[row_index]) == 0) {
212 // Pin LO, set col bit
213 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
214 } else {
215 // Pin HI, clear col bit
216 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
217 }
218 }
219
220 // Unselect col
221 unselect_col(current_col);
222 matrix_output_unselect_delay(); // wait for all Row signals to go HIGH
223}
224
225# else
226# error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
227# endif
228# endif // defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
229#else
230# error DIODE_DIRECTION is not defined!
231#endif
232
233void matrix_init(void) {
234 split_pre_init();
235
236 // Set pinout for right half if pinout for that half is defined
237 if (!isLeftHand) {
238#ifdef DIRECT_PINS_RIGHT
239 const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT;
240 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
241 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
242 direct_pins[i][j] = direct_pins_right[i][j];
243 }
244 }
245#endif
246#ifdef MATRIX_ROW_PINS_RIGHT
247 const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
248 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
249 row_pins[i] = row_pins_right[i];
250 }
251#endif
252#ifdef MATRIX_COL_PINS_RIGHT
253 const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
254 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
255 col_pins[i] = col_pins_right[i];
256 }
257#endif
258 }
259
260 thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
261 thatHand = ROWS_PER_HAND - thisHand;
262
263 // initialize key pins
264 matrix_init_pins();
265
266 // initialize matrix state: all keys off
267 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
268 raw_matrix[i] = 0;
269 matrix[i] = 0;
270 }
271
272 debounce_init(ROWS_PER_HAND);
273
274 matrix_init_quantum();
275
276 split_post_init();
277}
278
279bool matrix_post_scan(void) {
280 bool changed = false;
281 if (is_keyboard_master()) {
282 static uint8_t error_count;
283
284 matrix_row_t slave_matrix[ROWS_PER_HAND] = {0};
285 if (!transport_master(matrix + thisHand, slave_matrix)) {
286 error_count++;
287
288 if (error_count > ERROR_DISCONNECT_COUNT) {
289 // reset other half if disconnected
290 for (int i = 0; i < ROWS_PER_HAND; ++i) {
291 matrix[thatHand + i] = 0;
292 slave_matrix[i] = 0;
293 }
294
295 changed = true;
296 }
297 } else {
298 error_count = 0;
299
300 for (int i = 0; i < ROWS_PER_HAND; ++i) {
301 if (matrix[thatHand + i] != slave_matrix[i]) {
302 matrix[thatHand + i] = slave_matrix[i];
303 changed = true;
304 }
305 }
306 }
307
308 matrix_scan_quantum();
309 } else {
310 transport_slave(matrix + thatHand, matrix + thisHand);
311
312 matrix_slave_scan_kb();
313 }
314
315 return changed;
316}
317
318uint8_t matrix_scan(void) {
319 matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
320
321#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
322 // Set row, read cols
323 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
324 matrix_read_cols_on_row(curr_matrix, current_row);
325 }
326#elif (DIODE_DIRECTION == ROW2COL)
327 // Set col, read rows
328 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
329 matrix_read_rows_on_col(curr_matrix, current_col);
330 }
331#endif
332
333 bool local_changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
334 if (local_changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix));
335
336 debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, local_changed);
337
338 bool remote_changed = matrix_post_scan();
339 return (uint8_t)(local_changed || remote_changed);
340}