]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/xd84/matrix.c
Remove more commented out MCUs
[qmk_firmware.git] / keyboards / xd84 / matrix.c
1 /* Copyright 2019
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 #include <stdint.h>
17 #include <stdbool.h>
18 #include <avr/io.h>
19 #include <string.h>
20 #include "matrix.h"
21 #include "pca9555.h"
22 #include "quantum.h"
23
24 #include "debug.h"
25
26 // PCA9555 slave addresses
27 #define IC1 0x20
28 #define IC2 0x21
29
30 //_____Utility funcs___________________________________________________________
31
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
37   // init all rows - IC1 port0 input
38   pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT);//same as initial state
39 }
40
41 static void select_row(uint8_t row) {
42   // For the XD84 all rows are on the same IC and port
43   // so its safe enough to assume here row == pin
44   uint8_t pin = row;
45   uint8_t mask = 1 << pin;
46
47   pca9555_set_output(IC1, PCA9555_PORT0, ALL_HIGH & (~mask));
48   pca9555_set_config(IC1, PCA9555_PORT0, ALL_INPUT & (~mask));
49 }
50
51 static uint16_t read_cols(void) {
52   uint16_t state_1 = pca9555_readPins(IC2, PCA9555_PORT0);
53   uint16_t state_2 = pca9555_readPins(IC2, PCA9555_PORT1);
54
55   // For the XD84 all cols are on the same IC and mapped sequentially
56   // while this technically gives 16 column reads,
57   // the 16th column can never be set so is safely ignored
58   return ~((state_2 << 8) | state_1);
59 }
60
61 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
62   // Store last value of row prior to reading
63   matrix_row_t last_row_value = current_matrix[current_row];
64
65   // Clear data in matrix row
66   current_matrix[current_row] = 0;
67
68   // Select row and wait for row selecton to stabilize
69   select_row(current_row);
70   wait_us(30);
71
72   current_matrix[current_row] = read_cols();
73
74   // No need to Unselect row as the next `select_row` will blank everything
75
76   return (last_row_value != current_matrix[current_row]);
77 }
78
79 //_____CUSTOM MATRIX IMPLEMENTATION____________________________________________________
80
81 void custom_matrix_init(void) {
82   pca9555_init(IC1);
83   pca9555_init(IC2);
84
85   init_pins();
86 }
87
88 bool custom_matrix_scan(matrix_row_t current_matrix[]) {
89   bool changed = false;
90   for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
91     changed |= read_cols_on_row(current_matrix, current_row);
92   }
93   return changed;
94 }