-
Notifications
You must be signed in to change notification settings - Fork 3
/
keymap.c
180 lines (141 loc) · 3.61 KB
/
keymap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include QMK_KEYBOARD_H
#include <string.h>
#include <print.h>
#include "i2c.h"
extern keymap_config_t keymap_config;
// Fillers to make layering more clear
#define _______ KC_TRNS
#define XXXXXXX KC_NO
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
// Your key map here
};
#define MTCH6102_READ_ADDR ((0x25<<1))
#define MTCH6102_REG_STAT 0x10
typedef union {
struct {
uint8_t status, x_msb, y_msb, xy_lsb, gesture;
};
uint8_t dat[5];
} mtch6102_reg_t;
enum {
GES_TAP = 0x10,
GES_DOUBLE_TAP = 0x20
} GESTURE_CODE;
enum {
TOUCH = 1<<0,
GESTURE= 1<<1,
} MTCH6102_STAT_BIT;
/** https://github.com/swharden/AVR-projects/tree/master/ATMega328%202017-02-08%20i2c%20LM75A%20thermometer */
uint8_t i2c_read_ack(void)
{
// start TWI module and acknowledge data after reception
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
// wait for end of transmission
while( !(TWCR & (1<<TWINT)) );
// return received data from TWDR
return TWDR;
}
uint8_t i2c_read_nack(void)
{
// start receiving without acknowledging reception
TWCR = (1<<TWINT) | (1<<TWEN);
// wait for end of transmission
while( !(TWCR & (1<<TWINT)) );
// return received data from TWDR
return TWDR;
}
uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
{
if (i2c_master_start(address | I2C_WRITE)) return 1;
for (uint16_t i = 0; i < length; i++)
{
if (i2c_master_write(data[i])) return 1;
}
i2c_master_stop();
return 0;
}
uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
{
if (i2c_master_start(address | I2C_READ)) return 1;
for (uint16_t i = 0; i < (length-1); i++)
{
data[i] = i2c_read_ack();
}
data[(length-1)] = i2c_read_nack();
i2c_master_stop();
return 0;
}
uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
{
if (i2c_master_start(devaddr | 0x00)) return 1;
i2c_master_write(regaddr);
for (uint16_t i = 0; i < length; i++)
{
if (i2c_master_write(data[i])) return 1;
}
i2c_master_stop();
return 0;
}
uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
{
if (i2c_master_start(devaddr)) {
print("Cant master start 0\n");
return 1;
}
i2c_master_write(regaddr);
if (i2c_master_start(devaddr | 0x01)) {
// print("Cant master start 1\n");
return 1;
}
for (uint16_t i = 0; i < (length-1); i++)
{
data[i] = i2c_read_ack();
}
data[(length-1)] = i2c_read_nack();
i2c_master_stop();
return 0;
}
static int process_mtch6102(void) {
mtch6102_reg_t reg;
static uint16_t x, y;
static uint16_t x_buf, y_buf;
static bool touch_state;
static bool release_button = false;
bool send_flag = false;
report_mouse_t rep_mouse;
uint16_t x_dif, y_dif;
if (i2c_readReg(MTCH6102_READ_ADDR, MTCH6102_REG_STAT, reg.dat, sizeof(mtch6102_reg_t))) {
print("regread error\n");
return 1;
}
memset(&rep_mouse, 0, sizeof(report_mouse_t));
x = (reg.x_msb << 4) | (reg.xy_lsb >> 4);
y = (reg.y_msb << 4) | (reg.xy_lsb & 0xF);
if (touch_state && (reg.status & TOUCH)) {
x_dif = x - x_buf;
y_dif = y - y_buf;
rep_mouse.x = x_dif * -1;
rep_mouse.y = y_dif * -1;
send_flag = true;
}
if ((reg.status & GESTURE)
&& (reg.gesture == GES_TAP || reg.gesture == GES_DOUBLE_TAP)) {
rep_mouse.buttons = 1;
release_button = true;
send_flag = true;
} else if (release_button) {
rep_mouse.buttons = 0;
send_flag = true;
}
if (send_flag) {
host_get_driver()->send_mouse(&rep_mouse);
}
touch_state = reg.status & TOUCH;
x_buf = x;
y_buf = y;
return 0;
}
__attribute__((weak)) // overridable
void matrix_scan_user(void) {
process_mtch6102();
}