]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/matrix.c
Fix MATRIX_X_PINS_RIGHT ARM compilation (#6395)
[qmk_firmware.git] / quantum / split_common / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
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
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include "wait.h"
24 #include "util.h"
25 #include "matrix.h"
26 #include "split_util.h"
27 #include "config.h"
28 #include "quantum.h"
29 #include "debounce.h"
30 #include "transport.h"
31
32 #ifdef ENCODER_ENABLE
33   #include "encoder.h"
34 #endif
35
36 #if (MATRIX_COLS <= 8)
37 #  define print_matrix_header() print("\nr/c 01234567\n")
38 #  define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
39 #  define matrix_bitpop(i) bitpop(matrix[i])
40 #  define ROW_SHIFTER ((uint8_t)1)
41 #elif (MATRIX_COLS <= 16)
42 #  define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
43 #  define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
44 #  define matrix_bitpop(i) bitpop16(matrix[i])
45 #  define ROW_SHIFTER ((uint16_t)1)
46 #elif (MATRIX_COLS <= 32)
47 #  define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
48 #  define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
49 #  define matrix_bitpop(i) bitpop32(matrix[i])
50 #  define ROW_SHIFTER ((uint32_t)1)
51 #endif
52
53 #define ERROR_DISCONNECT_COUNT 5
54
55 #define ROWS_PER_HAND (MATRIX_ROWS / 2)
56
57 #ifdef DIRECT_PINS
58 static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
59 #else
60 static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
61 static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
62 #endif
63
64 /* matrix state(1:on, 0:off) */
65 static matrix_row_t matrix[MATRIX_ROWS];
66 static matrix_row_t raw_matrix[ROWS_PER_HAND];
67
68 // row offsets for each hand
69 uint8_t thisHand, thatHand;
70
71 // user-defined overridable functions
72
73 __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
74
75 __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
76
77 __attribute__((weak)) void matrix_init_user(void) {}
78
79 __attribute__((weak)) void matrix_scan_user(void) {}
80
81 __attribute__((weak)) void matrix_slave_scan_user(void) {}
82
83 // helper functions
84
85 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
86
87 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
88
89 bool matrix_is_modified(void) {
90   if (debounce_active()) return false;
91   return true;
92 }
93
94 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
95
96 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
97
98 void matrix_print(void) {
99   print_matrix_header();
100
101   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
102     phex(row);
103     print(": ");
104     print_matrix_row(row);
105     print("\n");
106   }
107 }
108
109 uint8_t matrix_key_count(void) {
110   uint8_t count = 0;
111   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
112     count += matrix_bitpop(i);
113   }
114   return count;
115 }
116
117 // matrix code
118
119 #ifdef DIRECT_PINS
120
121 static void init_pins(void) {
122   for (int row = 0; row < MATRIX_ROWS; row++) {
123     for (int col = 0; col < MATRIX_COLS; col++) {
124       pin_t pin = direct_pins[row][col];
125       if (pin != NO_PIN) {
126         setPinInputHigh(pin);
127       }
128     }
129   }
130 }
131
132 static 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];
134   current_matrix[current_row] = 0;
135
136   for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
137     pin_t pin = direct_pins[current_row][col_index];
138     if (pin != NO_PIN) {
139       current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index);
140     }
141   }
142
143   return (last_row_value != current_matrix[current_row]);
144 }
145
146 #elif (DIODE_DIRECTION == COL2ROW)
147
148 static void select_row(uint8_t row) {
149   setPinOutput(row_pins[row]);
150   writePinLow(row_pins[row]);
151 }
152
153 static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
154
155 static void unselect_rows(void) {
156   for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
157     setPinInputHigh(row_pins[x]);
158   }
159 }
160
161 static void init_pins(void) {
162   unselect_rows();
163   for (uint8_t x = 0; x < MATRIX_COLS; x++) {
164     setPinInputHigh(col_pins[x]);
165   }
166 }
167
168 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
169   // Store last value of row prior to reading
170   matrix_row_t last_row_value = current_matrix[current_row];
171
172   // Clear data in matrix row
173   current_matrix[current_row] = 0;
174
175   // Select row and wait for row selecton to stabilize
176   select_row(current_row);
177   wait_us(30);
178
179   // For each col...
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
182     current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (ROW_SHIFTER << col_index);
183   }
184
185   // Unselect row
186   unselect_row(current_row);
187
188   return (last_row_value != current_matrix[current_row]);
189 }
190
191 #elif (DIODE_DIRECTION == ROW2COL)
192
193 static void select_col(uint8_t col) {
194   setPinOutput(col_pins[col]);
195   writePinLow(col_pins[col]);
196 }
197
198 static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
199
200 static void unselect_cols(void) {
201   for (uint8_t x = 0; x < MATRIX_COLS; x++) {
202     setPinInputHigh(col_pins[x]);
203   }
204 }
205
206 static void init_pins(void) {
207   unselect_cols();
208   for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
209     setPinInputHigh(row_pins[x]);
210   }
211 }
212
213 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
214   bool matrix_changed = false;
215
216   // Select col and wait for col selecton to stabilize
217   select_col(current_col);
218   wait_us(30);
219
220   // For each row...
221   for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
222     // Store last value of row prior to reading
223     matrix_row_t last_row_value = current_matrix[row_index];
224
225     // Check row pin state
226     if (readPin(row_pins[row_index])) {
227       // Pin HI, clear col bit
228       current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
229     } else {
230       // Pin LO, set col bit
231       current_matrix[row_index] |= (ROW_SHIFTER << current_col);
232     }
233
234     // Determine if the matrix changed state
235     if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
236       matrix_changed = true;
237     }
238   }
239
240   // Unselect col
241   unselect_col(current_col);
242
243   return matrix_changed;
244 }
245
246 #endif
247
248 void matrix_init(void) {
249   debug_enable = true;
250   debug_matrix = true;
251   debug_mouse  = true;
252
253   // Set pinout for right half if pinout for that half is defined
254   if (!isLeftHand) {
255 #ifdef MATRIX_ROW_PINS_RIGHT
256     const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
257     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
258       row_pins[i] = row_pins_right[i];
259     }
260 #endif
261 #ifdef MATRIX_COL_PINS_RIGHT
262     const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
263     for (uint8_t i = 0; i < MATRIX_COLS; i++) {
264       col_pins[i] = col_pins_right[i];
265     }
266 #endif
267   }
268
269   thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
270   thatHand = ROWS_PER_HAND - thisHand;
271
272   // initialize key pins
273   init_pins();
274
275   // initialize matrix state: all keys off
276   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
277     matrix[i] = 0;
278   }
279
280   debounce_init(ROWS_PER_HAND);
281
282   matrix_init_quantum();
283 }
284
285 uint8_t _matrix_scan(void) {
286   bool changed = false;
287
288 #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
289   // Set row, read cols
290   for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
291     changed |= read_cols_on_row(raw_matrix, current_row);
292   }
293 #elif (DIODE_DIRECTION == ROW2COL)
294   // Set col, read rows
295   for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
296     changed |= read_rows_on_col(raw_matrix, current_col);
297   }
298 #endif
299
300   debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed);
301
302   return (uint8_t)changed;
303 }
304
305 uint8_t matrix_scan(void) {
306   uint8_t ret = _matrix_scan();
307
308   if (is_keyboard_master()) {
309     static uint8_t error_count;
310
311     if (!transport_master(matrix + thatHand)) {
312       error_count++;
313
314       if (error_count > ERROR_DISCONNECT_COUNT) {
315         // reset other half if disconnected
316         for (int i = 0; i < ROWS_PER_HAND; ++i) {
317           matrix[thatHand + i] = 0;
318         }
319       }
320     } else {
321       error_count = 0;
322     }
323
324     matrix_scan_quantum();
325   } else {
326     transport_slave(matrix + thisHand);
327 #ifdef ENCODER_ENABLE
328     encoder_read();
329 #endif
330     matrix_slave_scan_user();
331   }
332
333   return ret;
334 }