2 Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
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/>.
27 #if (MATRIX_COLS <= 8)
28 # define print_matrix_header() print("\nr/c 01234567\n")
29 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
30 # define matrix_bitpop(i) bitpop(matrix[i])
31 # define ROW_SHIFTER ((uint8_t)1)
32 #elif (MATRIX_COLS <= 16)
33 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
34 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
35 # define matrix_bitpop(i) bitpop16(matrix[i])
36 # define ROW_SHIFTER ((uint16_t)1)
37 #elif (MATRIX_COLS <= 32)
38 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
39 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
40 # define matrix_bitpop(i) bitpop32(matrix[i])
41 # define ROW_SHIFTER ((uint32_t)1)
45 extern const matrix_row_t matrix_mask[];
49 static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
50 #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
51 // static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
52 static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
55 /* matrix state(1:on, 0:off) */
56 static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
57 static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
59 __attribute__ ((weak))
60 void matrix_init_quantum(void) {
64 __attribute__ ((weak))
65 void matrix_scan_quantum(void) {
69 __attribute__ ((weak))
70 void matrix_init_kb(void) {
74 __attribute__ ((weak))
75 void matrix_scan_kb(void) {
79 __attribute__ ((weak))
80 void matrix_init_user(void) {
83 __attribute__ ((weak))
84 void matrix_scan_user(void) {
88 uint8_t matrix_rows(void) {
93 uint8_t matrix_cols(void) {
98 bool matrix_is_modified(void)
100 if (debounce_active()) return false;
105 bool matrix_is_on(uint8_t row, uint8_t col)
107 return (matrix[row] & ((matrix_row_t)1<<col));
111 matrix_row_t matrix_get_row(uint8_t row)
113 // 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.
116 return matrix[row] & matrix_mask[row];
122 void matrix_print(void)
124 print_matrix_header();
126 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
127 phex(row); print(": ");
128 print_matrix_row(row);
133 uint8_t matrix_key_count(void)
136 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137 count += matrix_bitpop(i);
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];
150 setPinInputHigh(pin);
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;
160 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
161 pin_t pin = direct_pins[current_row][col_index];
163 current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index);
167 return (last_row_value != current_matrix[current_row]);
170 #elif (DIODE_DIRECTION == COL2ROW)
172 * These rows use a 74HC237D 3 to 8 bit demultiplexer.
174 * row / pin: PB0 PB1 PB2
182 static void select_row(uint8_t col)
211 static void unselect_row(uint8_t col)
240 static void unselect_rows(void)
245 // make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
251 static void init_pins(void) {
253 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
254 setPinInputHigh(col_pins[x]);
258 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
260 // Store last value of row prior to reading
261 matrix_row_t last_row_value = current_matrix[current_row];
263 // Clear data in matrix row
264 current_matrix[current_row] = 0;
266 // Select row and wait for row selecton to stabilize
267 select_row(current_row);
271 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
273 // Select the col pin to read (active low)
274 uint8_t pin_state = readPin(col_pins[col_index]);
276 // Populate the matrix row with the state of the col pin
277 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
281 unselect_row(current_row);
283 return (last_row_value != current_matrix[current_row]);
286 #elif (DIODE_DIRECTION == ROW2COL)
288 static void select_col(uint8_t col)
290 setPinOutput(col_pins[col]);
291 writePinLow(col_pins[col]);
294 static void unselect_col(uint8_t col)
296 setPinInputHigh(col_pins[col]);
299 static void unselect_cols(void)
301 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
302 setPinInputHigh(col_pins[x]);
306 static void init_pins(void) {
308 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
309 setPinInputHigh(row_pins[x]);
313 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
315 bool matrix_changed = false;
317 // Select col and wait for col selecton to stabilize
318 select_col(current_col);
322 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
325 // Store last value of row prior to reading
326 matrix_row_t last_row_value = current_matrix[row_index];
328 // Check row pin state
329 if (readPin(row_pins[row_index]) == 0)
331 // Pin LO, set col bit
332 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
336 // Pin HI, clear col bit
337 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
340 // Determine if the matrix changed state
341 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
343 matrix_changed = true;
348 unselect_col(current_col);
350 return matrix_changed;
355 void matrix_init(void) {
357 // initialize key pins
360 // initialize matrix state: all keys off
361 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
366 debounce_init(MATRIX_ROWS);
368 matrix_init_quantum();
371 uint8_t matrix_scan(void)
373 bool changed = false;
375 #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
376 // Set row, read cols
377 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
378 changed |= read_cols_on_row(raw_matrix, current_row);
380 #elif (DIODE_DIRECTION == ROW2COL)
381 // Set col, read rows
382 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
383 changed |= read_rows_on_col(raw_matrix, current_col);
387 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
389 matrix_scan_quantum();