summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorフィルターペーパー <76888457+filterpaper@users.noreply.github.com>2025-10-19 10:14:37 +0800
committerGitHub <noreply@github.com>2025-10-19 03:14:37 +0100
commit81df54308687713371ed5fbf4947e38963c7867b (patch)
tree36d0a432adcf7613f8a362f3a534e68b4ddaad19
parent4f21beb7153c2b0a1f4d41de7dad5a2173f896b6 (diff)
Debounce: Deprecate num_rows parameter (#25632)
-rw-r--r--docs/ChangeLog/20251130/PR25632.md4
-rw-r--r--docs/custom_matrix.md4
-rw-r--r--keyboards/bpiphany/pegasushoof/2015/matrix.c198
-rw-r--r--keyboards/gboards/gergo/gergo.c3
-rw-r--r--keyboards/gboards/gergo/gergo.h3
-rw-r--r--keyboards/gboards/gergo/matrix.c319
-rw-r--r--keyboards/gboards/gergoplex/keymaps/default/config.h3
-rw-r--r--keyboards/gboards/gergoplex/matrix.c35
-rw-r--r--keyboards/halfcliff/matrix.c56
-rw-r--r--keyboards/handwired/owlet60/matrix.c144
-rw-r--r--keyboards/handwired/symmetric70_proto/matrix_debug/matrix.c42
-rw-r--r--keyboards/handwired/symmetric70_proto/matrix_fast/matrix.c4
-rw-r--r--keyboards/kakunpc/angel64/alpha/matrix.c160
-rw-r--r--keyboards/kakunpc/angel64/rev1/matrix.c160
-rw-r--r--keyboards/kakunpc/thedogkeyboard/matrix.c160
-rw-r--r--keyboards/kbdmania/kmac/matrix.c32
-rw-r--r--[-rwxr-xr-x]keyboards/redscarf_iiplus/verb/matrix.c183
-rw-r--r--[-rwxr-xr-x]keyboards/redscarf_iiplus/verc/matrix.c183
-rw-r--r--keyboards/redscarf_iiplus/verd/matrix.c183
-rw-r--r--keyboards/ymdk/sp64/keymaps/default/keymap.c3
-rw-r--r--keyboards/ymdk/sp64/matrix.c169
-rw-r--r--quantum/debounce.h5
-rw-r--r--quantum/debounce/asym_eager_defer_pk.c4
-rw-r--r--quantum/debounce/none.c6
-rw-r--r--quantum/debounce/sym_defer_g.c6
-rw-r--r--quantum/debounce/sym_defer_pk.c4
-rw-r--r--quantum/debounce/sym_defer_pr.c4
-rw-r--r--quantum/debounce/sym_eager_pk.c4
-rw-r--r--quantum/debounce/sym_eager_pr.c4
-rw-r--r--quantum/debounce/tests/debounce_test_common.cpp4
-rw-r--r--quantum/matrix.c6
-rw-r--r--quantum/matrix_common.c6
32 files changed, 989 insertions, 1112 deletions
diff --git a/docs/ChangeLog/20251130/PR25632.md b/docs/ChangeLog/20251130/PR25632.md
new file mode 100644
index 0000000000..39136067b9
--- /dev/null
+++ b/docs/ChangeLog/20251130/PR25632.md
@@ -0,0 +1,4 @@
1# Changes Requiring User Action
2## Debounce: Deprecate init and remove num_rows parameter [#25632](https://github.com/qmk/qmk_firmware/pull/25632)
3
4With dynamic memory allocation removed from all debounce implementations ([#25515](https://github.com/qmk/qmk_firmware/pull/25515)), the `num_rows` parameter has been removed from the `debounce_init()` and `debounce()` functions. The `MATRIX_ROWS_PER_HAND` is now used by default in every implementation.
diff --git a/docs/custom_matrix.md b/docs/custom_matrix.md
index ef206944e1..294b88bb0b 100644
--- a/docs/custom_matrix.md
+++ b/docs/custom_matrix.md
@@ -74,7 +74,7 @@ void matrix_init(void) {
74 // TODO: initialize hardware and global matrix state here 74 // TODO: initialize hardware and global matrix state here
75 75
76 // Unless hardware debouncing - Init the configured debounce routine 76 // Unless hardware debouncing - Init the configured debounce routine
77 debounce_init(MATRIX_ROWS); 77 debounce_init();
78 78
79 // This *must* be called for correct keyboard behavior 79 // This *must* be called for correct keyboard behavior
80 matrix_init_kb(); 80 matrix_init_kb();
@@ -86,7 +86,7 @@ uint8_t matrix_scan(void) {
86 // TODO: add matrix scanning routine here 86 // TODO: add matrix scanning routine here
87 87
88 // Unless hardware debouncing - use the configured debounce routine 88 // Unless hardware debouncing - use the configured debounce routine
89 changed = debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 89 changed = debounce(raw_matrix, matrix, changed);
90 90
91 // This *must* be called for correct keyboard behavior 91 // This *must* be called for correct keyboard behavior
92 matrix_scan_kb(); 92 matrix_scan_kb();
diff --git a/keyboards/bpiphany/pegasushoof/2015/matrix.c b/keyboards/bpiphany/pegasushoof/2015/matrix.c
index 6fc1fd6e9e..61135e5833 100644
--- a/keyboards/bpiphany/pegasushoof/2015/matrix.c
+++ b/keyboards/bpiphany/pegasushoof/2015/matrix.c
@@ -31,13 +31,17 @@ static matrix_row_t matrix[MATRIX_ROWS];
31static matrix_row_t matrix_debouncing[MATRIX_ROWS]; 31static matrix_row_t matrix_debouncing[MATRIX_ROWS];
32 32
33static matrix_row_t read_cols(void); 33static matrix_row_t read_cols(void);
34static void select_row(uint8_t col); 34static void select_row(uint8_t col);
35 35
36// user-defined overridable functions 36// user-defined overridable functions
37 37
38__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } 38__attribute__((weak)) void matrix_init_kb(void) {
39 matrix_init_user();
40}
39 41
40__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } 42__attribute__((weak)) void matrix_scan_kb(void) {
43 matrix_scan_user();
44}
41 45
42__attribute__((weak)) void matrix_init_user(void) {} 46__attribute__((weak)) void matrix_init_user(void) {}
43 47
@@ -45,105 +49,125 @@ __attribute__((weak)) void matrix_scan_user(void) {}
45 49
46// helper functions 50// helper functions
47 51
48inline uint8_t matrix_rows(void) 52inline uint8_t matrix_rows(void) {
49{ 53 return MATRIX_ROWS;
50 return MATRIX_ROWS;
51} 54}
52 55
53inline uint8_t matrix_cols(void) 56inline uint8_t matrix_cols(void) {
54{ 57 return MATRIX_COLS;
55 return MATRIX_COLS;
56} 58}
57 59
58void matrix_init(void) 60void matrix_init(void) {
59{ 61 /* Column output pins */
60 /* Column output pins */ 62 DDRD |= 0b01111011;
61 DDRD |= 0b01111011; 63 /* Row input pins */
62 /* Row input pins */ 64 DDRC &= ~0b10000000;
63 DDRC &= ~0b10000000; 65 DDRB &= ~0b01111111;
64 DDRB &= ~0b01111111; 66 PORTC |= 0b10000000;
65 PORTC |= 0b10000000; 67 PORTB |= 0b01111111;
66 PORTB |= 0b01111111; 68
67 69 for (uint8_t i = 0; i < matrix_rows(); i++) {
68 for (uint8_t i=0; i < matrix_rows(); i++) { 70 matrix[i] = 0;
69 matrix[i] = 0; 71 matrix_debouncing[i] = 0;
70 matrix_debouncing[i] = 0; 72 }
71 } 73
72 74 matrix_init_kb();
73 matrix_init_kb();
74} 75}
75 76
76uint8_t matrix_scan(void) 77uint8_t matrix_scan(void) {
77{ 78 bool changed = false;
78 bool changed = false; 79 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
79 for (uint8_t col = 0; col < MATRIX_COLS; col++) { 80 select_row(col);
80 select_row(col); 81 wait_us(30);
81 wait_us(30); 82 matrix_row_t rows = read_cols();
82 matrix_row_t rows = read_cols(); 83 for (uint8_t row = 0; row < matrix_rows(); row++) {
83 for (uint8_t row = 0; row < matrix_rows(); row++) { 84 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1 << col);
84 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col); 85 bool curr_bit = rows & (1 << row);
85 bool curr_bit = rows & (1<<row); 86 if ((changed |= prev_bit != curr_bit)) {
86 if ((changed |= prev_bit != curr_bit)) { 87 matrix_debouncing[row] ^= (matrix_row_t)1 << col;
87 matrix_debouncing[row] ^= (matrix_row_t) 1 << col; 88 }
88 } 89 }
89 } 90 }
90 }
91 91
92 debounce(matrix_debouncing, matrix, matrix_rows(), changed); 92 debounce(matrix_debouncing, matrix, changed);
93 matrix_scan_kb(); 93 matrix_scan_kb();
94 94
95 return (uint8_t)changed; 95 return (uint8_t)changed;
96} 96}
97 97
98inline 98inline matrix_row_t matrix_get_row(uint8_t row) {
99matrix_row_t matrix_get_row(uint8_t row) 99 return matrix[row];
100{
101 return matrix[row];
102} 100}
103 101
104void matrix_print(void) 102void matrix_print(void) {
105{ 103 print("\nr/c 0123456789ABCDEF\n");
106 print("\nr/c 0123456789ABCDEF\n"); 104 for (uint8_t row = 0; row < matrix_rows(); row++) {
107 for (uint8_t row = 0; row < matrix_rows(); row++) { 105 print_hex8(row);
108 print_hex8(row); print(": "); 106 print(": ");
109 print_bin_reverse16(matrix_get_row(row)); 107 print_bin_reverse16(matrix_get_row(row));
110 print("\n"); 108 print("\n");
111 } 109 }
112} 110}
113 111
114static matrix_row_t read_cols(void) 112static matrix_row_t read_cols(void) {
115{ 113 return (PINB & (1 << 5) ? 0 : ((matrix_row_t)1 << 0)) | (PINC & (1 << 7) ? 0 : ((matrix_row_t)1 << 1)) | (PINB & (1 << 4) ? 0 : ((matrix_row_t)1 << 2)) | (PINB & (1 << 6) ? 0 : ((matrix_row_t)1 << 3)) | (PINB & (1 << 1) ? 0 : ((matrix_row_t)1 << 4)) | (PINB & (1 << 2) ? 0 : ((matrix_row_t)1 << 5)) | (PINB & (1 << 3) ? 0 : ((matrix_row_t)1 << 6)) | (PINB & (1 << 0) ? 0 : ((matrix_row_t)1 << 7));
116 return
117 (PINB&(1<<5) ? 0 : ((matrix_row_t)1<<0)) |
118 (PINC&(1<<7) ? 0 : ((matrix_row_t)1<<1)) |
119 (PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) |
120 (PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) |
121 (PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) |
122 (PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) |
123 (PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) |
124 (PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7));
125} 114}
126 115
127static void select_row(uint8_t col) 116static void select_row(uint8_t col) {
128{ 117 switch (col) {
129 switch (col) { 118 case 0:
130 case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break; 119 PORTD = (PORTD & ~0b01111011) | 0b00011011;
131 case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break; 120 break;
132 case 2: PORTD = (PORTD & ~0b01111011) | 0b01100000; break; 121 case 1:
133 case 3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break; 122 PORTD = (PORTD & ~0b01111011) | 0b01000011;
134 case 4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break; 123 break;
135 case 5: PORTD = (PORTD & ~0b01111011) | 0b01101010; break; 124 case 2:
136 case 6: PORTD = (PORTD & ~0b01111011) | 0b01110001; break; 125 PORTD = (PORTD & ~0b01111011) | 0b01100000;
137 case 7: PORTD = (PORTD & ~0b01111011) | 0b01101001; break; 126 break;
138 case 8: PORTD = (PORTD & ~0b01111011) | 0b01100001; break; 127 case 3:
139 case 9: PORTD = (PORTD & ~0b01111011) | 0b01111000; break; 128 PORTD = (PORTD & ~0b01111011) | 0b01111001;
140 case 10: PORTD = (PORTD & ~0b01111011) | 0b00100011; break; 129 break;
141 case 11: PORTD = (PORTD & ~0b01111011) | 0b00101011; break; 130 case 4:
142 case 12: PORTD = (PORTD & ~0b01111011) | 0b00110011; break; 131 PORTD = (PORTD & ~0b01111011) | 0b01100010;
143 case 13: PORTD = (PORTD & ~0b01111011) | 0b01110000; break; 132 break;
144 case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break; 133 case 5:
145 case 15: PORTD = (PORTD & ~0b01111011) | 0b01101000; break; 134 PORTD = (PORTD & ~0b01111011) | 0b01101010;
146 case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break; 135 break;
147 case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break; 136 case 6:
148 } 137 PORTD = (PORTD & ~0b01111011) | 0b01110001;
138 break;
139 case 7:
140 PORTD = (PORTD & ~0b01111011) | 0b01101001;
141 break;
142 case 8:
143 PORTD = (PORTD & ~0b01111011) | 0b01100001;
144 break;
145 case 9:
146 PORTD = (PORTD & ~0b01111011) | 0b01111000;
147 break;
148 case 10:
149 PORTD = (PORTD & ~0b01111011) | 0b00100011;
150 break;
151 case 11:
152 PORTD = (PORTD & ~0b01111011) | 0b00101011;
153 break;
154 case 12:
155 PORTD = (PORTD & ~0b01111011) | 0b00110011;
156 break;
157 case 13:
158 PORTD = (PORTD & ~0b01111011) | 0b01110000;
159 break;
160 case 14:
161 PORTD = (PORTD & ~0b01111011) | 0b00010011;
162 break;
163 case 15:
164 PORTD = (PORTD & ~0b01111011) | 0b01101000;
165 break;
166 case 16:
167 PORTD = (PORTD & ~0b01111011) | 0b00001011;
168 break;
169 case 17:
170 PORTD = (PORTD & ~0b01111011) | 0b00111011;
171 break;
172 }
149} 173}
diff --git a/keyboards/gboards/gergo/gergo.c b/keyboards/gboards/gergo/gergo.c
index 1ec6105eeb..6ab9786007 100644
--- a/keyboards/gboards/gergo/gergo.c
+++ b/keyboards/gboards/gergo/gergo.c
@@ -1,3 +1,6 @@
1// Jane Bernhardt (https://github.com/germ)
2// SPDX-License-Identifier: GPL-2.0+
3
1#include "gergo.h" 4#include "gergo.h"
2 5
3bool i2c_initialized = 0; 6bool i2c_initialized = 0;
diff --git a/keyboards/gboards/gergo/gergo.h b/keyboards/gboards/gergo/gergo.h
index f6dc1498f0..734d6e5eea 100644
--- a/keyboards/gboards/gergo/gergo.h
+++ b/keyboards/gboards/gergo/gergo.h
@@ -1,3 +1,6 @@
1// Jane Bernhardt (https://github.com/germ)
2// SPDX-License-Identifier: GPL-2.0+
3
1#pragma once 4#pragma once
2 5
3#include "quantum.h" 6#include "quantum.h"
diff --git a/keyboards/gboards/gergo/matrix.c b/keyboards/gboards/gergo/matrix.c
index 1fa80a58a0..d73aaa1814 100644
--- a/keyboards/gboards/gergo/matrix.c
+++ b/keyboards/gboards/gergo/matrix.c
@@ -24,76 +24,75 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
24#include "gergo.h" 24#include "gergo.h"
25 25
26#ifdef BALLER 26#ifdef BALLER
27#include <avr/interrupt.h> 27# include <avr/interrupt.h>
28#include "pointing_device.h" 28# include "pointing_device.h"
29#endif 29#endif
30 30
31#ifndef DEBOUNCE 31#ifndef DEBOUNCE
32# define DEBOUNCE 5 32# define DEBOUNCE 5
33#endif 33#endif
34 34
35// MCP Pin Defs 35// MCP Pin Defs
36#define RROW1 (1u<<3) 36#define RROW1 (1u << 3)
37#define RROW2 (1u<<2) 37#define RROW2 (1u << 2)
38#define RROW3 (1u<<1) 38#define RROW3 (1u << 1)
39#define RROW4 (1u<<0) 39#define RROW4 (1u << 0)
40#define COL0 (1u<<0) 40#define COL0 (1u << 0)
41#define COL1 (1u<<1) 41#define COL1 (1u << 1)
42#define COL2 (1u<<2) 42#define COL2 (1u << 2)
43#define COL3 (1u<<3) 43#define COL3 (1u << 3)
44#define COL4 (1u<<4) 44#define COL4 (1u << 4)
45#define COL5 (1u<<5) 45#define COL5 (1u << 5)
46#define COL6 (1u<<6) 46#define COL6 (1u << 6)
47 47
48// ATmega pin defs 48// ATmega pin defs
49#define ROW1 (1u<<6) 49#define ROW1 (1u << 6)
50#define ROW2 (1u<<5) 50#define ROW2 (1u << 5)
51#define ROW3 (1u<<4) 51#define ROW3 (1u << 4)
52#define ROW4 (1u<<1) 52#define ROW4 (1u << 1)
53#define COL7 (1u<<0) 53#define COL7 (1u << 0)
54#define COL8 (1u<<1) 54#define COL8 (1u << 1)
55#define COL9 (1u<<2) 55#define COL9 (1u << 2)
56#define COL10 (1u<<3) 56#define COL10 (1u << 3)
57#define COL11 (1u<<2) 57#define COL11 (1u << 2)
58#define COL12 (1u<<3) 58#define COL12 (1u << 3)
59#define COL13 (1u<<6) 59#define COL13 (1u << 6)
60 60
61//Trackball pin defs 61// Trackball pin defs
62#define TRKUP (1u<<4) 62#define TRKUP (1u << 4)
63#define TRKDN (1u<<5) 63#define TRKDN (1u << 5)
64#define TRKLT (1u<<6) 64#define TRKLT (1u << 6)
65#define TRKRT (1u<<7) 65#define TRKRT (1u << 7)
66#define TRKBTN (1u<<6) 66#define TRKBTN (1u << 6)
67
68 67
69// Multiple for mouse moves 68// Multiple for mouse moves
70#ifndef TRKSTEP 69#ifndef TRKSTEP
71#define TRKSTEP 20 70# define TRKSTEP 20
72#endif 71#endif
73 72
74// multiple for mouse scroll 73// multiple for mouse scroll
75#ifndef SCROLLSTEP 74#ifndef SCROLLSTEP
76#define SCROLLSTEP 5 75# define SCROLLSTEP 5
77#endif 76#endif
78 77
79// bit masks 78// bit masks
80#define BMASK (COL7 | COL8 | COL9 | COL10) 79#define BMASK (COL7 | COL8 | COL9 | COL10)
81#define CMASK (COL13) 80#define CMASK (COL13)
82#define DMASK (COL11 | COL12) 81#define DMASK (COL11 | COL12)
83#define FMASK (ROW1 | ROW2 | ROW3 | ROW4) 82#define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
84#define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4) 83#define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
85#define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6) 84#define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
86#define TRKMASK (TRKUP | TRKDN | TRKRT | TRKLT) 85#define TRKMASK (TRKUP | TRKDN | TRKRT | TRKLT)
87 86
88// Trackball interrupts accumulate over here. Processed on scan 87// Trackball interrupts accumulate over here. Processed on scan
89// Stores prev state of mouse, high bits store direction 88// Stores prev state of mouse, high bits store direction
90uint8_t trkState = 0; 89uint8_t trkState = 0;
91uint8_t trkBtnState = 0; 90uint8_t trkBtnState = 0;
92 91
93volatile uint8_t tbUpCnt = 0; 92volatile uint8_t tbUpCnt = 0;
94volatile uint8_t tbDnCnt = 0; 93volatile uint8_t tbDnCnt = 0;
95volatile uint8_t tbLtCnt = 0; 94volatile uint8_t tbLtCnt = 0;
96volatile uint8_t tbRtCnt = 0; 95volatile uint8_t tbRtCnt = 0;
97 96
98/* matrix state(1:on, 0:off) */ 97/* matrix state(1:on, 0:off) */
99static matrix_row_t matrix[MATRIX_ROWS]; 98static matrix_row_t matrix[MATRIX_ROWS];
@@ -111,29 +110,30 @@ static matrix_row_t read_cols(uint8_t row);
111static void init_cols(void); 110static void init_cols(void);
112static void unselect_rows(void); 111static void unselect_rows(void);
113static void select_row(uint8_t row); 112static void select_row(uint8_t row);
114static void enableInterrupts(void); 113static void enableInterrupts(void);
115 114
116static uint8_t mcp23018_reset_loop; 115static uint8_t mcp23018_reset_loop;
117// static uint16_t mcp23018_reset_loop; 116// static uint16_t mcp23018_reset_loop;
118 117
119__attribute__ ((weak)) void matrix_init_user(void) {} 118__attribute__((weak)) void matrix_init_user(void) {}
120 119
121__attribute__ ((weak)) void matrix_scan_user(void) {} 120__attribute__((weak)) void matrix_scan_user(void) {}
122 121
123__attribute__ ((weak)) 122__attribute__((weak)) void matrix_init_kb(void) {
124void matrix_init_kb(void) { 123 matrix_init_user();
125 matrix_init_user();
126} 124}
127 125
128__attribute__ ((weak)) 126__attribute__((weak)) void matrix_scan_kb(void) {
129void matrix_scan_kb(void) { 127 matrix_scan_user();
130 matrix_scan_user();
131} 128}
132 129
133inline uint8_t matrix_rows(void) { return MATRIX_ROWS; } 130inline uint8_t matrix_rows(void) {
134 131 return MATRIX_ROWS;
135inline uint8_t matrix_cols(void) { return MATRIX_COLS; } 132}
136 133
134inline uint8_t matrix_cols(void) {
135 return MATRIX_COLS;
136}
137 137
138void matrix_init(void) { 138void matrix_init(void) {
139 // initialize row and col 139 // initialize row and col
@@ -141,13 +141,13 @@ void matrix_init(void) {
141 unselect_rows(); 141 unselect_rows();
142 init_cols(); 142 init_cols();
143 143
144 // initialize matrix state: all keys off 144 // initialize matrix state: all keys off
145 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 145 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
146 matrix[i] = 0; 146 matrix[i] = 0;
147 raw_matrix[i] = 0; 147 raw_matrix[i] = 0;
148 } 148 }
149 149
150 debounce_init(MATRIX_ROWS); 150 debounce_init();
151 matrix_init_kb(); 151 matrix_init_kb();
152} 152}
153 153
@@ -158,7 +158,7 @@ void matrix_power_up(void) {
158 init_cols(); 158 init_cols();
159 159
160 // initialize matrix state: all keys off 160 // initialize matrix state: all keys off
161 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 161 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
162 matrix[i] = 0; 162 matrix[i] = 0;
163 } 163 }
164} 164}
@@ -166,63 +166,66 @@ void matrix_power_up(void) {
166// Reads and stores a row, returning 166// Reads and stores a row, returning
167// whether a change occurred. 167// whether a change occurred.
168static inline bool store_raw_matrix_row(uint8_t index) { 168static inline bool store_raw_matrix_row(uint8_t index) {
169 matrix_row_t temp = read_cols(index); 169 matrix_row_t temp = read_cols(index);
170 if (raw_matrix[index] != temp) { 170 if (raw_matrix[index] != temp) {
171 raw_matrix[index] = temp; 171 raw_matrix[index] = temp;
172 return true; 172 return true;
173 } 173 }
174 return false; 174 return false;
175} 175}
176 176
177
178
179uint8_t matrix_scan(void) { 177uint8_t matrix_scan(void) {
180 // TODO: Find what is trashing interrupts 178 // TODO: Find what is trashing interrupts
181 enableInterrupts(); 179 enableInterrupts();
182 180
183 // First we handle the mouse inputs 181 // First we handle the mouse inputs
184#ifdef BALLER 182#ifdef BALLER
185 uint8_t pBtn = PINE & TRKBTN; 183 uint8_t pBtn = PINE & TRKBTN;
186 184
187 #ifdef DEBUG_BALLER 185# ifdef DEBUG_BALLER
188 // Compare to previous, mod report 186 // Compare to previous, mod report
189 if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0) 187 if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0) xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
190 xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6)); 188# endif
191 #endif
192 189
193 // Modify the report 190 // Modify the report
194 report_mouse_t pRprt = pointing_device_get_report(); 191 report_mouse_t pRprt = pointing_device_get_report();
195 192
196 // Scroll by default, move on layer 193 // Scroll by default, move on layer
197 if (layer_state == 0) { 194 if (layer_state == 0) {
198 pRprt.h += tbLtCnt * SCROLLSTEP; tbLtCnt = 0; 195 pRprt.h += tbLtCnt * SCROLLSTEP;
199 pRprt.h -= tbRtCnt * SCROLLSTEP; tbRtCnt = 0; 196 tbLtCnt = 0;
200 pRprt.v -= tbUpCnt * SCROLLSTEP; tbUpCnt = 0; 197 pRprt.h -= tbRtCnt * SCROLLSTEP;
201 pRprt.v += tbDnCnt * SCROLLSTEP; tbDnCnt = 0; 198 tbRtCnt = 0;
199 pRprt.v -= tbUpCnt * SCROLLSTEP;
200 tbUpCnt = 0;
201 pRprt.v += tbDnCnt * SCROLLSTEP;
202 tbDnCnt = 0;
202 } else { 203 } else {
203 pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1); tbLtCnt = 0; 204 pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1);
204 pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1); tbRtCnt = 0; 205 tbLtCnt = 0;
205 pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1); tbUpCnt = 0; 206 pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1);
206 pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1); tbDnCnt = 0; 207 tbRtCnt = 0;
208 pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1);
209 tbUpCnt = 0;
210 pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1);
211 tbDnCnt = 0;
207 } 212 }
208 213
209#ifdef DEBUG_BALLER 214# ifdef DEBUG_BALLER
210 if (pRprt.x != 0 || pRprt.y != 0) 215 if (pRprt.x != 0 || pRprt.y != 0) xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
211 xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y); 216# endif
212#endif
213 217
214 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1; 218 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1;
215 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1; 219 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1;
216 220
217 // Save state, push update 221 // Save state, push update
218 if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn)) 222 if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn)) pointing_device_set_report(pRprt);
219 pointing_device_set_report(pRprt);
220 223
221 trkBtnState = pBtn; 224 trkBtnState = pBtn;
222#endif 225#endif
223 226
224 // Then the keyboard 227 // Then the keyboard
225 if (mcp23018_status) { // if there was an error 228 if (mcp23018_status) { // if there was an error
226 if (++mcp23018_reset_loop == 0) { 229 if (++mcp23018_reset_loop == 0) {
227 // if (++mcp23018_reset_loop >= 1300) { 230 // if (++mcp23018_reset_loop >= 1300) {
228 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans 231 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
@@ -240,7 +243,7 @@ uint8_t matrix_scan(void) {
240 bool changed = false; 243 bool changed = false;
241 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) { 244 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
242 // select rows from left and right hands 245 // select rows from left and right hands
243 uint8_t left_index = i; 246 uint8_t left_index = i;
244 uint8_t right_index = i + MATRIX_ROWS_PER_SIDE; 247 uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
245 select_row(left_index); 248 select_row(left_index);
246 select_row(right_index); 249 select_row(right_index);
@@ -254,41 +257,46 @@ uint8_t matrix_scan(void) {
254 unselect_rows(); 257 unselect_rows();
255 } 258 }
256 259
257 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 260 debounce(raw_matrix, matrix, changed);
258 matrix_scan_kb(); 261 matrix_scan_kb();
259 262
260 enableInterrupts(); 263 enableInterrupts();
261 264
262#ifdef DEBUG_MATRIX 265#ifdef DEBUG_MATRIX
263 for (uint8_t c = 0; c < MATRIX_COLS; c++) 266 for (uint8_t c = 0; c < MATRIX_COLS; c++)
264 for (uint8_t r = 0; r < MATRIX_ROWS; r++) 267 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
265 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c); 268 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
266#endif 269#endif
267 270
268 return 1; 271 return 1;
269} 272}
270 273
271inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } 274inline bool matrix_is_on(uint8_t row, uint8_t col) {
275 return (matrix[row] & ((matrix_row_t)1 << col));
276}
272 277
273inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } 278inline matrix_row_t matrix_get_row(uint8_t row) {
279 return matrix[row];
280}
274 281
275void matrix_print(void) { 282void matrix_print(void) {
276 print("\nr/c 0123456789ABCDEF\n"); 283 print("\nr/c 0123456789ABCDEF\n");
277 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 284 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
278 print_hex8(row); print(": "); 285 print_hex8(row);
286 print(": ");
279 print_bin_reverse16(matrix_get_row(row)); 287 print_bin_reverse16(matrix_get_row(row));
280 print("\n"); 288 print("\n");
281 } 289 }
282} 290}
283 291
284// Remember this means ROWS 292// Remember this means ROWS
285static void init_cols(void) { 293static void init_cols(void) {
286 // init on mcp23018 294 // init on mcp23018
287 // not needed, already done as part of init_mcp23018() 295 // not needed, already done as part of init_mcp23018()
288 296
289 // Input with pull-up(DDR:0, PORT:1) 297 // Input with pull-up(DDR:0, PORT:1)
290 DDRF &= ~FMASK; 298 DDRF &= ~FMASK;
291 PORTF |= FMASK; 299 PORTF |= FMASK;
292} 300}
293 301
294static matrix_row_t read_cols(uint8_t row) { 302static matrix_row_t read_cols(uint8_t row) {
@@ -296,7 +304,7 @@ static matrix_row_t read_cols(uint8_t row) {
296 if (mcp23018_status) { // if there was an error 304 if (mcp23018_status) { // if there was an error
297 return 0; 305 return 0;
298 } else { 306 } else {
299 uint8_t data = 0; 307 uint8_t data = 0;
300 mcp23018_status = i2c_read_register(I2C_ADDR, GPIOB, &data, 1, I2C_TIMEOUT); 308 mcp23018_status = i2c_read_register(I2C_ADDR, GPIOB, &data, 1, I2C_TIMEOUT);
301 309
302#ifdef DEBUG_MATRIX 310#ifdef DEBUG_MATRIX
@@ -305,113 +313,106 @@ static matrix_row_t read_cols(uint8_t row) {
305 return ~data; 313 return ~data;
306 } 314 }
307 } else { 315 } else {
308 /* read from teensy 316 /* read from teensy
309 * bitmask is 0b0111001, but we want the lower four 317 * bitmask is 0b0111001, but we want the lower four
310 * we'll return 1s for the top two, but that's harmless. 318 * we'll return 1s for the top two, but that's harmless.
311 */ 319 */
312 // So I need to confuckulate all this 320 // So I need to confuckulate all this
313 //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN))); 321 // return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
314 //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2)); 322 // return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
315 return ~( 323 return ~((((PINF & ROW4) >> 1) | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3)) & 0xF);
316 (((PINF & ROW4) >> 1)
317 | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
318 & 0xF);
319 } 324 }
320} 325}
321 326
322// Row pin configuration 327// Row pin configuration
323static void unselect_rows(void) 328static void unselect_rows(void) {
324{
325 // no need to unselect on mcp23018, because the select step sets all 329 // no need to unselect on mcp23018, because the select step sets all
326 // the other row bits high, and it's not changing to a different 330 // the other row bits high, and it's not changing to a different
327 // direction 331 // direction
328 // Hi-Z(DDR:0, PORT:0) to unselect 332 // Hi-Z(DDR:0, PORT:0) to unselect
329 DDRB &= ~(BMASK | TRKMASK); 333 DDRB &= ~(BMASK | TRKMASK);
330 PORTB &= ~(BMASK); 334 PORTB &= ~(BMASK);
331 DDRC &= ~CMASK; 335 DDRC &= ~CMASK;
332 PORTC &= ~CMASK; 336 PORTC &= ~CMASK;
333 DDRD &= ~DMASK; 337 DDRD &= ~DMASK;
334 PORTD &= ~DMASK; 338 PORTD &= ~DMASK;
335 339
336 // Fix trashing of DDRB for TB 340 // Fix trashing of DDRB for TB
337 PORTB |= TRKMASK; 341 PORTB |= TRKMASK;
338} 342}
339 343
340static void select_row(uint8_t row) 344static void select_row(uint8_t row) {
341{
342 if (row < 7) { 345 if (row < 7) {
343 // select on mcp23018 346 // select on mcp23018
344 if (mcp23018_status) { // do nothing on error 347 if (mcp23018_status) { // do nothing on error
345 } else { // set active row low : 0 // set other rows hi-Z : 1 348 } else { // set active row low : 0 // set other rows hi-Z : 1
346 uint8_t data = 0xFF & ~(1<<row); 349 uint8_t data = 0xFF & ~(1 << row);
347 mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT); 350 mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
348 } 351 }
349 } else { 352 } else {
350 // Output low(DDR:1, PORT:0) to select 353 // Output low(DDR:1, PORT:0) to select
351 switch (row) { 354 switch (row) {
352 case 7: 355 case 7:
353 DDRB |= COL7; 356 DDRB |= COL7;
354 PORTB &= ~COL7; 357 PORTB &= ~COL7;
355 break; 358 break;
356 case 8: 359 case 8:
357 DDRB |= COL8; 360 DDRB |= COL8;
358 PORTB &= ~COL8; 361 PORTB &= ~COL8;
359 break; 362 break;
360 case 9: 363 case 9:
361 DDRB |= COL9; 364 DDRB |= COL9;
362 PORTB &= ~COL9; 365 PORTB &= ~COL9;
363 break; 366 break;
364 case 10: 367 case 10:
365 DDRB |= COL10; 368 DDRB |= COL10;
366 PORTB &= ~COL10; 369 PORTB &= ~COL10;
367 break; 370 break;
368 case 11: 371 case 11:
369 DDRD |= COL11; 372 DDRD |= COL11;
370 PORTD &= ~COL11; 373 PORTD &= ~COL11;
371 break; 374 break;
372 case 12: 375 case 12:
373 DDRD |= COL12; 376 DDRD |= COL12;
374 PORTD &= ~COL12; 377 PORTD &= ~COL12;
375 break; 378 break;
376 case 13: 379 case 13:
377 DDRC |= COL13; 380 DDRC |= COL13;
378 PORTC &= ~COL13; 381 PORTC &= ~COL13;
379 break; 382 break;
380 } 383 }
381 } 384 }
382} 385}
383 386
384
385// Trackball Interrupts 387// Trackball Interrupts
386static void enableInterrupts(void) { 388static void enableInterrupts(void) {
387 #ifdef BALLER 389#ifdef BALLER
388 // Set interrupt mask 390 // Set interrupt mask
389 // Set port defs 391 // Set port defs
390 DDRB &= ~TRKMASK; 392 DDRB &= ~TRKMASK;
391 PORTB |= TRKMASK; 393 PORTB |= TRKMASK;
392 DDRE &= ~TRKBTN; 394 DDRE &= ~TRKBTN;
393 PORTE |= TRKBTN; 395 PORTE |= TRKBTN;
394 396
395 // Interrupt shenanigans 397 // Interrupt shenanigans
396 //EIMSK |= (1 << PCIE0); 398 // EIMSK |= (1 << PCIE0);
397 PCMSK0 |= TRKMASK; 399 PCMSK0 |= TRKMASK;
398 PCICR |= (1 << PCIE0); 400 PCICR |= (1 << PCIE0);
399 sei(); 401 sei();
400 #endif 402#endif
401 403
402 return; 404 return;
403} 405}
404#ifdef BALLER 406#ifdef BALLER
405ISR (PCINT0_vect) { 407ISR(PCINT0_vect) {
406 // Don't get fancy, we're in a interrupt here 408 // Don't get fancy, we're in a interrupt here
407 // PCINT reports a interrupt for a change on the bus 409 // PCINT reports a interrupt for a change on the bus
408 // We hand the button at scantime for debounce 410 // We hand the button at scantime for debounce
409 volatile uint8_t pState = PINB & TRKMASK; 411 volatile uint8_t pState = PINB & TRKMASK;
410 if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++; 412 if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
411 if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++; 413 if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
412 if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++; 414 if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
413 if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++; 415 if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;
414 trkState = pState; 416 trkState = pState;
415
416} 417}
417#endif 418#endif
diff --git a/keyboards/gboards/gergoplex/keymaps/default/config.h b/keyboards/gboards/gergoplex/keymaps/default/config.h
index 1b30cc73b3..62e0070d79 100644
--- a/keyboards/gboards/gergoplex/keymaps/default/config.h
+++ b/keyboards/gboards/gergoplex/keymaps/default/config.h
@@ -1,3 +1,6 @@
1// Jane Bernhardt (https://github.com/germ)
2// SPDX-License-Identifier: GPL-2.0+
3
1#pragma once 4#pragma once
2 5
3#define COMBO_ALLOW_ACTION_KEYS 6#define COMBO_ALLOW_ACTION_KEYS
diff --git a/keyboards/gboards/gergoplex/matrix.c b/keyboards/gboards/gergoplex/matrix.c
index 3ea6a13385..bed2178d4b 100644
--- a/keyboards/gboards/gergoplex/matrix.c
+++ b/keyboards/gboards/gergoplex/matrix.c
@@ -68,7 +68,9 @@ static uint8_t mcp23018_reset_loop;
68 68
69__attribute__((weak)) void matrix_init_user(void) {} 69__attribute__((weak)) void matrix_init_user(void) {}
70__attribute__((weak)) void matrix_scan_user(void) {} 70__attribute__((weak)) void matrix_scan_user(void) {}
71__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } 71__attribute__((weak)) void matrix_scan_kb(void) {
72 matrix_scan_user();
73}
72 74
73void matrix_init(void) { 75void matrix_init(void) {
74 // initialize row and col 76 // initialize row and col
@@ -82,7 +84,7 @@ void matrix_init(void) {
82 raw_matrix[i] = 0; 84 raw_matrix[i] = 0;
83 } 85 }
84 86
85 debounce_init(MATRIX_ROWS); 87 debounce_init();
86 matrix_init_kb(); 88 matrix_init_kb();
87} 89}
88void matrix_power_up(void) { 90void matrix_power_up(void) {
@@ -108,7 +110,7 @@ static inline bool store_raw_matrix_row(uint8_t index) {
108 return false; 110 return false;
109} 111}
110uint8_t matrix_scan(void) { 112uint8_t matrix_scan(void) {
111 if (mcp23018_status) { // if there was an error 113 if (mcp23018_status) { // if there was an error
112 if (++mcp23018_reset_loop == 0) { 114 if (++mcp23018_reset_loop == 0) {
113 // if (++mcp23018_reset_loop >= 1300) { 115 // if (++mcp23018_reset_loop >= 1300) {
114 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans 116 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
@@ -140,7 +142,7 @@ uint8_t matrix_scan(void) {
140 unselect_rows(); 142 unselect_rows();
141 } 143 }
142 144
143 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 145 debounce(raw_matrix, matrix, changed);
144 matrix_scan_kb(); 146 matrix_scan_kb();
145 147
146#ifdef DEBUG_MATRIX 148#ifdef DEBUG_MATRIX
@@ -152,8 +154,12 @@ uint8_t matrix_scan(void) {
152 return 1; 154 return 1;
153} 155}
154 156
155inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } 157inline bool matrix_is_on(uint8_t row, uint8_t col) {
156inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } 158 return (matrix[row] & ((matrix_row_t)1 << col));
159}
160inline matrix_row_t matrix_get_row(uint8_t row) {
161 return matrix[row];
162}
157 163
158void matrix_print(void) { 164void matrix_print(void) {
159 print("\nr/c 0123456789ABCDEF\n"); 165 print("\nr/c 0123456789ABCDEF\n");
@@ -168,16 +174,16 @@ void matrix_print(void) {
168// Remember this means ROWS 174// Remember this means ROWS
169static void init_cols(void) { 175static void init_cols(void) {
170 for (uint8_t col = 0; col < MATRIX_COLS; col++) { 176 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
171 gpio_set_pin_input_high(col_pins[col]); 177 gpio_set_pin_input_high(col_pins[col]);
172 } 178 }
173} 179}
174 180
175static matrix_row_t read_cols(uint8_t row) { 181static matrix_row_t read_cols(uint8_t row) {
176 if (row < 5) { 182 if (row < 5) {
177 if (mcp23018_status) { // if there was an error 183 if (mcp23018_status) { // if there was an error
178 return 0; 184 return 0;
179 } else { 185 } else {
180 uint8_t data = 0; 186 uint8_t data = 0;
181 mcp23018_status = i2c_receive(I2C_ADDR, &data, 1, I2C_TIMEOUT); 187 mcp23018_status = i2c_receive(I2C_ADDR, &data, 1, I2C_TIMEOUT);
182#ifdef DEBUG_MATRIX 188#ifdef DEBUG_MATRIX
183 if (~data != 0x00) xprintf("I2C: %d\n", ~data); 189 if (~data != 0x00) xprintf("I2C: %d\n", ~data);
@@ -195,20 +201,19 @@ static void unselect_rows(void) {
195 // the other row bits high, and it's not changing to a different direction 201 // the other row bits high, and it's not changing to a different direction
196 202
197 for (uint8_t row = 0; row < MATRIX_ROWS_PER_SIDE; row++) { 203 for (uint8_t row = 0; row < MATRIX_ROWS_PER_SIDE; row++) {
198 gpio_set_pin_input(row_pins[row]); 204 gpio_set_pin_input(row_pins[row]);
199 gpio_write_pin_low(row_pins[row]); 205 gpio_write_pin_low(row_pins[row]);
200 } 206 }
201} 207}
202 208
203static void select_row(uint8_t row) { 209static void select_row(uint8_t row) {
204 if (row < 5) { 210 if (row < 5) {
205 // select on mcp23018 211 // select on mcp23018
206 if (mcp23018_status) { // do nothing on error 212 if (mcp23018_status) { // do nothing on error
207 } else { // set active row low : 0 // set other rows hi-Z : 1 213 } else { // set active row low : 0 // set other rows hi-Z : 1
208 uint8_t data; 214 uint8_t data;
209 data = 0xFF & ~(1 << (row + 1)); 215 data = 0xFF & ~(1 << (row + 1));
210 mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT); 216 mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
211
212 } 217 }
213 } else { 218 } else {
214 gpio_set_pin_output(row_pins[row - MATRIX_ROWS_PER_SIDE]); 219 gpio_set_pin_output(row_pins[row - MATRIX_ROWS_PER_SIDE]);
diff --git a/keyboards/halfcliff/matrix.c b/keyboards/halfcliff/matrix.c
index 790f8c41bb..fc9d437a45 100644
--- a/keyboards/halfcliff/matrix.c
+++ b/keyboards/halfcliff/matrix.c
@@ -29,16 +29,20 @@ static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
29static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 29static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
30 30
31/* matrix state(1:on, 0:off) */ 31/* matrix state(1:on, 0:off) */
32static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values 32static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
33static matrix_row_t matrix[MATRIX_ROWS]; // debounced values 33static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
34 34
35// row offsets for each hand 35// row offsets for each hand
36uint8_t thisHand, thatHand; 36uint8_t thisHand, thatHand;
37 37
38// user-defined overridable functions 38// user-defined overridable functions
39__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } 39__attribute__((weak)) void matrix_init_kb(void) {
40 matrix_init_user();
41}
40 42
41__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } 43__attribute__((weak)) void matrix_scan_kb(void) {
44 matrix_scan_user();
45}
42 46
43__attribute__((weak)) void matrix_init_user(void) {} 47__attribute__((weak)) void matrix_init_user(void) {}
44 48
@@ -46,7 +50,9 @@ __attribute__((weak)) void matrix_scan_user(void) {}
46 50
47__attribute__((weak)) void matrix_slave_scan_user(void) {} 51__attribute__((weak)) void matrix_slave_scan_user(void) {}
48 52
49matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } 53matrix_row_t matrix_get_row(uint8_t row) {
54 return matrix[row];
55}
50 56
51void matrix_print(void) {} 57void matrix_print(void) {}
52 58
@@ -58,13 +64,19 @@ static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
58} 64}
59 65
60static inline void gpio_atomic_set_pin_input_high(pin_t pin) { 66static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
61 ATOMIC_BLOCK_FORCEON { gpio_set_pin_input_high(pin); } 67 ATOMIC_BLOCK_FORCEON {
68 gpio_set_pin_input_high(pin);
69 }
62} 70}
63 71
64// matrix code 72// matrix code
65static void select_row(uint8_t row) { gpio_atomic_set_pin_output_low(row_pins[row]); } 73static void select_row(uint8_t row) {
74 gpio_atomic_set_pin_output_low(row_pins[row]);
75}
66 76
67static void unselect_row(uint8_t row) { gpio_atomic_set_pin_input_high(row_pins[row]); } 77static void unselect_row(uint8_t row) {
78 gpio_atomic_set_pin_input_high(row_pins[row]);
79}
68 80
69static void unselect_rows(void) { 81static void unselect_rows(void) {
70 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) { 82 for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
@@ -92,7 +104,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
92 // Unselect row 104 // Unselect row
93 unselect_row(current_row); 105 unselect_row(current_row);
94 if (current_row + 1 < MATRIX_ROWS) { 106 if (current_row + 1 < MATRIX_ROWS) {
95 wait_us(30); // wait for row signal to go HIGH 107 wait_us(30); // wait for row signal to go HIGH
96 } 108 }
97 109
98 // If the row has changed, store the row and return the changed flag. 110 // If the row has changed, store the row and return the changed flag.
@@ -103,9 +115,13 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
103 return false; 115 return false;
104} 116}
105 117
106static void select_col(uint8_t col) { gpio_atomic_set_pin_output_low(col_pins[col]); } 118static void select_col(uint8_t col) {
119 gpio_atomic_set_pin_output_low(col_pins[col]);
120}
107 121
108static void unselect_col(uint8_t col) { gpio_atomic_set_pin_input_high(col_pins[col]); } 122static void unselect_col(uint8_t col) {
123 gpio_atomic_set_pin_input_high(col_pins[col]);
124}
109 125
110static void unselect_cols(void) { 126static void unselect_cols(void) {
111 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 127 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
@@ -156,7 +172,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
156 // Unselect col 172 // Unselect col
157 unselect_col(current_col); 173 unselect_col(current_col);
158 if (current_col + 1 < MATRIX_COLS) { 174 if (current_col + 1 < MATRIX_COLS) {
159 wait_us(30); // wait for col signal to go HIGH 175 wait_us(30); // wait for col signal to go HIGH
160 } 176 }
161 177
162 return matrix_changed; 178 return matrix_changed;
@@ -201,7 +217,7 @@ void matrix_init(void) {
201 matrix[i] = 0; 217 matrix[i] = 0;
202 } 218 }
203 219
204 debounce_init(ROWS_PER_HAND); 220 debounce_init();
205 221
206 matrix_init_kb(); 222 matrix_init_kb();
207 223
@@ -248,26 +264,26 @@ bool matrix_post_scan(void) {
248} 264}
249 265
250uint8_t matrix_scan(void) { 266uint8_t matrix_scan(void) {
251 bool local_changed = false; 267 bool local_changed = false;
252 static matrix_row_t temp_raw_matrix[MATRIX_ROWS]; // temp raw values 268 static matrix_row_t temp_raw_matrix[MATRIX_ROWS]; // temp raw values
253 269
254 // Set row, read cols 270 // Set row, read cols
255 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND/2; current_row++) { 271 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND / 2; current_row++) {
256 local_changed |= read_cols_on_row(raw_matrix, current_row); 272 local_changed |= read_cols_on_row(raw_matrix, current_row);
257 } 273 }
258 274
259 // Set col, read rows 275 // Set col, read rows
260 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 276 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
261 local_changed |= read_rows_on_col(temp_raw_matrix, current_col); 277 local_changed |= read_rows_on_col(temp_raw_matrix, current_col);
262 //Updated key matrix on lines 6-10 (or lines 16-20) 278 // Updated key matrix on lines 6-10 (or lines 16-20)
263 if(local_changed) { 279 if (local_changed) {
264 for (uint8_t i = ROWS_PER_HAND/2; i < ROWS_PER_HAND; i++) { 280 for (uint8_t i = ROWS_PER_HAND / 2; i < ROWS_PER_HAND; i++) {
265 raw_matrix[i] = temp_raw_matrix[i]; 281 raw_matrix[i] = temp_raw_matrix[i];
266 } 282 }
267 } 283 }
268 } 284 }
269 285
270 debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, local_changed); 286 debounce(raw_matrix, matrix + thisHand, local_changed);
271 287
272 bool remote_changed = matrix_post_scan(); 288 bool remote_changed = matrix_post_scan();
273 return (uint8_t)(local_changed || remote_changed); 289 return (uint8_t)(local_changed || remote_changed);
diff --git a/keyboards/handwired/owlet60/matrix.c b/keyboards/handwired/owlet60/matrix.c
index 7ef5d66a9f..2017b4de85 100644
--- a/keyboards/handwired/owlet60/matrix.c
+++ b/keyboards/handwired/owlet60/matrix.c
@@ -29,82 +29,62 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
29#include "timer.h" 29#include "timer.h"
30 30
31#if (MATRIX_COLS <= 8) 31#if (MATRIX_COLS <= 8)
32# define print_matrix_header() print("\nr/c 01234567\n") 32# define print_matrix_header() print("\nr/c 01234567\n")
33# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 33# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
34# define ROW_SHIFTER ((uint8_t)1) 34# define ROW_SHIFTER ((uint8_t)1)
35#elif (MATRIX_COLS <= 16) 35#elif (MATRIX_COLS <= 16)
36# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") 36# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
37# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) 37# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
38# define ROW_SHIFTER ((uint16_t)1) 38# define ROW_SHIFTER ((uint16_t)1)
39#elif (MATRIX_COLS <= 32) 39#elif (MATRIX_COLS <= 32)
40# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") 40# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
41# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) 41# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
42# define ROW_SHIFTER ((uint32_t)1) 42# define ROW_SHIFTER ((uint32_t)1)
43#endif 43#endif
44 44
45static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 45static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
46static const uint8_t col_select_pins[3] = MATRIX_COL_SELECT_PINS; 46static const uint8_t col_select_pins[3] = MATRIX_COL_SELECT_PINS;
47static const uint8_t dat_pin = MATRIX_COL_DATA_PIN; 47static const uint8_t dat_pin = MATRIX_COL_DATA_PIN;
48 48
49/* matrix state(1:on, 0:off) */ 49/* matrix state(1:on, 0:off) */
50static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values 50static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
51static matrix_row_t matrix[MATRIX_ROWS]; //raw values 51static matrix_row_t matrix[MATRIX_ROWS]; // raw values
52 52
53/* 2d array containing binary representation of its index */ 53/* 2d array containing binary representation of its index */
54static const uint8_t num_in_binary[8][3] = { 54static const uint8_t num_in_binary[8][3] = {
55 {0, 0, 0}, 55 {0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1},
56 {0, 0, 1},
57 {0, 1, 0},
58 {0, 1, 1},
59 {1, 0, 0},
60 {1, 0, 1},
61 {1, 1, 0},
62 {1, 1, 1},
63}; 56};
64 57
65static void select_col_analog(uint8_t col); 58static void select_col_analog(uint8_t col);
66static void mux_pin_control(const uint8_t binary[]); 59static void mux_pin_control(const uint8_t binary[]);
67void debounce_init(uint8_t num_rows); 60void debounce_init(void);
68void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed); 61void debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed);
69 62
63__attribute__((weak)) void matrix_init_user(void) {}
70 64
71__attribute__ ((weak)) 65__attribute__((weak)) void matrix_scan_user(void) {}
72void matrix_init_user(void) {}
73 66
74__attribute__ ((weak)) 67__attribute__((weak)) void matrix_init_kb(void) {
75void matrix_scan_user(void) {} 68 matrix_init_user();
76
77__attribute__ ((weak))
78void matrix_init_kb(void) {
79 matrix_init_user();
80} 69}
81 70
82__attribute__ ((weak)) 71__attribute__((weak)) void matrix_scan_kb(void) {
83void matrix_scan_kb(void) { 72 matrix_scan_user();
84 matrix_scan_user();
85} 73}
86 74
87inline 75inline uint8_t matrix_rows(void) {
88uint8_t matrix_rows(void)
89{
90 return MATRIX_ROWS; 76 return MATRIX_ROWS;
91} 77}
92 78
93inline 79inline uint8_t matrix_cols(void) {
94uint8_t matrix_cols(void)
95{
96 return MATRIX_COLS; 80 return MATRIX_COLS;
97} 81}
98 82
99inline 83inline bool matrix_is_on(uint8_t row, uint8_t col) {
100bool matrix_is_on(uint8_t row, uint8_t col) 84 return (matrix[row] & ((matrix_row_t)1 << col));
101{
102 return (matrix[row] & ((matrix_row_t)1<<col));
103} 85}
104 86
105inline 87inline matrix_row_t matrix_get_row(uint8_t row) {
106matrix_row_t matrix_get_row(uint8_t row)
107{
108 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a 88 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
109 // switch blocker installed and the switch is always pressed. 89 // switch blocker installed and the switch is always pressed.
110#ifdef MATRIX_MASKED 90#ifdef MATRIX_MASKED
@@ -114,48 +94,44 @@ matrix_row_t matrix_get_row(uint8_t row)
114#endif 94#endif
115} 95}
116 96
117void matrix_print(void) 97void matrix_print(void) {
118{
119 print_matrix_header(); 98 print_matrix_header();
120 99
121 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 100 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
122 print_hex8(row); print(": "); 101 print_hex8(row);
102 print(": ");
123 print_matrix_row(row); 103 print_matrix_row(row);
124 print("\n"); 104 print("\n");
125 } 105 }
126} 106}
127 107
128// uses standard row code 108// uses standard row code
129static void select_row(uint8_t row) 109static void select_row(uint8_t row) {
130{
131 gpio_set_pin_output(row_pins[row]); 110 gpio_set_pin_output(row_pins[row]);
132 gpio_write_pin_low(row_pins[row]); 111 gpio_write_pin_low(row_pins[row]);
133} 112}
134 113
135static void unselect_row(uint8_t row) 114static void unselect_row(uint8_t row) {
136{
137 gpio_set_pin_input_high(row_pins[row]); 115 gpio_set_pin_input_high(row_pins[row]);
138} 116}
139 117
140static void unselect_rows(void) 118static void unselect_rows(void) {
141{ 119 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
142 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
143 gpio_set_pin_input_high(row_pins[x]); 120 gpio_set_pin_input_high(row_pins[x]);
144 } 121 }
145} 122}
146 123
147static void init_pins(void) { // still need some fixing, this might not work 124static void init_pins(void) { // still need some fixing, this might not work
148 unselect_rows(); // with the loop 125 unselect_rows(); // with the loop
149 /* 126 /*
150 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 127 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
151 gpio_set_pin_input_high(col_pins[x]); 128 gpio_set_pin_input_high(col_pins[x]);
152 } 129 }
153 */ 130 */
154 gpio_set_pin_input_high(dat_pin); 131 gpio_set_pin_input_high(dat_pin);
155} 132}
156 133
157static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 134static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
158{
159 // Store last value of row prior to reading 135 // Store last value of row prior to reading
160 matrix_row_t last_row_value = current_matrix[current_row]; 136 matrix_row_t last_row_value = current_matrix[current_row];
161 137
@@ -167,15 +143,14 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
167 wait_us(30); 143 wait_us(30);
168 144
169 // For each col... 145 // For each col...
170 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 146 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
171
172 // Select the col pin to read (active low) 147 // Select the col pin to read (active low)
173 select_col_analog(col_index); 148 select_col_analog(col_index);
174 wait_us(30); 149 wait_us(30);
175 uint8_t pin_state = gpio_read_pin(dat_pin); 150 uint8_t pin_state = gpio_read_pin(dat_pin);
176 151
177 // Populate the matrix row with the state of the col pin 152 // Populate the matrix row with the state of the col pin
178 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 153 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
179 } 154 }
180 155
181 // Unselect row 156 // Unselect row
@@ -184,19 +159,17 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
184 return (last_row_value != current_matrix[current_row]); 159 return (last_row_value != current_matrix[current_row]);
185} 160}
186 161
187
188void matrix_init(void) { 162void matrix_init(void) {
189
190 // initialize key pins 163 // initialize key pins
191 init_pins(); 164 init_pins();
192 165
193 // initialize matrix state: all keys off 166 // initialize matrix state: all keys off
194 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 167 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
195 raw_matrix[i] = 0; 168 raw_matrix[i] = 0;
196 matrix[i] = 0; 169 matrix[i] = 0;
197 } 170 }
198 171
199 debounce_init(MATRIX_ROWS); 172 debounce_init();
200 173
201 matrix_init_kb(); 174 matrix_init_kb();
202 175
@@ -205,15 +178,14 @@ void matrix_init(void) {
205} 178}
206 179
207// modified for per col read matrix scan 180// modified for per col read matrix scan
208uint8_t matrix_scan(void) 181uint8_t matrix_scan(void) {
209{
210 bool changed = false; 182 bool changed = false;
211 183
212 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 184 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
213 changed |= read_cols_on_row(raw_matrix, current_row); 185 changed |= read_cols_on_row(raw_matrix, current_row);
214 } 186 }
215 187
216 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 188 debounce(raw_matrix, matrix, changed);
217 189
218 matrix_scan_kb(); 190 matrix_scan_kb();
219 return (uint8_t)changed; 191 return (uint8_t)changed;
@@ -231,7 +203,7 @@ uint8_t matrix_scan(void)
231 } 203 }
232#endif 204#endif
233 205
234 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 206 debounce(raw_matrix, matrix, changed);
235 207
236 matrix_scan_kb(); 208 matrix_scan_kb();
237 return (uint8_t)changed; 209 return (uint8_t)changed;
@@ -239,8 +211,7 @@ uint8_t matrix_scan(void)
239*/ 211*/
240 212
241static void select_col_analog(uint8_t col) { 213static void select_col_analog(uint8_t col) {
242 switch(col) { 214 switch (col) {
243
244 case 0: 215 case 0:
245 mux_pin_control(num_in_binary[0]); 216 mux_pin_control(num_in_binary[0]);
246 break; 217 break;
@@ -273,26 +244,23 @@ static void select_col_analog(uint8_t col) {
273static void mux_pin_control(const uint8_t binary[]) { 244static void mux_pin_control(const uint8_t binary[]) {
274 // set pin0 245 // set pin0
275 gpio_set_pin_output(col_select_pins[0]); 246 gpio_set_pin_output(col_select_pins[0]);
276 if(binary[2] == 0) { 247 if (binary[2] == 0) {
277 gpio_write_pin_low(col_select_pins[0]); 248 gpio_write_pin_low(col_select_pins[0]);
278 } 249 } else {
279 else {
280 gpio_write_pin_high(col_select_pins[0]); 250 gpio_write_pin_high(col_select_pins[0]);
281 } 251 }
282 // set pin1 252 // set pin1
283 gpio_set_pin_output(col_select_pins[1]); 253 gpio_set_pin_output(col_select_pins[1]);
284 if(binary[1] == 0) { 254 if (binary[1] == 0) {
285 gpio_write_pin_low(col_select_pins[1]); 255 gpio_write_pin_low(col_select_pins[1]);
286 } 256 } else {
287 else {
288 gpio_write_pin_high(col_select_pins[1]); 257 gpio_write_pin_high(col_select_pins[1]);
289 } 258 }
290 // set pin2 259 // set pin2
291 gpio_set_pin_output(col_select_pins[2]); 260 gpio_set_pin_output(col_select_pins[2]);
292 if(binary[0] == 0) { 261 if (binary[0] == 0) {
293 gpio_write_pin_low(col_select_pins[2]); 262 gpio_write_pin_low(col_select_pins[2]);
294 } 263 } else {
295 else {
296 gpio_write_pin_high(col_select_pins[2]); 264 gpio_write_pin_high(col_select_pins[2]);
297 } 265 }
298} 266}
diff --git a/keyboards/handwired/symmetric70_proto/matrix_debug/matrix.c b/keyboards/handwired/symmetric70_proto/matrix_debug/matrix.c
index fa9b6ef0fd..2fa06ec7ce 100644
--- a/keyboards/handwired/symmetric70_proto/matrix_debug/matrix.c
+++ b/keyboards/handwired/symmetric70_proto/matrix_debug/matrix.c
@@ -44,7 +44,7 @@ static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
44static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 44static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
45static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 45static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
46# ifdef MATRIX_MUL_SELECT 46# ifdef MATRIX_MUL_SELECT
47static const pin_t col_sel[MATRIX_COLS] = MATRIX_MUL_SEL; 47static const pin_t col_sel[MATRIX_COLS] = MATRIX_MUL_SEL;
48# endif 48# endif
49#endif 49#endif
50 50
@@ -57,8 +57,8 @@ static const uint8_t delay_sel[] = {MATRIX_IO_DELAY_MULSEL};
57#endif 57#endif
58 58
59/* matrix state(1:on, 0:off) */ 59/* matrix state(1:on, 0:off) */
60extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values 60extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
61extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values 61extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
62 62
63static inline void gpio_atomic_set_pin_output_low(pin_t pin) { 63static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
64 ATOMIC_BLOCK_FORCEON { 64 ATOMIC_BLOCK_FORCEON {
@@ -68,7 +68,9 @@ static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
68} 68}
69 69
70static inline void gpio_atomic_set_pin_input_high(pin_t pin) { 70static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
71 ATOMIC_BLOCK_FORCEON { gpio_set_pin_input_high(pin); } 71 ATOMIC_BLOCK_FORCEON {
72 gpio_set_pin_input_high(pin);
73 }
72} 74}
73 75
74// matrix code 76// matrix code
@@ -108,9 +110,13 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
108#elif defined(DIODE_DIRECTION) 110#elif defined(DIODE_DIRECTION)
109# if (DIODE_DIRECTION == COL2ROW) 111# if (DIODE_DIRECTION == COL2ROW)
110 112
111static void select_row(uint8_t row) { gpio_atomic_set_pin_output_low(row_pins[row]); } 113static void select_row(uint8_t row) {
114 gpio_atomic_set_pin_output_low(row_pins[row]);
115}
112 116
113static void unselect_row(uint8_t row) { gpio_atomic_set_pin_input_high(row_pins[row]); } 117static void unselect_row(uint8_t row) {
118 gpio_atomic_set_pin_input_high(row_pins[row]);
119}
114 120
115static void unselect_rows(void) { 121static void unselect_rows(void) {
116 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 122 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
@@ -153,7 +159,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
153 // Unselect row 159 // Unselect row
154 unselect_row(current_row); 160 unselect_row(current_row);
155# ifdef MATRIX_IO_DELAY_PORTS 161# ifdef MATRIX_IO_DELAY_PORTS
156 if (current_row_value) { // wait for col signal to go HIGH 162 if (current_row_value) { // wait for col signal to go HIGH
157 bool is_pressed; 163 bool is_pressed;
158 do { 164 do {
159 MATRIX_DEBUG_DELAY_START(); 165 MATRIX_DEBUG_DELAY_START();
@@ -170,7 +176,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
170 } 176 }
171# endif 177# endif
172# ifdef MATRIX_IO_DELAY_ADAPTIVE 178# ifdef MATRIX_IO_DELAY_ADAPTIVE
173 if (current_row_value) { // wait for col signal to go HIGH 179 if (current_row_value) { // wait for col signal to go HIGH
174 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 180 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
175 MATRIX_DEBUG_DELAY_START(); 181 MATRIX_DEBUG_DELAY_START();
176# ifdef MATRIX_MUL_SELECT 182# ifdef MATRIX_MUL_SELECT
@@ -184,7 +190,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
184 } 190 }
185# endif 191# endif
186# ifdef MATRIX_IO_DELAY_ADAPTIVE2 192# ifdef MATRIX_IO_DELAY_ADAPTIVE2
187 if (current_row_value) { // wait for col signal to go HIGH 193 if (current_row_value) { // wait for col signal to go HIGH
188 pin_t state; 194 pin_t state;
189 do { 195 do {
190 MATRIX_DEBUG_DELAY_START(); 196 MATRIX_DEBUG_DELAY_START();
@@ -204,7 +210,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
204# endif 210# endif
205 if (MATRIX_IO_DELAY_ALWAYS || current_row + 1 < MATRIX_ROWS) { 211 if (MATRIX_IO_DELAY_ALWAYS || current_row + 1 < MATRIX_ROWS) {
206 MATRIX_DEBUG_DELAY_START(); 212 MATRIX_DEBUG_DELAY_START();
207 matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for col signal to go HIGH 213 matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for col signal to go HIGH
208 MATRIX_DEBUG_DELAY_END(); 214 MATRIX_DEBUG_DELAY_END();
209 } 215 }
210 216
@@ -218,9 +224,13 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
218 224
219# elif (DIODE_DIRECTION == ROW2COL) 225# elif (DIODE_DIRECTION == ROW2COL)
220 226
221static void select_col(uint8_t col) { gpio_atomic_set_pin_output_low(col_pins[col]); } 227static void select_col(uint8_t col) {
228 gpio_atomic_set_pin_output_low(col_pins[col]);
229}
222 230
223static void unselect_col(uint8_t col) { gpio_atomic_set_pin_input_high(col_pins[col]); } 231static void unselect_col(uint8_t col) {
232 gpio_atomic_set_pin_input_high(col_pins[col]);
233}
224 234
225static void unselect_cols(void) { 235static void unselect_cols(void) {
226 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 236 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
@@ -237,7 +247,7 @@ static void init_pins(void) {
237 247
238static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { 248static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
239 bool matrix_changed = false; 249 bool matrix_changed = false;
240 bool key_pressed = false; 250 bool key_pressed = false;
241 251
242 // Select col 252 // Select col
243 select_col(current_col); 253 select_col(current_col);
@@ -269,7 +279,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
269 // Unselect col 279 // Unselect col
270 unselect_col(current_col); 280 unselect_col(current_col);
271 if (MATRIX_IO_DELAY_ALWAYS || current_col + 1 < MATRIX_COLS) { 281 if (MATRIX_IO_DELAY_ALWAYS || current_col + 1 < MATRIX_COLS) {
272 matrix_output_unselect_delay(current_col, key_pressed); // wait for col signal to go HIGH 282 matrix_output_unselect_delay(current_col, key_pressed); // wait for col signal to go HIGH
273 } 283 }
274 284
275 return matrix_changed; 285 return matrix_changed;
@@ -292,7 +302,7 @@ void matrix_init(void) {
292 matrix[i] = 0; 302 matrix[i] = 0;
293 } 303 }
294 304
295 debounce_init(MATRIX_ROWS); 305 debounce_init();
296 306
297 matrix_init_kb(); 307 matrix_init_kb();
298} 308}
@@ -317,7 +327,7 @@ uint8_t matrix_scan(void) {
317 MATRIX_DEBUG_GAP(); 327 MATRIX_DEBUG_GAP();
318 328
319 MATRIX_DEBUG_SCAN_START(); 329 MATRIX_DEBUG_SCAN_START();
320 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 330 debounce(raw_matrix, matrix, changed);
321 MATRIX_DEBUG_SCAN_END(); 331 MATRIX_DEBUG_SCAN_END();
322 MATRIX_DEBUG_GAP(); 332 MATRIX_DEBUG_GAP();
323 333
diff --git a/keyboards/handwired/symmetric70_proto/matrix_fast/matrix.c b/keyboards/handwired/symmetric70_proto/matrix_fast/matrix.c
index 842df65dbd..99ce2f2c2d 100644
--- a/keyboards/handwired/symmetric70_proto/matrix_fast/matrix.c
+++ b/keyboards/handwired/symmetric70_proto/matrix_fast/matrix.c
@@ -166,7 +166,7 @@ void matrix_init(void) {
166 matrix[i] = 0; 166 matrix[i] = 0;
167 } 167 }
168 168
169 debounce_init(MATRIX_ROWS); 169 debounce_init();
170 170
171 matrix_init_kb(); 171 matrix_init_kb();
172} 172}
@@ -221,7 +221,7 @@ uint8_t matrix_scan(void) {
221 MATRIX_DEBUG_SCAN_END(); MATRIX_DEBUG_GAP(); MATRIX_DEBUG_SCAN_START(); 221 MATRIX_DEBUG_SCAN_END(); MATRIX_DEBUG_GAP(); MATRIX_DEBUG_SCAN_START();
222 222
223 // debounce raw_matrix[] to matrix[] 223 // debounce raw_matrix[] to matrix[]
224 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 224 debounce(raw_matrix, matrix, changed);
225 MATRIX_DEBUG_SCAN_END(); MATRIX_DEBUG_GAP(); 225 MATRIX_DEBUG_SCAN_END(); MATRIX_DEBUG_GAP();
226 226
227 MATRIX_DEBUG_SCAN_START(); 227 MATRIX_DEBUG_SCAN_START();
diff --git a/keyboards/kakunpc/angel64/alpha/matrix.c b/keyboards/kakunpc/angel64/alpha/matrix.c
index ff2b8a801e..b1bcadc9dd 100644
--- a/keyboards/kakunpc/angel64/alpha/matrix.c
+++ b/keyboards/kakunpc/angel64/alpha/matrix.c
@@ -22,67 +22,55 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
22#include "debounce.h" 22#include "debounce.h"
23 23
24#if (MATRIX_COLS <= 8) 24#if (MATRIX_COLS <= 8)
25# define print_matrix_header() print("\nr/c 01234567\n") 25# define print_matrix_header() print("\nr/c 01234567\n")
26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
27# define ROW_SHIFTER ((uint8_t)1) 27# define ROW_SHIFTER ((uint8_t)1)
28#elif (MATRIX_COLS <= 16) 28#elif (MATRIX_COLS <= 16)
29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") 29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) 30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
31# define ROW_SHIFTER ((uint16_t)1) 31# define ROW_SHIFTER ((uint16_t)1)
32#elif (MATRIX_COLS <= 32) 32#elif (MATRIX_COLS <= 32)
33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") 33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) 34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
35# define ROW_SHIFTER ((uint32_t)1) 35# define ROW_SHIFTER ((uint32_t)1)
36#endif 36#endif
37 37
38#ifdef MATRIX_MASKED 38#ifdef MATRIX_MASKED
39 extern const matrix_row_t matrix_mask[]; 39extern const matrix_row_t matrix_mask[];
40#endif 40#endif
41 41
42static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 42static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
43static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 43static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
44 44
45/* matrix state(1:on, 0:off) */ 45/* matrix state(1:on, 0:off) */
46static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values 46static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
47static matrix_row_t matrix[MATRIX_ROWS]; //debounced values 47static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
48 48
49__attribute__ ((weak)) 49__attribute__((weak)) void matrix_init_kb(void) {
50void matrix_init_kb(void) {
51 matrix_init_user(); 50 matrix_init_user();
52} 51}
53 52
54__attribute__ ((weak)) 53__attribute__((weak)) void matrix_scan_kb(void) {
55void matrix_scan_kb(void) {
56 matrix_scan_user(); 54 matrix_scan_user();
57} 55}
58 56
59__attribute__ ((weak)) 57__attribute__((weak)) void matrix_init_user(void) {}
60void matrix_init_user(void) {
61}
62 58
63__attribute__ ((weak)) 59__attribute__((weak)) void matrix_scan_user(void) {}
64void matrix_scan_user(void) {
65}
66 60
67inline 61inline uint8_t matrix_rows(void) {
68uint8_t matrix_rows(void) {
69 return MATRIX_ROWS; 62 return MATRIX_ROWS;
70} 63}
71 64
72inline 65inline uint8_t matrix_cols(void) {
73uint8_t matrix_cols(void) {
74 return MATRIX_COLS; 66 return MATRIX_COLS;
75} 67}
76 68
77inline 69inline bool matrix_is_on(uint8_t row, uint8_t col) {
78bool matrix_is_on(uint8_t row, uint8_t col) 70 return (matrix[row] & ((matrix_row_t)1 << col));
79{
80 return (matrix[row] & ((matrix_row_t)1<<col));
81} 71}
82 72
83inline 73inline matrix_row_t matrix_get_row(uint8_t row) {
84matrix_row_t matrix_get_row(uint8_t row)
85{
86 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a 74 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
87 // switch blocker installed and the switch is always pressed. 75 // switch blocker installed and the switch is always pressed.
88#ifdef MATRIX_MASKED 76#ifdef MATRIX_MASKED
@@ -92,66 +80,59 @@ matrix_row_t matrix_get_row(uint8_t row)
92#endif 80#endif
93} 81}
94 82
95void matrix_print(void) 83void matrix_print(void) {
96{
97 print_matrix_header(); 84 print_matrix_header();
98 85
99 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 86 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
100 print_hex8(row); print(": "); 87 print_hex8(row);
88 print(": ");
101 print_matrix_row(row); 89 print_matrix_row(row);
102 print("\n"); 90 print("\n");
103 } 91 }
104} 92}
105 93
106static void select_row(uint8_t row) 94static void select_row(uint8_t row) {
107{
108 gpio_set_pin_output(row_pins[row]); 95 gpio_set_pin_output(row_pins[row]);
109 gpio_write_pin_low(row_pins[row]); 96 gpio_write_pin_low(row_pins[row]);
110} 97}
111 98
112static void unselect_row(uint8_t row) 99static void unselect_row(uint8_t row) {
113{
114 gpio_set_pin_input_high(row_pins[row]); 100 gpio_set_pin_input_high(row_pins[row]);
115} 101}
116 102
117static void unselect_rows(void) 103static void unselect_rows(void) {
118{ 104 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
119 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
120 gpio_set_pin_input_high(row_pins[x]); 105 gpio_set_pin_input_high(row_pins[x]);
121 } 106 }
122} 107}
123 108
124static void select_col(uint8_t col) 109static void select_col(uint8_t col) {
125{
126 gpio_set_pin_output(col_pins[col]); 110 gpio_set_pin_output(col_pins[col]);
127 gpio_write_pin_low(col_pins[col]); 111 gpio_write_pin_low(col_pins[col]);
128} 112}
129 113
130static void unselect_col(uint8_t col) 114static void unselect_col(uint8_t col) {
131{
132 gpio_set_pin_input_high(col_pins[col]); 115 gpio_set_pin_input_high(col_pins[col]);
133} 116}
134 117
135static void unselect_cols(void) 118static void unselect_cols(void) {
136{ 119 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
137 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
138 gpio_set_pin_input_high(col_pins[x]); 120 gpio_set_pin_input_high(col_pins[x]);
139 } 121 }
140} 122}
141 123
142static void init_pins(void) { 124static void init_pins(void) {
143 unselect_rows(); 125 unselect_rows();
144 unselect_cols(); 126 unselect_cols();
145 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 127 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
146 gpio_set_pin_input_high(col_pins[x]); 128 gpio_set_pin_input_high(col_pins[x]);
147 } 129 }
148 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 130 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
149 gpio_set_pin_input_high(row_pins[x]); 131 gpio_set_pin_input_high(row_pins[x]);
150 } 132 }
151} 133}
152 134
153static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 135static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
154{
155 // Store last value of row prior to reading 136 // Store last value of row prior to reading
156 matrix_row_t last_row_value = current_matrix[current_row]; 137 matrix_row_t last_row_value = current_matrix[current_row];
157 138
@@ -163,13 +144,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
163 wait_us(30); 144 wait_us(30);
164 145
165 // For each col... 146 // For each col...
166 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 147 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
167
168 // Select the col pin to read (active low) 148 // Select the col pin to read (active low)
169 uint8_t pin_state = gpio_read_pin(col_pins[col_index]); 149 uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
170 150
171 // Populate the matrix row with the state of the col pin 151 // Populate the matrix row with the state of the col pin
172 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 152 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
173 } 153 }
174 154
175 // Unselect row 155 // Unselect row
@@ -178,8 +158,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
178 return (last_row_value != current_matrix[current_row]); 158 return (last_row_value != current_matrix[current_row]);
179} 159}
180 160
181static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) 161static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
182{
183 bool matrix_changed = false; 162 bool matrix_changed = false;
184 163
185 // Select col and wait for col selecton to stabilize 164 // Select col and wait for col selecton to stabilize
@@ -187,27 +166,22 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
187 wait_us(30); 166 wait_us(30);
188 167
189 // For each row... 168 // For each row...
190 for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++) 169 for (uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
191 { 170 uint8_t tmp = row_index + MATRIX_ROWS / 2;
192 uint8_t tmp = row_index + MATRIX_ROWS/2;
193 // Store last value of row prior to reading 171 // Store last value of row prior to reading
194 matrix_row_t last_row_value = current_matrix[tmp]; 172 matrix_row_t last_row_value = current_matrix[tmp];
195 173
196 // Check row pin state 174 // Check row pin state
197 if (gpio_read_pin(row_pins[row_index]) == 0) 175 if (gpio_read_pin(row_pins[row_index]) == 0) {
198 {
199 // Pin LO, set col bit 176 // Pin LO, set col bit
200 current_matrix[tmp] |= (ROW_SHIFTER << current_col); 177 current_matrix[tmp] |= (ROW_SHIFTER << current_col);
201 } 178 } else {
202 else
203 {
204 // Pin HI, clear col bit 179 // Pin HI, clear col bit
205 current_matrix[tmp] &= ~(ROW_SHIFTER << current_col); 180 current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
206 } 181 }
207 182
208 // Determine if the matrix changed state 183 // Determine if the matrix changed state
209 if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) 184 if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
210 {
211 matrix_changed = true; 185 matrix_changed = true;
212 } 186 }
213 } 187 }
@@ -219,37 +193,35 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
219} 193}
220 194
221void matrix_init(void) { 195void matrix_init(void) {
222
223 // initialize key pins 196 // initialize key pins
224 init_pins(); 197 init_pins();
225 198
226 // initialize matrix state: all keys off 199 // initialize matrix state: all keys off
227 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 200 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
228 raw_matrix[i] = 0; 201 raw_matrix[i] = 0;
229 matrix[i] = 0; 202 matrix[i] = 0;
230 } 203 }
231 204
232 debounce_init(MATRIX_ROWS); 205 debounce_init();
233 206
234 matrix_init_kb(); 207 matrix_init_kb();
235} 208}
236 209
237uint8_t matrix_scan(void) 210uint8_t matrix_scan(void) {
238{ 211 bool changed = false;
239 bool changed = false;
240 212
241 // Set row, read cols 213 // Set row, read cols
242 for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) { 214 for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
243 changed |= read_cols_on_row(raw_matrix, current_row); 215 changed |= read_cols_on_row(raw_matrix, current_row);
244 } 216 }
245 //else 217 // else
246 // Set col, read rows 218 // Set col, read rows
247 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 219 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
248 changed |= read_rows_on_col(raw_matrix, current_col); 220 changed |= read_rows_on_col(raw_matrix, current_col);
249 } 221 }
250 222
251 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 223 debounce(raw_matrix, matrix, changed);
252 224
253 matrix_scan_kb(); 225 matrix_scan_kb();
254 return (uint8_t)changed; 226 return (uint8_t)changed;
255} 227}
diff --git a/keyboards/kakunpc/angel64/rev1/matrix.c b/keyboards/kakunpc/angel64/rev1/matrix.c
index ff2b8a801e..b1bcadc9dd 100644
--- a/keyboards/kakunpc/angel64/rev1/matrix.c
+++ b/keyboards/kakunpc/angel64/rev1/matrix.c
@@ -22,67 +22,55 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
22#include "debounce.h" 22#include "debounce.h"
23 23
24#if (MATRIX_COLS <= 8) 24#if (MATRIX_COLS <= 8)
25# define print_matrix_header() print("\nr/c 01234567\n") 25# define print_matrix_header() print("\nr/c 01234567\n")
26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
27# define ROW_SHIFTER ((uint8_t)1) 27# define ROW_SHIFTER ((uint8_t)1)
28#elif (MATRIX_COLS <= 16) 28#elif (MATRIX_COLS <= 16)
29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") 29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) 30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
31# define ROW_SHIFTER ((uint16_t)1) 31# define ROW_SHIFTER ((uint16_t)1)
32#elif (MATRIX_COLS <= 32) 32#elif (MATRIX_COLS <= 32)
33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") 33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) 34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
35# define ROW_SHIFTER ((uint32_t)1) 35# define ROW_SHIFTER ((uint32_t)1)
36#endif 36#endif
37 37
38#ifdef MATRIX_MASKED 38#ifdef MATRIX_MASKED
39 extern const matrix_row_t matrix_mask[]; 39extern const matrix_row_t matrix_mask[];
40#endif 40#endif
41 41
42static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 42static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
43static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 43static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
44 44
45/* matrix state(1:on, 0:off) */ 45/* matrix state(1:on, 0:off) */
46static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values 46static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
47static matrix_row_t matrix[MATRIX_ROWS]; //debounced values 47static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
48 48
49__attribute__ ((weak)) 49__attribute__((weak)) void matrix_init_kb(void) {
50void matrix_init_kb(void) {
51 matrix_init_user(); 50 matrix_init_user();
52} 51}
53 52
54__attribute__ ((weak)) 53__attribute__((weak)) void matrix_scan_kb(void) {
55void matrix_scan_kb(void) {
56 matrix_scan_user(); 54 matrix_scan_user();
57} 55}
58 56
59__attribute__ ((weak)) 57__attribute__((weak)) void matrix_init_user(void) {}
60void matrix_init_user(void) {
61}
62 58
63__attribute__ ((weak)) 59__attribute__((weak)) void matrix_scan_user(void) {}
64void matrix_scan_user(void) {
65}
66 60
67inline 61inline uint8_t matrix_rows(void) {
68uint8_t matrix_rows(void) {
69 return MATRIX_ROWS; 62 return MATRIX_ROWS;
70} 63}
71 64
72inline 65inline uint8_t matrix_cols(void) {
73uint8_t matrix_cols(void) {
74 return MATRIX_COLS; 66 return MATRIX_COLS;
75} 67}
76 68
77inline 69inline bool matrix_is_on(uint8_t row, uint8_t col) {
78bool matrix_is_on(uint8_t row, uint8_t col) 70 return (matrix[row] & ((matrix_row_t)1 << col));
79{
80 return (matrix[row] & ((matrix_row_t)1<<col));
81} 71}
82 72
83inline 73inline matrix_row_t matrix_get_row(uint8_t row) {
84matrix_row_t matrix_get_row(uint8_t row)
85{
86 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a 74 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
87 // switch blocker installed and the switch is always pressed. 75 // switch blocker installed and the switch is always pressed.
88#ifdef MATRIX_MASKED 76#ifdef MATRIX_MASKED
@@ -92,66 +80,59 @@ matrix_row_t matrix_get_row(uint8_t row)
92#endif 80#endif
93} 81}
94 82
95void matrix_print(void) 83void matrix_print(void) {
96{
97 print_matrix_header(); 84 print_matrix_header();
98 85
99 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 86 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
100 print_hex8(row); print(": "); 87 print_hex8(row);
88 print(": ");
101 print_matrix_row(row); 89 print_matrix_row(row);
102 print("\n"); 90 print("\n");
103 } 91 }
104} 92}
105 93
106static void select_row(uint8_t row) 94static void select_row(uint8_t row) {
107{
108 gpio_set_pin_output(row_pins[row]); 95 gpio_set_pin_output(row_pins[row]);
109 gpio_write_pin_low(row_pins[row]); 96 gpio_write_pin_low(row_pins[row]);
110} 97}
111 98
112static void unselect_row(uint8_t row) 99static void unselect_row(uint8_t row) {
113{
114 gpio_set_pin_input_high(row_pins[row]); 100 gpio_set_pin_input_high(row_pins[row]);
115} 101}
116 102
117static void unselect_rows(void) 103static void unselect_rows(void) {
118{ 104 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
119 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
120 gpio_set_pin_input_high(row_pins[x]); 105 gpio_set_pin_input_high(row_pins[x]);
121 } 106 }
122} 107}
123 108
124static void select_col(uint8_t col) 109static void select_col(uint8_t col) {
125{
126 gpio_set_pin_output(col_pins[col]); 110 gpio_set_pin_output(col_pins[col]);
127 gpio_write_pin_low(col_pins[col]); 111 gpio_write_pin_low(col_pins[col]);
128} 112}
129 113
130static void unselect_col(uint8_t col) 114static void unselect_col(uint8_t col) {
131{
132 gpio_set_pin_input_high(col_pins[col]); 115 gpio_set_pin_input_high(col_pins[col]);
133} 116}
134 117
135static void unselect_cols(void) 118static void unselect_cols(void) {
136{ 119 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
137 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
138 gpio_set_pin_input_high(col_pins[x]); 120 gpio_set_pin_input_high(col_pins[x]);
139 } 121 }
140} 122}
141 123
142static void init_pins(void) { 124static void init_pins(void) {
143 unselect_rows(); 125 unselect_rows();
144 unselect_cols(); 126 unselect_cols();
145 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 127 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
146 gpio_set_pin_input_high(col_pins[x]); 128 gpio_set_pin_input_high(col_pins[x]);
147 } 129 }
148 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 130 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
149 gpio_set_pin_input_high(row_pins[x]); 131 gpio_set_pin_input_high(row_pins[x]);
150 } 132 }
151} 133}
152 134
153static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 135static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
154{
155 // Store last value of row prior to reading 136 // Store last value of row prior to reading
156 matrix_row_t last_row_value = current_matrix[current_row]; 137 matrix_row_t last_row_value = current_matrix[current_row];
157 138
@@ -163,13 +144,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
163 wait_us(30); 144 wait_us(30);
164 145
165 // For each col... 146 // For each col...
166 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 147 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
167
168 // Select the col pin to read (active low) 148 // Select the col pin to read (active low)
169 uint8_t pin_state = gpio_read_pin(col_pins[col_index]); 149 uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
170 150
171 // Populate the matrix row with the state of the col pin 151 // Populate the matrix row with the state of the col pin
172 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 152 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
173 } 153 }
174 154
175 // Unselect row 155 // Unselect row
@@ -178,8 +158,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
178 return (last_row_value != current_matrix[current_row]); 158 return (last_row_value != current_matrix[current_row]);
179} 159}
180 160
181static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) 161static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
182{
183 bool matrix_changed = false; 162 bool matrix_changed = false;
184 163
185 // Select col and wait for col selecton to stabilize 164 // Select col and wait for col selecton to stabilize
@@ -187,27 +166,22 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
187 wait_us(30); 166 wait_us(30);
188 167
189 // For each row... 168 // For each row...
190 for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++) 169 for (uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
191 { 170 uint8_t tmp = row_index + MATRIX_ROWS / 2;
192 uint8_t tmp = row_index + MATRIX_ROWS/2;
193 // Store last value of row prior to reading 171 // Store last value of row prior to reading
194 matrix_row_t last_row_value = current_matrix[tmp]; 172 matrix_row_t last_row_value = current_matrix[tmp];
195 173
196 // Check row pin state 174 // Check row pin state
197 if (gpio_read_pin(row_pins[row_index]) == 0) 175 if (gpio_read_pin(row_pins[row_index]) == 0) {
198 {
199 // Pin LO, set col bit 176 // Pin LO, set col bit
200 current_matrix[tmp] |= (ROW_SHIFTER << current_col); 177 current_matrix[tmp] |= (ROW_SHIFTER << current_col);
201 } 178 } else {
202 else
203 {
204 // Pin HI, clear col bit 179 // Pin HI, clear col bit
205 current_matrix[tmp] &= ~(ROW_SHIFTER << current_col); 180 current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
206 } 181 }
207 182
208 // Determine if the matrix changed state 183 // Determine if the matrix changed state
209 if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) 184 if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
210 {
211 matrix_changed = true; 185 matrix_changed = true;
212 } 186 }
213 } 187 }
@@ -219,37 +193,35 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
219} 193}
220 194
221void matrix_init(void) { 195void matrix_init(void) {
222
223 // initialize key pins 196 // initialize key pins
224 init_pins(); 197 init_pins();
225 198
226 // initialize matrix state: all keys off 199 // initialize matrix state: all keys off
227 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 200 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
228 raw_matrix[i] = 0; 201 raw_matrix[i] = 0;
229 matrix[i] = 0; 202 matrix[i] = 0;
230 } 203 }
231 204
232 debounce_init(MATRIX_ROWS); 205 debounce_init();
233 206
234 matrix_init_kb(); 207 matrix_init_kb();
235} 208}
236 209
237uint8_t matrix_scan(void) 210uint8_t matrix_scan(void) {
238{ 211 bool changed = false;
239 bool changed = false;
240 212
241 // Set row, read cols 213 // Set row, read cols
242 for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) { 214 for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
243 changed |= read_cols_on_row(raw_matrix, current_row); 215 changed |= read_cols_on_row(raw_matrix, current_row);
244 } 216 }
245 //else 217 // else
246 // Set col, read rows 218 // Set col, read rows
247 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 219 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
248 changed |= read_rows_on_col(raw_matrix, current_col); 220 changed |= read_rows_on_col(raw_matrix, current_col);
249 } 221 }
250 222
251 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 223 debounce(raw_matrix, matrix, changed);
252 224
253 matrix_scan_kb(); 225 matrix_scan_kb();
254 return (uint8_t)changed; 226 return (uint8_t)changed;
255} 227}
diff --git a/keyboards/kakunpc/thedogkeyboard/matrix.c b/keyboards/kakunpc/thedogkeyboard/matrix.c
index ff2b8a801e..b1bcadc9dd 100644
--- a/keyboards/kakunpc/thedogkeyboard/matrix.c
+++ b/keyboards/kakunpc/thedogkeyboard/matrix.c
@@ -22,67 +22,55 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
22#include "debounce.h" 22#include "debounce.h"
23 23
24#if (MATRIX_COLS <= 8) 24#if (MATRIX_COLS <= 8)
25# define print_matrix_header() print("\nr/c 01234567\n") 25# define print_matrix_header() print("\nr/c 01234567\n")
26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
27# define ROW_SHIFTER ((uint8_t)1) 27# define ROW_SHIFTER ((uint8_t)1)
28#elif (MATRIX_COLS <= 16) 28#elif (MATRIX_COLS <= 16)
29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") 29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) 30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
31# define ROW_SHIFTER ((uint16_t)1) 31# define ROW_SHIFTER ((uint16_t)1)
32#elif (MATRIX_COLS <= 32) 32#elif (MATRIX_COLS <= 32)
33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") 33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) 34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
35# define ROW_SHIFTER ((uint32_t)1) 35# define ROW_SHIFTER ((uint32_t)1)
36#endif 36#endif
37 37
38#ifdef MATRIX_MASKED 38#ifdef MATRIX_MASKED
39 extern const matrix_row_t matrix_mask[]; 39extern const matrix_row_t matrix_mask[];
40#endif 40#endif
41 41
42static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 42static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
43static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 43static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
44 44
45/* matrix state(1:on, 0:off) */ 45/* matrix state(1:on, 0:off) */
46static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values 46static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
47static matrix_row_t matrix[MATRIX_ROWS]; //debounced values 47static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
48 48
49__attribute__ ((weak)) 49__attribute__((weak)) void matrix_init_kb(void) {
50void matrix_init_kb(void) {
51 matrix_init_user(); 50 matrix_init_user();
52} 51}
53 52
54__attribute__ ((weak)) 53__attribute__((weak)) void matrix_scan_kb(void) {
55void matrix_scan_kb(void) {
56 matrix_scan_user(); 54 matrix_scan_user();
57} 55}
58 56
59__attribute__ ((weak)) 57__attribute__((weak)) void matrix_init_user(void) {}
60void matrix_init_user(void) {
61}
62 58
63__attribute__ ((weak)) 59__attribute__((weak)) void matrix_scan_user(void) {}
64void matrix_scan_user(void) {
65}
66 60
67inline 61inline uint8_t matrix_rows(void) {
68uint8_t matrix_rows(void) {
69 return MATRIX_ROWS; 62 return MATRIX_ROWS;
70} 63}
71 64
72inline 65inline uint8_t matrix_cols(void) {
73uint8_t matrix_cols(void) {
74 return MATRIX_COLS; 66 return MATRIX_COLS;
75} 67}
76 68
77inline 69inline bool matrix_is_on(uint8_t row, uint8_t col) {
78bool matrix_is_on(uint8_t row, uint8_t col) 70 return (matrix[row] & ((matrix_row_t)1 << col));
79{
80 return (matrix[row] & ((matrix_row_t)1<<col));
81} 71}
82 72
83inline 73inline matrix_row_t matrix_get_row(uint8_t row) {
84matrix_row_t matrix_get_row(uint8_t row)
85{
86 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a 74 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
87 // switch blocker installed and the switch is always pressed. 75 // switch blocker installed and the switch is always pressed.
88#ifdef MATRIX_MASKED 76#ifdef MATRIX_MASKED
@@ -92,66 +80,59 @@ matrix_row_t matrix_get_row(uint8_t row)
92#endif 80#endif
93} 81}
94 82
95void matrix_print(void) 83void matrix_print(void) {
96{
97 print_matrix_header(); 84 print_matrix_header();
98 85
99 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 86 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
100 print_hex8(row); print(": "); 87 print_hex8(row);
88 print(": ");
101 print_matrix_row(row); 89 print_matrix_row(row);
102 print("\n"); 90 print("\n");
103 } 91 }
104} 92}
105 93
106static void select_row(uint8_t row) 94static void select_row(uint8_t row) {
107{
108 gpio_set_pin_output(row_pins[row]); 95 gpio_set_pin_output(row_pins[row]);
109 gpio_write_pin_low(row_pins[row]); 96 gpio_write_pin_low(row_pins[row]);
110} 97}
111 98
112static void unselect_row(uint8_t row) 99static void unselect_row(uint8_t row) {
113{
114 gpio_set_pin_input_high(row_pins[row]); 100 gpio_set_pin_input_high(row_pins[row]);
115} 101}
116 102
117static void unselect_rows(void) 103static void unselect_rows(void) {
118{ 104 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
119 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
120 gpio_set_pin_input_high(row_pins[x]); 105 gpio_set_pin_input_high(row_pins[x]);
121 } 106 }
122} 107}
123 108
124static void select_col(uint8_t col) 109static void select_col(uint8_t col) {
125{
126 gpio_set_pin_output(col_pins[col]); 110 gpio_set_pin_output(col_pins[col]);
127 gpio_write_pin_low(col_pins[col]); 111 gpio_write_pin_low(col_pins[col]);
128} 112}
129 113
130static void unselect_col(uint8_t col) 114static void unselect_col(uint8_t col) {
131{
132 gpio_set_pin_input_high(col_pins[col]); 115 gpio_set_pin_input_high(col_pins[col]);
133} 116}
134 117
135static void unselect_cols(void) 118static void unselect_cols(void) {
136{ 119 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
137 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
138 gpio_set_pin_input_high(col_pins[x]); 120 gpio_set_pin_input_high(col_pins[x]);
139 } 121 }
140} 122}
141 123
142static void init_pins(void) { 124static void init_pins(void) {
143 unselect_rows(); 125 unselect_rows();
144 unselect_cols(); 126 unselect_cols();
145 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 127 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
146 gpio_set_pin_input_high(col_pins[x]); 128 gpio_set_pin_input_high(col_pins[x]);
147 } 129 }
148 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 130 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
149 gpio_set_pin_input_high(row_pins[x]); 131 gpio_set_pin_input_high(row_pins[x]);
150 } 132 }
151} 133}
152 134
153static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 135static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
154{
155 // Store last value of row prior to reading 136 // Store last value of row prior to reading
156 matrix_row_t last_row_value = current_matrix[current_row]; 137 matrix_row_t last_row_value = current_matrix[current_row];
157 138
@@ -163,13 +144,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
163 wait_us(30); 144 wait_us(30);
164 145
165 // For each col... 146 // For each col...
166 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 147 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
167
168 // Select the col pin to read (active low) 148 // Select the col pin to read (active low)
169 uint8_t pin_state = gpio_read_pin(col_pins[col_index]); 149 uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
170 150
171 // Populate the matrix row with the state of the col pin 151 // Populate the matrix row with the state of the col pin
172 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 152 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
173 } 153 }
174 154
175 // Unselect row 155 // Unselect row
@@ -178,8 +158,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
178 return (last_row_value != current_matrix[current_row]); 158 return (last_row_value != current_matrix[current_row]);
179} 159}
180 160
181static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) 161static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
182{
183 bool matrix_changed = false; 162 bool matrix_changed = false;
184 163
185 // Select col and wait for col selecton to stabilize 164 // Select col and wait for col selecton to stabilize
@@ -187,27 +166,22 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
187 wait_us(30); 166 wait_us(30);
188 167
189 // For each row... 168 // For each row...
190 for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++) 169 for (uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
191 { 170 uint8_t tmp = row_index + MATRIX_ROWS / 2;
192 uint8_t tmp = row_index + MATRIX_ROWS/2;
193 // Store last value of row prior to reading 171 // Store last value of row prior to reading
194 matrix_row_t last_row_value = current_matrix[tmp]; 172 matrix_row_t last_row_value = current_matrix[tmp];
195 173
196 // Check row pin state 174 // Check row pin state
197 if (gpio_read_pin(row_pins[row_index]) == 0) 175 if (gpio_read_pin(row_pins[row_index]) == 0) {
198 {
199 // Pin LO, set col bit 176 // Pin LO, set col bit
200 current_matrix[tmp] |= (ROW_SHIFTER << current_col); 177 current_matrix[tmp] |= (ROW_SHIFTER << current_col);
201 } 178 } else {
202 else
203 {
204 // Pin HI, clear col bit 179 // Pin HI, clear col bit
205 current_matrix[tmp] &= ~(ROW_SHIFTER << current_col); 180 current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
206 } 181 }
207 182
208 // Determine if the matrix changed state 183 // Determine if the matrix changed state
209 if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) 184 if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
210 {
211 matrix_changed = true; 185 matrix_changed = true;
212 } 186 }
213 } 187 }
@@ -219,37 +193,35 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
219} 193}
220 194
221void matrix_init(void) { 195void matrix_init(void) {
222
223 // initialize key pins 196 // initialize key pins
224 init_pins(); 197 init_pins();
225 198
226 // initialize matrix state: all keys off 199 // initialize matrix state: all keys off
227 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 200 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
228 raw_matrix[i] = 0; 201 raw_matrix[i] = 0;
229 matrix[i] = 0; 202 matrix[i] = 0;
230 } 203 }
231 204
232 debounce_init(MATRIX_ROWS); 205 debounce_init();
233 206
234 matrix_init_kb(); 207 matrix_init_kb();
235} 208}
236 209
237uint8_t matrix_scan(void) 210uint8_t matrix_scan(void) {
238{ 211 bool changed = false;
239 bool changed = false;
240 212
241 // Set row, read cols 213 // Set row, read cols
242 for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) { 214 for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
243 changed |= read_cols_on_row(raw_matrix, current_row); 215 changed |= read_cols_on_row(raw_matrix, current_row);
244 } 216 }
245 //else 217 // else
246 // Set col, read rows 218 // Set col, read rows
247 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 219 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
248 changed |= read_rows_on_col(raw_matrix, current_col); 220 changed |= read_rows_on_col(raw_matrix, current_col);
249 } 221 }
250 222
251 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 223 debounce(raw_matrix, matrix, changed);
252 224
253 matrix_scan_kb(); 225 matrix_scan_kb();
254 return (uint8_t)changed; 226 return (uint8_t)changed;
255} 227}
diff --git a/keyboards/kbdmania/kmac/matrix.c b/keyboards/kbdmania/kmac/matrix.c
index ea149c49ad..885dcd407c 100644
--- a/keyboards/kbdmania/kmac/matrix.c
+++ b/keyboards/kbdmania/kmac/matrix.c
@@ -39,24 +39,36 @@ static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
39static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 39static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
40 40
41/* matrix state(1:on, 0:off) */ 41/* matrix state(1:on, 0:off) */
42static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values 42static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
43static matrix_row_t matrix[MATRIX_ROWS]; // debounced values 43static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
44 44
45__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } 45__attribute__((weak)) void matrix_init_kb(void) {
46 matrix_init_user();
47}
46 48
47__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } 49__attribute__((weak)) void matrix_scan_kb(void) {
50 matrix_scan_user();
51}
48 52
49__attribute__((weak)) void matrix_init_user(void) {} 53__attribute__((weak)) void matrix_init_user(void) {}
50 54
51__attribute__((weak)) void matrix_scan_user(void) {} 55__attribute__((weak)) void matrix_scan_user(void) {}
52 56
53inline uint8_t matrix_rows(void) { return MATRIX_ROWS; } 57inline uint8_t matrix_rows(void) {
58 return MATRIX_ROWS;
59}
54 60
55inline uint8_t matrix_cols(void) { return MATRIX_COLS; } 61inline uint8_t matrix_cols(void) {
62 return MATRIX_COLS;
63}
56 64
57inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); } 65inline bool matrix_is_on(uint8_t row, uint8_t col) {
66 return (matrix[row] & ((matrix_row_t)1 << col));
67}
58 68
59inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } 69inline matrix_row_t matrix_get_row(uint8_t row) {
70 return matrix[row];
71}
60 72
61void matrix_print(void) { 73void matrix_print(void) {
62 print_matrix_header(); 74 print_matrix_header();
@@ -182,7 +194,7 @@ void matrix_init(void) {
182 matrix[i] = 0; 194 matrix[i] = 0;
183 } 195 }
184 196
185 debounce_init(MATRIX_ROWS); 197 debounce_init();
186 198
187 matrix_init_kb(); 199 matrix_init_kb();
188} 200}
@@ -194,7 +206,7 @@ uint8_t matrix_scan(void) {
194 changed |= read_rows_on_col(raw_matrix, current_col); 206 changed |= read_rows_on_col(raw_matrix, current_col);
195 } 207 }
196 208
197 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 209 debounce(raw_matrix, matrix, changed);
198 210
199 matrix_scan_kb(); 211 matrix_scan_kb();
200 212
diff --git a/keyboards/redscarf_iiplus/verb/matrix.c b/keyboards/redscarf_iiplus/verb/matrix.c
index 886704f9ef..4895c71737 100755..100644
--- a/keyboards/redscarf_iiplus/verb/matrix.c
+++ b/keyboards/redscarf_iiplus/verb/matrix.c
@@ -22,21 +22,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
22#include "debounce.h" 22#include "debounce.h"
23 23
24#if (MATRIX_COLS <= 8) 24#if (MATRIX_COLS <= 8)
25# define print_matrix_header() print("\nr/c 01234567\n") 25# define print_matrix_header() print("\nr/c 01234567\n")
26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
27# define ROW_SHIFTER ((uint8_t)1) 27# define ROW_SHIFTER ((uint8_t)1)
28#elif (MATRIX_COLS <= 16) 28#elif (MATRIX_COLS <= 16)
29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") 29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) 30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
31# define ROW_SHIFTER ((uint16_t)1) 31# define ROW_SHIFTER ((uint16_t)1)
32#elif (MATRIX_COLS <= 32) 32#elif (MATRIX_COLS <= 32)
33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") 33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) 34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
35# define ROW_SHIFTER ((uint32_t)1) 35# define ROW_SHIFTER ((uint32_t)1)
36#endif 36#endif
37 37
38#ifdef MATRIX_MASKED 38#ifdef MATRIX_MASKED
39 extern const matrix_row_t matrix_mask[]; 39extern const matrix_row_t matrix_mask[];
40#endif 40#endif
41 41
42#ifdef DIRECT_PINS 42#ifdef DIRECT_PINS
@@ -47,46 +47,34 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
47#endif 47#endif
48 48
49/* matrix state(1:on, 0:off) */ 49/* matrix state(1:on, 0:off) */
50static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values 50static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
51static matrix_row_t matrix[MATRIX_ROWS]; //debounced values 51static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
52 52
53__attribute__ ((weak)) 53__attribute__((weak)) void matrix_init_kb(void) {
54void matrix_init_kb(void) {
55 matrix_init_user(); 54 matrix_init_user();
56} 55}
57 56
58__attribute__ ((weak)) 57__attribute__((weak)) void matrix_scan_kb(void) {
59void matrix_scan_kb(void) {
60 matrix_scan_user(); 58 matrix_scan_user();
61} 59}
62 60
63__attribute__ ((weak)) 61__attribute__((weak)) void matrix_init_user(void) {}
64void matrix_init_user(void) {
65}
66 62
67__attribute__ ((weak)) 63__attribute__((weak)) void matrix_scan_user(void) {}
68void matrix_scan_user(void) {
69}
70 64
71inline 65inline uint8_t matrix_rows(void) {
72uint8_t matrix_rows(void) {
73 return MATRIX_ROWS; 66 return MATRIX_ROWS;
74} 67}
75 68
76inline 69inline uint8_t matrix_cols(void) {
77uint8_t matrix_cols(void) {
78 return MATRIX_COLS; 70 return MATRIX_COLS;
79} 71}
80 72
81inline 73inline bool matrix_is_on(uint8_t row, uint8_t col) {
82bool matrix_is_on(uint8_t row, uint8_t col) 74 return (matrix[row] & ((matrix_row_t)1 << col));
83{
84 return (matrix[row] & ((matrix_row_t)1<<col));
85} 75}
86 76
87inline 77inline matrix_row_t matrix_get_row(uint8_t row) {
88matrix_row_t matrix_get_row(uint8_t row)
89{
90 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a 78 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
91 // switch blocker installed and the switch is always pressed. 79 // switch blocker installed and the switch is always pressed.
92#ifdef MATRIX_MASKED 80#ifdef MATRIX_MASKED
@@ -96,12 +84,12 @@ matrix_row_t matrix_get_row(uint8_t row)
96#endif 84#endif
97} 85}
98 86
99void matrix_print(void) 87void matrix_print(void) {
100{
101 print_matrix_header(); 88 print_matrix_header();
102 89
103 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 90 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
104 print_hex8(row); print(": "); 91 print_hex8(row);
92 print(": ");
105 print_matrix_row(row); 93 print_matrix_row(row);
106 print("\n"); 94 print("\n");
107 } 95 }
@@ -110,28 +98,28 @@ void matrix_print(void)
110#ifdef DIRECT_PINS 98#ifdef DIRECT_PINS
111 99
112static void init_pins(void) { 100static void init_pins(void) {
113 for (int row = 0; row < MATRIX_ROWS; row++) { 101 for (int row = 0; row < MATRIX_ROWS; row++) {
114 for (int col = 0; col < MATRIX_COLS; col++) { 102 for (int col = 0; col < MATRIX_COLS; col++) {
115 pin_t pin = direct_pins[row][col]; 103 pin_t pin = direct_pins[row][col];
116 if (pin != NO_PIN) { 104 if (pin != NO_PIN) {
117 gpio_set_pin_input_high(pin); 105 gpio_set_pin_input_high(pin);
118 } 106 }
107 }
119 } 108 }
120 }
121} 109}
122 110
123static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 111static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
124 matrix_row_t last_row_value = current_matrix[current_row]; 112 matrix_row_t last_row_value = current_matrix[current_row];
125 current_matrix[current_row] = 0; 113 current_matrix[current_row] = 0;
126 114
127 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 115 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
128 pin_t pin = direct_pins[current_row][col_index]; 116 pin_t pin = direct_pins[current_row][col_index];
129 if (pin != NO_PIN) { 117 if (pin != NO_PIN) {
130 current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index); 118 current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index);
119 }
131 } 120 }
132 }
133 121
134 return (last_row_value != current_matrix[current_row]); 122 return (last_row_value != current_matrix[current_row]);
135} 123}
136 124
137#elif (DIODE_DIRECTION == COL2ROW) 125#elif (DIODE_DIRECTION == COL2ROW)
@@ -146,8 +134,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
146 * 4: 1 0 0 134 * 4: 1 0 0
147 * 5: 1 0 1 135 * 5: 1 0 1
148 */ 136 */
149static void select_row(uint8_t col) 137static void select_row(uint8_t col) {
150{
151 switch (col) { 138 switch (col) {
152 case 0: 139 case 0:
153 gpio_write_pin_low(B0); 140 gpio_write_pin_low(B0);
@@ -175,8 +162,7 @@ static void select_row(uint8_t col)
175 } 162 }
176} 163}
177 164
178static void unselect_row(uint8_t col) 165static void unselect_row(uint8_t col) {
179{
180 switch (col) { 166 switch (col) {
181 case 0: 167 case 0:
182 gpio_write_pin_high(B0); 168 gpio_write_pin_high(B0);
@@ -204,26 +190,24 @@ static void unselect_row(uint8_t col)
204 } 190 }
205} 191}
206 192
207static void unselect_rows(void) 193static void unselect_rows(void) {
208{
209 gpio_set_pin_output(B0); 194 gpio_set_pin_output(B0);
210 gpio_set_pin_output(B1); 195 gpio_set_pin_output(B1);
211 gpio_set_pin_output(B2); 196 gpio_set_pin_output(B2);
212 // make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird) 197 // make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
213 gpio_write_pin_high(B0); 198 gpio_write_pin_high(B0);
214 gpio_write_pin_high(B1); 199 gpio_write_pin_high(B1);
215 gpio_write_pin_high(B2); 200 gpio_write_pin_high(B2);
216} 201}
217 202
218static void init_pins(void) { 203static void init_pins(void) {
219 unselect_rows(); 204 unselect_rows();
220 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 205 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
221 gpio_set_pin_input_high(col_pins[x]); 206 gpio_set_pin_input_high(col_pins[x]);
222 } 207 }
223} 208}
224 209
225static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 210static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
226{
227 // Store last value of row prior to reading 211 // Store last value of row prior to reading
228 matrix_row_t last_row_value = current_matrix[current_row]; 212 matrix_row_t last_row_value = current_matrix[current_row];
229 213
@@ -235,13 +219,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
235 wait_us(30); 219 wait_us(30);
236 220
237 // For each col... 221 // For each col...
238 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 222 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
239
240 // Select the col pin to read (active low) 223 // Select the col pin to read (active low)
241 uint8_t pin_state = gpio_read_pin(col_pins[col_index]); 224 uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
242 225
243 // Populate the matrix row with the state of the col pin 226 // Populate the matrix row with the state of the col pin
244 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 227 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
245 } 228 }
246 229
247 // Unselect row 230 // Unselect row
@@ -252,33 +235,29 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
252 235
253#elif (DIODE_DIRECTION == ROW2COL) 236#elif (DIODE_DIRECTION == ROW2COL)
254 237
255static void select_col(uint8_t col) 238static void select_col(uint8_t col) {
256{
257 gpio_set_pin_output(col_pins[col]); 239 gpio_set_pin_output(col_pins[col]);
258 gpio_write_pin_low(col_pins[col]); 240 gpio_write_pin_low(col_pins[col]);
259} 241}
260 242
261static void unselect_col(uint8_t col) 243static void unselect_col(uint8_t col) {
262{
263 gpio_set_pin_input_high(col_pins[col]); 244 gpio_set_pin_input_high(col_pins[col]);
264} 245}
265 246
266static void unselect_cols(void) 247static void unselect_cols(void) {
267{ 248 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
268 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
269 gpio_set_pin_input_high(col_pins[x]); 249 gpio_set_pin_input_high(col_pins[x]);
270 } 250 }
271} 251}
272 252
273static void init_pins(void) { 253static void init_pins(void) {
274 unselect_cols(); 254 unselect_cols();
275 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 255 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
276 gpio_set_pin_input_high(row_pins[x]); 256 gpio_set_pin_input_high(row_pins[x]);
277 } 257 }
278} 258}
279 259
280static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) 260static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
281{
282 bool matrix_changed = false; 261 bool matrix_changed = false;
283 262
284 // Select col and wait for col selecton to stabilize 263 // Select col and wait for col selecton to stabilize
@@ -286,27 +265,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
286 wait_us(30); 265 wait_us(30);
287 266
288 // For each row... 267 // For each row...
289 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) 268 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
290 {
291
292 // Store last value of row prior to reading 269 // Store last value of row prior to reading
293 matrix_row_t last_row_value = current_matrix[row_index]; 270 matrix_row_t last_row_value = current_matrix[row_index];
294 271
295 // Check row pin state 272 // Check row pin state
296 if (gpio_read_pin(row_pins[row_index]) == 0) 273 if (gpio_read_pin(row_pins[row_index]) == 0) {
297 {
298 // Pin LO, set col bit 274 // Pin LO, set col bit
299 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 275 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
300 } 276 } else {
301 else
302 {
303 // Pin HI, clear col bit 277 // Pin HI, clear col bit
304 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 278 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
305 } 279 }
306 280
307 // Determine if the matrix changed state 281 // Determine if the matrix changed state
308 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) 282 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
309 {
310 matrix_changed = true; 283 matrix_changed = true;
311 } 284 }
312 } 285 }
@@ -320,39 +293,37 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
320#endif 293#endif
321 294
322void matrix_init(void) { 295void matrix_init(void) {
323
324 // initialize key pins 296 // initialize key pins
325 init_pins(); 297 init_pins();
326 298
327 // initialize matrix state: all keys off 299 // initialize matrix state: all keys off
328 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 300 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
329 raw_matrix[i] = 0; 301 raw_matrix[i] = 0;
330 matrix[i] = 0; 302 matrix[i] = 0;
331 } 303 }
332 304
333 debounce_init(MATRIX_ROWS); 305 debounce_init();
334 306
335 matrix_init_kb(); 307 matrix_init_kb();
336} 308}
337 309
338uint8_t matrix_scan(void) 310uint8_t matrix_scan(void) {
339{ 311 bool changed = false;
340 bool changed = false;
341 312
342#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) 313#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
343 // Set row, read cols 314 // Set row, read cols
344 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 315 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
345 changed |= read_cols_on_row(raw_matrix, current_row); 316 changed |= read_cols_on_row(raw_matrix, current_row);
346 } 317 }
347#elif (DIODE_DIRECTION == ROW2COL) 318#elif (DIODE_DIRECTION == ROW2COL)
348 // Set col, read rows 319 // Set col, read rows
349 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 320 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
350 changed |= read_rows_on_col(raw_matrix, current_col); 321 changed |= read_rows_on_col(raw_matrix, current_col);
351 } 322 }
352#endif 323#endif
353 324
354 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 325 debounce(raw_matrix, matrix, changed);
355 326
356 matrix_scan_kb(); 327 matrix_scan_kb();
357 return 1; 328 return 1;
358} 329}
diff --git a/keyboards/redscarf_iiplus/verc/matrix.c b/keyboards/redscarf_iiplus/verc/matrix.c
index 886704f9ef..4895c71737 100755..100644
--- a/keyboards/redscarf_iiplus/verc/matrix.c
+++ b/keyboards/redscarf_iiplus/verc/matrix.c
@@ -22,21 +22,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
22#include "debounce.h" 22#include "debounce.h"
23 23
24#if (MATRIX_COLS <= 8) 24#if (MATRIX_COLS <= 8)
25# define print_matrix_header() print("\nr/c 01234567\n") 25# define print_matrix_header() print("\nr/c 01234567\n")
26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
27# define ROW_SHIFTER ((uint8_t)1) 27# define ROW_SHIFTER ((uint8_t)1)
28#elif (MATRIX_COLS <= 16) 28#elif (MATRIX_COLS <= 16)
29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") 29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) 30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
31# define ROW_SHIFTER ((uint16_t)1) 31# define ROW_SHIFTER ((uint16_t)1)
32#elif (MATRIX_COLS <= 32) 32#elif (MATRIX_COLS <= 32)
33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") 33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) 34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
35# define ROW_SHIFTER ((uint32_t)1) 35# define ROW_SHIFTER ((uint32_t)1)
36#endif 36#endif
37 37
38#ifdef MATRIX_MASKED 38#ifdef MATRIX_MASKED
39 extern const matrix_row_t matrix_mask[]; 39extern const matrix_row_t matrix_mask[];
40#endif 40#endif
41 41
42#ifdef DIRECT_PINS 42#ifdef DIRECT_PINS
@@ -47,46 +47,34 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
47#endif 47#endif
48 48
49/* matrix state(1:on, 0:off) */ 49/* matrix state(1:on, 0:off) */
50static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values 50static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
51static matrix_row_t matrix[MATRIX_ROWS]; //debounced values 51static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
52 52
53__attribute__ ((weak)) 53__attribute__((weak)) void matrix_init_kb(void) {
54void matrix_init_kb(void) {
55 matrix_init_user(); 54 matrix_init_user();
56} 55}
57 56
58__attribute__ ((weak)) 57__attribute__((weak)) void matrix_scan_kb(void) {
59void matrix_scan_kb(void) {
60 matrix_scan_user(); 58 matrix_scan_user();
61} 59}
62 60
63__attribute__ ((weak)) 61__attribute__((weak)) void matrix_init_user(void) {}
64void matrix_init_user(void) {
65}
66 62
67__attribute__ ((weak)) 63__attribute__((weak)) void matrix_scan_user(void) {}
68void matrix_scan_user(void) {
69}
70 64
71inline 65inline uint8_t matrix_rows(void) {
72uint8_t matrix_rows(void) {
73 return MATRIX_ROWS; 66 return MATRIX_ROWS;
74} 67}
75 68
76inline 69inline uint8_t matrix_cols(void) {
77uint8_t matrix_cols(void) {
78 return MATRIX_COLS; 70 return MATRIX_COLS;
79} 71}
80 72
81inline 73inline bool matrix_is_on(uint8_t row, uint8_t col) {
82bool matrix_is_on(uint8_t row, uint8_t col) 74 return (matrix[row] & ((matrix_row_t)1 << col));
83{
84 return (matrix[row] & ((matrix_row_t)1<<col));
85} 75}
86 76
87inline 77inline matrix_row_t matrix_get_row(uint8_t row) {
88matrix_row_t matrix_get_row(uint8_t row)
89{
90 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a 78 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
91 // switch blocker installed and the switch is always pressed. 79 // switch blocker installed and the switch is always pressed.
92#ifdef MATRIX_MASKED 80#ifdef MATRIX_MASKED
@@ -96,12 +84,12 @@ matrix_row_t matrix_get_row(uint8_t row)
96#endif 84#endif
97} 85}
98 86
99void matrix_print(void) 87void matrix_print(void) {
100{
101 print_matrix_header(); 88 print_matrix_header();
102 89
103 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 90 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
104 print_hex8(row); print(": "); 91 print_hex8(row);
92 print(": ");
105 print_matrix_row(row); 93 print_matrix_row(row);
106 print("\n"); 94 print("\n");
107 } 95 }
@@ -110,28 +98,28 @@ void matrix_print(void)
110#ifdef DIRECT_PINS 98#ifdef DIRECT_PINS
111 99
112static void init_pins(void) { 100static void init_pins(void) {
113 for (int row = 0; row < MATRIX_ROWS; row++) { 101 for (int row = 0; row < MATRIX_ROWS; row++) {
114 for (int col = 0; col < MATRIX_COLS; col++) { 102 for (int col = 0; col < MATRIX_COLS; col++) {
115 pin_t pin = direct_pins[row][col]; 103 pin_t pin = direct_pins[row][col];
116 if (pin != NO_PIN) { 104 if (pin != NO_PIN) {
117 gpio_set_pin_input_high(pin); 105 gpio_set_pin_input_high(pin);
118 } 106 }
107 }
119 } 108 }
120 }
121} 109}
122 110
123static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 111static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
124 matrix_row_t last_row_value = current_matrix[current_row]; 112 matrix_row_t last_row_value = current_matrix[current_row];
125 current_matrix[current_row] = 0; 113 current_matrix[current_row] = 0;
126 114
127 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 115 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
128 pin_t pin = direct_pins[current_row][col_index]; 116 pin_t pin = direct_pins[current_row][col_index];
129 if (pin != NO_PIN) { 117 if (pin != NO_PIN) {
130 current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index); 118 current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index);
119 }
131 } 120 }
132 }
133 121
134 return (last_row_value != current_matrix[current_row]); 122 return (last_row_value != current_matrix[current_row]);
135} 123}
136 124
137#elif (DIODE_DIRECTION == COL2ROW) 125#elif (DIODE_DIRECTION == COL2ROW)
@@ -146,8 +134,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
146 * 4: 1 0 0 134 * 4: 1 0 0
147 * 5: 1 0 1 135 * 5: 1 0 1
148 */ 136 */
149static void select_row(uint8_t col) 137static void select_row(uint8_t col) {
150{
151 switch (col) { 138 switch (col) {
152 case 0: 139 case 0:
153 gpio_write_pin_low(B0); 140 gpio_write_pin_low(B0);
@@ -175,8 +162,7 @@ static void select_row(uint8_t col)
175 } 162 }
176} 163}
177 164
178static void unselect_row(uint8_t col) 165static void unselect_row(uint8_t col) {
179{
180 switch (col) { 166 switch (col) {
181 case 0: 167 case 0:
182 gpio_write_pin_high(B0); 168 gpio_write_pin_high(B0);
@@ -204,26 +190,24 @@ static void unselect_row(uint8_t col)
204 } 190 }
205} 191}
206 192
207static void unselect_rows(void) 193static void unselect_rows(void) {
208{
209 gpio_set_pin_output(B0); 194 gpio_set_pin_output(B0);
210 gpio_set_pin_output(B1); 195 gpio_set_pin_output(B1);
211 gpio_set_pin_output(B2); 196 gpio_set_pin_output(B2);
212 // make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird) 197 // make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
213 gpio_write_pin_high(B0); 198 gpio_write_pin_high(B0);
214 gpio_write_pin_high(B1); 199 gpio_write_pin_high(B1);
215 gpio_write_pin_high(B2); 200 gpio_write_pin_high(B2);
216} 201}
217 202
218static void init_pins(void) { 203static void init_pins(void) {
219 unselect_rows(); 204 unselect_rows();
220 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 205 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
221 gpio_set_pin_input_high(col_pins[x]); 206 gpio_set_pin_input_high(col_pins[x]);
222 } 207 }
223} 208}
224 209
225static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 210static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
226{
227 // Store last value of row prior to reading 211 // Store last value of row prior to reading
228 matrix_row_t last_row_value = current_matrix[current_row]; 212 matrix_row_t last_row_value = current_matrix[current_row];
229 213
@@ -235,13 +219,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
235 wait_us(30); 219 wait_us(30);
236 220
237 // For each col... 221 // For each col...
238 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 222 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
239
240 // Select the col pin to read (active low) 223 // Select the col pin to read (active low)
241 uint8_t pin_state = gpio_read_pin(col_pins[col_index]); 224 uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
242 225
243 // Populate the matrix row with the state of the col pin 226 // Populate the matrix row with the state of the col pin
244 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 227 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
245 } 228 }
246 229
247 // Unselect row 230 // Unselect row
@@ -252,33 +235,29 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
252 235
253#elif (DIODE_DIRECTION == ROW2COL) 236#elif (DIODE_DIRECTION == ROW2COL)
254 237
255static void select_col(uint8_t col) 238static void select_col(uint8_t col) {
256{
257 gpio_set_pin_output(col_pins[col]); 239 gpio_set_pin_output(col_pins[col]);
258 gpio_write_pin_low(col_pins[col]); 240 gpio_write_pin_low(col_pins[col]);
259} 241}
260 242
261static void unselect_col(uint8_t col) 243static void unselect_col(uint8_t col) {
262{
263 gpio_set_pin_input_high(col_pins[col]); 244 gpio_set_pin_input_high(col_pins[col]);
264} 245}
265 246
266static void unselect_cols(void) 247static void unselect_cols(void) {
267{ 248 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
268 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
269 gpio_set_pin_input_high(col_pins[x]); 249 gpio_set_pin_input_high(col_pins[x]);
270 } 250 }
271} 251}
272 252
273static void init_pins(void) { 253static void init_pins(void) {
274 unselect_cols(); 254 unselect_cols();
275 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 255 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
276 gpio_set_pin_input_high(row_pins[x]); 256 gpio_set_pin_input_high(row_pins[x]);
277 } 257 }
278} 258}
279 259
280static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) 260static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
281{
282 bool matrix_changed = false; 261 bool matrix_changed = false;
283 262
284 // Select col and wait for col selecton to stabilize 263 // Select col and wait for col selecton to stabilize
@@ -286,27 +265,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
286 wait_us(30); 265 wait_us(30);
287 266
288 // For each row... 267 // For each row...
289 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) 268 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
290 {
291
292 // Store last value of row prior to reading 269 // Store last value of row prior to reading
293 matrix_row_t last_row_value = current_matrix[row_index]; 270 matrix_row_t last_row_value = current_matrix[row_index];
294 271
295 // Check row pin state 272 // Check row pin state
296 if (gpio_read_pin(row_pins[row_index]) == 0) 273 if (gpio_read_pin(row_pins[row_index]) == 0) {
297 {
298 // Pin LO, set col bit 274 // Pin LO, set col bit
299 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 275 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
300 } 276 } else {
301 else
302 {
303 // Pin HI, clear col bit 277 // Pin HI, clear col bit
304 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 278 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
305 } 279 }
306 280
307 // Determine if the matrix changed state 281 // Determine if the matrix changed state
308 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) 282 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
309 {
310 matrix_changed = true; 283 matrix_changed = true;
311 } 284 }
312 } 285 }
@@ -320,39 +293,37 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
320#endif 293#endif
321 294
322void matrix_init(void) { 295void matrix_init(void) {
323
324 // initialize key pins 296 // initialize key pins
325 init_pins(); 297 init_pins();
326 298
327 // initialize matrix state: all keys off 299 // initialize matrix state: all keys off
328 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 300 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
329 raw_matrix[i] = 0; 301 raw_matrix[i] = 0;
330 matrix[i] = 0; 302 matrix[i] = 0;
331 } 303 }
332 304
333 debounce_init(MATRIX_ROWS); 305 debounce_init();
334 306
335 matrix_init_kb(); 307 matrix_init_kb();
336} 308}
337 309
338uint8_t matrix_scan(void) 310uint8_t matrix_scan(void) {
339{ 311 bool changed = false;
340 bool changed = false;
341 312
342#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) 313#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
343 // Set row, read cols 314 // Set row, read cols
344 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 315 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
345 changed |= read_cols_on_row(raw_matrix, current_row); 316 changed |= read_cols_on_row(raw_matrix, current_row);
346 } 317 }
347#elif (DIODE_DIRECTION == ROW2COL) 318#elif (DIODE_DIRECTION == ROW2COL)
348 // Set col, read rows 319 // Set col, read rows
349 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 320 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
350 changed |= read_rows_on_col(raw_matrix, current_col); 321 changed |= read_rows_on_col(raw_matrix, current_col);
351 } 322 }
352#endif 323#endif
353 324
354 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 325 debounce(raw_matrix, matrix, changed);
355 326
356 matrix_scan_kb(); 327 matrix_scan_kb();
357 return 1; 328 return 1;
358} 329}
diff --git a/keyboards/redscarf_iiplus/verd/matrix.c b/keyboards/redscarf_iiplus/verd/matrix.c
index 133898b652..942333b06f 100644
--- a/keyboards/redscarf_iiplus/verd/matrix.c
+++ b/keyboards/redscarf_iiplus/verd/matrix.c
@@ -22,21 +22,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
22#include "debounce.h" 22#include "debounce.h"
23 23
24#if (MATRIX_COLS <= 8) 24#if (MATRIX_COLS <= 8)
25# define print_matrix_header() print("\nr/c 01234567\n") 25# define print_matrix_header() print("\nr/c 01234567\n")
26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 26# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
27# define ROW_SHIFTER ((uint8_t)1) 27# define ROW_SHIFTER ((uint8_t)1)
28#elif (MATRIX_COLS <= 16) 28#elif (MATRIX_COLS <= 16)
29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") 29# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) 30# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
31# define ROW_SHIFTER ((uint16_t)1) 31# define ROW_SHIFTER ((uint16_t)1)
32#elif (MATRIX_COLS <= 32) 32#elif (MATRIX_COLS <= 32)
33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") 33# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) 34# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
35# define ROW_SHIFTER ((uint32_t)1) 35# define ROW_SHIFTER ((uint32_t)1)
36#endif 36#endif
37 37
38#ifdef MATRIX_MASKED 38#ifdef MATRIX_MASKED
39 extern const matrix_row_t matrix_mask[]; 39extern const matrix_row_t matrix_mask[];
40#endif 40#endif
41 41
42#ifdef DIRECT_PINS 42#ifdef DIRECT_PINS
@@ -47,46 +47,34 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
47#endif 47#endif
48 48
49/* matrix state(1:on, 0:off) */ 49/* matrix state(1:on, 0:off) */
50static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values 50static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
51static matrix_row_t matrix[MATRIX_ROWS]; //debounced values 51static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
52 52
53__attribute__ ((weak)) 53__attribute__((weak)) void matrix_init_kb(void) {
54void matrix_init_kb(void) {
55 matrix_init_user(); 54 matrix_init_user();
56} 55}
57 56
58__attribute__ ((weak)) 57__attribute__((weak)) void matrix_scan_kb(void) {
59void matrix_scan_kb(void) {
60 matrix_scan_user(); 58 matrix_scan_user();
61} 59}
62 60
63__attribute__ ((weak)) 61__attribute__((weak)) void matrix_init_user(void) {}
64void matrix_init_user(void) {
65}
66 62
67__attribute__ ((weak)) 63__attribute__((weak)) void matrix_scan_user(void) {}
68void matrix_scan_user(void) {
69}
70 64
71inline 65inline uint8_t matrix_rows(void) {
72uint8_t matrix_rows(void) {
73 return MATRIX_ROWS; 66 return MATRIX_ROWS;
74} 67}
75 68
76inline 69inline uint8_t matrix_cols(void) {
77uint8_t matrix_cols(void) {
78 return MATRIX_COLS; 70 return MATRIX_COLS;
79} 71}
80 72
81inline 73inline bool matrix_is_on(uint8_t row, uint8_t col) {
82bool matrix_is_on(uint8_t row, uint8_t col) 74 return (matrix[row] & ((matrix_row_t)1 << col));
83{
84 return (matrix[row] & ((matrix_row_t)1<<col));
85} 75}
86 76
87inline 77inline matrix_row_t matrix_get_row(uint8_t row) {
88matrix_row_t matrix_get_row(uint8_t row)
89{
90 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a 78 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
91 // switch blocker installed and the switch is always pressed. 79 // switch blocker installed and the switch is always pressed.
92#ifdef MATRIX_MASKED 80#ifdef MATRIX_MASKED
@@ -96,12 +84,12 @@ matrix_row_t matrix_get_row(uint8_t row)
96#endif 84#endif
97} 85}
98 86
99void matrix_print(void) 87void matrix_print(void) {
100{
101 print_matrix_header(); 88 print_matrix_header();
102 89
103 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 90 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
104 print_hex8(row); print(": "); 91 print_hex8(row);
92 print(": ");
105 print_matrix_row(row); 93 print_matrix_row(row);
106 print("\n"); 94 print("\n");
107 } 95 }
@@ -110,28 +98,28 @@ void matrix_print(void)
110#ifdef DIRECT_PINS 98#ifdef DIRECT_PINS
111 99
112static void init_pins(void) { 100static void init_pins(void) {
113 for (int row = 0; row < MATRIX_ROWS; row++) { 101 for (int row = 0; row < MATRIX_ROWS; row++) {
114 for (int col = 0; col < MATRIX_COLS; col++) { 102 for (int col = 0; col < MATRIX_COLS; col++) {
115 pin_t pin = direct_pins[row][col]; 103 pin_t pin = direct_pins[row][col];
116 if (pin != NO_PIN) { 104 if (pin != NO_PIN) {
117 gpio_set_pin_input_high(pin); 105 gpio_set_pin_input_high(pin);
118 } 106 }
107 }
119 } 108 }
120 }
121} 109}
122 110
123static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 111static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
124 matrix_row_t last_row_value = current_matrix[current_row]; 112 matrix_row_t last_row_value = current_matrix[current_row];
125 current_matrix[current_row] = 0; 113 current_matrix[current_row] = 0;
126 114
127 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 115 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
128 pin_t pin = direct_pins[current_row][col_index]; 116 pin_t pin = direct_pins[current_row][col_index];
129 if (pin != NO_PIN) { 117 if (pin != NO_PIN) {
130 current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index); 118 current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index);
119 }
131 } 120 }
132 }
133 121
134 return (last_row_value != current_matrix[current_row]); 122 return (last_row_value != current_matrix[current_row]);
135} 123}
136 124
137#elif (DIODE_DIRECTION == COL2ROW) 125#elif (DIODE_DIRECTION == COL2ROW)
@@ -146,8 +134,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
146 * 4: 1 0 0 134 * 4: 1 0 0
147 * 5: 1 0 1 135 * 5: 1 0 1
148 */ 136 */
149static void select_row(uint8_t col) 137static void select_row(uint8_t col) {
150{
151 switch (col) { 138 switch (col) {
152 case 0: 139 case 0:
153 gpio_write_pin_low(B0); 140 gpio_write_pin_low(B0);
@@ -175,8 +162,7 @@ static void select_row(uint8_t col)
175 } 162 }
176} 163}
177 164
178static void unselect_row(uint8_t col) 165static void unselect_row(uint8_t col) {
179{
180 switch (col) { 166 switch (col) {
181 case 0: 167 case 0:
182 gpio_write_pin_high(B0); 168 gpio_write_pin_high(B0);
@@ -204,26 +190,24 @@ static void unselect_row(uint8_t col)
204 } 190 }
205} 191}
206 192
207static void unselect_rows(void) 193static void unselect_rows(void) {
208{
209 gpio_set_pin_output(B0); 194 gpio_set_pin_output(B0);
210 gpio_set_pin_output(B1); 195 gpio_set_pin_output(B1);
211 gpio_set_pin_output(B2); 196 gpio_set_pin_output(B2);
212 // make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird) 197 // make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
213 gpio_write_pin_high(B0); 198 gpio_write_pin_high(B0);
214 gpio_write_pin_high(B1); 199 gpio_write_pin_high(B1);
215 gpio_write_pin_high(B2); 200 gpio_write_pin_high(B2);
216} 201}
217 202
218static void init_pins(void) { 203static void init_pins(void) {
219 unselect_rows(); 204 unselect_rows();
220 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 205 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
221 gpio_set_pin_input_high(col_pins[x]); 206 gpio_set_pin_input_high(col_pins[x]);
222 } 207 }
223} 208}
224 209
225static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 210static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
226{
227 // Store last value of row prior to reading 211 // Store last value of row prior to reading
228 matrix_row_t last_row_value = current_matrix[current_row]; 212 matrix_row_t last_row_value = current_matrix[current_row];
229 213
@@ -235,13 +219,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
235 wait_us(30); 219 wait_us(30);
236 220
237 // For each col... 221 // For each col...
238 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 222 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
239
240 // Select the col pin to read (active low) 223 // Select the col pin to read (active low)
241 uint8_t pin_state = gpio_read_pin(col_pins[col_index]); 224 uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
242 225
243 // Populate the matrix row with the state of the col pin 226 // Populate the matrix row with the state of the col pin
244 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 227 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
245 } 228 }
246 229
247 // Unselect row 230 // Unselect row
@@ -252,33 +235,29 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
252 235
253#elif (DIODE_DIRECTION == ROW2COL) 236#elif (DIODE_DIRECTION == ROW2COL)
254 237
255static void select_col(uint8_t col) 238static void select_col(uint8_t col) {
256{
257 gpio_set_pin_output(col_pins[col]); 239 gpio_set_pin_output(col_pins[col]);
258 gpio_write_pin_low(col_pins[col]); 240 gpio_write_pin_low(col_pins[col]);
259} 241}
260 242
261static void unselect_col(uint8_t col) 243static void unselect_col(uint8_t col) {
262{
263 gpio_set_pin_input_high(col_pins[col]); 244 gpio_set_pin_input_high(col_pins[col]);
264} 245}
265 246
266static void unselect_cols(void) 247static void unselect_cols(void) {
267{ 248 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
268 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
269 gpio_set_pin_input_high(col_pins[x]); 249 gpio_set_pin_input_high(col_pins[x]);
270 } 250 }
271} 251}
272 252
273static void init_pins(void) { 253static void init_pins(void) {
274 unselect_cols(); 254 unselect_cols();
275 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 255 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
276 gpio_set_pin_input_high(row_pins[x]); 256 gpio_set_pin_input_high(row_pins[x]);
277 } 257 }
278} 258}
279 259
280static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) 260static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
281{
282 bool matrix_changed = false; 261 bool matrix_changed = false;
283 262
284 // Select col and wait for col selecton to stabilize 263 // Select col and wait for col selecton to stabilize
@@ -286,27 +265,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
286 wait_us(30); 265 wait_us(30);
287 266
288 // For each row... 267 // For each row...
289 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) 268 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
290 {
291
292 // Store last value of row prior to reading 269 // Store last value of row prior to reading
293 matrix_row_t last_row_value = current_matrix[row_index]; 270 matrix_row_t last_row_value = current_matrix[row_index];
294 271
295 // Check row pin state 272 // Check row pin state
296 if (gpio_read_pin(row_pins[row_index]) == 0) 273 if (gpio_read_pin(row_pins[row_index]) == 0) {
297 {
298 // Pin LO, set col bit 274 // Pin LO, set col bit
299 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 275 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
300 } 276 } else {
301 else
302 {
303 // Pin HI, clear col bit 277 // Pin HI, clear col bit
304 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 278 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
305 } 279 }
306 280
307 // Determine if the matrix changed state 281 // Determine if the matrix changed state
308 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) 282 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
309 {
310 matrix_changed = true; 283 matrix_changed = true;
311 } 284 }
312 } 285 }
@@ -320,39 +293,37 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
320#endif 293#endif
321 294
322void matrix_init(void) { 295void matrix_init(void) {
323
324 // initialize key pins 296 // initialize key pins
325 init_pins(); 297 init_pins();
326 298
327 // initialize matrix state: all keys off 299 // initialize matrix state: all keys off
328 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 300 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
329 raw_matrix[i] = 0; 301 raw_matrix[i] = 0;
330 matrix[i] = 0; 302 matrix[i] = 0;
331 } 303 }
332 304
333 debounce_init(MATRIX_ROWS); 305 debounce_init();
334 306
335 matrix_init_kb(); 307 matrix_init_kb();
336} 308}
337 309
338uint8_t matrix_scan(void) 310uint8_t matrix_scan(void) {
339{ 311 bool changed = false;
340 bool changed = false;
341 312
342#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) 313#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
343 // Set row, read cols 314 // Set row, read cols
344 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 315 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
345 changed |= read_cols_on_row(raw_matrix, current_row); 316 changed |= read_cols_on_row(raw_matrix, current_row);
346 } 317 }
347#elif (DIODE_DIRECTION == ROW2COL) 318#elif (DIODE_DIRECTION == ROW2COL)
348 // Set col, read rows 319 // Set col, read rows
349 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 320 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
350 changed |= read_rows_on_col(raw_matrix, current_col); 321 changed |= read_rows_on_col(raw_matrix, current_col);
351 } 322 }
352#endif 323#endif
353 324
354 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 325 debounce(raw_matrix, matrix, changed);
355 326
356 matrix_scan_kb(); 327 matrix_scan_kb();
357 return 1; 328 return 1;
358} 329}
diff --git a/keyboards/ymdk/sp64/keymaps/default/keymap.c b/keyboards/ymdk/sp64/keymaps/default/keymap.c
index dbdd6f0cb2..fac693b1ad 100644
--- a/keyboards/ymdk/sp64/keymaps/default/keymap.c
+++ b/keyboards/ymdk/sp64/keymaps/default/keymap.c
@@ -1,3 +1,6 @@
1// Copyright 2019 Neil Kettle
2// SPDX-License-Identifier: GPL-2.0+
3
1#include QMK_KEYBOARD_H 4#include QMK_KEYBOARD_H
2 5
3enum layer_names { 6enum layer_names {
diff --git a/keyboards/ymdk/sp64/matrix.c b/keyboards/ymdk/sp64/matrix.c
index 6f8c7962eb..97fdb87ecb 100644
--- a/keyboards/ymdk/sp64/matrix.c
+++ b/keyboards/ymdk/sp64/matrix.c
@@ -40,132 +40,129 @@ static uint8_t mcp23018_reset_loop = 0;
40 40
41// user-defined overridable functions 41// user-defined overridable functions
42 42
43__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } 43__attribute__((weak)) void matrix_init_kb(void) {
44 matrix_init_user();
45}
44 46
45__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } 47__attribute__((weak)) void matrix_scan_kb(void) {
48 matrix_scan_user();
49}
46 50
47__attribute__((weak)) void matrix_init_user(void) {} 51__attribute__((weak)) void matrix_init_user(void) {}
48 52
49__attribute__((weak)) void matrix_scan_user(void) {} 53__attribute__((weak)) void matrix_scan_user(void) {}
50 54
51// helper functions 55// helper functions
52void matrix_init(void) 56void matrix_init(void) {
53{ 57 // all outputs for rows high
54 // all outputs for rows high 58 DDRB = 0xFF;
55 DDRB = 0xFF; 59 PORTB = 0xFF;
56 PORTB = 0xFF; 60 // all inputs for columns
57 // all inputs for columns 61 DDRA = 0x00;
58 DDRA = 0x00; 62 DDRC &= ~(0x111111 << 2);
59 DDRC &= ~(0x111111<<2); 63 DDRD &= ~(1 << PIND7);
60 DDRD &= ~(1<<PIND7); 64 // all columns are pulled-up
61 // all columns are pulled-up 65 PORTA = 0xFF;
62 PORTA = 0xFF; 66 PORTC |= (0b111111 << 2);
63 PORTC |= (0b111111<<2); 67 PORTD |= (1 << PIND7);
64 PORTD |= (1<<PIND7);
65 68
66#ifdef RIGHT_HALF 69#ifdef RIGHT_HALF
67 // initialize row and col 70 // initialize row and col
68 mcp23018_status = init_mcp23018(); 71 mcp23018_status = init_mcp23018();
69#endif 72#endif
70 73
71 // initialize matrix state: all keys off 74 // initialize matrix state: all keys off
72 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 75 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
73 matrix[row] = 0; 76 matrix[row] = 0;
74 matrix_debouncing[row] = 0; 77 matrix_debouncing[row] = 0;
75 } 78 }
76 debounce_init(MATRIX_ROWS); 79 debounce_init();
77 matrix_init_kb(); 80 matrix_init_kb();
78} 81}
79 82
80uint8_t matrix_scan(void) 83uint8_t matrix_scan(void) {
81{
82#ifdef RIGHT_HALF 84#ifdef RIGHT_HALF
83 // Then the keyboard 85 // Then the keyboard
84 if (mcp23018_status != I2C_STATUS_SUCCESS) { 86 if (mcp23018_status != I2C_STATUS_SUCCESS) {
85 if (++mcp23018_reset_loop == 0) { 87 if (++mcp23018_reset_loop == 0) {
86 // if (++mcp23018_reset_loop >= 1300) { 88 // if (++mcp23018_reset_loop >= 1300) {
87 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans 89 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
88 // this will be approx bit more frequent than once per second 90 // this will be approx bit more frequent than once per second
89 print("trying to reset mcp23018\n"); 91 print("trying to reset mcp23018\n");
90 mcp23018_status = init_mcp23018(); 92 mcp23018_status = init_mcp23018();
91 if (mcp23018_status) { 93 if (mcp23018_status) {
92 print("left side not responding\n"); 94 print("left side not responding\n");
93 } else { 95 } else {
94 print("left side attached\n"); 96 print("left side attached\n");
95 } 97 }
98 }
96 } 99 }
97 }
98#endif 100#endif
99 bool changed = false; 101 bool changed = false;
100 for (uint8_t row = 0; row < MATRIX_ROWS; row++) 102 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
101 { 103 matrix_row_t cols;
102 matrix_row_t cols;
103 104
104 matrix_select_row(row); 105 matrix_select_row(row);
105#ifndef RIGHT_HALF 106#ifndef RIGHT_HALF
106 _delay_us(5); 107 _delay_us(5);
107#endif 108#endif
108 109
109 cols = ( 110 cols = (
110 // cols 0..7, PORTA 0 -> 7 111 // cols 0..7, PORTA 0 -> 7
111 (~PINA) & 0xFF 112 (~PINA) & 0xFF);
112 );
113 113
114#ifdef RIGHT_HALF 114#ifdef RIGHT_HALF
115 uint8_t data = 0x7F; 115 uint8_t data = 0x7F;
116 // Receive the columns from right half 116 // Receive the columns from right half
117 i2c_receive(I2C_ADDR, &data, 1, MCP23018_I2C_TIMEOUT); 117 i2c_receive(I2C_ADDR, &data, 1, MCP23018_I2C_TIMEOUT);
118 cols |= ((~(data) & 0x7F) << 7); 118 cols |= ((~(data) & 0x7F) << 7);
119#endif 119#endif
120 120
121 if (matrix_debouncing[row] != cols) { 121 if (matrix_debouncing[row] != cols) {
122 matrix_debouncing[row] = cols; 122 matrix_debouncing[row] = cols;
123 //debouncing = DEBOUNCE; 123 // debouncing = DEBOUNCE;
124 changed = true; 124 changed = true;
125 }
125 } 126 }
126 }
127 127
128 debounce(matrix_debouncing, matrix, MATRIX_ROWS, changed); 128 debounce(matrix_debouncing, matrix, changed);
129 129
130 matrix_scan_kb(); 130 matrix_scan_kb();
131 131
132#ifdef DEBUG_MATRIX 132#ifdef DEBUG_MATRIX
133 for (uint8_t c = 0; c < MATRIX_COLS; c++) 133 for (uint8_t c = 0; c < MATRIX_COLS; c++)
134 for (uint8_t r = 0; r < MATRIX_ROWS; r++) 134 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
135 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c); 135 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
136#endif 136#endif
137 137
138 return (uint8_t)changed; 138 return (uint8_t)changed;
139} 139}
140 140
141inline 141inline matrix_row_t matrix_get_row(uint8_t row) {
142matrix_row_t matrix_get_row(uint8_t row)
143{
144 return matrix[row]; 142 return matrix[row];
145} 143}
146 144
147void matrix_print(void) 145void matrix_print(void) {
148{ 146 print("\nr/c 0123456789ABCDEF\n");
149 print("\nr/c 0123456789ABCDEF\n"); 147 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
150 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 148 print_hex8(row);
151 print_hex8(row); print(": "); 149 print(": ");
152 print_bin_reverse16(matrix_get_row(row)); 150 print_bin_reverse16(matrix_get_row(row));
153 print("\n"); 151 print("\n");
154 } 152 }
155} 153}
156 154
157static void matrix_select_row(uint8_t row) 155static void matrix_select_row(uint8_t row) {
158{
159#ifdef RIGHT_HALF 156#ifdef RIGHT_HALF
160 uint8_t txdata[3]; 157 uint8_t txdata[3];
161 158
162 //Set the remote row on port A 159 // Set the remote row on port A
163 txdata[0] = GPIOA; 160 txdata[0] = GPIOA;
164 txdata[1] = 0xFF & ~(1<<row); 161 txdata[1] = 0xFF & ~(1 << row);
165 mcp23018_status = i2c_transmit(I2C_ADDR, (uint8_t *)txdata, 2, MCP23018_I2C_TIMEOUT); 162 mcp23018_status = i2c_transmit(I2C_ADDR, (uint8_t *)txdata, 2, MCP23018_I2C_TIMEOUT);
166#endif 163#endif
167 164
168 // select other half 165 // select other half
169 DDRB = (1 << row); 166 DDRB = (1 << row);
170 PORTB = ~(1 << row); 167 PORTB = ~(1 << row);
171} 168}
diff --git a/quantum/debounce.h b/quantum/debounce.h
index 30d2621d18..e26106cd3b 100644
--- a/quantum/debounce.h
+++ b/quantum/debounce.h
@@ -9,11 +9,10 @@
9 * 9 *
10 * @param raw The current key state 10 * @param raw The current key state
11 * @param cooked The debounced key state 11 * @param cooked The debounced key state
12 * @param num_rows Number of rows to debounce
13 * @param changed True if raw has changed since the last call 12 * @param changed True if raw has changed since the last call
14 * @return true Cooked has new keychanges after debouncing 13 * @return true Cooked has new keychanges after debouncing
15 * @return false Cooked is the same as before 14 * @return false Cooked is the same as before
16 */ 15 */
17bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed); 16bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed);
18 17
19void debounce_init(uint8_t num_rows); 18void debounce_init(void);
diff --git a/quantum/debounce/asym_eager_defer_pk.c b/quantum/debounce/asym_eager_defer_pk.c
index a385301c90..edd07eabc0 100644
--- a/quantum/debounce/asym_eager_defer_pk.c
+++ b/quantum/debounce/asym_eager_defer_pk.c
@@ -38,9 +38,9 @@ static bool cooked_changed;
38static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time); 38static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
39static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]); 39static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
40 40
41void debounce_init(uint8_t num_rows) {} 41void debounce_init(void) {}
42 42
43bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { 43bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
44 static fast_timer_t last_time; 44 static fast_timer_t last_time;
45 bool updated_last = false; 45 bool updated_last = false;
46 cooked_changed = false; 46 cooked_changed = false;
diff --git a/quantum/debounce/none.c b/quantum/debounce/none.c
index 0111dd6e31..e614f41a6b 100644
--- a/quantum/debounce/none.c
+++ b/quantum/debounce/none.c
@@ -17,13 +17,13 @@
17#include "debounce.h" 17#include "debounce.h"
18#include <string.h> 18#include <string.h>
19 19
20void debounce_init(uint8_t num_rows) {} 20void debounce_init(void) {}
21 21
22bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { 22bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
23 bool cooked_changed = false; 23 bool cooked_changed = false;
24 24
25 if (changed) { 25 if (changed) {
26 size_t matrix_size = num_rows * sizeof(matrix_row_t); 26 size_t matrix_size = MATRIX_ROWS_PER_HAND * sizeof(matrix_row_t);
27 if (memcmp(cooked, raw, matrix_size) != 0) { 27 if (memcmp(cooked, raw, matrix_size) != 0) {
28 memcpy(cooked, raw, matrix_size); 28 memcpy(cooked, raw, matrix_size);
29 cooked_changed = true; 29 cooked_changed = true;
diff --git a/quantum/debounce/sym_defer_g.c b/quantum/debounce/sym_defer_g.c
index 81f351c126..a60a131072 100644
--- a/quantum/debounce/sym_defer_g.c
+++ b/quantum/debounce/sym_defer_g.c
@@ -20,9 +20,9 @@
20 20
21#if DEBOUNCE > 0 21#if DEBOUNCE > 0
22 22
23void debounce_init(uint8_t num_rows) {} 23void debounce_init(void) {}
24 24
25bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { 25bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
26 static fast_timer_t debouncing_time; 26 static fast_timer_t debouncing_time;
27 static bool debouncing = false; 27 static bool debouncing = false;
28 bool cooked_changed = false; 28 bool cooked_changed = false;
@@ -31,7 +31,7 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
31 debouncing = true; 31 debouncing = true;
32 debouncing_time = timer_read_fast(); 32 debouncing_time = timer_read_fast();
33 } else if (debouncing && timer_elapsed_fast(debouncing_time) >= DEBOUNCE) { 33 } else if (debouncing && timer_elapsed_fast(debouncing_time) >= DEBOUNCE) {
34 size_t matrix_size = num_rows * sizeof(matrix_row_t); 34 size_t matrix_size = MATRIX_ROWS_PER_HAND * sizeof(matrix_row_t);
35 if (memcmp(cooked, raw, matrix_size) != 0) { 35 if (memcmp(cooked, raw, matrix_size) != 0) {
36 memcpy(cooked, raw, matrix_size); 36 memcpy(cooked, raw, matrix_size);
37 cooked_changed = true; 37 cooked_changed = true;
diff --git a/quantum/debounce/sym_defer_pk.c b/quantum/debounce/sym_defer_pk.c
index 063094efe5..b910571219 100644
--- a/quantum/debounce/sym_defer_pk.c
+++ b/quantum/debounce/sym_defer_pk.c
@@ -32,9 +32,9 @@ static bool cooked_changed;
32static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time); 32static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
33static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]); 33static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]);
34 34
35void debounce_init(uint8_t num_rows) {} 35void debounce_init(void) {}
36 36
37bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { 37bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
38 static fast_timer_t last_time; 38 static fast_timer_t last_time;
39 bool updated_last = false; 39 bool updated_last = false;
40 cooked_changed = false; 40 cooked_changed = false;
diff --git a/quantum/debounce/sym_defer_pr.c b/quantum/debounce/sym_defer_pr.c
index 2382fae898..feaf55b08a 100644
--- a/quantum/debounce/sym_defer_pr.c
+++ b/quantum/debounce/sym_defer_pr.c
@@ -33,9 +33,9 @@ static bool cooked_changed;
33static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time); 33static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
34static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]); 34static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]);
35 35
36void debounce_init(uint8_t num_rows) {} 36void debounce_init(void) {}
37 37
38bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { 38bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
39 static fast_timer_t last_time; 39 static fast_timer_t last_time;
40 bool updated_last = false; 40 bool updated_last = false;
41 cooked_changed = false; 41 cooked_changed = false;
diff --git a/quantum/debounce/sym_eager_pk.c b/quantum/debounce/sym_eager_pk.c
index c3a7afde24..1f53330e9c 100644
--- a/quantum/debounce/sym_eager_pk.c
+++ b/quantum/debounce/sym_eager_pk.c
@@ -46,9 +46,9 @@ static bool cooked_changed;
46static inline void update_debounce_counters(uint8_t elapsed_time); 46static inline void update_debounce_counters(uint8_t elapsed_time);
47static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]); 47static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
48 48
49void debounce_init(uint8_t num_rows) {} 49void debounce_init(void) {}
50 50
51bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { 51bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
52 static fast_timer_t last_time; 52 static fast_timer_t last_time;
53 bool updated_last = false; 53 bool updated_last = false;
54 cooked_changed = false; 54 cooked_changed = false;
diff --git a/quantum/debounce/sym_eager_pr.c b/quantum/debounce/sym_eager_pr.c
index 5a1e3a1bda..c929ff53dc 100644
--- a/quantum/debounce/sym_eager_pr.c
+++ b/quantum/debounce/sym_eager_pr.c
@@ -33,9 +33,9 @@ static bool cooked_changed;
33static inline void update_debounce_counters(uint8_t elapsed_time); 33static inline void update_debounce_counters(uint8_t elapsed_time);
34static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]); 34static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
35 35
36void debounce_init(uint8_t num_rows) {} 36void debounce_init(void) {}
37 37
38bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { 38bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
39 static fast_timer_t last_time; 39 static fast_timer_t last_time;
40 bool updated_last = false; 40 bool updated_last = false;
41 cooked_changed = false; 41 cooked_changed = false;
diff --git a/quantum/debounce/tests/debounce_test_common.cpp b/quantum/debounce/tests/debounce_test_common.cpp
index 3782f51411..84b91f85e1 100644
--- a/quantum/debounce/tests/debounce_test_common.cpp
+++ b/quantum/debounce/tests/debounce_test_common.cpp
@@ -60,7 +60,7 @@ void DebounceTest::runEventsInternal() {
60 bool first = true; 60 bool first = true;
61 61
62 /* Initialise keyboard with start time (offset to avoid testing at 0) and all keys UP */ 62 /* Initialise keyboard with start time (offset to avoid testing at 0) and all keys UP */
63 debounce_init(MATRIX_ROWS); 63 debounce_init();
64 set_time(time_offset_); 64 set_time(time_offset_);
65 simulate_async_tick(async_time_jumps_); 65 simulate_async_tick(async_time_jumps_);
66 std::fill(std::begin(input_matrix_), std::end(input_matrix_), 0); 66 std::fill(std::begin(input_matrix_), std::end(input_matrix_), 0);
@@ -129,7 +129,7 @@ void DebounceTest::runDebounce(bool changed) {
129 129
130 reset_access_counter(); 130 reset_access_counter();
131 131
132 bool cooked_changed = debounce(raw_matrix_, cooked_matrix_, MATRIX_ROWS, changed); 132 bool cooked_changed = debounce(raw_matrix_, cooked_matrix_, changed);
133 133
134 if (!std::equal(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_))) { 134 if (!std::equal(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_))) {
135 FAIL() << "Fatal error: debounce() modified raw matrix at " << strTime() << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) << "\nraw_matrix:\n" << strMatrix(raw_matrix_); 135 FAIL() << "Fatal error: debounce() modified raw matrix at " << strTime() << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) << "\nraw_matrix:\n" << strMatrix(raw_matrix_);
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 167a70e5b6..2e7ea085f4 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -303,7 +303,7 @@ void matrix_init(void) {
303 memset(matrix, 0, sizeof(matrix)); 303 memset(matrix, 0, sizeof(matrix));
304 memset(raw_matrix, 0, sizeof(raw_matrix)); 304 memset(raw_matrix, 0, sizeof(raw_matrix));
305 305
306 debounce_init(MATRIX_ROWS_PER_HAND); 306 debounce_init();
307 307
308 matrix_init_kb(); 308 matrix_init_kb();
309} 309}
@@ -336,9 +336,9 @@ uint8_t matrix_scan(void) {
336 if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix)); 336 if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix));
337 337
338#ifdef SPLIT_KEYBOARD 338#ifdef SPLIT_KEYBOARD
339 changed = debounce(raw_matrix, matrix + thisHand, MATRIX_ROWS_PER_HAND, changed) | matrix_post_scan(); 339 changed = debounce(raw_matrix, matrix + thisHand, changed) | matrix_post_scan();
340#else 340#else
341 changed = debounce(raw_matrix, matrix, MATRIX_ROWS_PER_HAND, changed); 341 changed = debounce(raw_matrix, matrix, changed);
342 matrix_scan_kb(); 342 matrix_scan_kb();
343#endif 343#endif
344 return (uint8_t)changed; 344 return (uint8_t)changed;
diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c
index b4a86fc483..26589f29a6 100644
--- a/quantum/matrix_common.c
+++ b/quantum/matrix_common.c
@@ -156,7 +156,7 @@ __attribute__((weak)) void matrix_init(void) {
156 matrix[i] = 0; 156 matrix[i] = 0;
157 } 157 }
158 158
159 debounce_init(MATRIX_ROWS_PER_HAND); 159 debounce_init();
160 160
161 matrix_init_kb(); 161 matrix_init_kb();
162} 162}
@@ -165,9 +165,9 @@ __attribute__((weak)) uint8_t matrix_scan(void) {
165 bool changed = matrix_scan_custom(raw_matrix); 165 bool changed = matrix_scan_custom(raw_matrix);
166 166
167#ifdef SPLIT_KEYBOARD 167#ifdef SPLIT_KEYBOARD
168 changed = debounce(raw_matrix, matrix + thisHand, MATRIX_ROWS_PER_HAND, changed) | matrix_post_scan(); 168 changed = debounce(raw_matrix, matrix + thisHand, changed) | matrix_post_scan();
169#else 169#else
170 changed = debounce(raw_matrix, matrix, MATRIX_ROWS_PER_HAND, changed); 170 changed = debounce(raw_matrix, matrix, changed);
171 matrix_scan_kb(); 171 matrix_scan_kb();
172#endif 172#endif
173 173