matrix.c (6026B)
1 /* Copyright 2021 @ Keychron (https://www.keychron.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 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 "atomic_util.h" 19 #include <string.h> 20 21 // Pin connected to DS of 74HC595 22 #define DATA_PIN A7 23 // Pin connected to SH_CP of 74HC595 24 #define CLOCK_PIN B1 25 // Pin connected to ST_CP of 74HC595 26 #define LATCH_PIN B0 27 28 #ifdef MATRIX_ROW_PINS 29 static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 30 #endif // MATRIX_ROW_PINS 31 #ifdef MATRIX_COL_PINS 32 static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 33 #endif // MATRIX_COL_PINS 34 35 #define ROWS_PER_HAND (MATRIX_ROWS) 36 37 static inline void gpio_atomic_set_pin_output_low(pin_t pin) { 38 ATOMIC_BLOCK_FORCEON { 39 gpio_set_pin_output(pin); 40 gpio_write_pin_low(pin); 41 } 42 } 43 44 static inline void gpio_atomic_set_pin_output_high(pin_t pin) { 45 ATOMIC_BLOCK_FORCEON { 46 gpio_set_pin_output(pin); 47 gpio_write_pin_high(pin); 48 } 49 } 50 51 static inline void gpio_atomic_set_pin_input_high(pin_t pin) { 52 ATOMIC_BLOCK_FORCEON { 53 gpio_set_pin_input_high(pin); 54 } 55 } 56 57 static inline uint8_t readMatrixPin(pin_t pin) { 58 if (pin != NO_PIN) { 59 return gpio_read_pin(pin); 60 } else { 61 return 1; 62 } 63 } 64 65 static void shiftOutMultiple(uint16_t dataOut) { 66 for (uint8_t i = 0; i < 8; i++) { 67 if (dataOut & 0x1) { 68 gpio_atomic_set_pin_output_high(DATA_PIN); 69 } else { 70 gpio_atomic_set_pin_output_low(DATA_PIN); 71 } 72 dataOut = dataOut >> 1; 73 gpio_atomic_set_pin_output_high(CLOCK_PIN); 74 gpio_atomic_set_pin_output_low(CLOCK_PIN); 75 } 76 gpio_atomic_set_pin_output_high(LATCH_PIN); 77 gpio_atomic_set_pin_output_low(LATCH_PIN); 78 } 79 80 static void shiftOut_single(uint8_t dataOut) { 81 if (dataOut & 0x1) { 82 gpio_atomic_set_pin_output_high(DATA_PIN); 83 } else { 84 gpio_atomic_set_pin_output_low(DATA_PIN); 85 } 86 gpio_atomic_set_pin_output_high(CLOCK_PIN); 87 gpio_atomic_set_pin_output_low(CLOCK_PIN); 88 gpio_atomic_set_pin_output_high(LATCH_PIN); 89 gpio_atomic_set_pin_output_low(LATCH_PIN); 90 } 91 92 static bool select_col(uint8_t col) { 93 pin_t pin = col_pins[col]; 94 95 if (pin != NO_PIN) { 96 gpio_atomic_set_pin_output_low(pin); 97 return true; 98 } else { 99 if (col == (MATRIX_COLS - 8)) { 100 shiftOut_single(0x00); 101 } else { 102 shiftOut_single(0x01); 103 } 104 return true; 105 } 106 return false; 107 } 108 109 static void unselect_col(uint8_t col) { 110 pin_t pin = col_pins[col]; 111 112 if (pin != NO_PIN) { 113 #ifdef MATRIX_UNSELECT_DRIVE_HIGH 114 gpio_atomic_set_pin_output_high(pin); 115 #else 116 gpio_atomic_set_pin_input_high(pin); 117 #endif 118 } else { 119 if (col == (MATRIX_COLS - 1)) { 120 gpio_atomic_set_pin_output_high(CLOCK_PIN); 121 gpio_atomic_set_pin_output_low(CLOCK_PIN); 122 gpio_atomic_set_pin_output_high(LATCH_PIN); 123 gpio_atomic_set_pin_output_low(LATCH_PIN); 124 } 125 } 126 } 127 128 static void unselect_cols(void) { 129 // unselect column pins 130 for (uint8_t x = 0; x < MATRIX_COLS; x++) { 131 pin_t pin = col_pins[x]; 132 if (pin != NO_PIN) { 133 #ifdef MATRIX_UNSELECT_DRIVE_HIGH 134 gpio_atomic_set_pin_output_high(pin); 135 #else 136 gpio_atomic_set_pin_input_high(pin); 137 #endif 138 } else { 139 if (x == (MATRIX_COLS - 1)) { 140 // unselect Shift Register 141 shiftOutMultiple(0xFF); 142 } 143 } 144 } 145 } 146 147 static void matrix_init_pins(void) { 148 unselect_cols(); 149 for (uint8_t x = 0; x < MATRIX_ROWS; x++) { 150 if (row_pins[x] != NO_PIN) { 151 gpio_atomic_set_pin_input_high(row_pins[x]); 152 } 153 } 154 } 155 156 static void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) { 157 bool key_pressed = false; 158 159 // Select col 160 if (!select_col(current_col)) { // select col 161 return; // skip NO_PIN col 162 } 163 164 if (current_col < (MATRIX_COLS - 8)) { 165 matrix_output_select_delay(); 166 } else { 167 matrix_output_select_delay(); 168 matrix_output_select_delay(); 169 matrix_output_select_delay(); 170 matrix_output_select_delay(); 171 } 172 173 // For each row... 174 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { 175 // Check row pin state 176 if (readMatrixPin(row_pins[row_index]) == 0) { 177 // Pin LO, set col bit 178 current_matrix[row_index] |= row_shifter; 179 key_pressed = true; 180 } else { 181 // Pin HI, clear col bit 182 current_matrix[row_index] &= ~row_shifter; 183 } 184 } 185 186 // Unselect col 187 unselect_col(current_col); 188 matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH 189 } 190 191 void matrix_init_custom(void) { 192 // initialize key pins 193 matrix_init_pins(); 194 } 195 196 bool matrix_scan_custom(matrix_row_t current_matrix[]) { 197 matrix_row_t curr_matrix[MATRIX_ROWS] = {0}; 198 199 // Set col, read rows 200 matrix_row_t row_shifter = MATRIX_ROW_SHIFTER; 201 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) { 202 matrix_read_rows_on_col(curr_matrix, current_col, row_shifter); 203 } 204 205 bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0; 206 if (changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix)); 207 208 return changed; 209 }