-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdafruit_FT6206.cpp
More file actions
executable file
·303 lines (255 loc) · 8.57 KB
/
Adafruit_FT6206.cpp
File metadata and controls
executable file
·303 lines (255 loc) · 8.57 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
/***************************************************
This is a library for the Adafruit Capacitive Touch Screens
----> http://www.adafruit.com/products/1947
Check out the links above for our tutorials and wiring diagrams
This chipset uses I2C to communicate
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
// #include "Arduino.h"
// #include <Wire.h>
#include "Adafruit_FT6206.h"
#if defined(__SAM3X8E__)
#define Wire Wire1
#endif
//#define FT6206_DEBUG
//#define I2C_DEBUG
//
#ifndef byte
#define byte uint8_t
#endif
//I2C i2c(I2C_SDA , I2C_SCL );
I2C i2c(PB_3 , PB_10);
extern Serial pc;
/**************************************************************************/
/*!
@brief Instantiates a new FT6206 class
*/
/**************************************************************************/
// I2C, no address adjustments or pins
Adafruit_FT6206::Adafruit_FT6206() {
touches = 0;
}
/**************************************************************************/
/*!
@brief Setups the I2C interface and hardware, identifies if chip is found
@param thresh Optional threshhold-for-touch value, default is FT6206_DEFAULT_THRESSHOLD but you can try changing it if your screen is too/not sensitive.
@returns True if an FT6206 is found, false on any failure
*/
/**************************************************************************/
boolean Adafruit_FT6206::begin(uint8_t thresh) {
//Wire.begin();
#ifdef FT6206_DEBUG
pc.printf("Vend ID: 0x%02x\r\n", readRegister8(FT62XX_REG_VENDID));
pc.printf("Chip ID: 0x%02x\r\n", readRegister8(FT62XX_REG_CHIPID));
pc.printf("Firm V: %d\r\n", readRegister8(FT62XX_REG_FIRMVERS));
pc.printf("Point Rate Hz: %d\r\n", readRegister8(FT62XX_REG_POINTRATE));
pc.printf("Thresh: %d\r\n", readRegister8(FT62XX_REG_THRESHHOLD));
// dump all registers
for (int16_t i=0; i<0x10; i++) {
pc.printf("I2C $%02x = 0x%02x\r\n", i, readRegister8(i));
}
#endif
// change threshhold to be higher/lower
writeRegister8(FT62XX_REG_THRESHHOLD, thresh);
if (readRegister8(FT62XX_REG_VENDID) != FT62XX_VENDID) {
return false;
}
uint8_t id = readRegister8(FT62XX_REG_CHIPID);
if ((id != FT6206_CHIPID) && (id != FT6236_CHIPID) && (id != FT6236U_CHIPID)) {
return false;
}
return true;
}
/**************************************************************************/
/*!
@brief Determines if there are any touches detected
@returns Number of touches detected, can be 0, 1 or 2
*/
/**************************************************************************/
uint8_t Adafruit_FT6206::touched(void) {
uint8_t n = readRegister8(FT62XX_REG_NUMTOUCHES);
if (n > 2) {
n = 0;
}
return n;
}
/**************************************************************************/
/*!
@brief Queries the chip and retrieves a point data
@param n The # index (0 or 1) to the points we can detect. In theory we can detect 2 points but we've found that you should only use this for single-touch since the two points cant share the same half of the screen.
@returns {@link TS_Point} object that has the x and y coordinets set. If the z coordinate is 0 it means the point is not touched. If z is 1, it is currently touched.
*/
/**************************************************************************/
TS_Point Adafruit_FT6206::getPoint(uint8_t n) {
readData();
if ((touches == 0) || (n > 1)) {
return TS_Point(0, 0, 0);
} else {
return TS_Point(touchX[n], touchY[n], 1);
}
}
/************ lower level i/o **************/
/**************************************************************************/
/*!
@brief Reads the bulk of data from captouch chip. Fill in {@link touches}, {@link touchX}, {@link touchY} and {@link touchID} with results
*/
/**************************************************************************/
void Adafruit_FT6206::readData(void) {
uint8_t i2cdat[16];
char data = 0;
// Wire.beginTransmission(FT62XX_ADDR);
// i2c.write((byte)0);
// Wire.endTransmission();
i2c.write(FT62XX_ADDR, &data, 1);
i2c.read(FT62XX_ADDR, (char *)i2cdat, (int)sizeof(i2cdat));
#ifdef KILL
i2c.requestFrom((byte)FT62XX_ADDR, (byte)16);
for (uint8_t i=0; i<16; i++)
i2cdat[i] = i2c.read();
#endif
#ifdef FT6206_DEBUG
for (int16_t i=0; i<16; i++) {
pc.printf("I2C $%x = 0x%02x\r\n", i, i2cdat[i]);
}
#endif
touches = i2cdat[0x02];
if ((touches > 2) || (touches == 0)) {
touches = 0;
}
#ifdef FT6206_DEBUG
pc.printf("# Touches: %d\r\n", touches);
for (uint8_t i=0; i<16; i++) {
pc.printf("0x%02x ", i2cdat[i]);
}
pc.puts("");
if (i2cdat[0x01] != 0x00) {
pc.printf("Gesture #%d\r\n", i2cdat[0x01]);
}
#endif
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;
}
#ifdef FT6206_DEBUG
pc.puts("");
for (uint8_t i=0; i<touches; i++) {
pc.printf("ID #%d ", touchID[i]);
pc.printf("\t(%d, %d)\r\n", touchX[i], touchY[i]);
}
pc.puts("");
#endif
}
uint8_t Adafruit_FT6206::readRegister8(uint8_t reg) {
uint8_t x;
int retval;
// use i2c
// i2c.beginTransmission(FT62XX_ADDR);
// i2c.write((byte)reg);
// i2c.endTransmission();
retval = i2c.write(FT62XX_ADDR, (char *)®, 1);
#ifdef FT6206_DEBUG
if (retval != 0) {
pc.printf("NAK received while setting the read address to 0x%02x.\r\n", reg);
}
#endif
// i2c.requestFrom((byte)FT62XX_ADDR, (byte)1);
// x = i2c.read();
retval = i2c.read(FT62XX_ADDR, (char *)&x, 1);
#ifdef FT6206_DEBUG
if (retval != 0) {
pc.printf("NAK received while reading register 0x%02x.\r\n", reg);
}
#endif
#ifdef I2C_DEBUG
pc.printf("$"); pc.printf(reg, HEX);
pc.printf(": 0x"); pc.printf(x, HEX);
#endif
return x;
}
void Adafruit_FT6206::writeRegister8(uint8_t reg, uint8_t val) {
// use i2c
char data[2];
data[0] = reg;
data[1] = val;
i2c.write(FT62XX_ADDR, data, 2);
// i2c.beginTransmission(FT62XX_ADDR);
// i2c.write((byte)reg);
// i2c.write((byte)val);
// i2c.endTransmission();
}
/*
// DONT DO THIS - REALLY - IT DOESNT WORK
void Adafruit_FT6206::autoCalibrate(void) {
writeRegister8(FT06_REG_MODE, FT6206_REG_FACTORYMODE);
delay(100);
//pc.printf("Calibrating...");
writeRegister8(FT6206_REG_CALIBRATE, 4);
delay(300);
for (uint8_t i = 0; i < 100; i++) {
uint8_t temp;
temp = readRegister8(FT6206_REG_MODE);
pc.printf(temp, HEX);
//return to normal mode, calibration finish
if (0x0 == ((temp & 0x70) >> 4))
break;
}
delay(200);
//pc.printf("Calibrated");
delay(300);
writeRegister8(FT6206_REG_MODE, FT6206_REG_FACTORYMODE);
delay(100);
writeRegister8(FT6206_REG_CALIBRATE, 5);
delay(300);
writeRegister8(FT6206_REG_MODE, FT6206_REG_WORKMODE);
delay(300);
}
*/
/****************/
/**************************************************************************/
/*!
@brief Instantiates a new FT6206 class with x, y and z set to 0 by default
*/
/**************************************************************************/
TS_Point::TS_Point(void) {
x = y = z = 0;
}
/**************************************************************************/
/*!
@brief Instantiates a new FT6206 class with x, y and z set by params.
@param _x The X coordinate
@param _y The Y coordinate
@param _z The Z coordinate
*/
/**************************************************************************/
TS_Point::TS_Point(int16_t _x, int16_t _y, int16_t _z) {
x = _x;
y = _y;
z = _z;
}
/**************************************************************************/
/*!
@brief Simple == comparator for two TS_Point objects
@returns True if x, y and z are the same for both points, False otherwise.
*/
/**************************************************************************/
bool TS_Point::operator==(TS_Point p1) {
return ((p1.x == x) && (p1.y == y) && (p1.z == z));
}
/**************************************************************************/
/*!
@brief Simple != comparator for two TS_Point objects
@returns False if x, y and z are the same for both points, True otherwise.
*/
/**************************************************************************/
bool TS_Point::operator!=(TS_Point p1) {
return ((p1.x != x) || (p1.y != y) || (p1.z != z));
}