diff options
Diffstat (limited to 'keyboards/evyd13/wasdat_code/matrix.c')
| -rw-r--r-- | keyboards/evyd13/wasdat_code/matrix.c | 369 |
1 files changed, 369 insertions, 0 deletions
diff --git a/keyboards/evyd13/wasdat_code/matrix.c b/keyboards/evyd13/wasdat_code/matrix.c new file mode 100644 index 000000000..7844db7ab --- /dev/null +++ b/keyboards/evyd13/wasdat_code/matrix.c | |||
| @@ -0,0 +1,369 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar | ||
| 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 | #include <stdint.h> | ||
| 18 | #include <stdbool.h> | ||
| 19 | #include "wait.h" | ||
| 20 | #include "util.h" | ||
| 21 | #include "matrix.h" | ||
| 22 | #include "debounce.h" | ||
| 23 | #include "quantum.h" | ||
| 24 | |||
| 25 | #ifdef DIRECT_PINS | ||
| 26 | static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; | ||
| 27 | #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) | ||
| 28 | static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
| 29 | //static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 30 | #endif | ||
| 31 | |||
| 32 | // matrix code | ||
| 33 | |||
| 34 | #ifdef DIRECT_PINS | ||
| 35 | |||
| 36 | static void init_pins(void) { | ||
| 37 | for (int row = 0; row < MATRIX_ROWS; row++) { | ||
| 38 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
| 39 | pin_t pin = direct_pins[row][col]; | ||
| 40 | if (pin != NO_PIN) { | ||
| 41 | setPinInputHigh(pin); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | ||
| 48 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
| 49 | current_matrix[current_row] = 0; | ||
| 50 | |||
| 51 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
| 52 | pin_t pin = direct_pins[current_row][col_index]; | ||
| 53 | if (pin != NO_PIN) { | ||
| 54 | current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | return (last_row_value != current_matrix[current_row]); | ||
| 59 | } | ||
| 60 | |||
| 61 | #elif (DIODE_DIRECTION == COL2ROW) | ||
| 62 | |||
| 63 | static void select_row(uint8_t row) { | ||
| 64 | setPinOutput(row_pins[row]); | ||
| 65 | writePinLow(row_pins[row]); | ||
| 66 | } | ||
| 67 | |||
| 68 | static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); } | ||
| 69 | |||
| 70 | static void unselect_rows(void) { | ||
| 71 | for (uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
| 72 | setPinInputHigh(row_pins[x]); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | static void init_pins(void) { | ||
| 77 | unselect_rows(); | ||
| 78 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
| 79 | setPinInputHigh(col_pins[x]); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | ||
| 84 | // Store last value of row prior to reading | ||
| 85 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
| 86 | |||
| 87 | // Clear data in matrix row | ||
| 88 | current_matrix[current_row] = 0; | ||
| 89 | |||
| 90 | // Select row and wait for row selecton to stabilize | ||
| 91 | select_row(current_row); | ||
| 92 | wait_us(30); | ||
| 93 | |||
| 94 | // For each col... | ||
| 95 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
| 96 | |||
| 97 | // Select the col pin to read (active low) | ||
| 98 | uint8_t pin_state = readPin(col_pins[col_index]); | ||
| 99 | |||
| 100 | // Populate the matrix row with the state of the col pin | ||
| 101 | current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); | ||
| 102 | } | ||
| 103 | |||
| 104 | // Unselect row | ||
| 105 | unselect_row(current_row); | ||
| 106 | |||
| 107 | return (last_row_value != current_matrix[current_row]); | ||
| 108 | } | ||
| 109 | |||
| 110 | #elif (DIODE_DIRECTION == ROW2COL) | ||
| 111 | |||
| 112 | /* Cols 0 - 15 | ||
| 113 | * col 0: F7 | ||
| 114 | * col 1: F5 | ||
| 115 | * col 2: F6 | ||
| 116 | * col 3: F1 | ||
| 117 | * col 4: F4 | ||
| 118 | * col 5: F0 | ||
| 119 | * These columns use a 74HC237D 3 to 8 bit demultiplexer. D4 is the enable pin, must be set high to use it. | ||
| 120 | * A0 A1 A2 | ||
| 121 | * col / pin: PD2 PD1 PD0 | ||
| 122 | * 6: 1 1 1 | ||
| 123 | * col 7: D3 | ||
| 124 | * col 8: B7 | ||
| 125 | * 9: 0 1 1 | ||
| 126 | * 10: 1 0 1 | ||
| 127 | * 11: 0 0 1 | ||
| 128 | * 12: 1 1 0 | ||
| 129 | * 13: 0 1 0 | ||
| 130 | * 14: 1 0 0 | ||
| 131 | * 15: 0 0 0 | ||
| 132 | */ | ||
| 133 | static void select_col(uint8_t col) { | ||
| 134 | switch (col) { | ||
| 135 | case 0: | ||
| 136 | writePinLow(F7); | ||
| 137 | break; | ||
| 138 | case 1: | ||
| 139 | writePinLow(F5); | ||
| 140 | break; | ||
| 141 | case 2: | ||
| 142 | writePinLow(F6); | ||
| 143 | break; | ||
| 144 | case 3: | ||
| 145 | writePinLow(F1); | ||
| 146 | break; | ||
| 147 | case 4: | ||
| 148 | writePinLow(F4); | ||
| 149 | break; | ||
| 150 | case 5: | ||
| 151 | writePinLow(F0); | ||
| 152 | break; | ||
| 153 | case 6: | ||
| 154 | writePinHigh(D4); | ||
| 155 | writePinHigh(D2); | ||
| 156 | writePinHigh(D1); | ||
| 157 | writePinHigh(D0); | ||
| 158 | break; | ||
| 159 | case 7: | ||
| 160 | writePinLow(D5); | ||
| 161 | break; | ||
| 162 | case 8: | ||
| 163 | writePinLow(D3); | ||
| 164 | break; | ||
| 165 | case 9: | ||
| 166 | writePinHigh(D4); | ||
| 167 | writePinHigh(D1); | ||
| 168 | writePinHigh(D0); | ||
| 169 | break; | ||
| 170 | case 10: | ||
| 171 | writePinHigh(D4); | ||
| 172 | writePinHigh(D2); | ||
| 173 | writePinHigh(D0); | ||
| 174 | break; | ||
| 175 | case 11: | ||
| 176 | writePinHigh(D4); | ||
| 177 | writePinHigh(D0); | ||
| 178 | break; | ||
| 179 | case 12: | ||
| 180 | writePinHigh(D4); | ||
| 181 | writePinHigh(D2); | ||
| 182 | writePinHigh(D1); | ||
| 183 | break; | ||
| 184 | case 13: | ||
| 185 | writePinHigh(D4); | ||
| 186 | writePinHigh(D1); | ||
| 187 | break; | ||
| 188 | case 14: | ||
| 189 | writePinHigh(D4); | ||
| 190 | writePinHigh(D2); | ||
| 191 | break; | ||
| 192 | case 15: | ||
| 193 | writePinHigh(D4); | ||
| 194 | break; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | static void unselect_col(uint8_t col) { | ||
| 199 | switch (col) { | ||
| 200 | case 0: | ||
| 201 | writePinHigh(F7); | ||
| 202 | break; | ||
| 203 | case 1: | ||
| 204 | writePinHigh(F5); | ||
| 205 | break; | ||
| 206 | case 2: | ||
| 207 | writePinHigh(F6); | ||
| 208 | break; | ||
| 209 | case 3: | ||
| 210 | writePinHigh(F1); | ||
| 211 | break; | ||
| 212 | case 4: | ||
| 213 | writePinHigh(F4); | ||
| 214 | break; | ||
| 215 | case 5: | ||
| 216 | writePinHigh(F0); | ||
| 217 | break; | ||
| 218 | case 6: | ||
| 219 | writePinLow(D4); | ||
| 220 | writePinLow(D2); | ||
| 221 | writePinLow(D1); | ||
| 222 | writePinLow(D0); | ||
| 223 | break; | ||
| 224 | case 7: | ||
| 225 | writePinHigh(D5); | ||
| 226 | break; | ||
| 227 | case 8: | ||
| 228 | writePinHigh(D3); | ||
| 229 | break; | ||
| 230 | case 9: | ||
| 231 | writePinLow(D4); | ||
| 232 | writePinLow(D2); | ||
| 233 | writePinLow(D1); | ||
| 234 | writePinLow(D0); | ||
| 235 | break; | ||
| 236 | case 10: | ||
| 237 | writePinLow(D4); | ||
| 238 | writePinLow(D2); | ||
| 239 | writePinLow(D1); | ||
| 240 | writePinLow(D0); | ||
| 241 | break; | ||
| 242 | case 11: | ||
| 243 | writePinLow(D4); | ||
| 244 | writePinLow(D2); | ||
| 245 | writePinLow(D1); | ||
| 246 | writePinLow(D0); | ||
| 247 | break; | ||
| 248 | case 12: | ||
| 249 | writePinLow(D4); | ||
| 250 | writePinLow(D2); | ||
| 251 | writePinLow(D1); | ||
| 252 | writePinLow(D0); | ||
| 253 | break; | ||
| 254 | case 13: | ||
| 255 | writePinLow(D4); | ||
| 256 | writePinLow(D2); | ||
| 257 | writePinLow(D1); | ||
| 258 | writePinLow(D0); | ||
| 259 | break; | ||
| 260 | case 14: | ||
| 261 | writePinLow(D4); | ||
| 262 | writePinLow(D2); | ||
| 263 | writePinLow(D1); | ||
| 264 | writePinLow(D0); | ||
| 265 | break; | ||
| 266 | case 15: | ||
| 267 | writePinLow(D4); | ||
| 268 | writePinLow(D2); | ||
| 269 | writePinLow(D1); | ||
| 270 | writePinLow(D0); | ||
| 271 | break; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | static void unselect_cols(void) { | ||
| 276 | //Native | ||
| 277 | writePinHigh(F7); | ||
| 278 | writePinHigh(F5); | ||
| 279 | writePinHigh(F6); | ||
| 280 | writePinHigh(F1); | ||
| 281 | writePinHigh(F4); | ||
| 282 | writePinHigh(F0); | ||
| 283 | writePinHigh(D3); | ||
| 284 | writePinHigh(D5); | ||
| 285 | |||
| 286 | //Demultiplexer | ||
| 287 | writePinLow(D4); | ||
| 288 | writePinLow(D2); | ||
| 289 | writePinLow(D1); | ||
| 290 | writePinLow(D0); | ||
| 291 | } | ||
| 292 | |||
| 293 | static void init_pins(void) { | ||
| 294 | unselect_cols(); | ||
| 295 | for (uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
| 296 | setPinInputHigh(row_pins[x]); | ||
| 297 | } | ||
| 298 | setPinOutput(D0); | ||
| 299 | setPinOutput(D1); | ||
| 300 | setPinOutput(D2); | ||
| 301 | setPinOutput(D3); | ||
| 302 | setPinOutput(F7); | ||
| 303 | setPinOutput(F5); | ||
| 304 | setPinOutput(F6); | ||
| 305 | setPinOutput(F1); | ||
| 306 | setPinOutput(F4); | ||
| 307 | setPinOutput(F0); | ||
| 308 | setPinOutput(D3); | ||
| 309 | setPinOutput(D5); | ||
| 310 | setPinOutput(D4); | ||
| 311 | } | ||
| 312 | |||
| 313 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { | ||
| 314 | bool matrix_changed = false; | ||
| 315 | |||
| 316 | // Select col and wait for col selecton to stabilize | ||
| 317 | select_col(current_col); | ||
| 318 | wait_us(30); | ||
| 319 | |||
| 320 | // For each row... | ||
| 321 | for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { | ||
| 322 | // Store last value of row prior to reading | ||
| 323 | matrix_row_t last_row_value = current_matrix[row_index]; | ||
| 324 | |||
| 325 | // Check row pin state | ||
| 326 | if (readPin(row_pins[row_index]) == 0) { | ||
| 327 | // Pin LO, set col bit | ||
| 328 | current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); | ||
| 329 | } else { | ||
| 330 | // Pin HI, clear col bit | ||
| 331 | current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); | ||
| 332 | } | ||
| 333 | |||
| 334 | // Determine if the matrix changed state | ||
| 335 | if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { | ||
| 336 | matrix_changed = true; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | // Unselect col | ||
| 341 | unselect_col(current_col); | ||
| 342 | |||
| 343 | return matrix_changed; | ||
| 344 | } | ||
| 345 | |||
| 346 | #endif | ||
| 347 | |||
| 348 | void matrix_init_custom(void) { | ||
| 349 | // initialize key pins | ||
| 350 | init_pins(); | ||
| 351 | } | ||
| 352 | |||
| 353 | bool matrix_scan_custom(matrix_row_t current_matrix[]) { | ||
| 354 | bool changed = false; | ||
| 355 | |||
| 356 | #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) | ||
| 357 | // Set row, read cols | ||
| 358 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | ||
| 359 | changed |= read_cols_on_row(current_matrix, current_row); | ||
| 360 | } | ||
| 361 | #elif (DIODE_DIRECTION == ROW2COL) | ||
| 362 | // Set col, read rows | ||
| 363 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
| 364 | changed |= read_rows_on_col(current_matrix, current_col); | ||
| 365 | } | ||
| 366 | #endif | ||
| 367 | |||
| 368 | return changed; | ||
| 369 | } | ||
