aboutsummaryrefslogtreecommitdiff
path: root/quantum/split_common/matrix.c
diff options
context:
space:
mode:
authorskullY <skullydazed@gmail.com>2019-08-30 11:19:03 -0700
committerskullydazed <skullydazed@users.noreply.github.com>2019-08-30 15:01:52 -0700
commitb624f32f944acdc59dcb130674c09090c5c404cb (patch)
treebc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /quantum/split_common/matrix.c
parent61af76a10d00aba185b8338604171de490a13e3b (diff)
downloadqmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz
qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip
clang-format changes
Diffstat (limited to 'quantum/split_common/matrix.c')
-rw-r--r--quantum/split_common/matrix.c332
1 files changed, 166 insertions, 166 deletions
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c
index e0f094e34..313f7830b 100644
--- a/quantum/split_common/matrix.c
+++ b/quantum/split_common/matrix.c
@@ -30,24 +30,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
30#include "transport.h" 30#include "transport.h"
31 31
32#ifdef ENCODER_ENABLE 32#ifdef ENCODER_ENABLE
33 #include "encoder.h" 33# include "encoder.h"
34#endif 34#endif
35 35
36#if (MATRIX_COLS <= 8) 36#if (MATRIX_COLS <= 8)
37# define print_matrix_header() print("\nr/c 01234567\n") 37# define print_matrix_header() print("\nr/c 01234567\n")
38# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 38# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
39# define matrix_bitpop(i) bitpop(matrix[i]) 39# define matrix_bitpop(i) bitpop(matrix[i])
40# define ROW_SHIFTER ((uint8_t)1) 40# define ROW_SHIFTER ((uint8_t)1)
41#elif (MATRIX_COLS <= 16) 41#elif (MATRIX_COLS <= 16)
42# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") 42# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
43# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) 43# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
44# define matrix_bitpop(i) bitpop16(matrix[i]) 44# define matrix_bitpop(i) bitpop16(matrix[i])
45# define ROW_SHIFTER ((uint16_t)1) 45# define ROW_SHIFTER ((uint16_t)1)
46#elif (MATRIX_COLS <= 32) 46#elif (MATRIX_COLS <= 32)
47# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") 47# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
48# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) 48# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
49# define matrix_bitpop(i) bitpop32(matrix[i]) 49# define matrix_bitpop(i) bitpop32(matrix[i])
50# define ROW_SHIFTER ((uint32_t)1) 50# define ROW_SHIFTER ((uint32_t)1)
51#endif 51#endif
52 52
53#define ERROR_DISCONNECT_COUNT 5 53#define ERROR_DISCONNECT_COUNT 5
@@ -87,8 +87,8 @@ inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
87inline uint8_t matrix_cols(void) { return MATRIX_COLS; } 87inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
88 88
89bool matrix_is_modified(void) { 89bool matrix_is_modified(void) {
90 if (debounce_active()) return false; 90 if (debounce_active()) return false;
91 return true; 91 return true;
92} 92}
93 93
94inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } 94inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
@@ -96,22 +96,22 @@ inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((mat
96inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } 96inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
97 97
98void matrix_print(void) { 98void matrix_print(void) {
99 print_matrix_header(); 99 print_matrix_header();
100 100
101 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 101 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
102 phex(row); 102 phex(row);
103 print(": "); 103 print(": ");
104 print_matrix_row(row); 104 print_matrix_row(row);
105 print("\n"); 105 print("\n");
106 } 106 }
107} 107}
108 108
109uint8_t matrix_key_count(void) { 109uint8_t matrix_key_count(void) {
110 uint8_t count = 0; 110 uint8_t count = 0;
111 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 111 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
112 count += matrix_bitpop(i); 112 count += matrix_bitpop(i);
113 } 113 }
114 return count; 114 return count;
115} 115}
116 116
117// matrix code 117// matrix code
@@ -119,224 +119,224 @@ uint8_t matrix_key_count(void) {
119#ifdef DIRECT_PINS 119#ifdef DIRECT_PINS
120 120
121static void init_pins(void) { 121static void init_pins(void) {
122 for (int row = 0; row < MATRIX_ROWS; row++) { 122 for (int row = 0; row < MATRIX_ROWS; row++) {
123 for (int col = 0; col < MATRIX_COLS; col++) { 123 for (int col = 0; col < MATRIX_COLS; col++) {
124 pin_t pin = direct_pins[row][col]; 124 pin_t pin = direct_pins[row][col];
125 if (pin != NO_PIN) { 125 if (pin != NO_PIN) {
126 setPinInputHigh(pin); 126 setPinInputHigh(pin);
127 } 127 }
128 }
128 } 129 }
129 }
130} 130}
131 131
132static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 132static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
133 matrix_row_t last_row_value = current_matrix[current_row]; 133 matrix_row_t last_row_value = current_matrix[current_row];
134 current_matrix[current_row] = 0; 134 current_matrix[current_row] = 0;
135 135
136 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 136 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
137 pin_t pin = direct_pins[current_row][col_index]; 137 pin_t pin = direct_pins[current_row][col_index];
138 if (pin != NO_PIN) { 138 if (pin != NO_PIN) {
139 current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index); 139 current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index);
140 }
140 } 141 }
141 }
142 142
143 return (last_row_value != current_matrix[current_row]); 143 return (last_row_value != current_matrix[current_row]);
144} 144}
145 145
146#elif (DIODE_DIRECTION == COL2ROW) 146#elif (DIODE_DIRECTION == COL2ROW)
147 147
148static void select_row(uint8_t row) { 148static void select_row(uint8_t row) {
149 setPinOutput(row_pins[row]); 149 setPinOutput(row_pins[row]);
150 writePinLow(row_pins[row]); 150 writePinLow(row_pins[row]);
151} 151}
152 152
153static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); } 153static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
154 154
155static void unselect_rows(void) { 155static void unselect_rows(void) {
156 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) { 156 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
157 setPinInputHigh(row_pins[x]); 157 setPinInputHigh(row_pins[x]);
158 } 158 }
159} 159}
160 160
161static void init_pins(void) { 161static void init_pins(void) {
162 unselect_rows(); 162 unselect_rows();
163 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 163 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
164 setPinInputHigh(col_pins[x]); 164 setPinInputHigh(col_pins[x]);
165 } 165 }
166} 166}
167 167
168static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 168static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
169 // Store last value of row prior to reading 169 // Store last value of row prior to reading
170 matrix_row_t last_row_value = current_matrix[current_row]; 170 matrix_row_t last_row_value = current_matrix[current_row];
171 171
172 // Clear data in matrix row 172 // Clear data in matrix row
173 current_matrix[current_row] = 0; 173 current_matrix[current_row] = 0;
174 174
175 // Select row and wait for row selecton to stabilize 175 // Select row and wait for row selecton to stabilize
176 select_row(current_row); 176 select_row(current_row);
177 wait_us(30); 177 wait_us(30);
178 178
179 // For each col... 179 // For each col...
180 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 180 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
181 // Populate the matrix row with the state of the col pin 181 // Populate the matrix row with the state of the col pin
182 current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (ROW_SHIFTER << col_index); 182 current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (ROW_SHIFTER << col_index);
183 } 183 }
184 184
185 // Unselect row 185 // Unselect row
186 unselect_row(current_row); 186 unselect_row(current_row);
187 187
188 return (last_row_value != current_matrix[current_row]); 188 return (last_row_value != current_matrix[current_row]);
189} 189}
190 190
191#elif (DIODE_DIRECTION == ROW2COL) 191#elif (DIODE_DIRECTION == ROW2COL)
192 192
193static void select_col(uint8_t col) { 193static void select_col(uint8_t col) {
194 setPinOutput(col_pins[col]); 194 setPinOutput(col_pins[col]);
195 writePinLow(col_pins[col]); 195 writePinLow(col_pins[col]);
196} 196}
197 197
198static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); } 198static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
199 199
200static void unselect_cols(void) { 200static void unselect_cols(void) {
201 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 201 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
202 setPinInputHigh(col_pins[x]); 202 setPinInputHigh(col_pins[x]);
203 } 203 }
204} 204}
205 205
206static void init_pins(void) { 206static void init_pins(void) {
207 unselect_cols(); 207 unselect_cols();
208 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) { 208 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
209 setPinInputHigh(row_pins[x]); 209 setPinInputHigh(row_pins[x]);
210 } 210 }
211} 211}
212 212
213static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { 213static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
214 bool matrix_changed = false; 214 bool matrix_changed = false;
215 215
216 // Select col and wait for col selecton to stabilize 216 // Select col and wait for col selecton to stabilize
217 select_col(current_col); 217 select_col(current_col);
218 wait_us(30); 218 wait_us(30);
219 219
220 // For each row... 220 // For each row...
221 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { 221 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
222 // Store last value of row prior to reading 222 // Store last value of row prior to reading
223 matrix_row_t last_row_value = current_matrix[row_index]; 223 matrix_row_t last_row_value = current_matrix[row_index];
224 224
225 // Check row pin state 225 // Check row pin state
226 if (readPin(row_pins[row_index])) { 226 if (readPin(row_pins[row_index])) {
227 // Pin HI, clear col bit 227 // Pin HI, clear col bit
228 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 228 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
229 } else { 229 } else {
230 // Pin LO, set col bit 230 // Pin LO, set col bit
231 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 231 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
232 } 232 }
233 233
234 // Determine if the matrix changed state 234 // Determine if the matrix changed state
235 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { 235 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
236 matrix_changed = true; 236 matrix_changed = true;
237 }
237 } 238 }
238 }
239 239
240 // Unselect col 240 // Unselect col
241 unselect_col(current_col); 241 unselect_col(current_col);
242 242
243 return matrix_changed; 243 return matrix_changed;
244} 244}
245 245
246#endif 246#endif
247 247
248void matrix_init(void) { 248void matrix_init(void) {
249 debug_enable = true; 249 debug_enable = true;
250 debug_matrix = true; 250 debug_matrix = true;
251 debug_mouse = true; 251 debug_mouse = true;
252 252
253 // Set pinout for right half if pinout for that half is defined 253 // Set pinout for right half if pinout for that half is defined
254 if (!isLeftHand) { 254 if (!isLeftHand) {
255#ifdef DIRECT_PINS_RIGHT 255#ifdef DIRECT_PINS_RIGHT
256 const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT; 256 const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT;
257 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 257 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
258 for (uint8_t j = 0; j < MATRIX_COLS; j++) { 258 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
259 direct_pins[i][j] = direct_pins_right[i][j]; 259 direct_pins[i][j] = direct_pins_right[i][j];
260 } 260 }
261 } 261 }
262#endif 262#endif
263#ifdef MATRIX_ROW_PINS_RIGHT 263#ifdef MATRIX_ROW_PINS_RIGHT
264 const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT; 264 const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
265 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 265 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
266 row_pins[i] = row_pins_right[i]; 266 row_pins[i] = row_pins_right[i];
267 } 267 }
268#endif 268#endif
269#ifdef MATRIX_COL_PINS_RIGHT 269#ifdef MATRIX_COL_PINS_RIGHT
270 const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT; 270 const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
271 for (uint8_t i = 0; i < MATRIX_COLS; i++) { 271 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
272 col_pins[i] = col_pins_right[i]; 272 col_pins[i] = col_pins_right[i];
273 } 273 }
274#endif 274#endif
275 } 275 }
276 276
277 thisHand = isLeftHand ? 0 : (ROWS_PER_HAND); 277 thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
278 thatHand = ROWS_PER_HAND - thisHand; 278 thatHand = ROWS_PER_HAND - thisHand;
279 279
280 // initialize key pins 280 // initialize key pins
281 init_pins(); 281 init_pins();
282 282
283 // initialize matrix state: all keys off 283 // initialize matrix state: all keys off
284 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 284 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
285 matrix[i] = 0; 285 matrix[i] = 0;
286 } 286 }
287 287
288 debounce_init(ROWS_PER_HAND); 288 debounce_init(ROWS_PER_HAND);
289 289
290 matrix_init_quantum(); 290 matrix_init_quantum();
291} 291}
292 292
293uint8_t _matrix_scan(void) { 293uint8_t _matrix_scan(void) {
294 bool changed = false; 294 bool changed = false;
295 295
296#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) 296#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
297 // Set row, read cols 297 // Set row, read cols
298 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) { 298 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
299 changed |= read_cols_on_row(raw_matrix, current_row); 299 changed |= read_cols_on_row(raw_matrix, current_row);
300 } 300 }
301#elif (DIODE_DIRECTION == ROW2COL) 301#elif (DIODE_DIRECTION == ROW2COL)
302 // Set col, read rows 302 // Set col, read rows
303 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 303 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
304 changed |= read_rows_on_col(raw_matrix, current_col); 304 changed |= read_rows_on_col(raw_matrix, current_col);
305 } 305 }
306#endif 306#endif
307 307
308 debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed); 308 debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed);
309 309
310 return (uint8_t)changed; 310 return (uint8_t)changed;
311} 311}
312 312
313uint8_t matrix_scan(void) { 313uint8_t matrix_scan(void) {
314 uint8_t ret = _matrix_scan(); 314 uint8_t ret = _matrix_scan();
315 315
316 if (is_keyboard_master()) { 316 if (is_keyboard_master()) {
317 static uint8_t error_count; 317 static uint8_t error_count;
318 318
319 if (!transport_master(matrix + thatHand)) { 319 if (!transport_master(matrix + thatHand)) {
320 error_count++; 320 error_count++;
321 321
322 if (error_count > ERROR_DISCONNECT_COUNT) { 322 if (error_count > ERROR_DISCONNECT_COUNT) {
323 // reset other half if disconnected 323 // reset other half if disconnected
324 for (int i = 0; i < ROWS_PER_HAND; ++i) { 324 for (int i = 0; i < ROWS_PER_HAND; ++i) {
325 matrix[thatHand + i] = 0; 325 matrix[thatHand + i] = 0;
326 }
327 }
328 } else {
329 error_count = 0;
326 } 330 }
327 }
328 } else {
329 error_count = 0;
330 }
331 331
332 matrix_scan_quantum(); 332 matrix_scan_quantum();
333 } else { 333 } else {
334 transport_slave(matrix + thisHand); 334 transport_slave(matrix + thisHand);
335#ifdef ENCODER_ENABLE 335#ifdef ENCODER_ENABLE
336 encoder_read(); 336 encoder_read();
337#endif 337#endif
338 matrix_slave_scan_user(); 338 matrix_slave_scan_user();
339 } 339 }
340 340
341 return ret; 341 return ret;
342} 342}