aboutsummaryrefslogtreecommitdiff
path: root/quantum/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/matrix.c')
-rw-r--r--quantum/matrix.c71
1 files changed, 32 insertions, 39 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 9298661ad..71ef27089 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -16,6 +16,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17#include <stdint.h> 17#include <stdint.h>
18#include <stdbool.h> 18#include <stdbool.h>
19#include <string.h>
19#include "util.h" 20#include "util.h"
20#include "matrix.h" 21#include "matrix.h"
21#include "debounce.h" 22#include "debounce.h"
@@ -24,14 +25,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
24#ifdef DIRECT_PINS 25#ifdef DIRECT_PINS
25static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; 26static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
26#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) 27#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
28# ifdef MATRIX_ROW_PINS
27static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 29static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
30# endif // MATRIX_ROW_PINS
31# ifdef MATRIX_COL_PINS
28static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 32static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
33# endif // MATRIX_COL_PINS
29#endif 34#endif
30 35
31/* matrix state(1:on, 0:off) */ 36/* matrix state(1:on, 0:off) */
32extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values 37extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
33extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values 38extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
34 39
40// user-defined overridable functions
41__attribute__((weak)) void matrix_init_pins(void);
42__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
43__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
44
35static inline void setPinOutput_writeLow(pin_t pin) { 45static inline void setPinOutput_writeLow(pin_t pin) {
36 ATOMIC_BLOCK_FORCEON { 46 ATOMIC_BLOCK_FORCEON {
37 setPinOutput(pin); 47 setPinOutput(pin);
@@ -58,7 +68,7 @@ __attribute__((weak)) void matrix_init_pins(void) {
58 } 68 }
59} 69}
60 70
61__attribute__((weak)) bool matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 71__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
62 // Start with a clear matrix row 72 // Start with a clear matrix row
63 matrix_row_t current_row_value = 0; 73 matrix_row_t current_row_value = 0;
64 74
@@ -69,16 +79,13 @@ __attribute__((weak)) bool matrix_read_cols_on_row(matrix_row_t current_matrix[]
69 } 79 }
70 } 80 }
71 81
72 // If the row has changed, store the row and return the changed flag. 82 // Update the matrix
73 if (current_matrix[current_row] != current_row_value) { 83 current_matrix[current_row] = current_row_value;
74 current_matrix[current_row] = current_row_value;
75 return true;
76 }
77 return false;
78} 84}
79 85
80#elif defined(DIODE_DIRECTION) 86#elif defined(DIODE_DIRECTION)
81# if (DIODE_DIRECTION == COL2ROW) 87# if defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
88# if (DIODE_DIRECTION == COL2ROW)
82 89
83static void select_row(uint8_t row) { setPinOutput_writeLow(row_pins[row]); } 90static void select_row(uint8_t row) { setPinOutput_writeLow(row_pins[row]); }
84 91
@@ -97,7 +104,7 @@ __attribute__((weak)) void matrix_init_pins(void) {
97 } 104 }
98} 105}
99 106
100__attribute__((weak)) bool matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 107__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
101 // Start with a clear matrix row 108 // Start with a clear matrix row
102 matrix_row_t current_row_value = 0; 109 matrix_row_t current_row_value = 0;
103 110
@@ -118,15 +125,11 @@ __attribute__((weak)) bool matrix_read_cols_on_row(matrix_row_t current_matrix[]
118 unselect_row(current_row); 125 unselect_row(current_row);
119 matrix_output_unselect_delay(); // wait for all Col signals to go HIGH 126 matrix_output_unselect_delay(); // wait for all Col signals to go HIGH
120 127
121 // If the row has changed, store the row and return the changed flag. 128 // Update the matrix
122 if (current_matrix[current_row] != current_row_value) { 129 current_matrix[current_row] = current_row_value;
123 current_matrix[current_row] = current_row_value;
124 return true;
125 }
126 return false;
127} 130}
128 131
129# elif (DIODE_DIRECTION == ROW2COL) 132# elif (DIODE_DIRECTION == ROW2COL)
130 133
131static void select_col(uint8_t col) { setPinOutput_writeLow(col_pins[col]); } 134static void select_col(uint8_t col) { setPinOutput_writeLow(col_pins[col]); }
132 135
@@ -145,45 +148,32 @@ __attribute__((weak)) void matrix_init_pins(void) {
145 } 148 }
146} 149}
147 150
148__attribute__((weak)) bool matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { 151__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
149 bool matrix_changed = false;
150
151 // Select col 152 // Select col
152 select_col(current_col); 153 select_col(current_col);
153 matrix_output_select_delay(); 154 matrix_output_select_delay();
154 155
155 // For each row... 156 // For each row...
156 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { 157 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
157 // Store last value of row prior to reading
158 matrix_row_t last_row_value = current_matrix[row_index];
159 matrix_row_t current_row_value = last_row_value;
160
161 // Check row pin state 158 // Check row pin state
162 if (readPin(row_pins[row_index]) == 0) { 159 if (readPin(row_pins[row_index]) == 0) {
163 // Pin LO, set col bit 160 // Pin LO, set col bit
164 current_row_value |= (MATRIX_ROW_SHIFTER << current_col); 161 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
165 } else { 162 } else {
166 // Pin HI, clear col bit 163 // Pin HI, clear col bit
167 current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col); 164 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
168 }
169
170 // Determine if the matrix changed state
171 if ((last_row_value != current_row_value)) {
172 matrix_changed |= true;
173 current_matrix[row_index] = current_row_value;
174 } 165 }
175 } 166 }
176 167
177 // Unselect col 168 // Unselect col
178 unselect_col(current_col); 169 unselect_col(current_col);
179 matrix_output_unselect_delay(); // wait for all Row signals to go HIGH 170 matrix_output_unselect_delay(); // wait for all Row signals to go HIGH
180
181 return matrix_changed;
182} 171}
183 172
184# else 173# else
185# error DIODE_DIRECTION must be one of COL2ROW or ROW2COL! 174# error DIODE_DIRECTION must be one of COL2ROW or ROW2COL!
186# endif 175# endif
176# endif // defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS)
187#else 177#else
188# error DIODE_DIRECTION is not defined! 178# error DIODE_DIRECTION is not defined!
189#endif 179#endif
@@ -204,20 +194,23 @@ void matrix_init(void) {
204} 194}
205 195
206uint8_t matrix_scan(void) { 196uint8_t matrix_scan(void) {
207 bool changed = false; 197 matrix_row_t curr_matrix[MATRIX_ROWS] = {0};
208 198
209#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) 199#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
210 // Set row, read cols 200 // Set row, read cols
211 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 201 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
212 changed |= matrix_read_cols_on_row(raw_matrix, current_row); 202 matrix_read_cols_on_row(curr_matrix, current_row);
213 } 203 }
214#elif (DIODE_DIRECTION == ROW2COL) 204#elif (DIODE_DIRECTION == ROW2COL)
215 // Set col, read rows 205 // Set col, read rows
216 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 206 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
217 changed |= matrix_read_rows_on_col(raw_matrix, current_col); 207 matrix_read_rows_on_col(curr_matrix, current_col);
218 } 208 }
219#endif 209#endif
220 210
211 bool changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
212 if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix));
213
221 debounce(raw_matrix, matrix, MATRIX_ROWS, changed); 214 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
222 215
223 matrix_scan_quantum(); 216 matrix_scan_quantum();