aboutsummaryrefslogtreecommitdiff
path: root/quantum/split_common/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/split_common/matrix.c')
-rw-r--r--quantum/split_common/matrix.c124
1 files changed, 29 insertions, 95 deletions
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c
index 7176d0cc4..ed1ff5acf 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,15 @@ 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]; 44extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
66static matrix_row_t raw_matrix[ROWS_PER_HAND]; 45extern 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
84
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)); }
95
96inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
97
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 53// matrix code
118 54
119#ifdef DIRECT_PINS 55#ifdef DIRECT_PINS
@@ -136,7 +72,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++) { 72 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
137 pin_t pin = direct_pins[current_row][col_index]; 73 pin_t pin = direct_pins[current_row][col_index];
138 if (pin != NO_PIN) { 74 if (pin != NO_PIN) {
139 current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index); 75 current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
140 } 76 }
141 } 77 }
142 78
@@ -179,7 +115,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
179 // For each col... 115 // For each col...
180 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 116 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
181 // Populate the matrix row with the state of the col pin 117 // 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); 118 current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
183 } 119 }
184 120
185 // Unselect row 121 // Unselect row
@@ -225,10 +161,10 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
225 // Check row pin state 161 // Check row pin state
226 if (readPin(row_pins[row_index])) { 162 if (readPin(row_pins[row_index])) {
227 // Pin HI, clear col bit 163 // Pin HI, clear col bit
228 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 164 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
229 } else { 165 } else {
230 // Pin LO, set col bit 166 // Pin LO, set col bit
231 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 167 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
232 } 168 }
233 169
234 // Determine if the matrix changed state 170 // Determine if the matrix changed state
@@ -280,7 +216,8 @@ void matrix_init(void) {
280 216
281 // initialize matrix state: all keys off 217 // initialize matrix state: all keys off
282 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 218 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
283 matrix[i] = 0; 219 raw_matrix[i] = 0;
220 matrix[i] = 0;
284 } 221 }
285 222
286 debounce_init(ROWS_PER_HAND); 223 debounce_init(ROWS_PER_HAND);
@@ -288,29 +225,7 @@ void matrix_init(void) {
288 matrix_init_quantum(); 225 matrix_init_quantum();
289} 226}
290 227
291uint8_t _matrix_scan(void) { 228void 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()) { 229 if (is_keyboard_master()) {
315 static uint8_t error_count; 230 static uint8_t error_count;
316 231
@@ -335,6 +250,25 @@ uint8_t matrix_scan(void) {
335#endif 250#endif
336 matrix_slave_scan_user(); 251 matrix_slave_scan_user();
337 } 252 }
253}
254
255uint8_t matrix_scan(void) {
256 bool changed = false;
257
258#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
259 // Set row, read cols
260 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
261 changed |= read_cols_on_row(raw_matrix, current_row);
262 }
263#elif (DIODE_DIRECTION == ROW2COL)
264 // Set col, read rows
265 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
266 changed |= read_rows_on_col(raw_matrix, current_col);
267 }
268#endif
269
270 debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed);
338 271
339 return ret; 272 matrix_post_scan();
273 return (uint8_t)changed;
340} 274}