-
Notifications
You must be signed in to change notification settings - Fork 1
/
FocalTech.cpp
executable file
·210 lines (178 loc) · 4.63 KB
/
FocalTech.cpp
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
/*
This is a library for the FocalTech touchscreen controllers.
A lot of this library is originally written by Limor Fried/Ladyada.
Because Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section author Author
Written by Limor Fried/Ladyada for Adafruit Industries.
@section license License
MIT license, all text above must be included in any redistribution
*/
#include <Arduino.h>
#include "FocalTech.h"
#include <Wire.h>
/* New class. */
FocalTech::FocalTech(uint16_t width, uint16_t height)
{
touches = 0;
_touch_width = width;
_touch_height = height;
}
/* Start I2C and check if a FocalTech controller is found. */
boolean FocalTech::begin(uint8_t thresh, int8_t sda, int8_t scl)
{
// Begin I2C
#if defined(ESP32)
if (sda != -1 && scl != -1)
{
Wire.begin(sda, scl);
}
else
{
Wire.begin();
}
#else
Wire.begin();
#endif // defined(ESP32)
// Check if our chip has the correct Vendor ID
uint8_t vendid = readRegister8(FT_REG_VENDID);
if ((vendid != FT6236_VENDID) && (vendid != FT6234_VENVID) && (vendid != FT5436_VENDID))
{
return false;
}
// Check if our chip has the one of the correct Chip ID's.
uint8_t id = readRegister8(FT_REG_CHIPID);
if ((id != FT6236_CHIPID) && (id != FT6236U_CHIPID) &&
(id != FT6206_CHIPID) && (id != FT6234_CHIPID) && (id != FT5436_CHIPID))
{
return false;
}
// Adjust threshold
writeRegister8(FT_REG_THRESHHOLD, thresh);
return true;
}
void FocalTech::setRotation(uint8_t rotation)
{
_rotation = rotation;
}
/* Returns the number of touches */
uint8_t FocalTech::touched(void)
{
uint8_t n = readRegister8(FT_REG_NUMTOUCHES);
if (n > 2)
{
n = 0;
}
return n;
}
/* Get a touch point */
TS_Point FocalTech::getPoint(uint8_t n)
{
readData();
if ((touches == 0) || (n > 1))
{
return TS_Point(0, 0, 0, _touch_width, _touch_height, _rotation);
}
else
{
return TS_Point(touchX[n], touchY[n], 1, _touch_width, _touch_height, _rotation);
}
}
void FocalTech::readData(void)
{
uint8_t i2cdat[16];
Wire.beginTransmission(FT_ADDR);
Wire.write((byte)0);
Wire.endTransmission();
Wire.requestFrom((byte)FT_ADDR, (byte)16);
for (uint8_t i = 0; i < 16; i++)
i2cdat[i] = Wire.read();
touches = i2cdat[0x02];
if ((touches > 2) || (touches == 0))
{
touches = 0;
}
for (uint8_t i = 0; i < 2; i++)
{
touchX[i] = i2cdat[0x03 + i * 6] & 0x0F;
touchX[i] <<= 8;
touchX[i] |= i2cdat[0x04 + i * 6];
touchY[i] = i2cdat[0x05 + i * 6] & 0x0F;
touchY[i] <<= 8;
touchY[i] |= i2cdat[0x06 + i * 6];
touchID[i] = i2cdat[0x05 + i * 6] >> 4;
}
}
/* Reading a byte from a register */
uint8_t FocalTech::readRegister8(uint8_t reg)
{
uint8_t x;
Wire.beginTransmission(FT_ADDR);
Wire.write((byte)reg);
Wire.endTransmission();
Wire.requestFrom((byte)FT_ADDR, (byte)1);
x = Wire.read();
return x;
}
/* Writing a byte to a register */
void FocalTech::writeRegister8(uint8_t reg, uint8_t val)
{
Wire.beginTransmission(FT_ADDR);
Wire.write((byte)reg);
Wire.write((byte)val);
Wire.endTransmission();
}
/* Debug */
void FocalTech::debug(void)
{
Serial.print("Vend ID: 0x");
Serial.println(readRegister8(FT_REG_VENDID), HEX);
Serial.print("Chip ID: 0x");
Serial.println(readRegister8(FT_REG_CHIPID), HEX);
Serial.print("Firm V: ");
Serial.println(readRegister8(FT_REG_FIRMVERS));
Serial.print("Point Rate Hz: ");
Serial.println(readRegister8(FT_REG_POINTRATE));
Serial.print("Thresh: ");
Serial.println(readRegister8(FT_REG_THRESHHOLD));
}
TS_Point::TS_Point(void) { x = y = z = 0; }
TS_Point::TS_Point(int16_t _x, int16_t _y, int16_t _z, uint16_t width, uint16_t height, uint8_t rotation)
{
uint16_t t;
switch (rotation)
{
case 0:
_x = height - _x;
_y = width - _y;
break;
case 1:
t = _x;
_x = _y;
_y = t;
_x = width - _x;
break;
case 2:
break;
case 3:
t = _x;
_x = _y;
_y = t;
_y = height - _y;
break;
}
x = _x;
y = _y;
z = _z;
}
/* == comparator between two points */
bool TS_Point::operator==(TS_Point p1)
{
return ((p1.x == x) && (p1.y == y) && (p1.z == z));
}
/* != comparator netween two points */
bool TS_Point::operator!=(TS_Point p1)
{
return ((p1.x != x) || (p1.y != y) || (p1.z != z));
}