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