diff options
Diffstat (limited to 'keyboards/matrix/m12og/rev1/matrix.c')
| -rw-r--r-- | keyboards/matrix/m12og/rev1/matrix.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/keyboards/matrix/m12og/rev1/matrix.c b/keyboards/matrix/m12og/rev1/matrix.c new file mode 100644 index 000000000..9c36153da --- /dev/null +++ b/keyboards/matrix/m12og/rev1/matrix.c | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | /* Copyright 2021 xyzz | ||
| 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 <stdint.h> | ||
| 18 | #include <stdbool.h> | ||
| 19 | #include "util.h" | ||
| 20 | #include "matrix.h" | ||
| 21 | #include "debounce.h" | ||
| 22 | #include "quantum.h" | ||
| 23 | |||
| 24 | static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
| 25 | static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 26 | |||
| 27 | static void select_col(uint8_t col) { | ||
| 28 | setPinOutput(col_pins[col]); | ||
| 29 | writePinHigh(col_pins[col]); | ||
| 30 | } | ||
| 31 | |||
| 32 | static void unselect_col(uint8_t col) { setPinInputLow(col_pins[col]); } | ||
| 33 | |||
| 34 | static void unselect_cols(void) { | ||
| 35 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
| 36 | setPinInputLow(col_pins[x]); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | static void init_pins(void) { | ||
| 41 | unselect_cols(); | ||
| 42 | for (uint8_t x = 0; x < MATRIX_ROWS; x++) { | ||
| 43 | setPinInputLow(row_pins[x]); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { | ||
| 48 | bool matrix_changed = false; | ||
| 49 | |||
| 50 | // Select col and wait for col selecton to stabilize | ||
| 51 | select_col(current_col); | ||
| 52 | matrix_io_delay(); | ||
| 53 | |||
| 54 | // For each row... | ||
| 55 | for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { | ||
| 56 | // Store last value of row prior to reading | ||
| 57 | matrix_row_t last_row_value = current_matrix[row_index]; | ||
| 58 | matrix_row_t current_row_value = last_row_value; | ||
| 59 | |||
| 60 | // Check row pin state | ||
| 61 | if (readPin(row_pins[row_index]) != 0) { | ||
| 62 | // Pin LO, set col bit | ||
| 63 | current_row_value |= (MATRIX_ROW_SHIFTER << current_col); | ||
| 64 | } else { | ||
| 65 | // Pin HI, clear col bit | ||
| 66 | current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col); | ||
| 67 | } | ||
| 68 | |||
| 69 | // Determine if the matrix changed state | ||
| 70 | if ((last_row_value != current_row_value)) { | ||
| 71 | matrix_changed |= true; | ||
| 72 | current_matrix[row_index] = current_row_value; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | // Unselect col | ||
| 77 | unselect_col(current_col); | ||
| 78 | |||
| 79 | return matrix_changed; | ||
| 80 | } | ||
| 81 | |||
| 82 | void matrix_init_custom(void) { | ||
| 83 | // initialize key pins | ||
| 84 | init_pins(); | ||
| 85 | } | ||
| 86 | |||
| 87 | uint8_t matrix_scan_custom(matrix_row_t current_matrix[]) { | ||
| 88 | bool changed = false; | ||
| 89 | |||
| 90 | // Set col, read rows | ||
| 91 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
| 92 | changed |= read_rows_on_col(current_matrix, current_col); | ||
| 93 | } | ||
| 94 | |||
| 95 | return (uint8_t)changed; | ||
| 96 | } | ||
