2 Copyright 2019 worthlessowl
4 Jun Wako <wakojun@gmail.com>
5 Cole Markham <cole@ccmcomputing.net>
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #if (MATRIX_COLS <= 8)
34 # define print_matrix_header() print("\nr/c 01234567\n")
35 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
36 # define matrix_bitpop(i) bitpop(matrix[i])
37 # define ROW_SHIFTER ((uint8_t)1)
38 #elif (MATRIX_COLS <= 16)
39 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
40 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
41 # define matrix_bitpop(i) bitpop16(matrix[i])
42 # define ROW_SHIFTER ((uint16_t)1)
43 #elif (MATRIX_COLS <= 32)
44 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
45 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
46 # define matrix_bitpop(i) bitpop32(matrix[i])
47 # define ROW_SHIFTER ((uint32_t)1)
50 static const uint8_t row_pins
[MATRIX_ROWS
] = MATRIX_ROW_PINS
;
51 static const uint8_t col_select_pins
[3] = MATRIX_COL_SELECT_PINS
;
52 static const uint8_t dat_pin
= MATRIX_COL_DATA_PIN
;
54 /* matrix state(1:on, 0:off) */
55 static matrix_row_t raw_matrix
[MATRIX_ROWS
]; //raw values
56 static matrix_row_t matrix
[MATRIX_ROWS
]; //raw values
58 /* 2d array containing binary representation of its index */
59 static const uint8_t num_in_binary
[8][3] = {
70 static void select_col_analog(uint8_t col
);
71 static void mux_pin_control(const uint8_t binary
[]);
72 void debounce_init(uint8_t num_rows
);
73 void debounce(matrix_row_t raw
[], matrix_row_t cooked
[], uint8_t num_rows
, bool changed
);
76 __attribute__ ((weak
))
77 void matrix_init_user(void) {}
79 __attribute__ ((weak
))
80 void matrix_scan_user(void) {}
82 __attribute__ ((weak
))
83 void matrix_init_kb(void) {
87 __attribute__ ((weak
))
88 void matrix_scan_kb(void) {
93 uint8_t matrix_rows(void)
99 uint8_t matrix_cols(void)
105 bool matrix_is_on(uint8_t row
, uint8_t col
)
107 return (matrix
[row
] & ((matrix_row_t
)1<<col
));
111 matrix_row_t
matrix_get_row(uint8_t row
)
113 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
114 // switch blocker installed and the switch is always pressed.
116 return matrix
[row
] & matrix_mask
[row
];
122 void matrix_print(void)
124 print_matrix_header();
126 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
127 phex(row
); print(": ");
128 print_matrix_row(row
);
133 uint8_t matrix_key_count(void)
136 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
137 count
+= matrix_bitpop(i
);
142 // uses standard row code
143 static void select_row(uint8_t row
)
145 setPinOutput(row_pins
[row
]);
146 writePinLow(row_pins
[row
]);
149 static void unselect_row(uint8_t row
)
151 setPinInputHigh(row_pins
[row
]);
154 static void unselect_rows(void)
156 for(uint8_t x
= 0; x
< MATRIX_ROWS
; x
++) {
157 setPinInputHigh(row_pins
[x
]);
161 static void init_pins(void) { // still need some fixing, this might not work
162 unselect_rows(); // with the loop
164 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
165 setPinInputHigh(col_pins[x]);
168 setPinInputHigh(dat_pin
);
171 static bool read_cols_on_row(matrix_row_t current_matrix
[], uint8_t current_row
)
173 // Store last value of row prior to reading
174 matrix_row_t last_row_value
= current_matrix
[current_row
];
176 // Clear data in matrix row
177 current_matrix
[current_row
] = 0;
179 // Select row and wait for row selecton to stabilize
180 select_row(current_row
);
184 for(uint8_t col_index
= 0; col_index
< MATRIX_COLS
; col_index
++) {
186 // Select the col pin to read (active low)
187 select_col_analog(col_index
);
189 uint8_t pin_state
= readPin(dat_pin
);
191 // Populate the matrix row with the state of the col pin
192 current_matrix
[current_row
] |= pin_state
? 0 : (ROW_SHIFTER
<< col_index
);
196 unselect_row(current_row
);
198 return (last_row_value
!= current_matrix
[current_row
]);
202 void matrix_init(void) {
204 // initialize key pins
207 // initialize matrix state: all keys off
208 for (uint8_t i
=0; i
< MATRIX_ROWS
; i
++) {
213 debounce_init(MATRIX_ROWS
);
215 matrix_init_quantum();
224 // modified for per col read matrix scan
225 uint8_t matrix_scan(void)
227 bool changed
= false;
229 for (uint8_t current_row
= 0; current_row
< MATRIX_ROWS
; current_row
++) {
230 changed
|= read_cols_on_row(raw_matrix
, current_row
);
233 debounce(raw_matrix
, matrix
, MATRIX_ROWS
, changed
);
235 matrix_scan_quantum();
236 return (uint8_t)changed
;
240 uint8_t matrix_scan(void)
242 bool changed = false;
244 #if (DIODE_DIRECTION == COL2ROW)
245 // Set row, read cols
246 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
247 changed |= read_cols_on_row(raw_matrix, current_row);
251 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
253 matrix_scan_quantum();
254 return (uint8_t)changed;
258 static void select_col_analog(uint8_t col
) {
262 mux_pin_control(num_in_binary
[0]);
265 mux_pin_control(num_in_binary
[1]);
268 mux_pin_control(num_in_binary
[2]);
271 mux_pin_control(num_in_binary
[3]);
274 mux_pin_control(num_in_binary
[4]);
277 mux_pin_control(num_in_binary
[5]);
280 mux_pin_control(num_in_binary
[6]);
283 mux_pin_control(num_in_binary
[7]);
290 static void mux_pin_control(const uint8_t binary
[]) {
292 setPinOutput(col_select_pins
[0]);
294 writePinLow(col_select_pins
[0]);
297 writePinHigh(col_select_pins
[0]);
300 setPinOutput(col_select_pins
[1]);
302 writePinLow(col_select_pins
[1]);
305 writePinHigh(col_select_pins
[1]);
308 setPinOutput(col_select_pins
[2]);
310 writePinLow(col_select_pins
[2]);
313 writePinHigh(col_select_pins
[2]);