aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
Diffstat (limited to 'quantum')
-rw-r--r--quantum/matrix.c68
-rw-r--r--quantum/matrix_common.c59
-rw-r--r--quantum/split_common/matrix.c118
-rw-r--r--quantum/split_common/matrix.h3
4 files changed, 94 insertions, 154 deletions
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
45extern const matrix_row_t matrix_mask[]; 26extern 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;
56static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values 37static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
57static matrix_row_t matrix[MATRIX_ROWS]; // debounced values 38static 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
71inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
72
73inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
74
75// Deprecated.
76bool matrix_is_modified(void) {
77 if (debounce_active()) return false;
78 return true;
79}
80 41
81inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } 42inline 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
93void 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
104uint8_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
18inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
19
20inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
21
22// Deprecated.
23bool 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
42void 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
53uint8_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.
14You should have received a copy of the GNU General Public License 14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>. 15along 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) */
65static matrix_row_t matrix[MATRIX_ROWS]; 44static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
66static matrix_row_t raw_matrix[ROWS_PER_HAND]; 45static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
67 46
68// row offsets for each hand 47// row offsets for each hand
69uint8_t thisHand, thatHand; 48uint8_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
85inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
86
87inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
88
89bool matrix_is_modified(void) {
90 if (debounce_active()) return false;
91 return true;
92}
93
94inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } 55inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
95 56
96inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } 57inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
97 58
98void 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
109uint8_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
291uint8_t _matrix_scan(void) { 234void 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
311uint8_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
261uint8_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>