diff options
| author | mechlovin <57231893+mechlovin@users.noreply.github.com> | 2021-04-09 11:48:30 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-09 20:48:30 +0200 |
| commit | f744e22b4944c4e8e5e71a4eb424dd4619c3e57d (patch) | |
| tree | 71dd02ac6eda2304e6acabd902b94abdd9bd568a /keyboards/mechlovin/infinity87/rev2/matrix.c | |
| parent | 1a913aa12eda0c62faba160dad55befeafdf4dc0 (diff) | |
| download | qmk_firmware-f744e22b4944c4e8e5e71a4eb424dd4619c3e57d.tar.gz qmk_firmware-f744e22b4944c4e8e5e71a4eb424dd4619c3e57d.zip | |
Add TH1800 PCB, Hex6C PCB, Rogue87 PCB, Rouge87 PCB, Infinity87 series (#11977)
Co-authored-by: Drashna Jaelre <drashna@live.com>
Co-authored-by: Ryan <fauxpark@gmail.com>
Co-authored-by: Erovia <Erovia@users.noreply.github.com>
Co-authored-by: vuhopkep <boy9x0@gmail.com>
Diffstat (limited to 'keyboards/mechlovin/infinity87/rev2/matrix.c')
| -rw-r--r-- | keyboards/mechlovin/infinity87/rev2/matrix.c | 442 |
1 files changed, 442 insertions, 0 deletions
diff --git a/keyboards/mechlovin/infinity87/rev2/matrix.c b/keyboards/mechlovin/infinity87/rev2/matrix.c new file mode 100644 index 000000000..6f417765a --- /dev/null +++ b/keyboards/mechlovin/infinity87/rev2/matrix.c | |||
| @@ -0,0 +1,442 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar | ||
| 3 | Copyright 2019 Evy Dekkers | ||
| 4 | |||
| 5 | This program is free software: you can redistribute it and/or modify | ||
| 6 | it under the terms of the GNU General Public License as published by | ||
| 7 | the Free Software Foundation, either version 2 of the License, or | ||
| 8 | (at your option) any later version. | ||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, | ||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | GNU General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | */ | ||
| 18 | |||
| 19 | #include <stdint.h> | ||
| 20 | #include <stdbool.h> | ||
| 21 | #include "wait.h" | ||
| 22 | #include "util.h" | ||
| 23 | #include "matrix.h" | ||
| 24 | #include "debounce.h" | ||
| 25 | #include "quantum.h" | ||
| 26 | |||
| 27 | #ifdef DIRECT_PINS | ||
| 28 | static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; | ||
| 29 | #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) | ||
| 30 | static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
| 31 | //static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 32 | #endif | ||
| 33 | |||
| 34 | // matrix code | ||
| 35 | |||
| 36 | #ifdef DIRECT_PINS | ||
| 37 | |||
| 38 | static void init_pins(void) { | ||
| 39 | for (int row = 0; row < MATRIX_ROWS; row++) { | ||
| 40 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
| 41 | pin_t pin = direct_pins[row][col]; | ||
| 42 | if (pin != NO_PIN) { | ||
| 43 | setPinInputHigh(pin); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | ||
| 50 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
| 51 | current_matrix[current_row] = 0; | ||
| 52 | |||
| 53 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
| 54 | pin_t pin = direct_pins[current_row][col_index]; | ||
| 55 | if (pin != NO_PIN) { | ||
| 56 | current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | return (last_row_value != current_matrix[current_row]); | ||
| 61 | } | ||
| 62 | |||
| 63 | #elif (DIODE_DIRECTION == COL2ROW) | ||
| 64 | |||
| 65 | static void select_row(uint8_t row) { | ||
| 66 | setPinOutput(row_pins[row]); | ||
| 67 | writePinLow(row_pins[row]); | ||
| 68 | } | ||
| 69 | |||
| 70 | static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); } | ||
| 71 | |||
| 72 | static void unselect_rows(void) { | ||
| 73 | for (uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
| 74 | setPinInputHigh(row_pins[x]); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | static void init_pins(void) { | ||
| 79 | unselect_rows(); | ||
| 80 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
| 81 | setPinInputHigh(col_pins[x]); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | ||
| 86 | // Store last value of row prior to reading | ||
| 87 | matrix_row_t last_row_value = current_matrix[current_row]; | ||
| 88 | |||
| 89 | // Clear data in matrix row | ||
| 90 | current_matrix[current_row] = 0; | ||
| 91 | |||
| 92 | // Select row and wait for row selecton to stabilize | ||
| 93 | select_row(current_row); | ||
| 94 | wait_us(30); | ||
| 95 | |||
| 96 | // For each col... | ||
| 97 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
| 98 | |||
| 99 | // Select the col pin to read (active low) | ||
| 100 | uint8_t pin_state = readPin(col_pins[col_index]); | ||
| 101 | |||
| 102 | // Populate the matrix row with the state of the col pin | ||
| 103 | current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); | ||
| 104 | } | ||
| 105 | |||
| 106 | // Unselect row | ||
| 107 | unselect_row(current_row); | ||
| 108 | |||
| 109 | return (last_row_value != current_matrix[current_row]); | ||
| 110 | } | ||
| 111 | |||
| 112 | #elif (DIODE_DIRECTION == ROW2COL) | ||
| 113 | |||
| 114 | /* Cols 0 - 16 | ||
| 115 | * These columns use two 74HC138 3 to 8 bit demultiplexer. D6, D7 is the enable pin, must be set high (1) to use it. | ||
| 116 | * | ||
| 117 | * col / pin: PA0 PA1 PA2 PD6 PD7 PC4 | ||
| 118 | * 0: 0 ── 0 ── 0 1 ── 0 0 | ||
| 119 | * ──────────────────────────────────────────── | ||
| 120 | * 1: 0 ── 0 ── 1 1 ── 0 0 | ||
| 121 | * ──────────────────────────────────────────── | ||
| 122 | * 2: 0 ── 1 ── 0 1 ── 0 0 | ||
| 123 | * ──────────────────────────────────────────── | ||
| 124 | * 3: 0 ── 1 ── 1 1 ── 0 0 | ||
| 125 | * ──────────────────────────────────────────── | ||
| 126 | * 4: 1 ── 0 ── 0 1 ── 0 0 | ||
| 127 | * ──────────────────────────────────────────── | ||
| 128 | * 5: 1 ── 0 ── 1 1 ── 0 0 | ||
| 129 | * ──────────────────────────────────────────── | ||
| 130 | * 6: 1 ── 1 ── 0 1 ── 0 0 | ||
| 131 | * ──────────────────────────────────────────── | ||
| 132 | * 7: 1 ── 1 ── 1 1 ── 0 0 | ||
| 133 | * ──────────────────────────────────────────── | ||
| 134 | * 8: 0 ── 0 ── 0 0 ── 1 0 | ||
| 135 | * ──────────────────────────────────────────── | ||
| 136 | * 9: 0 ── 0 ── 1 0 ── 1 0 | ||
| 137 | * ──────────────────────────────────────────── | ||
| 138 | *10: 0 ── 1 ── 0 0 ── 1 0 | ||
| 139 | * ──────────────────────────────────────────── | ||
| 140 | *11: 0 ── 1 ── 1 0 ── 1 0 | ||
| 141 | * ──────────────────────────────────────────── | ||
| 142 | *12: 1 ── 0 ── 0 0 ── 1 0 | ||
| 143 | * ──────────────────────────────────────────── | ||
| 144 | *13: 1 ── 0 ── 1 0 ── 1 0 | ||
| 145 | * ──────────────────────────────────────────── | ||
| 146 | *14: 1 ── 1 ── 1 0 ── 1 0 | ||
| 147 | * ──────────────────────────────────────────── | ||
| 148 | *15: 1 ── 1 ── 0 0 ── 1 0 | ||
| 149 | * ──────────────────────────────────────────── | ||
| 150 | *16: 0 ── 0 ── 0 0 ── 0 1 | ||
| 151 | * | ||
| 152 | */ | ||
| 153 | static void select_col(uint8_t col) { | ||
| 154 | switch (col) { | ||
| 155 | case 0: | ||
| 156 | writePinLow(A0); | ||
| 157 | writePinLow(A1); | ||
| 158 | writePinLow(A2); | ||
| 159 | writePinHigh(D6); | ||
| 160 | break; | ||
| 161 | case 1: | ||
| 162 | writePinLow(A0); | ||
| 163 | writePinLow(A1); | ||
| 164 | writePinHigh(A2); | ||
| 165 | writePinHigh(D6); | ||
| 166 | break; | ||
| 167 | case 2: | ||
| 168 | writePinLow(A0); | ||
| 169 | writePinHigh(A1); | ||
| 170 | writePinLow(A2); | ||
| 171 | writePinHigh(D6); | ||
| 172 | break; | ||
| 173 | case 3: | ||
| 174 | writePinLow(A0); | ||
| 175 | writePinHigh(A1); | ||
| 176 | writePinHigh(A2); | ||
| 177 | writePinHigh(D6); | ||
| 178 | break; | ||
| 179 | case 4: | ||
| 180 | writePinHigh(A0); | ||
| 181 | writePinLow(A1); | ||
| 182 | writePinLow(A2); | ||
| 183 | writePinHigh(D6); | ||
| 184 | break; | ||
| 185 | case 5: | ||
| 186 | writePinHigh(A0); | ||
| 187 | writePinLow(A1); | ||
| 188 | writePinHigh(A2); | ||
| 189 | writePinHigh(D6); | ||
| 190 | break; | ||
| 191 | case 6: | ||
| 192 | writePinHigh(A0); | ||
| 193 | writePinHigh(A1); | ||
| 194 | writePinLow(A2); | ||
| 195 | writePinHigh(D6); | ||
| 196 | break; | ||
| 197 | case 7: | ||
| 198 | writePinHigh(A0); | ||
| 199 | writePinHigh(A1); | ||
| 200 | writePinHigh(A2); | ||
| 201 | writePinHigh(D6); | ||
| 202 | break; | ||
| 203 | case 8: | ||
| 204 | writePinLow(A0); | ||
| 205 | writePinLow(A1); | ||
| 206 | writePinLow(A2); | ||
| 207 | writePinHigh(D7); | ||
| 208 | break; | ||
| 209 | case 9: | ||
| 210 | writePinLow(A0); | ||
| 211 | writePinLow(A1); | ||
| 212 | writePinHigh(A2); | ||
| 213 | writePinHigh(D7); | ||
| 214 | break; | ||
| 215 | case 10: | ||
| 216 | writePinLow(A0); | ||
| 217 | writePinHigh(A1); | ||
| 218 | writePinLow(A2); | ||
| 219 | writePinHigh(D7); | ||
| 220 | break; | ||
| 221 | case 11: | ||
| 222 | writePinLow(A0); | ||
| 223 | writePinHigh(A1); | ||
| 224 | writePinHigh(A2); | ||
| 225 | writePinHigh(D7); | ||
| 226 | break; | ||
| 227 | case 12: | ||
| 228 | writePinHigh(A0); | ||
| 229 | writePinLow(A1); | ||
| 230 | writePinLow(A2); | ||
| 231 | writePinHigh(D7); | ||
| 232 | break; | ||
| 233 | case 13: | ||
| 234 | writePinHigh(A0); | ||
| 235 | writePinLow(A1); | ||
| 236 | writePinHigh(A2); | ||
| 237 | writePinHigh(D7); | ||
| 238 | break; | ||
| 239 | case 14: | ||
| 240 | writePinHigh(A0); | ||
| 241 | writePinHigh(A1); | ||
| 242 | writePinHigh(A2); | ||
| 243 | writePinHigh(D7); | ||
| 244 | break; | ||
| 245 | case 15: | ||
| 246 | writePinHigh(A0); | ||
| 247 | writePinHigh(A1); | ||
| 248 | writePinLow(A2); | ||
| 249 | writePinHigh(D7); | ||
| 250 | break; | ||
| 251 | case 16: | ||
| 252 | writePinLow(C4); | ||
| 253 | break; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | static void unselect_col(uint8_t col) { | ||
| 258 | switch (col) { | ||
| 259 | case 0: | ||
| 260 | writePinHigh(A0); | ||
| 261 | writePinHigh(A1); | ||
| 262 | writePinHigh(A2); | ||
| 263 | writePinLow(D6); | ||
| 264 | break; | ||
| 265 | case 1: | ||
| 266 | writePinHigh(A0); | ||
| 267 | writePinHigh(A1); | ||
| 268 | writePinLow(A2); | ||
| 269 | writePinLow(D6); | ||
| 270 | break; | ||
| 271 | case 2: | ||
| 272 | writePinHigh(A0); | ||
| 273 | writePinLow(A1); | ||
| 274 | writePinHigh(A2); | ||
| 275 | writePinLow(D6); | ||
| 276 | break; | ||
| 277 | case 3: | ||
| 278 | writePinHigh(A0); | ||
| 279 | writePinLow(A1); | ||
| 280 | writePinLow(A2); | ||
| 281 | writePinLow(D6); | ||
| 282 | break; | ||
| 283 | case 4: | ||
| 284 | writePinLow(A0); | ||
| 285 | writePinHigh(A1); | ||
| 286 | writePinHigh(A2); | ||
| 287 | writePinLow(D6); | ||
| 288 | break; | ||
| 289 | case 5: | ||
| 290 | writePinLow(A0); | ||
| 291 | writePinHigh(A1); | ||
| 292 | writePinLow(A2); | ||
| 293 | writePinLow(D6); | ||
| 294 | break; | ||
| 295 | case 6: | ||
| 296 | writePinLow(A0); | ||
| 297 | writePinLow(A1); | ||
| 298 | writePinHigh(A2); | ||
| 299 | writePinLow(D6); | ||
| 300 | break; | ||
| 301 | case 7: | ||
| 302 | writePinLow(A0); | ||
| 303 | writePinLow(A1); | ||
| 304 | writePinLow(A2); | ||
| 305 | writePinLow(D6); | ||
| 306 | break; | ||
| 307 | case 8: | ||
| 308 | writePinHigh(A0); | ||
| 309 | writePinHigh(A1); | ||
| 310 | writePinHigh(A2); | ||
| 311 | writePinLow(D7); | ||
| 312 | break; | ||
| 313 | case 9: | ||
| 314 | writePinHigh(A0); | ||
| 315 | writePinHigh(A1); | ||
| 316 | writePinLow(A2); | ||
| 317 | writePinLow(D7); | ||
| 318 | break; | ||
| 319 | case 10: | ||
| 320 | writePinHigh(A0); | ||
| 321 | writePinLow(A1); | ||
| 322 | writePinHigh(A2); | ||
| 323 | writePinLow(D7); | ||
| 324 | break; | ||
| 325 | case 11: | ||
| 326 | writePinHigh(A0); | ||
| 327 | writePinLow(A1); | ||
| 328 | writePinLow(A2); | ||
| 329 | writePinLow(D7); | ||
| 330 | break; | ||
| 331 | case 12: | ||
| 332 | writePinLow(A0); | ||
| 333 | writePinHigh(A1); | ||
| 334 | writePinHigh(A2); | ||
| 335 | writePinLow(D7); | ||
| 336 | break; | ||
| 337 | case 13: | ||
| 338 | writePinLow(A0); | ||
| 339 | writePinHigh(A1); | ||
| 340 | writePinLow(A2); | ||
| 341 | writePinLow(D7); | ||
| 342 | break; | ||
| 343 | case 14: | ||
| 344 | writePinLow(A0); | ||
| 345 | writePinLow(A1); | ||
| 346 | writePinLow(A2); | ||
| 347 | writePinLow(D7); | ||
| 348 | break; | ||
| 349 | case 15: | ||
| 350 | writePinLow(A0); | ||
| 351 | writePinLow(A1); | ||
| 352 | writePinHigh(A2); | ||
| 353 | writePinLow(D7); | ||
| 354 | break; | ||
| 355 | case 16: | ||
| 356 | writePinHigh(C4); | ||
| 357 | break; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | static void unselect_cols(void) { | ||
| 362 | //Native | ||
| 363 | writePinHigh(C4); | ||
| 364 | |||
| 365 | //Demultiplexer | ||
| 366 | writePinLow(D6); | ||
| 367 | writePinLow(D7); | ||
| 368 | writePinHigh(A0); | ||
| 369 | writePinHigh(A1); | ||
| 370 | writePinHigh(A2); | ||
| 371 | } | ||
| 372 | |||
| 373 | static void init_pins(void) { | ||
| 374 | unselect_cols(); | ||
| 375 | for (uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
| 376 | setPinInputHigh(row_pins[x]); | ||
| 377 | } | ||
| 378 | setPinOutput(A0); | ||
| 379 | setPinOutput(A1); | ||
| 380 | setPinOutput(A2); | ||
| 381 | setPinOutput(D6); | ||
| 382 | setPinOutput(D7); | ||
| 383 | setPinOutput(C4); | ||
| 384 | } | ||
| 385 | |||
| 386 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { | ||
| 387 | bool matrix_changed = false; | ||
| 388 | |||
| 389 | // Select col and wait for col selecton to stabilize | ||
| 390 | select_col(current_col); | ||
| 391 | wait_us(30); | ||
| 392 | |||
| 393 | // For each row... | ||
| 394 | for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { | ||
| 395 | // Store last value of row prior to reading | ||
| 396 | matrix_row_t last_row_value = current_matrix[row_index]; | ||
| 397 | |||
| 398 | // Check row pin state | ||
| 399 | if (readPin(row_pins[row_index]) == 0) { | ||
| 400 | // Pin LO, set col bit | ||
| 401 | current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); | ||
| 402 | } else { | ||
| 403 | // Pin HI, clear col bit | ||
| 404 | current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); | ||
| 405 | } | ||
| 406 | |||
| 407 | // Determine if the matrix changed state | ||
| 408 | if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { | ||
| 409 | matrix_changed = true; | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | // Unselect col | ||
| 414 | unselect_col(current_col); | ||
| 415 | |||
| 416 | return matrix_changed; | ||
| 417 | } | ||
| 418 | |||
| 419 | #endif | ||
| 420 | |||
| 421 | void matrix_init_custom(void) { | ||
| 422 | // initialize key pins | ||
| 423 | init_pins(); | ||
| 424 | } | ||
| 425 | |||
| 426 | bool matrix_scan_custom(matrix_row_t current_matrix[]) { | ||
| 427 | bool changed = false; | ||
| 428 | |||
| 429 | #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) | ||
| 430 | // Set row, read cols | ||
| 431 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | ||
| 432 | changed |= read_cols_on_row(current_matrix, current_row); | ||
| 433 | } | ||
| 434 | #elif (DIODE_DIRECTION == ROW2COL) | ||
| 435 | // Set col, read rows | ||
| 436 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
| 437 | changed |= read_rows_on_col(current_matrix, current_col); | ||
| 438 | } | ||
| 439 | #endif | ||
| 440 | |||
| 441 | return changed; | ||
| 442 | } | ||
