matrix.c (3422B)
1 /* 2 Copyright 2023 QVEX Tech 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 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #include "matrix.h" 18 #include "wait.h" 19 20 static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 21 static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 22 23 extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values 24 extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values 25 26 static void select_row(uint8_t row) { 27 gpio_set_pin_output(row_pins[row]); 28 gpio_write_pin_low(row_pins[row]); 29 } 30 31 static void unselect_row(uint8_t row) { 32 gpio_set_pin_input_high(row_pins[row]); 33 } 34 35 static void unselect_rows(void) { 36 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 37 gpio_set_pin_input_high(row_pins[x]); 38 } 39 } 40 41 static void init_pins(void) { 42 unselect_rows(); 43 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 44 gpio_set_pin_input_high(col_pins[x]); 45 } 46 gpio_set_pin_input_high(PIN_JU); 47 gpio_set_pin_input_high(PIN_JD); 48 gpio_set_pin_input_high(PIN_JL); 49 gpio_set_pin_input_high(PIN_JR); 50 gpio_set_pin_input_high(PIN_JC); 51 gpio_set_pin_input_high(PIN_TC); 52 } 53 54 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 55 if (current_row > 2) return false; 56 57 matrix_row_t last_row_value = current_matrix[current_row]; 58 59 current_matrix[current_row] = 0; 60 61 select_row(current_row); 62 wait_us(30); 63 64 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 65 uint8_t pin_state = gpio_read_pin(col_pins[col_index]); 66 current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); 67 } 68 69 unselect_row(current_row); 70 71 return (last_row_value != current_matrix[current_row]); 72 } 73 74 static bool read_encoder_switches(matrix_row_t current_matrix[]) { 75 matrix_row_t last_fourth_row = current_matrix[3]; 76 matrix_row_t last_fifth_row = current_matrix[4]; 77 78 current_matrix[3] = 0; 79 current_matrix[4] = 0; 80 81 current_matrix[4] |= !gpio_read_pin(PIN_TC) ? (1 << 1) : 0; 82 83 if (!gpio_read_pin(PIN_JC)) { 84 if (!gpio_read_pin(PIN_JU)) { 85 current_matrix[3] |= (1 << 0); 86 } else if (!gpio_read_pin(PIN_JD)) { 87 current_matrix[3] |= (1 << 1); 88 } else if (!gpio_read_pin(PIN_JL)) { 89 current_matrix[3] |= (1 << 2); 90 } else if (!gpio_read_pin(PIN_JR)) { 91 current_matrix[3] |= (1 << 3); 92 } else { 93 current_matrix[4] |= (1 << 0); 94 } 95 } 96 97 return last_fourth_row != current_matrix[3] || last_fifth_row != current_matrix[4]; 98 } 99 100 void matrix_init_custom(void) { 101 init_pins(); 102 } 103 104 bool matrix_scan_custom(void) { 105 bool changed = false; 106 107 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 108 changed |= read_cols_on_row(raw_matrix, current_row); 109 } 110 111 changed |= read_encoder_switches(raw_matrix); 112 113 return changed; 114 }