matrix.c (3165B)
1 /* 2 Copyright 2021 talsu <talsu84@gmail.com> 3 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. 8 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. 13 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/>. 16 */ 17 18 #include "wait.h" 19 #include "matrix.h" 20 21 static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 22 static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 23 24 /* Columns 0 - 3 25 * col / pin: 26 * 0: C7 27 * 1: C6 28 * 2: B6 29 * 3: B5 30 */ 31 static void unselect_cols(void) { 32 for (uint8_t col = 0; col < MATRIX_COLS; col++) { 33 gpio_set_pin_output(col_pins[col]); 34 gpio_write_pin_low(col_pins[col]); 35 } 36 } 37 38 static void select_col(uint8_t col) { 39 gpio_write_pin_high(col_pins[col]); 40 } 41 42 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { 43 bool matrix_changed = false; 44 45 // Select col and wait for col selecton to stabilize 46 select_col(current_col); 47 wait_us(30); 48 49 // row:0 , col:0 FN key is DIRECT_PIN 50 if (current_col == 0) { 51 52 matrix_row_t last_row_value = current_matrix[0]; 53 if (gpio_read_pin(row_pins[0]) == 0) { 54 // Pin LO, set col bit 55 current_matrix[0] |= (1 << current_col); 56 } else { 57 // Pin HI, clear col bit 58 current_matrix[0] &= ~(1 << current_col); 59 } 60 61 // Determine if the matrix changed state 62 if ((last_row_value != current_matrix[0]) && !(matrix_changed)) { 63 matrix_changed = true; 64 } 65 } 66 67 // other row use MATRIX 68 for (uint8_t row_index = 1; row_index < MATRIX_ROWS; row_index++) { 69 70 matrix_row_t last_row_value = current_matrix[row_index]; 71 if (gpio_read_pin(row_pins[row_index]) == 0) { 72 // Pin HI, clear col bit 73 current_matrix[row_index] &= ~(1 << current_col); 74 } else { 75 // Pin LO, set col bit 76 current_matrix[row_index] |= (1 << current_col); 77 } 78 79 // Determine if the matrix changed state 80 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { 81 matrix_changed = true; 82 } 83 } 84 85 86 // Unselect cols 87 unselect_cols(); 88 89 return matrix_changed; 90 } 91 92 void matrix_init_custom(void) { 93 // initialize hardware and global matrix state here 94 unselect_cols(); 95 96 // initialize key pins 97 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { 98 gpio_set_pin_input_high(row_pins[row_index]); 99 } 100 } 101 102 bool matrix_scan_custom(matrix_row_t current_matrix[]) { 103 bool changed = false; 104 105 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { 106 changed |= read_rows_on_col(current_matrix, current_col); 107 } 108 109 return changed; 110 }