aboutsummaryrefslogtreecommitdiff
path: root/keyboards/hub16/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/hub16/matrix.c')
-rw-r--r--keyboards/hub16/matrix.c166
1 files changed, 14 insertions, 152 deletions
diff --git a/keyboards/hub16/matrix.c b/keyboards/hub16/matrix.c
index a0d8314de..4f32070e6 100644
--- a/keyboards/hub16/matrix.c
+++ b/keyboards/hub16/matrix.c
@@ -19,7 +19,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
19#include "wait.h" 19#include "wait.h"
20#include "util.h" 20#include "util.h"
21#include "matrix.h" 21#include "matrix.h"
22#include "debounce.h"
23#include "quantum.h" 22#include "quantum.h"
24 23
25// Encoder things 24// Encoder things
@@ -27,65 +26,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
27#define SWITCH_2 D7 26#define SWITCH_2 D7
28static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row); 27static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row);
29 28
30#ifdef MATRIX_MASKED
31extern const matrix_row_t matrix_mask[];
32#endif
33
34#ifdef DIRECT_PINS
35static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
36#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
37static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 29static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
38static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 30static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
39#endif
40 31
41/* matrix state(1:on, 0:off) */ 32/* matrix state(1:on, 0:off) */
42static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values 33extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
43static matrix_row_t matrix[MATRIX_ROWS]; // debounced values 34extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
44
45// helper functions
46
47inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
48
49inline matrix_row_t matrix_get_row(uint8_t row) {
50 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
51 // switch blocker installed and the switch is always pressed.
52#ifdef MATRIX_MASKED
53 return matrix[row] & matrix_mask[row];
54#else
55 return matrix[row];
56#endif
57}
58
59// matrix code
60
61#ifdef DIRECT_PINS
62
63static void init_pins(void) {
64 for (int row = 0; row < MATRIX_ROWS; row++) {
65 for (int col = 0; col < MATRIX_COLS; col++) {
66 pin_t pin = direct_pins[row][col];
67 if (pin != NO_PIN) {
68 setPinInputHigh(pin);
69 }
70 }
71 }
72}
73
74static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
75 matrix_row_t last_row_value = current_matrix[current_row];
76 current_matrix[current_row] = 0;
77
78 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
79 pin_t pin = direct_pins[current_row][col_index];
80 if (pin != NO_PIN) {
81 current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
82 }
83 }
84
85 return (last_row_value != current_matrix[current_row]);
86}
87
88#elif (DIODE_DIRECTION == COL2ROW)
89 35
90static void select_row(uint8_t row) { 36static void select_row(uint8_t row) {
91 setPinOutput(row_pins[row]); 37 setPinOutput(row_pins[row]);
@@ -133,112 +79,28 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
133 return (last_row_value != current_matrix[current_row]); 79 return (last_row_value != current_matrix[current_row]);
134} 80}
135 81
136#elif (DIODE_DIRECTION == ROW2COL)
137
138static void select_col(uint8_t col) {
139 setPinOutput(col_pins[col]);
140 writePinLow(col_pins[col]);
141}
142
143static void unselect_col(uint8_t col) { setPinInputHigh(col_pins[col]); }
144
145static void unselect_cols(void) {
146 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
147 setPinInputHigh(col_pins[x]);
148 }
149}
150
151static void init_pins(void) {
152 unselect_cols();
153 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
154 setPinInputHigh(row_pins[x]);
155 }
156}
157
158static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
159 bool matrix_changed = false;
160
161 // Select col and wait for col selecton to stabilize
162 select_col(current_col);
163 wait_us(30);
164
165 // For each row...
166 for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
167 // Store last value of row prior to reading
168 matrix_row_t last_row_value = current_matrix[row_index];
169
170 // Check row pin state
171 if (readPin(row_pins[row_index]) == 0) {
172 // Pin LO, set col bit
173 current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
174 } else {
175 // Pin HI, clear col bit
176 current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
177 }
178
179 // Determine if the matrix changed state
180 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
181 matrix_changed = true;
182 }
183 }
184
185 // Unselect col
186 unselect_col(current_col);
187
188 return matrix_changed;
189}
190
191#endif
192 82
193void matrix_init(void) { 83void matrix_init_custom(void) {
194 // initialize key pins 84 // initialize key pins
85 setPinInput(SWITCH_1);
86 setPinInput(SWITCH_2);
195 init_pins(); 87 init_pins();
196
197 // initialize matrix state: all keys off
198 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
199 raw_matrix[i] = 0;
200 matrix[i] = 0;
201 }
202
203 debounce_init(MATRIX_ROWS);
204
205 matrix_init_quantum();
206} 88}
207 89
208uint8_t matrix_scan(void) { 90bool matrix_scan_custom(void) {
209 bool changed = false; 91 bool changed = false;
210 92
211#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
212 // Set row, read cols 93 // Set row, read cols
213 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { 94 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
214 changed |= read_cols_on_row(raw_matrix, current_row); 95 changed |= read_cols_on_row(raw_matrix, current_row);
215 } 96 }
216#elif (DIODE_DIRECTION == ROW2COL)
217 // Set col, read rows
218 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
219 changed |= read_rows_on_col(raw_matrix, current_col);
220 }
221#endif
222
223 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
224 97
225 // Read encoder switches, already debounced 98 // Read encoder switches, already debounced
226 changed |= read_encoder_switches(matrix, 4); 99 changed |= read_encoder_switches(matrix, 4);
227 100
228 matrix_scan_quantum(); 101 return changed;
229 return (uint8_t)changed;
230} 102}
231 103
232// Customisations for the encoders
233void matrix_init_kb(void) {
234 setPinInput(SWITCH_1);
235 setPinInput(SWITCH_2);
236}
237
238void matrix_scan_kb(void) {}
239
240void matrix_print(void) {}
241
242static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row) { 104static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row) {
243 // Store last value of row prior to reading 105 // Store last value of row prior to reading
244 matrix_row_t last_row_value = current_matrix[current_row]; 106 matrix_row_t last_row_value = current_matrix[current_row];
@@ -249,18 +111,18 @@ static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current
249 // Debounce the encoder buttons using a shift register 111 // Debounce the encoder buttons using a shift register
250 static uint8_t btn_1_array; 112 static uint8_t btn_1_array;
251 static uint8_t btn_2_array; 113 static uint8_t btn_2_array;
252 bool btn_1_pressed = 0; 114 bool btn_1_rise = 0;
253 bool btn_2_pressed = 0; 115 bool btn_2_rise = 0;
254 btn_1_array <<= 1; 116 btn_1_array <<= 1;
255 btn_2_array <<= 1; 117 btn_2_array <<= 1;
256 btn_1_array |= readPin(SWITCH_1); 118 btn_1_array |= readPin(SWITCH_1);
257 btn_2_array |= readPin(SWITCH_2); 119 btn_2_array |= readPin(SWITCH_2);
258 (btn_1_array == 0b11111111) ? (btn_1_pressed = 1) : (btn_1_pressed = 0); 120 (btn_1_array == 0b01111111) ? (btn_1_rise = 1) : (btn_1_rise = 0);
259 (btn_2_array == 0b11111111) ? (btn_2_pressed = 1) : (btn_2_pressed = 0); 121 (btn_2_array == 0b01111111) ? (btn_2_rise = 1) : (btn_2_rise = 0);
260 122
261 // Populate the matrix row with the state of the encoder 123 // Populate the matrix row with the state of the encoder
262 current_matrix[current_row] |= btn_1_pressed ? (1 << 0) : 0; 124 current_matrix[current_row] |= btn_1_rise ? (1 << 0) : 0;
263 current_matrix[current_row] |= btn_2_pressed ? (1 << 1) : 0; 125 current_matrix[current_row] |= btn_2_rise ? (1 << 1) : 0;
264 126
265 return (last_row_value != current_matrix[current_row]); 127 return (last_row_value != current_matrix[current_row]);
266} \ No newline at end of file 128}