aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Brassel <nick@tzarc.org>2022-02-09 15:50:13 +1100
committerGitHub <noreply@github.com>2022-02-09 15:50:13 +1100
commitefdaa7f97205f8964c076677519d1848c5ac4b41 (patch)
treea9befb11625c69b06523de248fe8a6a312d5ca38
parente26778ceb572f3aa6cff7cc08479663899c32a92 (diff)
downloadqmk_firmware-efdaa7f97205f8964c076677519d1848c5ac4b41.tar.gz
qmk_firmware-efdaa7f97205f8964c076677519d1848c5ac4b41.zip
Add support for driving unselected row/col. (#16278)
-rw-r--r--docs/config_options.md2
-rw-r--r--keyboards/keychron/q2/config.h2
-rw-r--r--keyboards/keychron/q2/matrix.c139
-rw-r--r--quantum/matrix.c15
4 files changed, 19 insertions, 139 deletions
diff --git a/docs/config_options.md b/docs/config_options.md
index b661b55ee..15ad945b2 100644
--- a/docs/config_options.md
+++ b/docs/config_options.md
@@ -61,6 +61,8 @@ This is a C header file that is one of the first things included, and will persi
61 * pins unused by the keyboard for reference 61 * pins unused by the keyboard for reference
62* `#define MATRIX_HAS_GHOST` 62* `#define MATRIX_HAS_GHOST`
63 * define is matrix has ghost (unlikely) 63 * define is matrix has ghost (unlikely)
64* `#define MATRIX_UNSELECT_DRIVE_HIGH`
65 * On un-select of matrix pins, rather than setting pins to input-high, sets them to output-high.
64* `#define DIODE_DIRECTION COL2ROW` 66* `#define DIODE_DIRECTION COL2ROW`
65 * COL2ROW or ROW2COL - how your matrix is configured. COL2ROW means the black mark on your diode is facing to the rows, and between the switch and the rows. 67 * COL2ROW or ROW2COL - how your matrix is configured. COL2ROW means the black mark on your diode is facing to the rows, and between the switch and the rows.
66* `#define DIRECT_PINS { { F1, F0, B0, C7 }, { F4, F5, F6, F7 } }` 68* `#define DIRECT_PINS { { F1, F0, B0, C7 }, { F4, F5, F6, F7 } }`
diff --git a/keyboards/keychron/q2/config.h b/keyboards/keychron/q2/config.h
index 8252b83df..d019bcee1 100644
--- a/keyboards/keychron/q2/config.h
+++ b/keyboards/keychron/q2/config.h
@@ -29,6 +29,8 @@
29#define MATRIX_ROW_PINS { B4, B3, A15, A14, A13 } 29#define MATRIX_ROW_PINS { B4, B3, A15, A14, A13 }
30#define MATRIX_COL_PINS { C14, C15, A0, A1, A2, A3, A4, A5, A6, A7, B0, B1, A8, A9, H3 } 30#define MATRIX_COL_PINS { C14, C15, A0, A1, A2, A3, A4, A5, A6, A7, B0, B1, A8, A9, H3 }
31 31
32#define MATRIX_UNSELECT_DRIVE_HIGH
33
32/* DIP switch */ 34/* DIP switch */
33#define DIP_SWITCH_MATRIX_GRID { {4, 4} } 35#define DIP_SWITCH_MATRIX_GRID { {4, 4} }
34 36
diff --git a/keyboards/keychron/q2/matrix.c b/keyboards/keychron/q2/matrix.c
deleted file mode 100644
index 1e493d33e..000000000
--- a/keyboards/keychron/q2/matrix.c
+++ /dev/null
@@ -1,139 +0,0 @@
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
26static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
27#endif // MATRIX_ROW_PINS
28#ifdef MATRIX_COL_PINS
29static 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) */
35extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
36extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
37
38static inline void setPinOutput_writeLow(pin_t pin) {
39 ATOMIC_BLOCK_FORCEON {
40 setPinOutput(pin);
41 writePinLow(pin);
42 }
43}
44
45static inline void setPinOutput_writeHigh(pin_t pin) {
46 ATOMIC_BLOCK_FORCEON {
47 setPinOutput(pin);
48 writePinHigh(pin);
49 }
50}
51
52static inline void setPinInputHigh_atomic(pin_t pin) {
53 ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
54}
55
56static inline uint8_t readMatrixPin(pin_t pin) {
57 if (pin != NO_PIN) {
58 return readPin(pin);
59 } else {
60 return 1;
61 }
62}
63
64static 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
73static 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
80static void unselect_cols(void) {
81 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
82 unselect_col(x);
83 }
84}
85
86void 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
95void 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
122void matrix_init_custom(void) {
123 // initialize key pins
124 matrix_init_pins();
125}
126
127bool 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}
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 483d518ec..8596c2eab 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -82,6 +82,13 @@ static inline void setPinOutput_writeLow(pin_t pin) {
82 } 82 }
83} 83}
84 84
85static inline void setPinOutput_writeHigh(pin_t pin) {
86 ATOMIC_BLOCK_FORCEON {
87 setPinOutput(pin);
88 writePinHigh(pin);
89 }
90}
91
85static inline void setPinInputHigh_atomic(pin_t pin) { 92static inline void setPinInputHigh_atomic(pin_t pin) {
86 ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); } 93 ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); }
87} 94}
@@ -141,7 +148,11 @@ static bool select_row(uint8_t row) {
141static void unselect_row(uint8_t row) { 148static void unselect_row(uint8_t row) {
142 pin_t pin = row_pins[row]; 149 pin_t pin = row_pins[row];
143 if (pin != NO_PIN) { 150 if (pin != NO_PIN) {
151# ifdef MATRIX_UNSELECT_DRIVE_HIGH
152 setPinOutput_writeHigh(pin);
153# else
144 setPinInputHigh_atomic(pin); 154 setPinInputHigh_atomic(pin);
155# endif
145 } 156 }
146} 157}
147 158
@@ -200,7 +211,11 @@ static bool select_col(uint8_t col) {
200static void unselect_col(uint8_t col) { 211static void unselect_col(uint8_t col) {
201 pin_t pin = col_pins[col]; 212 pin_t pin = col_pins[col];
202 if (pin != NO_PIN) { 213 if (pin != NO_PIN) {
214# ifdef MATRIX_UNSELECT_DRIVE_HIGH
215 setPinOutput_writeHigh(pin);
216# else
203 setPinInputHigh_atomic(pin); 217 setPinInputHigh_atomic(pin);
218# endif
204 } 219 }
205} 220}
206 221