]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/model01/matrix.c
Usbasploader bootloader option addition (#6304)
[qmk_firmware.git] / keyboards / model01 / matrix.c
1 /* Copyright 2018 James Laird-Wah
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 <quantum.h>
17 #include <i2c_master.h>
18 #include <string.h>
19 #include "model01.h"
20
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
24
25 static matrix_row_t rows[MATRIX_ROWS];
26 #define ROWS_PER_HAND (MATRIX_ROWS / 2)
27
28 inline
29 uint8_t matrix_rows(void) {
30   return MATRIX_ROWS;
31 }
32
33 inline
34 uint8_t matrix_cols(void) {
35   return MATRIX_COLS;
36 }
37
38 static int i2c_read_hand(int hand) {
39   uint8_t buf[5];
40   i2c_status_t ret = i2c_receive(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT);
41   if (ret != I2C_STATUS_SUCCESS)
42     return 1;
43
44   if (buf[0] != TWI_REPLY_KEYDATA)
45     return 2;
46
47   int start_row = hand ? ROWS_PER_HAND : 0;
48   uint8_t *out = &rows[start_row];
49   memcpy(out, &buf[1], 4);
50   return 0;
51 }
52
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);
56   return ret;
57 }
58
59 void matrix_init(void) {
60   /* Ensure scanner power is on - else right hand will not work */
61   DDRC |= _BV(7);
62   PORTC |= _BV(7);
63
64   i2c_init();
65   i2c_set_keyscan_interval(LEFT, 2);
66   i2c_set_keyscan_interval(RIGHT, 2);
67   memset(rows, 0, sizeof(rows));
68
69   matrix_init_quantum();
70 }
71
72 uint8_t matrix_scan(void) {
73   uint8_t ret = 0;
74   ret |= i2c_read_hand(LEFT);
75   ret |= i2c_read_hand(RIGHT);
76   matrix_scan_quantum();
77   return ret;
78 }
79
80 inline
81 matrix_row_t matrix_get_row(uint8_t row) {
82   return rows[row];
83 }
84
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));
90     print("\n");
91   }
92 }
93
94 /* vim: set ts=2 sw=2 et: */