forked from juj/fbcp-ili9341
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathXPT2046.cpp
290 lines (234 loc) · 7.89 KB
/
XPT2046.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
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
/**
* @file XPT2046.cpp
* @date 19.02.2016
* @author Markus Sattler
*
* Copyright (c) 2015 Markus Sattler. All rights reserved.
* This file is originally part of the XPT2046 driver for Arduino.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the “Software”), to deal in the Software without restriction,
* including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "XPT2046.h"
#include <sys/types.h>
#include <stdio.h>
#define XPT2046_CFG_START 1<<7
#define XPT2046_CFG_MUX(v) ((v&0b111) << (4))
#define XPT2046_CFG_8BIT 1<<3
#define XPT2046_CFG_12BIT (0)
#define XPT2046_CFG_SER 1<<2
#define XPT2046_CFG_DFR (0)
#define XPT2046_CFG_PWR(v) ((v&0b11))
#define XPT2046_MUX_Y 0b101
#define XPT2046_MUX_X 0b001
#define XPT2046_MUX_Z1 0b011
#define XPT2046_MUX_Z2 0b100
#define INTERRUPTDEC 10
#define MAXLINE 128
XPT2046::XPT2046() {
spi_cs = 0;
z_average = 0;
tcfifo = "/tmp/TCfifo";
calibFile = "/etc/xpt2046.conf";
_maxValue = 0x0fff;
_width = 480;
_height = 320;
_rotation = 0;
_minX = 0;
_minY = 0;
_maxX = _maxValue;
_maxY = _maxValue;
_lastX = -1;
_lastY = -1;
_minChange = 10;
interruptpoll = INTERRUPTDEC;
spi_cs =
1 << 0 | //Chip select 1
0 << 3 | //low idle clock polarity
0 << 6 | //chip select active low
0 << 22 | //chip select low polarity
0 << 8 | //DMA disabled
0 << 11; //Manual chip select
// FIFO file path
// Creating the named file(FIFO)
// mkfifo(<pathname>, <permission>)
mkfifo(tcfifo, 0666);
initCalibration();
}
XPT2046::~XPT2046() {
}
int XPT2046::SpiWriteAndRead(unsigned char *data, int length)
{
for (int i = 0; i < length; i++)
{
spi->cs = spi_cs | 1 << 7;//Set TA to high
while (!(spi->cs & (1 << 18))) ; //Poll TX Fifo until it has space
spi->fifo = data[i];
while(! (spi->cs & (1 << 17))) ; //Wait until RX FIFO contains data
uint32_t spi_fifo = spi->fifo;
spi->cs = (spi_cs & (~(1 << 7))) | 1 << 5 | 1 << 4; //Set TA to LOW //Clear Fifo
data[i] = spi_fifo;
}
return 0;
}
void XPT2046::read_touchscreen(bool interruptEnable) {
uint16_t x, y, z;
uint32_t old_spi_cs = spi->cs;
uint32_t old_spi_clk = spi->clk;
//printBits(sizeof(old_spi_cs), (void*)&(old_spi_cs));
spi->clk = 256;
// touch on low
read(&x, &y, &z);
if (abs(x - _lastX) > _minChange || abs(y - _lastY) > _minChange) {
_lastX = x;
_lastY = y;
}
if(interruptEnable) {
// SPI requires 32bit alignment
uint8_t buf[3] = {
// re-enable interrupt
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z2)| XPT2046_CFG_PWR(0)), 0x00, 0x00
};
SpiWriteAndRead(buf, 3);
}
if (z > 100)
{
uint16_t words16Write[4] = { x, y, z, 0x0000FFFF};
fd = open(tcfifo, O_WRONLY | O_NONBLOCK);
write(fd, words16Write, 4 * sizeof(uint16_t));
close(fd);
this->lastTouchTick = tick();
}
//spi->cs = (old_spi_cs | 1 << 4 | 1 << 5) & (~(1 << 7)); //Clear Fifos and TA
spi->cs = old_spi_cs;
spi->clk = old_spi_clk;
}
void XPT2046::setRotation(uint8_t m) {
_rotation = m % 4;
}
void XPT2046::initCalibration() {
FILE *stream;
char *line = NULL;
int reti;
size_t len = 0;
ssize_t nread;
stream = fopen(calibFile, "r");
if(stream != NULL)
{
while ((nread = getline(&line, &len, stream)) != -1) {
//fprintf(stdout,"scanline: '%s'", line);
reti = sscanf((const char*)line,"%d,%d,%d,%d,%d,%d,%d",
&calib.An, &calib.Bn, &calib.Cn, &calib.Dn, &calib.En, &calib.Fn,&calib.Divider);
}
free(line);
fclose(stream);
} else { // Make standard identity calibration -- no translation
// Target on-screen points
POINT pointTargets[3] = {
{15, 15},
{-15, 15},
{15, -15},
};
// Set curser values initially
// Update for screen dimensions
for(int i = 0; i<3;i++ ) {
pointTargets[i].x = (pointTargets[i].x < 0 ? _width + pointTargets[i].x : pointTargets[i].x);
pointTargets[i].y = (pointTargets[i].y < 0 ? _height + pointTargets[i].y : pointTargets[i].y);
fprintf(stdout, "%d,%d\n", pointTargets[i].x, pointTargets[i].y);
}
setCalibrationMatrix( (POINT*)pointTargets, (POINT*)pointTargets, &calib); // initialized 1:1 matrix
}
fprintf(stdout,"M: %d,%d,%d,%d,%d,%d,%d\r\n"
,calib.An,calib.Bn,calib.Cn,calib.Dn,calib.En,calib.Fn,calib.Divider );
}
void XPT2046::read(uint16_t * oX, uint16_t * oY, uint16_t * oZ) {
uint16_t x, y;
POINT pointCorrected, pointRaw;
readRaw(&x, &y, oZ);
pointRaw.x = x;
pointRaw.y = y;
if(pointRaw.x < _minX) {
pointRaw.x = 0;
} else if(pointRaw.x > _maxX) {
pointRaw.x = _width;
} else {
pointRaw.x -= _minX;
pointRaw.x = ((pointRaw.x << 8) / (((_maxX - _minX) << 8) / (_width << 8)) )>> 8;
}
if(pointRaw.y < _minY) {
pointRaw.y = 0;
} else if(pointRaw.y > _maxY) {
pointRaw.y = _height;
} else {
pointRaw.y -= _minY;
pointRaw.y = ((pointRaw.y << 8) / (((_maxY - _minY) << 8) / (_height << 8))) >> 8;
}
getDisplayPoint((POINT *)&pointCorrected,(POINT *)&pointRaw,&calib);
*oX = pointCorrected.x;
*oY = pointCorrected.y;
*oZ = std::max(0,4096 - (int)(*oZ));
}
void XPT2046::readRaw(uint16_t * oX, uint16_t * oY, uint16_t * oZ) {
uint32_t x = 0;
uint32_t y = 0;
uint32_t z1 = 0;
uint32_t z2 = 0;
uint8_t i = 0;
for(; i < 4; i++) { // Sampling
// SPI requires 32bit alignment
uint8_t buf[12] = {
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Y) | XPT2046_CFG_PWR(3)), 0x00, 0x00,
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_X) | XPT2046_CFG_PWR(3)), 0x00, 0x00,
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z1)| XPT2046_CFG_PWR(3)), 0x00, 0x00,
(XPT2046_CFG_START | XPT2046_CFG_12BIT | XPT2046_CFG_DFR | XPT2046_CFG_MUX(XPT2046_MUX_Z2)| XPT2046_CFG_PWR(3)), 0x00, 0x00
};
SpiWriteAndRead(buf, 12);
y += (buf[1] << 8 | buf[2])>>3;
x += (buf[4] << 8 | buf[5])>>3;
z1 += (buf[7] << 8 | buf[8])>>3;
z2 += (buf[10] << 8 | buf[11])>>3;
}
if(i == 0) {
*oX = 0;
*oY = 0;
*oZ = 0;
return;
}
x /= i;
y /= i;
z1 /= i;
z2 /= i;
switch(_rotation) {
case 0:
default:
break;
case 1:
x = (_maxValue - x);
y = (_maxValue - y);
break;
case 2:
y = (_maxValue - y);
break;
case 3:
x = (_maxValue - x);
break;
}
int z = z1 + _maxValue - z2;
*oX = x;
*oY = y;
*oZ = z2;
}