2 Copyright 2012-2018 Jun Wako, Jack Humbert, Mike Roberts
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * This matrix.c has been hacked up to support some columns being on an ex pander in ROW2COL mode.
20 * The columns are only ever selected and unselected, never read. Unselecting a single column via the expander is not
21 * implemented because updating one column costs the same as updating all the columns in a bank. Currently both banks
22 * are unselected but two i2c transactions could be removed if we only unselect the the proper half.
37 #include "outputselect.h"
39 /* Set 0 if debouncing isn't needed */
41 #ifndef DEBOUNCING_DELAY
42 # define DEBOUNCING_DELAY 5
45 #if (DEBOUNCING_DELAY > 0)
46 static uint16_t debouncing_time;
47 static bool debouncing = false;
50 #if (MATRIX_COLS <= 8)
51 # define print_matrix_header() print("\nr/c 01234567\n")
52 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
53 # define matrix_bitpop(i) bitpop(matrix[i])
54 # define ROW_SHIFTER ((uint8_t)1)
55 #elif (MATRIX_COLS <= 16)
56 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
57 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
58 # define matrix_bitpop(i) bitpop16(matrix[i])
59 # define ROW_SHIFTER ((uint16_t)1)
60 #elif (MATRIX_COLS <= 32)
61 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
62 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
63 # define matrix_bitpop(i) bitpop32(matrix[i])
64 # define ROW_SHIFTER ((uint32_t)1)
68 extern const matrix_row_t matrix_mask[];
71 #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
72 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
73 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
74 static const bool col_expanded[MATRIX_COLS] = COL_EXPANDED;
77 /* matrix state(1:on, 0:off) */
78 static matrix_row_t matrix[MATRIX_ROWS];
80 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
83 #if (DIODE_DIRECTION == COL2ROW)
84 static void init_cols(void);
85 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
86 static void unselect_rows(void);
87 static void select_row(uint8_t row);
88 static void unselect_row(uint8_t row);
89 #elif (DIODE_DIRECTION == ROW2COL)
90 static void init_rows(void);
91 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
92 static void unselect_cols(void);
93 static void unselect_col(uint8_t col);
94 static void select_col(uint8_t col);
97 __attribute__ ((weak))
98 void matrix_init_quantum(void) {
103 __attribute__ ((weak))
104 void matrix_scan_quantum(void) {
108 __attribute__ ((weak))
109 void matrix_init_kb(void) {
113 __attribute__ ((weak))
114 void matrix_scan_kb(void) {
118 __attribute__ ((weak))
119 void matrix_init_user(void) {
122 __attribute__ ((weak))
123 void matrix_scan_user(void) {
127 uint8_t matrix_rows(void) {
132 uint8_t matrix_cols(void) {
136 void matrix_init(void) {
137 // initialize row and col
138 #if (DIODE_DIRECTION == COL2ROW)
141 #elif (DIODE_DIRECTION == ROW2COL)
146 // initialize matrix state: all keys off
147 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
149 matrix_debouncing[i] = 0;
152 matrix_init_quantum();
153 set_output(OUTPUT_AUTO);
156 uint8_t matrix_scan(void)
159 #if (DIODE_DIRECTION == COL2ROW)
161 // Set row, read cols
162 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
163 # if (DEBOUNCING_DELAY > 0)
164 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
166 if (matrix_changed) {
168 debouncing_time = timer_read();
172 read_cols_on_row(matrix, current_row);
177 #elif (DIODE_DIRECTION == ROW2COL)
179 // Set col, read rows
180 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
181 # if (DEBOUNCING_DELAY > 0)
182 bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
183 if (matrix_changed) {
185 debouncing_time = timer_read();
188 read_rows_on_col(matrix, current_col);
195 # if (DEBOUNCING_DELAY > 0)
196 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
197 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
198 matrix[i] = matrix_debouncing[i];
204 matrix_scan_quantum();
208 bool matrix_is_modified(void)
210 #if (DEBOUNCING_DELAY > 0)
211 if (debouncing) return false;
217 bool matrix_is_on(uint8_t row, uint8_t col)
219 return (matrix[row] & ((matrix_row_t)1<col));
223 matrix_row_t matrix_get_row(uint8_t row)
225 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
226 // switch blocker installed and the switch is always pressed.
228 return matrix[row] & matrix_mask[row];
234 void matrix_print(void)
236 print_matrix_header();
238 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
239 phex(row); print(": ");
240 print_matrix_row(row);
245 uint8_t matrix_key_count(void)
248 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
249 count += matrix_bitpop(i);
256 #if (DIODE_DIRECTION == COL2ROW)
258 static void init_cols(void)
260 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
261 uint8_t pin = col_pins[x];
262 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
263 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
267 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
269 // Store last value of row prior to reading
270 matrix_row_t last_row_value = current_matrix[current_row];
272 // Clear data in matrix row
273 current_matrix[current_row] = 0;
275 // Select row and wait for row selecton to stabilize
276 select_row(current_row);
280 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
282 // Select the col pin to read (active low)
283 uint8_t pin = col_pins[col_index];
284 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
286 // Populate the matrix row with the state of the col pin
287 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
291 unselect_row(current_row);
293 return (last_row_value != current_matrix[current_row]);
296 static void select_row(uint8_t row)
298 uint8_t pin = row_pins[row];
299 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
300 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
303 static void unselect_row(uint8_t row)
305 uint8_t pin = row_pins[row];
306 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
307 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
310 static void unselect_rows(void)
312 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
313 uint8_t pin = row_pins[x];
314 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
315 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
319 #elif (DIODE_DIRECTION == ROW2COL)
321 static void init_rows(void)
323 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
324 uint8_t pin = row_pins[x];
325 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
326 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
330 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
332 bool matrix_changed = false;
334 // Select col and wait for col selecton to stabilize
335 select_col(current_col);
339 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
341 // Store last value of row prior to reading
342 matrix_row_t last_row_value = current_matrix[row_index];
344 // Check row pin state
345 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
347 // Pin LO, set col bit
348 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
352 // Pin HI, clear col bit
353 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
356 // Determine if the matrix changed state
357 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
359 matrix_changed = true;
364 unselect_col(current_col);
366 return matrix_changed;
369 static void select_col(uint8_t col)
371 uint8_t pin = col_pins[col];
372 if (col_expanded[col])
374 expander_select(pin);
378 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
379 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
384 static void unselect_col(uint8_t col)
386 uint8_t pin = col_pins[col];
387 if (col_expanded[col])
389 expander_unselect_all();
393 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
394 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
398 static void unselect_cols(void)
400 expander_unselect_all();
402 for(uint8_t col = 0; col < MATRIX_COLS; col++) {
403 uint8_t pin = col_pins[col];
404 if (!col_expanded[col])
406 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
407 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI