aboutsummaryrefslogtreecommitdiff
path: root/quantum/matrix.c
diff options
context:
space:
mode:
authorskullY <skullydazed@gmail.com>2019-08-30 11:19:03 -0700
committerskullydazed <skullydazed@users.noreply.github.com>2019-08-30 15:01:52 -0700
commitb624f32f944acdc59dcb130674c09090c5c404cb (patch)
treebc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /quantum/matrix.c
parent61af76a10d00aba185b8338604171de490a13e3b (diff)
downloadqmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz
qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip
clang-format changes
Diffstat (limited to 'quantum/matrix.c')
-rw-r--r--quantum/matrix.c218
1 files changed, 83 insertions, 135 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 7ccac3533..907492a0f 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -25,24 +25,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
25#include "quantum.h" 25#include "quantum.h"
26 26
27#if (MATRIX_COLS <= 8) 27#if (MATRIX_COLS <= 8)
28# define print_matrix_header() print("\nr/c 01234567\n") 28# define print_matrix_header() print("\nr/c 01234567\n")
29# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 29# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
30# define matrix_bitpop(i) bitpop(matrix[i]) 30# define matrix_bitpop(i) bitpop(matrix[i])
31# define ROW_SHIFTER ((uint8_t)1) 31# define ROW_SHIFTER ((uint8_t)1)
32#elif (MATRIX_COLS <= 16) 32#elif (MATRIX_COLS <= 16)
33# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") 33# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
34# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) 34# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
35# define matrix_bitpop(i) bitpop16(matrix[i]) 35# define matrix_bitpop(i) bitpop16(matrix[i])
36# define ROW_SHIFTER ((uint16_t)1) 36# define ROW_SHIFTER ((uint16_t)1)
37#elif (MATRIX_COLS <= 32) 37#elif (MATRIX_COLS <= 32)
38# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") 38# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
39# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) 39# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
40# define matrix_bitpop(i) bitpop32(matrix[i]) 40# define matrix_bitpop(i) bitpop32(matrix[i])
41# define ROW_SHIFTER ((uint32_t)1) 41# define ROW_SHIFTER ((uint32_t)1)
42#endif 42#endif
43 43
44#ifdef MATRIX_MASKED 44#ifdef MATRIX_MASKED
45 extern const matrix_row_t matrix_mask[]; 45extern const matrix_row_t matrix_mask[];
46#endif 46#endif
47 47
48#ifdef DIRECT_PINS 48#ifdef DIRECT_PINS
@@ -53,63 +53,34 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
53#endif 53#endif
54 54
55/* matrix state(1:on, 0:off) */ 55/* matrix state(1:on, 0:off) */
56static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values 56static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
57static matrix_row_t matrix[MATRIX_ROWS]; //debounced values 57static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
58 58
59__attribute__ ((weak)) 59__attribute__((weak)) void matrix_init_quantum(void) { matrix_init_kb(); }
60void matrix_init_quantum(void) {
61 matrix_init_kb();
62}
63 60
64__attribute__ ((weak)) 61__attribute__((weak)) void matrix_scan_quantum(void) { matrix_scan_kb(); }
65void matrix_scan_quantum(void) {
66 matrix_scan_kb();
67}
68 62
69__attribute__ ((weak)) 63__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
70void matrix_init_kb(void) {
71 matrix_init_user();
72}
73 64
74__attribute__ ((weak)) 65__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
75void matrix_scan_kb(void) {
76 matrix_scan_user();
77}
78 66
79__attribute__ ((weak)) 67__attribute__((weak)) void matrix_init_user(void) {}
80void matrix_init_user(void) {
81}
82 68
83__attribute__ ((weak)) 69__attribute__((weak)) void matrix_scan_user(void) {}
84void matrix_scan_user(void) {
85}
86 70
87inline 71inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
88uint8_t matrix_rows(void) {
89 return MATRIX_ROWS;
90}
91 72
92inline 73inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
93uint8_t matrix_cols(void) {
94 return MATRIX_COLS;
95}
96 74
97//Deprecated. 75// Deprecated.
98bool matrix_is_modified(void) 76bool matrix_is_modified(void) {
99{
100 if (debounce_active()) return false; 77 if (debounce_active()) return false;
101 return true; 78 return true;
102} 79}
103 80
104inline 81inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
105bool matrix_is_on(uint8_t row, uint8_t col)
106{
107 return (matrix[row] & ((matrix_row_t)1<<col));
108}
109 82
110inline 83inline matrix_row_t matrix_get_row(uint8_t row) {
111matrix_row_t matrix_get_row(uint8_t row)
112{
113 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a 84 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
114 // switch blocker installed and the switch is always pressed. 85 // switch blocker installed and the switch is always pressed.
115#ifdef MATRIX_MASKED 86#ifdef MATRIX_MASKED
@@ -119,19 +90,18 @@ matrix_row_t matrix_get_row(uint8_t row)
119#endif 90#endif
120} 91}
121 92
122void matrix_print(void) 93void matrix_print(void) {
123{
124 print_matrix_header(); 94 print_matrix_header();
125 95
126 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { 96 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
127 phex(row); print(": "); 97 phex(row);
98 print(": ");
128 print_matrix_row(row); 99 print_matrix_row(row);
129 print("\n"); 100 print("\n");
130 } 101 }
131} 102}
132 103
133uint8_t matrix_key_count(void) 104uint8_t matrix_key_count(void) {
134{
135 uint8_t count = 0; 105 uint8_t count = 0;
136 for (uint8_t i = 0; i < MATRIX_ROWS; i++) { 106 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137 count += matrix_bitpop(i); 107 count += matrix_bitpop(i);
@@ -139,63 +109,56 @@ uint8_t matrix_key_count(void)
139 return count; 109 return count;
140} 110}
141 111
142
143#ifdef DIRECT_PINS 112#ifdef DIRECT_PINS
144 113
145static void init_pins(void) { 114static void init_pins(void) {
146 for (int row = 0; row < MATRIX_ROWS; row++) { 115 for (int row = 0; row < MATRIX_ROWS; row++) {
147 for (int col = 0; col < MATRIX_COLS; col++) { 116 for (int col = 0; col < MATRIX_COLS; col++) {
148 pin_t pin = direct_pins[row][col]; 117 pin_t pin = direct_pins[row][col];
149 if (pin != NO_PIN) { 118 if (pin != NO_PIN) {
150 setPinInputHigh(pin); 119 setPinInputHigh(pin);
151 } 120 }
121 }
152 } 122 }
153 }
154} 123}
155 124
156static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 125static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
157 matrix_row_t last_row_value = current_matrix[current_row]; 126 matrix_row_t last_row_value = current_matrix[current_row];
158 current_matrix[current_row] = 0; 127 current_matrix[current_row] = 0;
159 128
160 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 129 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
161 pin_t pin = direct_pins[current_row][col_index]; 130 pin_t pin = direct_pins[current_row][col_index];
162 if (pin != NO_PIN) { 131 if (pin != NO_PIN) {
163 current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index); 132 current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index);
133 }
164 } 134 }
165 }
166 135
167 return (last_row_value != current_matrix[current_row]); 136 return (last_row_value != current_matrix[current_row]);
168} 137}
169 138
170#elif (DIODE_DIRECTION == COL2ROW) 139#elif (DIODE_DIRECTION == COL2ROW)
171 140
172static void select_row(uint8_t row) 141static void select_row(uint8_t row) {
173{
174 setPinOutput(row_pins[row]); 142 setPinOutput(row_pins[row]);
175 writePinLow(row_pins[row]); 143 writePinLow(row_pins[row]);
176} 144}
177 145
178static void unselect_row(uint8_t row) 146static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
179{
180 setPinInputHigh(row_pins[row]);
181}
182 147
183static void unselect_rows(void) 148static void unselect_rows(void) {
184{ 149 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
185 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
186 setPinInputHigh(row_pins[x]); 150 setPinInputHigh(row_pins[x]);
187 } 151 }
188} 152}
189 153
190static void init_pins(void) { 154static void init_pins(void) {
191 unselect_rows(); 155 unselect_rows();
192 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 156 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
193 setPinInputHigh(col_pins[x]); 157 setPinInputHigh(col_pins[x]);
194 } 158 }
195} 159}
196 160
197static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) 161static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
198{
199 // Store last value of row prior to reading 162 // Store last value of row prior to reading
200 matrix_row_t last_row_value = current_matrix[current_row]; 163 matrix_row_t last_row_value = current_matrix[current_row];
201 164
@@ -207,13 +170,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
207 wait_us(30); 170 wait_us(30);
208 171
209 // For each col... 172 // For each col...
210 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 173 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
211
212 // Select the col pin to read (active low) 174 // Select the col pin to read (active low)
213 uint8_t pin_state = readPin(col_pins[col_index]); 175 uint8_t pin_state = readPin(col_pins[col_index]);
214 176
215 // Populate the matrix row with the state of the col pin 177 // Populate the matrix row with the state of the col pin
216 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 178 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
217 } 179 }
218 180
219 // Unselect row 181 // Unselect row
@@ -224,33 +186,27 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
224 186
225#elif (DIODE_DIRECTION == ROW2COL) 187#elif (DIODE_DIRECTION == ROW2COL)
226 188
227static void select_col(uint8_t col) 189static void select_col(uint8_t col) {
228{
229 setPinOutput(col_pins[col]); 190 setPinOutput(col_pins[col]);
230 writePinLow(col_pins[col]); 191 writePinLow(col_pins[col]);
231} 192}
232 193
233static void unselect_col(uint8_t col) 194static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
234{
235 setPinInputHigh(col_pins[col]);
236}
237 195
238static void unselect_cols(void) 196static void unselect_cols(void) {
239{ 197 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
240 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
241 setPinInputHigh(col_pins[x]); 198 setPinInputHigh(col_pins[x]);
242 } 199 }
243} 200}
244 201
245static void init_pins(void) { 202static void init_pins(void) {
246 unselect_cols(); 203 unselect_cols();
247 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 204 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
248 setPinInputHigh(row_pins[x]); 205 setPinInputHigh(row_pins[x]);
249 } 206 }
250} 207}
251 208
252static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) 209static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
253{
254 bool matrix_changed = false; 210 bool matrix_changed = false;
255 211
256 // Select col and wait for col selecton to stabilize 212 // Select col and wait for col selecton to stabilize
@@ -258,27 +214,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
258 wait_us(30); 214 wait_us(30);
259 215
260 // For each row... 216 // For each row...
261 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) 217 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
262 {
263
264 // Store last value of row prior to reading 218 // Store last value of row prior to reading
265 matrix_row_t last_row_value = current_matrix[row_index]; 219 matrix_row_t last_row_value = current_matrix[row_index];
266 220
267 // Check row pin state 221 // Check row pin state
268 if (readPin(row_pins[row_index]) == 0) 222 if (readPin(row_pins[row_index]) == 0) {
269 {
270 // Pin LO, set col bit 223 // Pin LO, set col bit
271 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 224 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
272 } 225 } else {
273 else
274 {
275 // Pin HI, clear col bit 226 // Pin HI, clear col bit
276 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 227 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
277 } 228 }
278 229
279 // Determine if the matrix changed state 230 // Determine if the matrix changed state
280 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) 231 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
281 {
282 matrix_changed = true; 232 matrix_changed = true;
283 } 233 }
284 } 234 }
@@ -292,14 +242,13 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
292#endif 242#endif
293 243
294void matrix_init(void) { 244void matrix_init(void) {
295
296 // initialize key pins 245 // initialize key pins
297 init_pins(); 246 init_pins();
298 247
299 // initialize matrix state: all keys off 248 // initialize matrix state: all keys off
300 for (uint8_t i=0; i < MATRIX_ROWS; i++) { 249 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
301 raw_matrix[i] = 0; 250 raw_matrix[i] = 0;
302 matrix[i] = 0; 251 matrix[i] = 0;
303 } 252 }
304 253
305 debounce_init(MATRIX_ROWS); 254 debounce_init(MATRIX_ROWS);
@@ -307,24 +256,23 @@ void matrix_init(void) {
307 matrix_init_quantum(); 256 matrix_init_quantum();
308} 257}
309 258
310uint8_t matrix_scan(void) 259uint8_t matrix_scan(void) {
311{ 260 bool changed = false;
312 bool changed = false;
313 261
314#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) 262#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
315 // Set row, read cols 263 // Set row, read cols
316 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 264 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
317 changed |= read_cols_on_row(raw_matrix, current_row); 265 changed |= read_cols_on_row(raw_matrix, current_row);
318 } 266 }
319#elif (DIODE_DIRECTION == ROW2COL) 267#elif (DIODE_DIRECTION == ROW2COL)
320 // Set col, read rows 268 // Set col, read rows
321 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 269 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
322 changed |= read_rows_on_col(raw_matrix, current_col); 270 changed |= read_rows_on_col(raw_matrix, current_col);
323 } 271 }
324#endif 272#endif
325 273
326 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 274 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
327 275
328 matrix_scan_quantum(); 276 matrix_scan_quantum();
329 return (uint8_t)changed; 277 return (uint8_t)changed;
330} 278}