-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGT911.cpp
More file actions
241 lines (195 loc) · 4.78 KB
/
GT911.cpp
File metadata and controls
241 lines (195 loc) · 4.78 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "GT911.h"
#include <Wire.h>
#ifndef ICACHE_RAM_ATTR
#define ICACHE_RAM_ATTR
#endif
// Interrupt handling
volatile bool gt911IRQ = false;
#if defined(ESP8266)
void ICACHE_RAM_ATTR _gt911_irq_handler() {
noInterrupts();
gt911IRQ = true;
interrupts();
}
#elif defined(ESP32)
void IRAM_ATTR _gt911_irq_handler() {
noInterrupts();
gt911IRQ = true;
interrupts();
}
#else
void _gt911_irq_handler() {
noInterrupts();
gt911IRQ = true;
interrupts();
}
#endif
GT911::GT911(TwoWire *twi) : _wire(twi ? twi : &Wire) {
}
void GT911::reset() {
delay(1);
pinMode(_intPin, OUTPUT);
pinMode(_rstPin, OUTPUT);
digitalWrite(_intPin, LOW);
digitalWrite(_rstPin, LOW);
delay(11);
digitalWrite(_intPin, _addr == GT911_I2C_ADDR_28);
delayMicroseconds(110);
pinMode(_rstPin, INPUT);
delay(6);
digitalWrite(_intPin, LOW);
delay(51);
}
void GT911::i2cStart(uint16_t reg) {
_wire->beginTransmission(_addr);
_wire->write(reg >> 8);
_wire->write(reg & 0xFF);
}
bool GT911::write(uint16_t reg, uint8_t data) {
i2cStart(reg);
_wire->write(data);
return _wire->endTransmission() == 0;
}
uint8_t GT911::read(uint16_t reg) {
i2cStart(reg);
_wire->endTransmission();
_wire->requestFrom(_addr, (uint8_t)1);
while (_wire->available()) {
return _wire->read();
}
return 0;
}
bool GT911::writeBytes(uint16_t reg, uint8_t *data, uint16_t size) {
i2cStart(reg);
for (uint16_t i = 0; i < size; i++) {
_wire->write(data[i]);
}
return _wire->endTransmission() == 0;
}
bool GT911::readBytes(uint16_t reg, uint8_t *data, uint16_t size) {
i2cStart(reg);
_wire->endTransmission();
uint16_t index = 0;
while (index < size) {
uint8_t req = _min(size - index, I2C_BUFFER_LENGTH);
_wire->requestFrom(_addr, req);
while (_wire->available()) {
data[index++] = _wire->read();
}
index++;
}
return size == index - 1;
}
uint8_t GT911::calcChecksum(uint8_t *buf, uint8_t len) {
uint8_t ccsum = 0;
for (uint8_t i = 0; i < len; i++) {
ccsum += buf[i];
}
return (~ccsum) + 1;
}
uint8_t GT911::readChecksum() {
return read(GT911_REG_CHECKSUM);
}
int8_t GT911::readTouches() {
uint32_t timeout = millis() + 20;
do {
uint8_t flag = read(GT911_REG_COORD_ADDR);
if ((flag & 0x80) && ((flag & 0x0F) < GT911_MAX_CONTACTS)) {
write(GT911_REG_COORD_ADDR, 0);
return flag & 0x0F;
}
delay(1);
} while (millis() < timeout);
return 0;
}
bool GT911::readTouchPoints() {
bool result = readBytes(GT911_REG_COORD_ADDR + 1, (uint8_t*)_points, sizeof(GTPoint) * GT911_MAX_CONTACTS);
if (result && _rotation != Rotate::_0) {
for (uint8_t i = 0; i < GT911_MAX_CONTACTS; i++) {
if (_rotation == Rotate::_180) {
_points[i].x = _info.xResolution - _points[i].x;
_points[i].y = _info.yResolution - _points[i].y;
}
}
}
return result;
}
bool GT911::begin(int8_t intPin, int8_t rstPin, uint8_t addr, uint32_t clk) {
_intPin = intPin;
_rstPin = rstPin;
_addr = addr;
if (_rstPin > 0) {
delay(300);
reset();
delay(200);
}
if (intPin > 0) {
pinMode(_intPin, INPUT);
attachInterrupt(_intPin, _gt911_irq_handler, FALLING);
}
_wire->begin();
_wire->setClock(clk);
_wire->beginTransmission(_addr);
if (_wire->endTransmission() == 0) {
readInfo(); // Need to get resolution to use rotation
return true;
}
return false;
}
bool GT911::productID(uint8_t *buf, uint8_t len) {
if (len < 4) {
return false;
}
memset(buf, 0, 4);
return readBytes(GT911_REG_ID, buf, 4);
}
GTConfig* GT911::readConfig() {
readBytes(GT911_REG_CFG, (uint8_t*)&_config, sizeof(_config));
if (readChecksum() == calcChecksum((uint8_t*)&_config, sizeof(_config))) {
_configLoaded = true;
return &_config;
}
return nullptr;
}
bool GT911::writeConfig() {
uint8_t checksum = calcChecksum((uint8_t*)&_config, sizeof(_config));
if (_configLoaded && readChecksum() != checksum) { // Config is different
writeBytes(GT911_REG_CFG, (uint8_t*)&_config, sizeof(_config));
uint8_t buf[2] = { checksum, 1 };
writeBytes(GT911_REG_CHECKSUM, buf, sizeof(buf));
return true;
}
return false;
}
GTInfo* GT911::readInfo() {
readBytes(GT911_REG_DATA, (uint8_t*)&_info, sizeof(_info));
return &_info;
}
uint8_t GT911::touched(uint8_t mode) {
bool irq = false;
if (mode == GT911_MODE_INTERRUPT) {
noInterrupts();
irq = gt911IRQ;
gt911IRQ = false;
interrupts();
} else if (mode == GT911_MODE_POLLING) {
irq = true;
}
uint8_t contacts = 0;
if (irq) {
contacts = readTouches();
if (contacts > 0) {
readTouchPoints();
}
}
return contacts;
}
GTPoint GT911::getPoint(uint8_t num) {
return _points[num];
}
GTPoint *GT911::getPoints() {
return _points;
}
void GT911::setRotation(Rotate rotation) {
_rotation = rotation;
}