-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinitGT911.cpp
More file actions
358 lines (299 loc) · 6.84 KB
/
initGT911.cpp
File metadata and controls
358 lines (299 loc) · 6.84 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "initGT911.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
initGT911::initGT911(TwoWire *twi, uint8_t addr) : _wire(twi ? twi : &Wire)
{
_addr = addr;
}
void initGT911::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 initGT911::i2cStart(uint16_t reg)
{
_wire->beginTransmission(_addr);
_wire->write(reg >> 8);
_wire->write(reg & 0xFF);
}
bool initGT911::write(uint16_t reg, uint8_t data)
{
i2cStart(reg);
_wire->write(data);
return _wire->endTransmission() == 0;
}
uint8_t initGT911::read(uint16_t reg)
{
i2cStart(reg);
if (_wire->endTransmission() != 0)
{
GT911_Log("I2C read single byte: endTransmission error");
return 0;
}
uint8_t got = _wire->requestFrom((int)_addr, 1);
if (got == 0 || !_wire->available())
{
GT911_Log("I2C read single byte: no data");
return 0;
}
return _wire->read();
}
bool initGT911::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 initGT911::readBytes(uint16_t reg, uint8_t *data, uint16_t size)
{
if (size == 0)
return true;
// Start write of register pointer
i2cStart(reg);
if (_wire->endTransmission() != 0)
{
GT911_Log("readBytes I2C error: endTransmission");
return false; // I2C error
}
uint16_t index = 0;
const unsigned long overallTimeout = 2000; // ms
unsigned long startTime = millis();
while (index < size)
{
if ((millis() - startTime) > overallTimeout)
{
GT911_Log("readBytes timeout");
return false;
}
uint8_t req = (uint8_t)min<uint16_t>(size - index, I2C_BUFFER_LENGTH);
uint8_t got = _wire->requestFrom((int)_addr, (int)req);
if (got == 0)
{
// small backoff and retry once
delay(5);
yield();
got = _wire->requestFrom((int)_addr, (int)req);
if (got == 0)
{
GT911_Log("I2C read error: no data available after retry");
return false;
}
}
uint8_t readCount = 0;
while (readCount < got && index < size)
{
if (_wire->available())
{
data[index++] = _wire->read();
readCount++;
}
else
{
// unexpected early end; break to outer loop to retry remaining
break;
}
}
// small yield to avoid WDT and give bus time
delayMicroseconds(50);
yield();
}
GT911_Logf("readBytes: read %d bytes\n", index);
return index == size;
}
uint8_t initGT911::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 initGT911::readChecksum()
{
return read(GT911_REG_CHECKSUM);
}
int8_t initGT911::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 initGT911::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 initGT911::begin(int8_t intPin, int8_t rstPin, uint32_t clk)
{
_intPin = intPin;
_rstPin = rstPin;
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 initGT911::productID(uint8_t *buf, uint8_t len)
{
if (len < 4)
{
return false;
}
memset(buf, 0, 4);
return readBytes(GT911_REG_ID, buf, 4);
}
GTConfig *initGT911::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 initGT911::updateConfig()
{
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));
delay(10); // Wait for config to be applied
return true;
}
return false;
}
GTInfo *initGT911::readInfo()
{
readBytes(GT911_REG_DATA, (uint8_t *)&_info, sizeof(_info));
return &_info;
}
uint8_t initGT911::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 initGT911::getPoint(uint8_t num)
{
return _points[num];
}
GTPoint *initGT911::getPoints()
{
return _points;
}
void initGT911::setupDisplay(uint16_t xRes, uint16_t yRes, Rotate rotation)
{
_rotation = rotation;
GTConfig *cfg = readConfig();
if (cfg == nullptr)
{
GT911_Log("Config Read Error");
return;
}
cfg->hSpace = (5 | (5 << 4)); // Set horizontal space
cfg->vSpace = (5 | (5 << 4)); // Set vertical space
cfg->xResolution = xRes; // Set X resolution
cfg->yResolution = yRes; // Set Y resolution
//cfg->moduleSwitch1 &= ~(1 << 7); // Set bit 7 to 0 to invert X-axis
//cfg->moduleSwitch1 &= ~(1 << 6); // Set bit 6 to 0 to invert Y-axis
cfg->moduleSwitch1 |= (1 << 7); // Set bit 7 to 1 to invert X-axis if not in 0 rotation
cfg->moduleSwitch1 |= (1 << 6); // Set bit 6 to 1 to invert Y-axis if not in 0 rotation
updateConfig();
}