diff options
author | Joel Challis <git@zvecr.com> | 2020-01-04 20:29:44 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-04 20:29:44 +0000 |
commit | dcb7ca3f7910420cfa85ba659d48285b3633a978 (patch) | |
tree | d571ff362775734c533ed5b9d1982c62a609d20b /quantum/split_common/matrix.c | |
parent | c1feeaa57f28c781e39996e5d4eea3a31f083439 (diff) | |
download | qmk_firmware-dcb7ca3f7910420cfa85ba659d48285b3633a978.tar.gz qmk_firmware-dcb7ca3f7910420cfa85ba659d48285b3633a978.zip |
Move some common matrix code to a common location (#7699)
* Move some common matrix code to a common location
* Refactor some 'custom_matrix_helper' logic to use custom matrix lite
* Fix build for kinesis/stapelberg - abuse of vpath was picking up matrix.c from core when custom matrix was enabled
* Add validation for CUSTOM_MATRIX
Diffstat (limited to 'quantum/split_common/matrix.c')
-rw-r--r-- | quantum/split_common/matrix.c | 118 |
1 files changed, 29 insertions, 89 deletions
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index 7176d0cc4..58602af85 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c | |||
@@ -14,10 +14,6 @@ GNU General Public License for more details. | |||
14 | You should have received a copy of the GNU General Public License | 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/>. | 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | */ | 16 | */ |
17 | |||
18 | /* | ||
19 | * scan matrix | ||
20 | */ | ||
21 | #include <stdint.h> | 17 | #include <stdint.h> |
22 | #include <stdbool.h> | 18 | #include <stdbool.h> |
23 | #include "wait.h" | 19 | #include "wait.h" |
@@ -33,23 +29,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
33 | # include "encoder.h" | 29 | # include "encoder.h" |
34 | #endif | 30 | #endif |
35 | 31 | ||
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 | 32 | #define ERROR_DISCONNECT_COUNT 5 |
54 | 33 | ||
55 | #define ROWS_PER_HAND (MATRIX_ROWS / 2) | 34 | #define ROWS_PER_HAND (MATRIX_ROWS / 2) |
@@ -62,58 +41,21 @@ static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | |||
62 | #endif | 41 | #endif |
63 | 42 | ||
64 | /* matrix state(1:on, 0:off) */ | 43 | /* matrix state(1:on, 0:off) */ |
65 | static matrix_row_t matrix[MATRIX_ROWS]; | 44 | static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values |
66 | static matrix_row_t raw_matrix[ROWS_PER_HAND]; | 45 | static matrix_row_t matrix[MATRIX_ROWS]; // debounced values |
67 | 46 | ||
68 | // row offsets for each hand | 47 | // row offsets for each hand |
69 | uint8_t thisHand, thatHand; | 48 | uint8_t thisHand, thatHand; |
70 | 49 | ||
71 | // user-defined overridable functions | 50 | // 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) {} | 51 | __attribute__((weak)) void matrix_slave_scan_user(void) {} |
82 | 52 | ||
83 | // helper functions | 53 | // helper functions |
84 | 54 | ||
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)); } | 55 | inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } |
95 | 56 | ||
96 | inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } | 57 | inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } |
97 | 58 | ||
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 | 59 | // matrix code |
118 | 60 | ||
119 | #ifdef DIRECT_PINS | 61 | #ifdef DIRECT_PINS |
@@ -136,7 +78,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
136 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | 78 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { |
137 | pin_t pin = direct_pins[current_row][col_index]; | 79 | pin_t pin = direct_pins[current_row][col_index]; |
138 | if (pin != NO_PIN) { | 80 | if (pin != NO_PIN) { |
139 | current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index); | 81 | current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); |
140 | } | 82 | } |
141 | } | 83 | } |
142 | 84 | ||
@@ -179,7 +121,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
179 | // For each col... | 121 | // For each col... |
180 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | 122 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { |
181 | // Populate the matrix row with the state of the col pin | 123 | // 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); | 124 | current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (MATRIX_ROW_SHIFTER << col_index); |
183 | } | 125 | } |
184 | 126 | ||
185 | // Unselect row | 127 | // Unselect row |
@@ -225,10 +167,10 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | |||
225 | // Check row pin state | 167 | // Check row pin state |
226 | if (readPin(row_pins[row_index])) { | 168 | if (readPin(row_pins[row_index])) { |
227 | // Pin HI, clear col bit | 169 | // Pin HI, clear col bit |
228 | current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); | 170 | current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); |
229 | } else { | 171 | } else { |
230 | // Pin LO, set col bit | 172 | // Pin LO, set col bit |
231 | current_matrix[row_index] |= (ROW_SHIFTER << current_col); | 173 | current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); |
232 | } | 174 | } |
233 | 175 | ||
234 | // Determine if the matrix changed state | 176 | // Determine if the matrix changed state |
@@ -280,7 +222,8 @@ void matrix_init(void) { | |||
280 | 222 | ||
281 | // initialize matrix state: all keys off | 223 | // initialize matrix state: all keys off |
282 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | 224 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { |
283 | matrix[i] = 0; | 225 | raw_matrix[i] = 0; |
226 | matrix[i] = 0; | ||
284 | } | 227 | } |
285 | 228 | ||
286 | debounce_init(ROWS_PER_HAND); | 229 | debounce_init(ROWS_PER_HAND); |
@@ -288,29 +231,7 @@ void matrix_init(void) { | |||
288 | matrix_init_quantum(); | 231 | matrix_init_quantum(); |
289 | } | 232 | } |
290 | 233 | ||
291 | uint8_t _matrix_scan(void) { | 234 | void matrix_post_scan(void) { |
292 | bool changed = false; | ||
293 | |||
294 | #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) | ||
295 | // Set row, read cols | ||
296 | for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) { | ||
297 | changed |= read_cols_on_row(raw_matrix, current_row); | ||
298 | } | ||
299 | #elif (DIODE_DIRECTION == ROW2COL) | ||
300 | // Set col, read rows | ||
301 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
302 | changed |= read_rows_on_col(raw_matrix, current_col); | ||
303 | } | ||
304 | #endif | ||
305 | |||
306 | debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed); | ||
307 | |||
308 | return (uint8_t)changed; | ||
309 | } | ||
310 | |||
311 | uint8_t matrix_scan(void) { | ||
312 | uint8_t ret = _matrix_scan(); | ||
313 | |||
314 | if (is_keyboard_master()) { | 235 | if (is_keyboard_master()) { |
315 | static uint8_t error_count; | 236 | static uint8_t error_count; |
316 | 237 | ||
@@ -335,6 +256,25 @@ uint8_t matrix_scan(void) { | |||
335 | #endif | 256 | #endif |
336 | matrix_slave_scan_user(); | 257 | matrix_slave_scan_user(); |
337 | } | 258 | } |
259 | } | ||
260 | |||
261 | uint8_t matrix_scan(void) { | ||
262 | bool changed = false; | ||
263 | |||
264 | #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) | ||
265 | // Set row, read cols | ||
266 | for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) { | ||
267 | changed |= read_cols_on_row(raw_matrix, current_row); | ||
268 | } | ||
269 | #elif (DIODE_DIRECTION == ROW2COL) | ||
270 | // Set col, read rows | ||
271 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
272 | changed |= read_rows_on_col(raw_matrix, current_col); | ||
273 | } | ||
274 | #endif | ||
338 | 275 | ||
339 | return ret; | 276 | debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed); |
277 | |||
278 | matrix_post_scan(); | ||
279 | return (uint8_t)changed; | ||
340 | } | 280 | } |