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 | |
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
-rw-r--r-- | common_features.mk | 26 | ||||
-rw-r--r-- | keyboards/kinesis/alvicstep/rules.mk | 4 | ||||
-rw-r--r-- | keyboards/kinesis/rules.mk | 4 | ||||
-rw-r--r-- | keyboards/xd84/custom_matrix_helper.c | 56 | ||||
-rw-r--r-- | keyboards/xd84/rules.mk | 2 | ||||
-rw-r--r-- | keyboards/xd96/custom_matrix_helper.c | 56 | ||||
-rw-r--r-- | keyboards/xd96/rules.mk | 2 | ||||
-rw-r--r-- | quantum/matrix.c | 68 | ||||
-rw-r--r-- | quantum/matrix_common.c | 59 | ||||
-rw-r--r-- | quantum/split_common/matrix.c | 118 | ||||
-rw-r--r-- | quantum/split_common/matrix.h | 3 | ||||
-rw-r--r-- | tmk_core/common/matrix.h | 7 |
12 files changed, 123 insertions, 282 deletions
diff --git a/common_features.mk b/common_features.mk index 92b24bb20..67c64b425 100644 --- a/common_features.mk +++ b/common_features.mk | |||
@@ -374,12 +374,28 @@ QUANTUM_SRC:= \ | |||
374 | $(QUANTUM_DIR)/keymap_common.c \ | 374 | $(QUANTUM_DIR)/keymap_common.c \ |
375 | $(QUANTUM_DIR)/keycode_config.c | 375 | $(QUANTUM_DIR)/keycode_config.c |
376 | 376 | ||
377 | # Include the standard or split matrix code if needed | 377 | |
378 | |||
379 | VALID_CUSTOM_MATRIX_TYPES:= yes lite no | ||
380 | |||
381 | CUSTOM_MATRIX ?= no | ||
382 | |||
378 | ifneq ($(strip $(CUSTOM_MATRIX)), yes) | 383 | ifneq ($(strip $(CUSTOM_MATRIX)), yes) |
379 | ifeq ($(strip $(SPLIT_KEYBOARD)), yes) | 384 | ifeq ($(filter $(CUSTOM_MATRIX),$(VALID_CUSTOM_MATRIX_TYPES)),) |
380 | QUANTUM_SRC += $(QUANTUM_DIR)/split_common/matrix.c | 385 | $(error CUSTOM_MATRIX="$(CUSTOM_MATRIX)" is not a valid custom matrix type) |
381 | else | 386 | endif |
382 | QUANTUM_SRC += $(QUANTUM_DIR)/matrix.c | 387 | |
388 | # Include common stuff for all non custom matrix users | ||
389 | QUANTUM_SRC += $(QUANTUM_DIR)/matrix_common.c | ||
390 | |||
391 | # if 'lite' then skip the actual matrix implementation | ||
392 | ifneq ($(strip $(CUSTOM_MATRIX)), lite) | ||
393 | # Include the standard or split matrix code if needed | ||
394 | ifeq ($(strip $(SPLIT_KEYBOARD)), yes) | ||
395 | QUANTUM_SRC += $(QUANTUM_DIR)/split_common/matrix.c | ||
396 | else | ||
397 | QUANTUM_SRC += $(QUANTUM_DIR)/matrix.c | ||
398 | endif | ||
383 | endif | 399 | endif |
384 | endif | 400 | endif |
385 | 401 | ||
diff --git a/keyboards/kinesis/alvicstep/rules.mk b/keyboards/kinesis/alvicstep/rules.mk index e69de29bb..232ff719c 100644 --- a/keyboards/kinesis/alvicstep/rules.mk +++ b/keyboards/kinesis/alvicstep/rules.mk | |||
@@ -0,0 +1,4 @@ | |||
1 | CUSTOM_MATRIX = yes # need to do our own thing with the matrix | ||
2 | |||
3 | # Project specific files | ||
4 | SRC += matrix.c | ||
diff --git a/keyboards/kinesis/rules.mk b/keyboards/kinesis/rules.mk index 295054d75..faa3e454c 100644 --- a/keyboards/kinesis/rules.mk +++ b/keyboards/kinesis/rules.mk | |||
@@ -28,9 +28,5 @@ MIDI_ENABLE = no # MIDI controls | |||
28 | UNICODE_ENABLE = no # Unicode | 28 | UNICODE_ENABLE = no # Unicode |
29 | BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID | 29 | BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID |
30 | AUDIO_ENABLE = no # Audio output should be port E6, current quantum library hardcodes C6, which we use for programming | 30 | AUDIO_ENABLE = no # Audio output should be port E6, current quantum library hardcodes C6, which we use for programming |
31 | CUSTOM_MATRIX=yes # need to do our own thing with the matrix | ||
32 | 31 | ||
33 | DEFAULT_FOLDER = kinesis/alvicstep | 32 | DEFAULT_FOLDER = kinesis/alvicstep |
34 | |||
35 | # Project specific files | ||
36 | SRC = matrix.c | ||
diff --git a/keyboards/xd84/custom_matrix_helper.c b/keyboards/xd84/custom_matrix_helper.c index a4c5b6afa..e4e256381 100644 --- a/keyboards/xd84/custom_matrix_helper.c +++ b/keyboards/xd84/custom_matrix_helper.c | |||
@@ -23,72 +23,16 @@ | |||
23 | #include "debounce.h" | 23 | #include "debounce.h" |
24 | #include "quantum.h" | 24 | #include "quantum.h" |
25 | 25 | ||
26 | //_____COMMON__________________________________________________________________ | ||
27 | // user-defined overridable functions | ||
28 | __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } | ||
29 | __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } | ||
30 | __attribute__((weak)) void matrix_init_user(void) {} | ||
31 | __attribute__((weak)) void matrix_scan_user(void) {} | ||
32 | |||
33 | |||
34 | //_____COULD BE COMMON_________________________________________________________ | 26 | //_____COULD BE COMMON_________________________________________________________ |
35 | /* matrix state(1:on, 0:off) */ | 27 | /* matrix state(1:on, 0:off) */ |
36 | /*static*/ matrix_row_t raw_matrix[MATRIX_ROWS]; | 28 | /*static*/ matrix_row_t raw_matrix[MATRIX_ROWS]; |
37 | /*static*/ matrix_row_t matrix[MATRIX_ROWS]; | 29 | /*static*/ matrix_row_t matrix[MATRIX_ROWS]; |
38 | 30 | ||
39 | #if (MATRIX_COLS <= 8) | ||
40 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
41 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
42 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
43 | # define ROW_SHIFTER ((uint8_t)1) | ||
44 | #elif (MATRIX_COLS <= 16) | ||
45 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
46 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
47 | # define matrix_bitpop(i) bitpop16(matrix[i]) | ||
48 | # define ROW_SHIFTER ((uint16_t)1) | ||
49 | #elif (MATRIX_COLS <= 32) | ||
50 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
51 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
52 | # define matrix_bitpop(i) bitpop32(matrix[i]) | ||
53 | # define ROW_SHIFTER ((uint32_t)1) | ||
54 | #endif | ||
55 | |||
56 | __attribute__ ((weak)) | ||
57 | uint8_t matrix_rows(void) { | ||
58 | return MATRIX_ROWS; | ||
59 | } | ||
60 | |||
61 | __attribute__ ((weak)) | ||
62 | uint8_t matrix_cols(void) { | ||
63 | return MATRIX_COLS; | ||
64 | } | ||
65 | |||
66 | __attribute__ ((weak)) | 31 | __attribute__ ((weak)) |
67 | matrix_row_t matrix_get_row(uint8_t row) { | 32 | matrix_row_t matrix_get_row(uint8_t row) { |
68 | return matrix[row]; | 33 | return matrix[row]; |
69 | } | 34 | } |
70 | 35 | ||
71 | __attribute__ ((weak)) | ||
72 | uint8_t matrix_key_count(void) { | ||
73 | uint8_t count = 0; | ||
74 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
75 | count += matrix_bitpop(i); | ||
76 | } | ||
77 | return count; | ||
78 | } | ||
79 | |||
80 | __attribute__ ((weak)) | ||
81 | void matrix_print(void) { | ||
82 | print_matrix_header(); | ||
83 | |||
84 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
85 | phex(row); print(": "); | ||
86 | print_matrix_row(row); | ||
87 | print("\n"); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | |||
92 | //_____CUSTOM MATRIX 'LITE'____________________________________________________ | 36 | //_____CUSTOM MATRIX 'LITE'____________________________________________________ |
93 | __attribute__ ((weak)) | 37 | __attribute__ ((weak)) |
94 | void custom_matrix_init(void) { | 38 | void custom_matrix_init(void) { |
diff --git a/keyboards/xd84/rules.mk b/keyboards/xd84/rules.mk index d9d316c13..4a460d91f 100644 --- a/keyboards/xd84/rules.mk +++ b/keyboards/xd84/rules.mk | |||
@@ -34,7 +34,7 @@ HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400) | |||
34 | LINK_TIME_OPTIMIZATION_ENABLE = yes | 34 | LINK_TIME_OPTIMIZATION_ENABLE = yes |
35 | 35 | ||
36 | # custom matrix setup | 36 | # custom matrix setup |
37 | CUSTOM_MATRIX = yes | 37 | CUSTOM_MATRIX = lite |
38 | 38 | ||
39 | VPATH += drivers/gpio | 39 | VPATH += drivers/gpio |
40 | SRC += custom_matrix_helper.c pca9555.c matrix.c | 40 | SRC += custom_matrix_helper.c pca9555.c matrix.c |
diff --git a/keyboards/xd96/custom_matrix_helper.c b/keyboards/xd96/custom_matrix_helper.c index a4c5b6afa..e4e256381 100644 --- a/keyboards/xd96/custom_matrix_helper.c +++ b/keyboards/xd96/custom_matrix_helper.c | |||
@@ -23,72 +23,16 @@ | |||
23 | #include "debounce.h" | 23 | #include "debounce.h" |
24 | #include "quantum.h" | 24 | #include "quantum.h" |
25 | 25 | ||
26 | //_____COMMON__________________________________________________________________ | ||
27 | // user-defined overridable functions | ||
28 | __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } | ||
29 | __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } | ||
30 | __attribute__((weak)) void matrix_init_user(void) {} | ||
31 | __attribute__((weak)) void matrix_scan_user(void) {} | ||
32 | |||
33 | |||
34 | //_____COULD BE COMMON_________________________________________________________ | 26 | //_____COULD BE COMMON_________________________________________________________ |
35 | /* matrix state(1:on, 0:off) */ | 27 | /* matrix state(1:on, 0:off) */ |
36 | /*static*/ matrix_row_t raw_matrix[MATRIX_ROWS]; | 28 | /*static*/ matrix_row_t raw_matrix[MATRIX_ROWS]; |
37 | /*static*/ matrix_row_t matrix[MATRIX_ROWS]; | 29 | /*static*/ matrix_row_t matrix[MATRIX_ROWS]; |
38 | 30 | ||
39 | #if (MATRIX_COLS <= 8) | ||
40 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
41 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
42 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
43 | # define ROW_SHIFTER ((uint8_t)1) | ||
44 | #elif (MATRIX_COLS <= 16) | ||
45 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
46 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
47 | # define matrix_bitpop(i) bitpop16(matrix[i]) | ||
48 | # define ROW_SHIFTER ((uint16_t)1) | ||
49 | #elif (MATRIX_COLS <= 32) | ||
50 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
51 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
52 | # define matrix_bitpop(i) bitpop32(matrix[i]) | ||
53 | # define ROW_SHIFTER ((uint32_t)1) | ||
54 | #endif | ||
55 | |||
56 | __attribute__ ((weak)) | ||
57 | uint8_t matrix_rows(void) { | ||
58 | return MATRIX_ROWS; | ||
59 | } | ||
60 | |||
61 | __attribute__ ((weak)) | ||
62 | uint8_t matrix_cols(void) { | ||
63 | return MATRIX_COLS; | ||
64 | } | ||
65 | |||
66 | __attribute__ ((weak)) | 31 | __attribute__ ((weak)) |
67 | matrix_row_t matrix_get_row(uint8_t row) { | 32 | matrix_row_t matrix_get_row(uint8_t row) { |
68 | return matrix[row]; | 33 | return matrix[row]; |
69 | } | 34 | } |
70 | 35 | ||
71 | __attribute__ ((weak)) | ||
72 | uint8_t matrix_key_count(void) { | ||
73 | uint8_t count = 0; | ||
74 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
75 | count += matrix_bitpop(i); | ||
76 | } | ||
77 | return count; | ||
78 | } | ||
79 | |||
80 | __attribute__ ((weak)) | ||
81 | void matrix_print(void) { | ||
82 | print_matrix_header(); | ||
83 | |||
84 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
85 | phex(row); print(": "); | ||
86 | print_matrix_row(row); | ||
87 | print("\n"); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | |||
92 | //_____CUSTOM MATRIX 'LITE'____________________________________________________ | 36 | //_____CUSTOM MATRIX 'LITE'____________________________________________________ |
93 | __attribute__ ((weak)) | 37 | __attribute__ ((weak)) |
94 | void custom_matrix_init(void) { | 38 | void custom_matrix_init(void) { |
diff --git a/keyboards/xd96/rules.mk b/keyboards/xd96/rules.mk index 2079e436c..d5ac5df36 100644 --- a/keyboards/xd96/rules.mk +++ b/keyboards/xd96/rules.mk | |||
@@ -34,7 +34,7 @@ HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400) | |||
34 | LINK_TIME_OPTIMIZATION_ENABLE = yes | 34 | LINK_TIME_OPTIMIZATION_ENABLE = yes |
35 | 35 | ||
36 | # custom matrix setup | 36 | # custom matrix setup |
37 | CUSTOM_MATRIX = yes | 37 | CUSTOM_MATRIX = lite |
38 | 38 | ||
39 | VPATH += drivers/gpio | 39 | VPATH += drivers/gpio |
40 | SRC += custom_matrix_helper.c pca9555.c matrix.c | 40 | SRC += custom_matrix_helper.c pca9555.c matrix.c |
diff --git a/quantum/matrix.c b/quantum/matrix.c index 907492a0f..62a86fba6 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c | |||
@@ -17,30 +17,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
17 | #include <stdint.h> | 17 | #include <stdint.h> |
18 | #include <stdbool.h> | 18 | #include <stdbool.h> |
19 | #include "wait.h" | 19 | #include "wait.h" |
20 | #include "print.h" | ||
21 | #include "debug.h" | ||
22 | #include "util.h" | 20 | #include "util.h" |
23 | #include "matrix.h" | 21 | #include "matrix.h" |
24 | #include "debounce.h" | 22 | #include "debounce.h" |
25 | #include "quantum.h" | 23 | #include "quantum.h" |
26 | 24 | ||
27 | #if (MATRIX_COLS <= 8) | ||
28 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
29 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
30 | # define matrix_bitpop(i) bitpop(matrix[i]) | ||
31 | # define ROW_SHIFTER ((uint8_t)1) | ||
32 | #elif (MATRIX_COLS <= 16) | ||
33 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
34 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
35 | # define matrix_bitpop(i) bitpop16(matrix[i]) | ||
36 | # define ROW_SHIFTER ((uint16_t)1) | ||
37 | #elif (MATRIX_COLS <= 32) | ||
38 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
39 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
40 | # define matrix_bitpop(i) bitpop32(matrix[i]) | ||
41 | # define ROW_SHIFTER ((uint32_t)1) | ||
42 | #endif | ||
43 | |||
44 | #ifdef MATRIX_MASKED | 25 | #ifdef MATRIX_MASKED |
45 | extern const matrix_row_t matrix_mask[]; | 26 | extern const matrix_row_t matrix_mask[]; |
46 | #endif | 27 | #endif |
@@ -56,27 +37,7 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | |||
56 | static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values | 37 | static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values |
57 | static matrix_row_t matrix[MATRIX_ROWS]; // debounced values | 38 | static matrix_row_t matrix[MATRIX_ROWS]; // debounced values |
58 | 39 | ||
59 | __attribute__((weak)) void matrix_init_quantum(void) { matrix_init_kb(); } | 40 | // helper functions |
60 | |||
61 | __attribute__((weak)) void matrix_scan_quantum(void) { matrix_scan_kb(); } | ||
62 | |||
63 | __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } | ||
64 | |||
65 | __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } | ||
66 | |||
67 | __attribute__((weak)) void matrix_init_user(void) {} | ||
68 | |||
69 | __attribute__((weak)) void matrix_scan_user(void) {} | ||
70 | |||
71 | inline uint8_t matrix_rows(void) { return MATRIX_ROWS; } | ||
72 | |||
73 | inline uint8_t matrix_cols(void) { return MATRIX_COLS; } | ||
74 | |||
75 | // Deprecated. | ||
76 | bool matrix_is_modified(void) { | ||
77 | if (debounce_active()) return false; | ||
78 | return true; | ||
79 | } | ||
80 | 41 | ||
81 | inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } | 42 | inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } |
82 | 43 | ||
@@ -90,24 +51,7 @@ inline matrix_row_t matrix_get_row(uint8_t row) { | |||
90 | #endif | 51 | #endif |
91 | } | 52 | } |
92 | 53 | ||
93 | void matrix_print(void) { | 54 | // matrix code |
94 | print_matrix_header(); | ||
95 | |||
96 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
97 | phex(row); | ||
98 | print(": "); | ||
99 | print_matrix_row(row); | ||
100 | print("\n"); | ||
101 | } | ||
102 | } | ||
103 | |||
104 | uint8_t matrix_key_count(void) { | ||
105 | uint8_t count = 0; | ||
106 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
107 | count += matrix_bitpop(i); | ||
108 | } | ||
109 | return count; | ||
110 | } | ||
111 | 55 | ||
112 | #ifdef DIRECT_PINS | 56 | #ifdef DIRECT_PINS |
113 | 57 | ||
@@ -129,7 +73,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
129 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | 73 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { |
130 | pin_t pin = direct_pins[current_row][col_index]; | 74 | pin_t pin = direct_pins[current_row][col_index]; |
131 | if (pin != NO_PIN) { | 75 | if (pin != NO_PIN) { |
132 | current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index); | 76 | current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); |
133 | } | 77 | } |
134 | } | 78 | } |
135 | 79 | ||
@@ -175,7 +119,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
175 | uint8_t pin_state = readPin(col_pins[col_index]); | 119 | uint8_t pin_state = readPin(col_pins[col_index]); |
176 | 120 | ||
177 | // Populate the matrix row with the state of the col pin | 121 | // Populate the matrix row with the state of the col pin |
178 | current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); | 122 | current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); |
179 | } | 123 | } |
180 | 124 | ||
181 | // Unselect row | 125 | // Unselect row |
@@ -221,10 +165,10 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | |||
221 | // Check row pin state | 165 | // Check row pin state |
222 | if (readPin(row_pins[row_index]) == 0) { | 166 | if (readPin(row_pins[row_index]) == 0) { |
223 | // Pin LO, set col bit | 167 | // Pin LO, set col bit |
224 | current_matrix[row_index] |= (ROW_SHIFTER << current_col); | 168 | current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); |
225 | } else { | 169 | } else { |
226 | // Pin HI, clear col bit | 170 | // Pin HI, clear col bit |
227 | current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); | 171 | current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); |
228 | } | 172 | } |
229 | 173 | ||
230 | // Determine if the matrix changed state | 174 | // Determine if the matrix changed state |
diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c new file mode 100644 index 000000000..22704e8ee --- /dev/null +++ b/quantum/matrix_common.c | |||
@@ -0,0 +1,59 @@ | |||
1 | #include "matrix.h" | ||
2 | #include "debounce.h" | ||
3 | #include "print.h" | ||
4 | #include "debug.h" | ||
5 | |||
6 | // user-defined overridable functions | ||
7 | |||
8 | __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } | ||
9 | |||
10 | __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } | ||
11 | |||
12 | __attribute__((weak)) void matrix_init_user(void) {} | ||
13 | |||
14 | __attribute__((weak)) void matrix_scan_user(void) {} | ||
15 | |||
16 | // helper functions | ||
17 | |||
18 | inline uint8_t matrix_rows(void) { return MATRIX_ROWS; } | ||
19 | |||
20 | inline uint8_t matrix_cols(void) { return MATRIX_COLS; } | ||
21 | |||
22 | // Deprecated. | ||
23 | bool matrix_is_modified(void) { | ||
24 | if (debounce_active()) return false; | ||
25 | return true; | ||
26 | } | ||
27 | |||
28 | #if (MATRIX_COLS <= 8) | ||
29 | # define print_matrix_header() print("\nr/c 01234567\n") | ||
30 | # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) | ||
31 | # define matrix_bitpop(row) bitpop(matrix_get_row(row)) | ||
32 | #elif (MATRIX_COLS <= 16) | ||
33 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") | ||
34 | # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) | ||
35 | # define matrix_bitpop(row) bitpop16(matrix_get_row(row)) | ||
36 | #elif (MATRIX_COLS <= 32) | ||
37 | # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") | ||
38 | # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) | ||
39 | # define matrix_bitpop(row) bitpop32(matrix_get_row(row)) | ||
40 | #endif | ||
41 | |||
42 | void matrix_print(void) { | ||
43 | print_matrix_header(); | ||
44 | |||
45 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
46 | phex(row); | ||
47 | print(": "); | ||
48 | print_matrix_row(row); | ||
49 | print("\n"); | ||
50 | } | ||
51 | } | ||
52 | |||
53 | uint8_t matrix_key_count(void) { | ||
54 | uint8_t count = 0; | ||
55 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
56 | count += matrix_bitpop(i); | ||
57 | } | ||
58 | return count; | ||
59 | } | ||
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 | } |
diff --git a/quantum/split_common/matrix.h b/quantum/split_common/matrix.h deleted file mode 100644 index c2bdd3098..000000000 --- a/quantum/split_common/matrix.h +++ /dev/null | |||
@@ -1,3 +0,0 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <common/matrix.h> | ||
diff --git a/tmk_core/common/matrix.h b/tmk_core/common/matrix.h index 7624d5137..a2fedf5ff 100644 --- a/tmk_core/common/matrix.h +++ b/tmk_core/common/matrix.h | |||
@@ -40,6 +40,8 @@ typedef uint32_t matrix_col_t; | |||
40 | # error "MATRIX_ROWS: invalid value" | 40 | # error "MATRIX_ROWS: invalid value" |
41 | #endif | 41 | #endif |
42 | 42 | ||
43 | #define MATRIX_ROW_SHIFTER ((matrix_row_t)1) | ||
44 | |||
43 | #define MATRIX_IS_ON(row, col) (matrix_get_row(row) && (1 << col)) | 45 | #define MATRIX_IS_ON(row, col) (matrix_get_row(row) && (1 << col)) |
44 | 46 | ||
45 | #ifdef __cplusplus | 47 | #ifdef __cplusplus |
@@ -79,11 +81,6 @@ void matrix_scan_kb(void); | |||
79 | void matrix_init_user(void); | 81 | void matrix_init_user(void); |
80 | void matrix_scan_user(void); | 82 | void matrix_scan_user(void); |
81 | 83 | ||
82 | #ifdef I2C_SPLIT | ||
83 | void slave_matrix_init(void); | ||
84 | uint8_t slave_matrix_scan(void); | ||
85 | #endif | ||
86 | |||
87 | #ifdef __cplusplus | 84 | #ifdef __cplusplus |
88 | } | 85 | } |
89 | #endif | 86 | #endif |