matrix.c
10467 bytes
1/*
2Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
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 "atomic_util.h"
24
25#ifdef SPLIT_KEYBOARD
26# include "split_common/split_util.h"
27# include "split_common/transactions.h"
28
29# define ROWS_PER_HAND (MATRIX_ROWS / 2)
30#else
31# define ROWS_PER_HAND (MATRIX_ROWS)
32#endif
33
34#ifdef DIRECT_PINS_RIGHT
35# define SPLIT_MUTABLE
36#else
37# define SPLIT_MUTABLE const
38#endif
39#ifdef MATRIX_ROW_PINS_RIGHT
40# define SPLIT_MUTABLE_ROW
41#else
42# define SPLIT_MUTABLE_ROW const
43#endif
44#ifdef MATRIX_COL_PINS_RIGHT
45# define SPLIT_MUTABLE_COL
46#else
47# define SPLIT_MUTABLE_COL const
48#endif
49
50#ifndef MATRIX_INPUT_PRESSED_STATE
51# define MATRIX_INPUT_PRESSED_STATE 0
52#endif
53
54#ifdef DIRECT_PINS
55static SPLIT_MUTABLE pin_t direct_pins[ROWS_PER_HAND][MATRIX_COLS] = DIRECT_PINS;
56#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
57# ifdef MATRIX_ROW_PINS
58static SPLIT_MUTABLE_ROW pin_t row_pins[ROWS_PER_HAND] = MATRIX_ROW_PINS;
59# endif // MATRIX_ROW_PINS
60# ifdef MATRIX_COL_PINS
61static SPLIT_MUTABLE_COL pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
62# endif // MATRIX_COL_PINS
63#endif
64
65/* matrix state(1:on, 0:off) */
66extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
67extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
68
69#ifdef SPLIT_KEYBOARD
70// row offsets for each hand
71extern uint8_t thisHand, thatHand;
72#endif
73
74// user-defined overridable functions
75__attribute__((weak)) void matrix_init_pins(void);
76__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
77__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter);
78
79static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
80 ATOMIC_BLOCK_FORCEON {
81 gpio_set_pin_output(pin);
82 gpio_write_pin_low(pin);
83 }
84}
85
86static inline void gpio_atomic_set_pin_output_high(pin_t pin) {
87 ATOMIC_BLOCK_FORCEON {
88 gpio_set_pin_output(pin);
89 gpio_write_pin_high(pin);
90 }
91}
92
93static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
94 ATOMIC_BLOCK_FORCEON {
95 gpio_set_pin_input_high(pin);
96 }
97}
98
99static inline uint8_t readMatrixPin(pin_t pin) {
100 if (pin != NO_PIN) {
101 return (gpio_read_pin(pin) == MATRIX_INPUT_PRESSED_STATE) ? 0 : 1;
102 } else {
103 return 1;
104 }
105}
106
107// matrix code
108
109#ifdef DIRECT_PINS
110
111__attribute__((weak)) void matrix_init_pins(void) {
112 for (int row = 0; row < ROWS_PER_HAND; row++) {
113 for (int col = 0; col < MATRIX_COLS; col++) {
114 pin_t pin = direct_pins[row][col];
115 if (pin != NO_PIN) {
116 gpio_set_pin_input_high(pin);
117 }
118 }
119 }
120}
121
122__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
123 // Start with a clear matrix row
124 matrix_row_t current_row_value = 0;
125
126 matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
127 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) {
128 pin_t pin = direct_pins[current_row][col_index];
129 current_row_value |= readMatrixPin(pin) ? 0 : row_shifter;
130 }
131
132 // Update the matrix
133 current_matrix[current_row] = current_row_value;
134}
135
136#elif defined(DIODE_DIRECTION)
137# if defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
138# if (DIODE_DIRECTION == COL2ROW)
139
140static bool select_row(uint8_t row) {
141 pin_t pin = row_pins[row];
142 if (pin != NO_PIN) {
143 gpio_atomic_set_pin_output_low(pin);
144 return true;
145 }
146 return false;
147}
148
149static void unselect_row(uint8_t row) {
150 pin_t pin = row_pins[row];
151 if (pin != NO_PIN) {
152# ifdef MATRIX_UNSELECT_DRIVE_HIGH
153 gpio_atomic_set_pin_output_high(pin);
154# else
155 gpio_atomic_set_pin_input_high(pin);
156# endif
157 }
158}
159
160static void unselect_rows(void) {
161 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
162 unselect_row(x);
163 }
164}
165
166__attribute__((weak)) void matrix_init_pins(void) {
167 unselect_rows();
168 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
169 if (col_pins[x] != NO_PIN) {
170 gpio_atomic_set_pin_input_high(col_pins[x]);
171 }
172 }
173}
174
175__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
176 // Start with a clear matrix row
177 matrix_row_t current_row_value = 0;
178
179 if (!select_row(current_row)) { // Select row
180 return; // skip NO_PIN row
181 }
182 matrix_output_select_delay();
183
184 // For each col...
185 matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
186 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) {
187 uint8_t pin_state = readMatrixPin(col_pins[col_index]);
188
189 // Populate the matrix row with the state of the col pin
190 current_row_value |= pin_state ? 0 : row_shifter;
191 }
192
193 // Unselect row
194 unselect_row(current_row);
195 matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for all Col signals to go HIGH
196
197 // Update the matrix
198 current_matrix[current_row] = current_row_value;
199}
200
201# elif (DIODE_DIRECTION == ROW2COL)
202
203static bool select_col(uint8_t col) {
204 pin_t pin = col_pins[col];
205 if (pin != NO_PIN) {
206 gpio_atomic_set_pin_output_low(pin);
207 return true;
208 }
209 return false;
210}
211
212static void unselect_col(uint8_t col) {
213 pin_t pin = col_pins[col];
214 if (pin != NO_PIN) {
215# ifdef MATRIX_UNSELECT_DRIVE_HIGH
216 gpio_atomic_set_pin_output_high(pin);
217# else
218 gpio_atomic_set_pin_input_high(pin);
219# endif
220 }
221}
222
223static void unselect_cols(void) {
224 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
225 unselect_col(x);
226 }
227}
228
229__attribute__((weak)) void matrix_init_pins(void) {
230 unselect_cols();
231 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
232 if (row_pins[x] != NO_PIN) {
233 gpio_atomic_set_pin_input_high(row_pins[x]);
234 }
235 }
236}
237
238__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
239 bool key_pressed = false;
240
241 // Select col
242 if (!select_col(current_col)) { // select col
243 return; // skip NO_PIN col
244 }
245 matrix_output_select_delay();
246
247 // For each row...
248 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
249 // Check row pin state
250 if (readMatrixPin(row_pins[row_index]) == 0) {
251 // Pin LO, set col bit
252 current_matrix[row_index] |= row_shifter;
253 key_pressed = true;
254 } else {
255 // Pin HI, clear col bit
256 current_matrix[row_index] &= ~row_shifter;
257 }
258 }
259
260 // Unselect col
261 unselect_col(current_col);
262 matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH
263}
264
265# else
266# error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
267# endif
268# endif // defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
269#else
270# error DIODE_DIRECTION is not defined!
271#endif
272
273void matrix_init(void) {
274#ifdef SPLIT_KEYBOARD
275 // Set pinout for right half if pinout for that half is defined
276 if (!isLeftHand) {
277# ifdef DIRECT_PINS_RIGHT
278 const pin_t direct_pins_right[ROWS_PER_HAND][MATRIX_COLS] = DIRECT_PINS_RIGHT;
279 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
280 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
281 direct_pins[i][j] = direct_pins_right[i][j];
282 }
283 }
284# endif
285# ifdef MATRIX_ROW_PINS_RIGHT
286 const pin_t row_pins_right[ROWS_PER_HAND] = MATRIX_ROW_PINS_RIGHT;
287 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
288 row_pins[i] = row_pins_right[i];
289 }
290# endif
291# ifdef MATRIX_COL_PINS_RIGHT
292 const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
293 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
294 col_pins[i] = col_pins_right[i];
295 }
296# endif
297 }
298
299 thisHand = isLeftHand ? 0 : (ROWS_PER_HAND);
300 thatHand = ROWS_PER_HAND - thisHand;
301#endif
302
303 // initialize key pins
304 matrix_init_pins();
305
306 // initialize matrix state: all keys off
307 memset(matrix, 0, sizeof(matrix));
308 memset(raw_matrix, 0, sizeof(raw_matrix));
309
310 debounce_init(ROWS_PER_HAND);
311
312 matrix_init_kb();
313}
314
315#ifdef SPLIT_KEYBOARD
316// Fallback implementation for keyboards not using the standard split_util.c
317__attribute__((weak)) bool transport_master_if_connected(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
318 transport_master(master_matrix, slave_matrix);
319 return true; // Treat the transport as always connected
320}
321#endif
322
323uint8_t matrix_scan(void) {
324 matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
325
326#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
327 // Set row, read cols
328 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
329 matrix_read_cols_on_row(curr_matrix, current_row);
330 }
331#elif (DIODE_DIRECTION == ROW2COL)
332 // Set col, read rows
333 matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
334 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
335 matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
336 }
337#endif
338
339 bool changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
340 if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix));
341
342#ifdef SPLIT_KEYBOARD
343 changed = debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed) | matrix_post_scan();
344#else
345 changed = debounce(raw_matrix, matrix, ROWS_PER_HAND, changed);
346 matrix_scan_kb();
347#endif
348 return (uint8_t)changed;
349}