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.
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.
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/>.
26 // PCA9555 slave addresses
30 //_____Utility funcs___________________________________________________________
32 static void init_pins(void) {
33 // init all cols high - IC2 all input
34 pca9555_set_config(IC2, PCA9555_PORT0, ALL_INPUT);//same as initial state
35 pca9555_set_config(IC2, PCA9555_PORT1, ALL_INPUT);//same as initial state
36 pca9555_set_config(IC1, PCA9555_PORT1, ALL_INPUT);//same as initial state
38 // init all rows - IC1 port0 input
39 pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT);//same as initial state
42 static void select_row(uint8_t row) {
43 // For the XD96 all rows are on the same IC and port
44 // so its safe enough to assume here row == pin
46 uint8_t mask = 1 << pin;
48 pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH & (~mask));
49 pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT & (~mask));
52 static uint32_t read_cols(void) {
53 uint32_t state_1 = pca9555_readPins(IC2, PCA9555_PORT0);
54 uint32_t state_2 = pca9555_readPins(IC2, PCA9555_PORT1);
55 uint32_t state_3 = pca9555_readPins(IC1, PCA9555_PORT1);
57 // For the XD96 the pins are mapped to port expanders as follows:
58 // all 8 pins port 0 IC2, first 6 pins port 1 IC2, first 4 pins port 1 IC1
59 uint32_t state = (((state_3 & 0b00001111) << 14) | ((state_2 & 0b00111111) << 8) | state_1);
60 return (~state) & 0b111111111111111111;
63 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
64 // Store last value of row prior to reading
65 matrix_row_t last_row_value = current_matrix[current_row];
67 // Clear data in matrix row
68 current_matrix[current_row] = 0;
70 // Select row and wait for row selecton to stabilize
71 select_row(current_row);
74 current_matrix[current_row] = read_cols();
76 // No need to Unselect row as the next `select_row` will blank everything
78 return (last_row_value != current_matrix[current_row]);
81 //_____CUSTOM MATRIX IMPLEMENTATION____________________________________________________
83 void custom_matrix_init(void) {
90 bool custom_matrix_scan(matrix_row_t current_matrix[]) {
92 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
93 changed |= read_cols_on_row(current_matrix, current_row);