-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathTouchScreen.cpp
291 lines (250 loc) · 6.42 KB
/
TouchScreen.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
291
// Touch screen library with X Y and Z (pressure) readings as well
// as oversampling to avoid 'bouncing'
// (c) ladyada / adafruit
// Code under MIT License
#include "Arduino.h"
#include "pins_arduino.h"
#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include "TouchScreen.h"
// increase or decrease the touchscreen oversampling. This is a little different
// than you make think: 1 is no oversampling, whatever data we get is
// immediately returned 2 is double-sampling and we only return valid data if
// both points are the same 3+ uses insert sort to get the median value. We
// found 2 is precise yet not too slow so we suggest sticking with it!
#define NUMSAMPLES 2
TSPoint::TSPoint(void) { x = y = z = 0; }
/**
* @brief Construct a new TSPoint::TSPoint object
*
* @param x0 The point's X value
* @param y0 The point's Y value
* @param z0 The point's Z value
*/
TSPoint::TSPoint(int16_t x0, int16_t y0, int16_t z0) {
x = x0;
y = y0;
z = z0;
}
/**
* @brief Check if the current point is equivalent to another point
*
* @param p1 The other point being checked for equivalence
* @return `true` : the two points are equivalent
* `false`: the two points are **not** equivalent
*/
bool TSPoint::operator==(TSPoint p1) {
return ((p1.x == x) && (p1.y == y) && (p1.z == z));
}
/**
* @brief Check if the current point is **not** equivalent to another point
*
* @param p1 The other point being checked for equivalence
* @return `true` :the two points are **not** equivalent
* `false`: the two points are equivalent
*/
bool TSPoint::operator!=(TSPoint p1) {
return ((p1.x != x) || (p1.y != y) || (p1.z != z));
}
#if (NUMSAMPLES > 2)
static void insert_sort(int array[], uint8_t size) {
uint8_t j;
int save;
for (int i = 1; i < size; i++) {
save = array[i];
for (j = i; j >= 1 && save < array[j - 1]; j--)
array[j] = array[j - 1];
array[j] = save;
}
}
#endif
/**
* @brief Measure the X, Y, and pressure and return a TSPoint with the
* measurements
*
* @return TSPoint The measured X, Y, and Z/pressure values
*/
TSPoint TouchScreen::getPoint(void) {
int x, y, z;
int samples[NUMSAMPLES];
uint8_t i, valid;
valid = 1;
pinMode(_yp, INPUT);
pinMode(_ym, INPUT);
pinMode(_xp, OUTPUT);
pinMode(_xm, OUTPUT);
#if defined(USE_FAST_PINIO)
*xp_port |= xp_pin;
*xm_port &= ~xm_pin;
#else
digitalWrite(_xp, HIGH);
digitalWrite(_xm, LOW);
#endif
#ifdef __arm__
delayMicroseconds(20); // Fast ARM chips need to allow voltages to settle
#endif
for (i = 0; i < NUMSAMPLES; i++) {
samples[i] = analogRead(_yp);
}
#if NUMSAMPLES > 2
insert_sort(samples, NUMSAMPLES);
#endif
#if NUMSAMPLES == 2
// Allow small amount of measurement noise, because capacitive
// coupling to a TFT display's signals can induce some noise.
if (samples[0] - samples[1] < -4 || samples[0] - samples[1] > 4) {
valid = 0;
} else {
samples[1] = (samples[0] + samples[1]) >> 1; // average 2 samples
}
#endif
x = (1023 - samples[NUMSAMPLES / 2]);
pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
pinMode(_yp, OUTPUT);
pinMode(_ym, OUTPUT);
#if defined(USE_FAST_PINIO)
*ym_port &= ~ym_pin;
*yp_port |= yp_pin;
#else
digitalWrite(_ym, LOW);
digitalWrite(_yp, HIGH);
#endif
#ifdef __arm__
delayMicroseconds(20); // Fast ARM chips need to allow voltages to settle
#endif
for (i = 0; i < NUMSAMPLES; i++) {
samples[i] = analogRead(_xm);
}
#if NUMSAMPLES > 2
insert_sort(samples, NUMSAMPLES);
#endif
#if NUMSAMPLES == 2
// Allow small amount of measurement noise, because capacitive
// coupling to a TFT display's signals can induce some noise.
if (samples[0] - samples[1] < -4 || samples[0] - samples[1] > 4) {
valid = 0;
} else {
samples[1] = (samples[0] + samples[1]) >> 1; // average 2 samples
}
#endif
y = (1023 - samples[NUMSAMPLES / 2]);
// Set X+ to ground
// Set Y- to VCC
// Hi-Z X- and Y+
pinMode(_xp, OUTPUT);
pinMode(_yp, INPUT);
#if defined(USE_FAST_PINIO)
*xp_port &= ~xp_pin;
*ym_port |= ym_pin;
#else
digitalWrite(_xp, LOW);
digitalWrite(_ym, HIGH);
#endif
int z1 = analogRead(_xm);
int z2 = analogRead(_yp);
if (_rxplate != 0) {
// now read the x
float rtouch;
rtouch = z2;
rtouch /= z1;
rtouch -= 1;
rtouch *= x;
rtouch *= _rxplate;
rtouch /= 1024;
z = rtouch;
} else {
z = (1023 - (z2 - z1));
}
if (!valid) {
z = 0;
}
return TSPoint(x, y, z);
}
TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym,
uint16_t rxplate = 0) {
_yp = yp;
_xm = xm;
_ym = ym;
_xp = xp;
_rxplate = rxplate;
#if defined(USE_FAST_PINIO)
xp_port = portOutputRegister(digitalPinToPort(_xp));
yp_port = portOutputRegister(digitalPinToPort(_yp));
xm_port = portOutputRegister(digitalPinToPort(_xm));
ym_port = portOutputRegister(digitalPinToPort(_ym));
xp_pin = digitalPinToBitMask(_xp);
yp_pin = digitalPinToBitMask(_yp);
xm_pin = digitalPinToBitMask(_xm);
ym_pin = digitalPinToBitMask(_ym);
#endif
pressureThreshhold = 10;
}
/**
* @brief Read the touch event's X value
*
* @return int the X measurement
*/
int TouchScreen::readTouchX(void) {
pinMode(_yp, INPUT);
pinMode(_ym, INPUT);
digitalWrite(_yp, LOW);
digitalWrite(_ym, LOW);
pinMode(_xp, OUTPUT);
digitalWrite(_xp, HIGH);
pinMode(_xm, OUTPUT);
digitalWrite(_xm, LOW);
return (1023 - analogRead(_yp));
}
/**
* @brief Read the touch event's Y value
*
* @return int the Y measurement
*/
int TouchScreen::readTouchY(void) {
pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
digitalWrite(_xp, LOW);
digitalWrite(_xm, LOW);
pinMode(_yp, OUTPUT);
digitalWrite(_yp, HIGH);
pinMode(_ym, OUTPUT);
digitalWrite(_ym, LOW);
return (1023 - analogRead(_xm));
}
/**
* @brief Read the touch event's Z/pressure value
*
* @return int the Z measurement
*/
uint16_t TouchScreen::pressure(void) {
// Set X+ to ground
pinMode(_xp, OUTPUT);
digitalWrite(_xp, LOW);
// Set Y- to VCC
pinMode(_ym, OUTPUT);
digitalWrite(_ym, HIGH);
// Hi-Z X- and Y+
digitalWrite(_xm, LOW);
pinMode(_xm, INPUT);
digitalWrite(_yp, LOW);
pinMode(_yp, INPUT);
int z1 = analogRead(_xm);
int z2 = analogRead(_yp);
if (_rxplate != 0) {
// now read the x
float rtouch;
rtouch = z2;
rtouch /= z1;
rtouch -= 1;
rtouch *= readTouchX();
rtouch *= _rxplate;
rtouch /= 1024;
return rtouch;
} else {
return (1023 - (z2 - z1));
}
}