Skip to content

Commit 7221222

Browse files
committed
Support for rotation
Touch points can now be rotated. Currently only 180 degrees.
1 parent fa6aa4e commit 7221222

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

GT911.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ bool GT911::readBytes(uint16_t reg, uint8_t *data, uint16_t size) {
9898
index++;
9999
}
100100

101-
return size == index;
101+
return size == index - 1;
102102
}
103103

104104
uint8_t GT911::calcChecksum(uint8_t *buf, uint8_t len) {
@@ -129,7 +129,18 @@ int8_t GT911::readTouches() {
129129
}
130130

131131
bool GT911::readTouchPoints() {
132-
return readBytes(GT911_REG_COORD_ADDR + 1, (uint8_t*)_points, sizeof(GTPoint) * GT911_MAX_CONTACTS);
132+
bool result = readBytes(GT911_REG_COORD_ADDR + 1, (uint8_t*)_points, sizeof(GTPoint) * GT911_MAX_CONTACTS);
133+
134+
if (result && _rotation != Rotate::_0) {
135+
for (uint8_t i = 0; i < GT911_MAX_CONTACTS; i++) {
136+
if (_rotation == Rotate::_180) {
137+
_points[i].x = _info.xResolution - _points[i].x;
138+
_points[i].y = _info.yResolution - _points[i].y;
139+
}
140+
}
141+
}
142+
143+
return result;
133144
}
134145

135146
bool GT911::begin(int8_t intPin, int8_t rstPin, uint8_t addr, uint32_t clk) {
@@ -151,7 +162,11 @@ bool GT911::begin(int8_t intPin, int8_t rstPin, uint8_t addr, uint32_t clk) {
151162
_wire->setClock(clk);
152163
_wire->begin();
153164
_wire->beginTransmission(_addr);
154-
return _wire->endTransmission() == 0;
165+
if (_wire->endTransmission() == 0) {
166+
readInfo(); // Need to get resolution to use rotation
167+
return true;
168+
}
169+
return false;
155170
}
156171

157172
bool GT911::productID(uint8_t *buf, uint8_t len) {
@@ -220,3 +235,7 @@ GTPoint GT911::getPoint(uint8_t num) {
220235
GTPoint *GT911::getPoints() {
221236
return _points;
222237
}
238+
239+
void GT911::setRotation(Rotate rotation) {
240+
_rotation = rotation;
241+
}

GT911.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define GT911_MAX_CONTACTS 5
1414

15-
#define GT911_REG_CFG 0x8047
15+
#define GT911_REG_CFG 0x8047
1616
#define GT911_REG_CHECKSUM 0x80FF
1717
#define GT911_REG_DATA 0x8140
1818
#define GT911_REG_ID 0x8140
@@ -24,6 +24,13 @@ enum : uint8_t {
2424
};
2525

2626
class GT911 {
27+
public:
28+
enum class Rotate {
29+
_0,
30+
_90,
31+
_180,
32+
_270,
33+
};
2734
private:
2835
TwoWire *_wire;
2936
uint8_t _intPin;
@@ -35,6 +42,8 @@ class GT911 {
3542
GTInfo _info;
3643
GTPoint _points[GT911_MAX_CONTACTS];
3744

45+
Rotate _rotation = Rotate::_0;
46+
3847
void reset();
3948
void i2cStart(uint16_t reg);
4049
bool write(uint16_t reg, uint8_t data);
@@ -57,6 +66,8 @@ class GT911 {
5766
uint8_t touched(uint8_t mode = GT911_MODE_INTERRUPT);
5867
GTPoint getPoint(uint8_t num);
5968
GTPoint *getPoints();
69+
70+
void setRotation(Rotate rotation);
6071
};
6172

6273
#endif

library.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "GT911",
3+
"version": "0.2.0"
4+
}

0 commit comments

Comments
 (0)