1 /* Copyright 2018 James Laird-Wah
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/>.
17 #include <i2c_master.h>
21 /* If no key events have occurred, the scanners will time out on reads.
22 * So we don't want to be too permissive here. */
23 #define I2C_TIMEOUT 10
25 static matrix_row_t rows
[MATRIX_ROWS
];
26 #define ROWS_PER_HAND (MATRIX_ROWS / 2)
29 uint8_t matrix_rows(void) {
34 uint8_t matrix_cols(void) {
38 static int i2c_read_hand(int hand
) {
40 i2c_status_t ret
= i2c_receive(I2C_ADDR(hand
), buf
, sizeof(buf
), I2C_TIMEOUT
);
41 if (ret
!= I2C_STATUS_SUCCESS
)
44 if (buf
[0] != TWI_REPLY_KEYDATA
)
47 int start_row
= hand
? ROWS_PER_HAND
: 0;
48 uint8_t *out
= &rows
[start_row
];
49 memcpy(out
, &buf
[1], 4);
53 static int i2c_set_keyscan_interval(int hand
, int delay
) {
54 uint8_t buf
[] = {TWI_CMD_KEYSCAN_INTERVAL
, delay
};
55 i2c_status_t ret
= i2c_transmit(I2C_ADDR(hand
), buf
, sizeof(buf
), I2C_TIMEOUT
);
59 void matrix_init(void) {
60 /* Ensure scanner power is on - else right hand will not work */
65 i2c_set_keyscan_interval(LEFT
, 2);
66 i2c_set_keyscan_interval(RIGHT
, 2);
67 memset(rows
, 0, sizeof(rows
));
69 matrix_init_quantum();
72 uint8_t matrix_scan(void) {
74 ret
|= i2c_read_hand(LEFT
);
75 ret
|= i2c_read_hand(RIGHT
);
76 matrix_scan_quantum();
81 matrix_row_t
matrix_get_row(uint8_t row
) {
85 void matrix_print(void) {
86 print("\nr/c 0123456789ABCDEF\n");
87 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
88 phex(row
); print(": ");
89 pbin_reverse16(matrix_get_row(row
));
94 /* vim: set ts=2 sw=2 et: */