diff options
Diffstat (limited to 'keyboards/keychron/q2/matrix.c')
| -rw-r--r-- | keyboards/keychron/q2/matrix.c | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/keyboards/keychron/q2/matrix.c b/keyboards/keychron/q2/matrix.c new file mode 100644 index 000000000..1e493d33e --- /dev/null +++ b/keyboards/keychron/q2/matrix.c | |||
| @@ -0,0 +1,139 @@ | |||
| 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 <stdint.h> | ||
| 18 | #include <stdbool.h> | ||
| 19 | #include <string.h> | ||
| 20 | #include "util.h" | ||
| 21 | #include "matrix.h" | ||
| 22 | #include "debounce.h" | ||
| 23 | #include "quantum.h" | ||
| 24 | |||
| 25 | #ifdef MATRIX_ROW_PINS | ||
| 26 | static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
| 27 | #endif // MATRIX_ROW_PINS | ||
| 28 | #ifdef MATRIX_COL_PINS | ||
| 29 | static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 30 | #endif // MATRIX_COL_PINS | ||
| 31 | |||
| 32 | #define ROWS_PER_HAND (MATRIX_ROWS) | ||
| 33 | |||
| 34 | /* matrix state(1:on, 0:off) */ | ||
| 35 | extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values | ||
| 36 | extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values | ||
| 37 | |||
| 38 | static inline void setPinOutput_writeLow(pin_t pin) { | ||
| 39 | ATOMIC_BLOCK_FORCEON { | ||
| 40 | setPinOutput(pin); | ||
| 41 | writePinLow(pin); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | static inline void setPinOutput_writeHigh(pin_t pin) { | ||
| 46 | ATOMIC_BLOCK_FORCEON { | ||
| 47 | setPinOutput(pin); | ||
| 48 | writePinHigh(pin); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | static inline void setPinInputHigh_atomic(pin_t pin) { | ||
| 53 | ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); } | ||
| 54 | } | ||
| 55 | |||
| 56 | static inline uint8_t readMatrixPin(pin_t pin) { | ||
| 57 | if (pin != NO_PIN) { | ||
| 58 | return readPin(pin); | ||
| 59 | } else { | ||
| 60 | return 1; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | static bool select_col(uint8_t col) { | ||
| 65 | pin_t pin = col_pins[col]; | ||
| 66 | if (pin != NO_PIN) { | ||
| 67 | setPinOutput_writeLow(pin); | ||
| 68 | return true; | ||
| 69 | } | ||
| 70 | return false; | ||
| 71 | } | ||
| 72 | |||
| 73 | static void unselect_col(uint8_t col) { | ||
| 74 | pin_t pin = col_pins[col]; | ||
| 75 | if (pin != NO_PIN) { | ||
| 76 | setPinOutput_writeHigh(pin); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | static void unselect_cols(void) { | ||
| 81 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
| 82 | unselect_col(x); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | void matrix_init_pins(void) { | ||
| 87 | unselect_cols(); | ||
| 88 | for (uint8_t x = 0; x < ROWS_PER_HAND; x++) { | ||
| 89 | if (row_pins[x] != NO_PIN) { | ||
| 90 | setPinInputHigh_atomic(row_pins[x]); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { | ||
| 96 | bool key_pressed = false; | ||
| 97 | |||
| 98 | // Select col | ||
| 99 | if (!select_col(current_col)) { // select col | ||
| 100 | return; // skip NO_PIN col | ||
| 101 | } | ||
| 102 | matrix_output_select_delay(); | ||
| 103 | |||
| 104 | // For each row... | ||
| 105 | for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { | ||
| 106 | // Check row pin state | ||
| 107 | if (readMatrixPin(row_pins[row_index]) == 0) { | ||
| 108 | // Pin LO, set col bit | ||
| 109 | current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); | ||
| 110 | key_pressed = true; | ||
| 111 | } else { | ||
| 112 | // Pin HI, clear col bit | ||
| 113 | current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | // Unselect col | ||
| 118 | unselect_col(current_col); | ||
| 119 | matrix_output_unselect_delay(current_col, key_pressed); // wait for all Row signals to go HIGH | ||
| 120 | } | ||
| 121 | |||
| 122 | void matrix_init_custom(void) { | ||
| 123 | // initialize key pins | ||
| 124 | matrix_init_pins(); | ||
| 125 | } | ||
| 126 | |||
| 127 | bool matrix_scan_custom(matrix_row_t current_matrix[]) { | ||
| 128 | matrix_row_t curr_matrix[MATRIX_ROWS] = {0}; | ||
| 129 | |||
| 130 | // Set col, read rows | ||
| 131 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
| 132 | matrix_read_rows_on_col(curr_matrix, current_col); | ||
| 133 | } | ||
| 134 | |||
| 135 | bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0; | ||
| 136 | if (changed) memcpy(current_matrix, curr_matrix, sizeof(curr_matrix)); | ||
| 137 | |||
| 138 | return (uint8_t)changed; | ||
| 139 | } | ||
