diff options
author | zvecr <git@zvecr.com> | 2019-04-11 19:51:55 +0100 |
---|---|---|
committer | MechMerlin <30334081+mechmerlin@users.noreply.github.com> | 2019-04-11 11:51:55 -0700 |
commit | 0137b0231957c0b2fde80ac0e2a769ba4cbd60e7 (patch) | |
tree | 581ae59d969aa725bf0af268a09808f1f4ae7bbf /quantum/matrix.c | |
parent | dc570b0b389d23b8ea8b46311294a7040b5e1e44 (diff) | |
download | qmk_firmware-0137b0231957c0b2fde80ac0e2a769ba4cbd60e7.tar.gz qmk_firmware-0137b0231957c0b2fde80ac0e2a769ba4cbd60e7.zip |
Port DIRECT_PINS from split_common/matrix.c to matrix.c (#5091)
* Port DIRECT_PINS from split_common/matrix.c to matrix.c
* Reorder matrix.c to remove foward declaration and match split_common/matrix.c
* Refactor nano to use DIRECT_PINS
* Reorder matrix.c to remove foward declaration and match split_common/matrix.c
* Add DIRECT_PINS documentation
* Reorder matrix.c to remove foward declaration and match split_common/matrix.c - fix logic from inherited from split_common
* Add DIRECT_PINS documentation - review comments
Diffstat (limited to 'quantum/matrix.c')
-rw-r--r-- | quantum/matrix.c | 189 |
1 files changed, 99 insertions, 90 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c index f7cad1a0f..ca63f50f2 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c | |||
@@ -45,7 +45,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
45 | extern const matrix_row_t matrix_mask[]; | 45 | extern const matrix_row_t matrix_mask[]; |
46 | #endif | 46 | #endif |
47 | 47 | ||
48 | #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) | 48 | #ifdef DIRECT_PINS |
49 | static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; | ||
50 | #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) | ||
49 | static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | 51 | static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; |
50 | static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | 52 | static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; |
51 | #endif | 53 | #endif |
@@ -54,20 +56,6 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | |||
54 | static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values | 56 | static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values |
55 | static matrix_row_t matrix[MATRIX_ROWS]; //debounced values | 57 | static matrix_row_t matrix[MATRIX_ROWS]; //debounced values |
56 | 58 | ||
57 | #if (DIODE_DIRECTION == COL2ROW) | ||
58 | static void init_cols(void); | ||
59 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); | ||
60 | static void unselect_rows(void); | ||
61 | static void select_row(uint8_t row); | ||
62 | static void unselect_row(uint8_t row); | ||
63 | #elif (DIODE_DIRECTION == ROW2COL) | ||
64 | static void init_rows(void); | ||
65 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); | ||
66 | static void unselect_cols(void); | ||
67 | static void unselect_col(uint8_t col); | ||
68 | static void select_col(uint8_t col); | ||
69 | #endif | ||
70 | |||
71 | __attribute__ ((weak)) | 59 | __attribute__ ((weak)) |
72 | void matrix_init_quantum(void) { | 60 | void matrix_init_quantum(void) { |
73 | matrix_init_kb(); | 61 | matrix_init_kb(); |
@@ -106,49 +94,6 @@ uint8_t matrix_cols(void) { | |||
106 | return MATRIX_COLS; | 94 | return MATRIX_COLS; |
107 | } | 95 | } |
108 | 96 | ||
109 | void matrix_init(void) { | ||
110 | |||
111 | // initialize row and col | ||
112 | #if (DIODE_DIRECTION == COL2ROW) | ||
113 | unselect_rows(); | ||
114 | init_cols(); | ||
115 | #elif (DIODE_DIRECTION == ROW2COL) | ||
116 | unselect_cols(); | ||
117 | init_rows(); | ||
118 | #endif | ||
119 | |||
120 | // initialize matrix state: all keys off | ||
121 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
122 | raw_matrix[i] = 0; | ||
123 | matrix[i] = 0; | ||
124 | } | ||
125 | debounce_init(MATRIX_ROWS); | ||
126 | |||
127 | matrix_init_quantum(); | ||
128 | } | ||
129 | |||
130 | uint8_t matrix_scan(void) | ||
131 | { | ||
132 | bool changed = false; | ||
133 | |||
134 | #if (DIODE_DIRECTION == COL2ROW) | ||
135 | // Set row, read cols | ||
136 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | ||
137 | changed |= read_cols_on_row(raw_matrix, current_row); | ||
138 | } | ||
139 | #elif (DIODE_DIRECTION == ROW2COL) | ||
140 | // Set col, read rows | ||
141 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
142 | changed |= read_rows_on_col(raw_matrix, current_col); | ||
143 | } | ||
144 | #endif | ||
145 | |||
146 | debounce(raw_matrix, matrix, MATRIX_ROWS, changed); | ||
147 | |||
148 | matrix_scan_quantum(); | ||
149 | return 1; | ||
150 | } | ||
151 | |||
152 | //Deprecated. | 97 | //Deprecated. |
153 | bool matrix_is_modified(void) | 98 | bool matrix_is_modified(void) |
154 | { | 99 | { |
@@ -195,16 +140,60 @@ uint8_t matrix_key_count(void) | |||
195 | } | 140 | } |
196 | 141 | ||
197 | 142 | ||
143 | #ifdef DIRECT_PINS | ||
144 | |||
145 | static void init_pins(void) { | ||
146 | for (int row = 0; row < MATRIX_ROWS; row++) { | ||
147 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
148 | pin_t pin = direct_pins[row][col]; | ||
149 | if (pin != NO_PIN) { | ||
150 | setPinInputHigh(pin); | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | } | ||
198 | 155 | ||
199 | #if (DIODE_DIRECTION == COL2ROW) | 156 | static 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]; | ||
158 | current_matrix[current_row] = 0; | ||
200 | 159 | ||
201 | static void init_cols(void) | 160 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { |
161 | pin_t pin = direct_pins[current_row][col_index]; | ||
162 | if (pin != NO_PIN) { | ||
163 | current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | return (last_row_value != current_matrix[current_row]); | ||
168 | } | ||
169 | |||
170 | #elif (DIODE_DIRECTION == COL2ROW) | ||
171 | |||
172 | static void select_row(uint8_t row) | ||
202 | { | 173 | { |
203 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | 174 | setPinOutput(row_pins[row]); |
204 | setPinInputHigh(col_pins[x]); | 175 | writePinLow(row_pins[row]); |
176 | } | ||
177 | |||
178 | static void unselect_row(uint8_t row) | ||
179 | { | ||
180 | setPinInputHigh(row_pins[row]); | ||
181 | } | ||
182 | |||
183 | static void unselect_rows(void) | ||
184 | { | ||
185 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
186 | setPinInput(row_pins[x]); | ||
205 | } | 187 | } |
206 | } | 188 | } |
207 | 189 | ||
190 | static void init_pins(void) { | ||
191 | unselect_rows(); | ||
192 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
193 | setPinInputHigh(col_pins[x]); | ||
194 | } | ||
195 | } | ||
196 | |||
208 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | 197 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) |
209 | { | 198 | { |
210 | // Store last value of row prior to reading | 199 | // Store last value of row prior to reading |
@@ -233,31 +222,31 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
233 | return (last_row_value != current_matrix[current_row]); | 222 | return (last_row_value != current_matrix[current_row]); |
234 | } | 223 | } |
235 | 224 | ||
236 | static void select_row(uint8_t row) | 225 | #elif (DIODE_DIRECTION == ROW2COL) |
226 | |||
227 | static void select_col(uint8_t col) | ||
237 | { | 228 | { |
238 | setPinOutput(row_pins[row]); | 229 | setPinOutput(col_pins[col]); |
239 | writePinLow(row_pins[row]); | 230 | writePinLow(col_pins[col]); |
240 | } | 231 | } |
241 | 232 | ||
242 | static void unselect_row(uint8_t row) | 233 | static void unselect_col(uint8_t col) |
243 | { | 234 | { |
244 | setPinInputHigh(row_pins[row]); | 235 | setPinInputHigh(col_pins[col]); |
245 | } | 236 | } |
246 | 237 | ||
247 | static void unselect_rows(void) | 238 | static void unselect_cols(void) |
248 | { | 239 | { |
249 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | 240 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { |
250 | setPinInput(row_pins[x]); | 241 | setPinInputHigh(col_pins[x]); |
251 | } | 242 | } |
252 | } | 243 | } |
253 | 244 | ||
254 | #elif (DIODE_DIRECTION == ROW2COL) | 245 | static void init_pins(void) { |
255 | 246 | unselect_cols(); | |
256 | static void init_rows(void) | 247 | for (uint8_t x = 0; x < MATRIX_ROWS; x++) { |
257 | { | 248 | setPinInputHigh(row_pins[x]); |
258 | for(uint8_t x = 0; x < MATRIX_ROWS; x++) { | 249 | } |
259 | setPinInputHigh(row_pins[x]); | ||
260 | } | ||
261 | } | 250 | } |
262 | 251 | ||
263 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | 252 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) |
@@ -300,22 +289,42 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | |||
300 | return matrix_changed; | 289 | return matrix_changed; |
301 | } | 290 | } |
302 | 291 | ||
303 | static void select_col(uint8_t col) | 292 | #endif |
304 | { | ||
305 | setPinOutput(col_pins[col]); | ||
306 | writePinLow(col_pins[col]); | ||
307 | } | ||
308 | 293 | ||
309 | static void unselect_col(uint8_t col) | 294 | void matrix_init(void) { |
310 | { | ||
311 | setPinInputHigh(col_pins[col]); | ||
312 | } | ||
313 | 295 | ||
314 | static void unselect_cols(void) | 296 | // initialize key pins |
315 | { | 297 | init_pins(); |
316 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | 298 | |
317 | setPinInputHigh(col_pins[x]); | 299 | // initialize matrix state: all keys off |
300 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
301 | raw_matrix[i] = 0; | ||
302 | matrix[i] = 0; | ||
318 | } | 303 | } |
304 | |||
305 | debounce_init(MATRIX_ROWS); | ||
306 | |||
307 | matrix_init_quantum(); | ||
319 | } | 308 | } |
320 | 309 | ||
310 | uint8_t matrix_scan(void) | ||
311 | { | ||
312 | bool changed = false; | ||
313 | |||
314 | #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) | ||
315 | // Set row, read cols | ||
316 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | ||
317 | changed |= read_cols_on_row(raw_matrix, current_row); | ||
318 | } | ||
319 | #elif (DIODE_DIRECTION == ROW2COL) | ||
320 | // Set col, read rows | ||
321 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
322 | changed |= read_rows_on_col(raw_matrix, current_col); | ||
323 | } | ||
321 | #endif | 324 | #endif |
325 | |||
326 | debounce(raw_matrix, matrix, MATRIX_ROWS, changed); | ||
327 | |||
328 | matrix_scan_quantum(); | ||
329 | return 1; | ||
330 | } | ||