aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2020-05-21 17:59:56 +0100
committerGitHub <noreply@github.com>2020-05-21 17:59:56 +0100
commit205321c37740caaffbca69e626a8432fd369d20c (patch)
treee0efcd82de1bd537368be7501684529e71a73dec
parent1816ad01d0efcc987b5aa7a833331b0f9d903d4f (diff)
downloadqmk_firmware-205321c37740caaffbca69e626a8432fd369d20c.tar.gz
qmk_firmware-205321c37740caaffbca69e626a8432fd369d20c.zip
Slight speed increases for matrix scanning (#9150)
-rw-r--r--quantum/matrix.c41
-rw-r--r--quantum/split_common/matrix.c41
2 files changed, 50 insertions, 32 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 67d8af6ee..9e998508a 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -48,17 +48,22 @@ static void init_pins(void) {
48} 48}
49 49
50static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 50static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
51 matrix_row_t last_row_value = current_matrix[current_row]; 51 // Start with a clear matrix row
52 current_matrix[current_row] = 0; 52 matrix_row_t current_row_value = 0;
53 53
54 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 54 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
55 pin_t pin = direct_pins[current_row][col_index]; 55 pin_t pin = direct_pins[current_row][col_index];
56 if (pin != NO_PIN) { 56 if (pin != NO_PIN) {
57 current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); 57 current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
58 } 58 }
59 } 59 }
60 60
61 return (last_row_value != current_matrix[current_row]); 61 // If the row has changed, store the row and return the changed flag.
62 if (current_matrix[current_row] != current_row_value) {
63 current_matrix[current_row] = current_row_value;
64 return true;
65 }
66 return false;
62} 67}
63 68
64#elif defined(DIODE_DIRECTION) 69#elif defined(DIODE_DIRECTION)
@@ -84,12 +89,9 @@ static void init_pins(void) {
84 } 89 }
85} 90}
86 91
87static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 92static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
88 // Store last value of row prior to reading 93 // Start with a clear matrix row
89 matrix_row_t last_row_value = current_matrix[current_row]; 94 matrix_row_t current_row_value = 0;
90
91 // Clear data in matrix row
92 current_matrix[current_row] = 0;
93 95
94 // Select row and wait for row selecton to stabilize 96 // Select row and wait for row selecton to stabilize
95 select_row(current_row); 97 select_row(current_row);
@@ -101,13 +103,18 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
101 uint8_t pin_state = readPin(col_pins[col_index]); 103 uint8_t pin_state = readPin(col_pins[col_index]);
102 104
103 // Populate the matrix row with the state of the col pin 105 // Populate the matrix row with the state of the col pin
104 current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); 106 current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
105 } 107 }
106 108
107 // Unselect row 109 // Unselect row
108 unselect_row(current_row); 110 unselect_row(current_row);
109 111
110 return (last_row_value != current_matrix[current_row]); 112 // If the row has changed, store the row and return the changed flag.
113 if (current_matrix[current_row] != current_row_value) {
114 current_matrix[current_row] = current_row_value;
115 return true;
116 }
117 return false;
111} 118}
112 119
113# elif (DIODE_DIRECTION == ROW2COL) 120# elif (DIODE_DIRECTION == ROW2COL)
@@ -143,19 +150,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
143 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { 150 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
144 // Store last value of row prior to reading 151 // Store last value of row prior to reading
145 matrix_row_t last_row_value = current_matrix[row_index]; 152 matrix_row_t last_row_value = current_matrix[row_index];
153 matrix_row_t current_row_value = last_row_value;
146 154
147 // Check row pin state 155 // Check row pin state
148 if (readPin(row_pins[row_index]) == 0) { 156 if (readPin(row_pins[row_index]) == 0) {
149 // Pin LO, set col bit 157 // Pin LO, set col bit
150 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); 158 current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
151 } else { 159 } else {
152 // Pin HI, clear col bit 160 // Pin HI, clear col bit
153 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); 161 current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
154 } 162 }
155 163
156 // Determine if the matrix changed state 164 // Determine if the matrix changed state
157 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { 165 if ((last_row_value != current_row_value)) {
158 matrix_changed = true; 166 matrix_changed |= true;
167 current_matrix[row_index] = current_row_value;
159 } 168 }
160 } 169 }
161 170
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c
index 8b91f8ca8..27c37d4b0 100644
--- a/quantum/split_common/matrix.c
+++ b/quantum/split_common/matrix.c
@@ -65,17 +65,22 @@ static void init_pins(void) {
65} 65}
66 66
67static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 67static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
68 matrix_row_t last_row_value = current_matrix[current_row]; 68 // Start with a clear matrix row
69 current_matrix[current_row] = 0; 69 matrix_row_t current_row_value = 0;
70 70
71 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 71 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
72 pin_t pin = direct_pins[current_row][col_index]; 72 pin_t pin = direct_pins[current_row][col_index];
73 if (pin != NO_PIN) { 73 if (pin != NO_PIN) {
74 current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); 74 current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
75 } 75 }
76 } 76 }
77 77
78 return (last_row_value != current_matrix[current_row]); 78 // If the row has changed, store the row and return the changed flag.
79 if (current_matrix[current_row] != current_row_value) {
80 current_matrix[current_row] = current_row_value;
81 return true;
82 }
83 return false;
79} 84}
80 85
81#elif defined(DIODE_DIRECTION) 86#elif defined(DIODE_DIRECTION)
@@ -101,12 +106,9 @@ static void init_pins(void) {
101 } 106 }
102} 107}
103 108
104static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { 109static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
105 // Store last value of row prior to reading 110 // Start with a clear matrix row
106 matrix_row_t last_row_value = current_matrix[current_row]; 111 matrix_row_t current_row_value = 0;
107
108 // Clear data in matrix row
109 current_matrix[current_row] = 0;
110 112
111 // Select row and wait for row selecton to stabilize 113 // Select row and wait for row selecton to stabilize
112 select_row(current_row); 114 select_row(current_row);
@@ -118,13 +120,18 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
118 uint8_t pin_state = readPin(col_pins[col_index]); 120 uint8_t pin_state = readPin(col_pins[col_index]);
119 121
120 // Populate the matrix row with the state of the col pin 122 // Populate the matrix row with the state of the col pin
121 current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); 123 current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
122 } 124 }
123 125
124 // Unselect row 126 // Unselect row
125 unselect_row(current_row); 127 unselect_row(current_row);
126 128
127 return (last_row_value != current_matrix[current_row]); 129 // If the row has changed, store the row and return the changed flag.
130 if (current_matrix[current_row] != current_row_value) {
131 current_matrix[current_row] = current_row_value;
132 return true;
133 }
134 return false;
128} 135}
129 136
130# elif (DIODE_DIRECTION == ROW2COL) 137# elif (DIODE_DIRECTION == ROW2COL)
@@ -160,19 +167,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
160 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { 167 for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
161 // Store last value of row prior to reading 168 // Store last value of row prior to reading
162 matrix_row_t last_row_value = current_matrix[row_index]; 169 matrix_row_t last_row_value = current_matrix[row_index];
170 matrix_row_t current_row_value = last_row_value;
163 171
164 // Check row pin state 172 // Check row pin state
165 if (readPin(row_pins[row_index]) == 0) { 173 if (readPin(row_pins[row_index]) == 0) {
166 // Pin LO, set col bit 174 // Pin LO, set col bit
167 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); 175 current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
168 } else { 176 } else {
169 // Pin HI, clear col bit 177 // Pin HI, clear col bit
170 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); 178 current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
171 } 179 }
172 180
173 // Determine if the matrix changed state 181 // Determine if the matrix changed state
174 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { 182 if ((last_row_value != current_row_value)) {
175 matrix_changed = true; 183 matrix_changed |= true;
184 current_matrix[row_index] = current_row_value;
176 } 185 }
177 } 186 }
178 187