-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathTFT_Touch.cpp
307 lines (264 loc) · 7.33 KB
/
TFT_Touch.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Library for 2046 controller based TFT touch screen.
Significantly modified by Rowboteer 22/11/15
Renamed TFT_Touch
See original header text at end of file
*/
#include "Arduino.h"
#include "TFT_Touch.h"
/* Constructor to initialise the GPIO */
TFT_Touch::TFT_Touch(byte CS_Pin, byte Clk_Pin, byte Din_Pin, byte Dout_Pin)
{
_CS = CS_Pin;
_Clk = Clk_Pin;
_Din = Din_Pin;
_Dout = Dout_Pin;
pinMode(_CS, OUTPUT);
pinMode(_Clk, OUTPUT);
pinMode(_Din, OUTPUT);
pinMode(_Dout, INPUT);
digitalWrite(_CS, HIGH);
digitalWrite(_Clk, LOW);
digitalWrite(_Din, LOW);
_hmin = 0;
_hmax = 4095;
_vmin = 0;
_vmax = 4095;
_hres = 320;
_vres = 240;
_xyswap = 0;
_xflip = 0;
_yflip = 0;
_axis = 0;
//_XYswap = 0;
//_Xflip = 0;
//_Yflip = 0;
}
/* Detects if the touch screen is currently pressed. Returns TRUE if pressed */
boolean TFT_Touch::Pressed(void)
{
// Get the raw contact coordinates
_xraw = _ReadAxis(_axis);
_yraw = _ReadAxis(!_axis);
// Now double check the touch is still near the initial contact point
// This helps to debounce the touch contact
// We are working with signed integers intentionally
delay(1);
if (abs(_xraw - _ReadAxis(_axis)) > _RAWERR) return false;
if (abs(_yraw - _ReadAxis(!_axis)) > _RAWERR) return false;
delay(1);
if (abs(_xraw - _ReadAxis(_axis)) > _RAWERR) return false;
if (abs(_yraw - _ReadAxis(!_axis)) > _RAWERR) return false;
/*
// Get the raw contact coordinates, temporary test to emulate a flip and rotation
_xraw = _ReadAxis(!_axis);
_yraw = 4095 - _ReadAxis(_axis);
// Now double check the touch is still near the initial contact point
// This helps to debounce the touch contact
// We are working with signed integers intentionally
delay(1);
if (abs(_xraw - _ReadAxis(!_axis)) > _RAWERR) return false;
if (abs(_yraw - (4095 - _ReadAxis(_axis))) > _RAWERR) return false;
delay(1);
if (abs(_xraw - _ReadAxis(!_axis)) > _RAWERR) return false;
if (abs(_yraw - (4095 - _ReadAxis(_axis))) > _RAWERR) return false;
*/
// Check values are in calibration range
//if((_yraw > _vmin && _yraw < _vmax) && (_xraw > _hmin && _xraw < _hmax))
if((_yraw > 0 && _yraw < 4095) && (_xraw > 0 && _xraw < 4095))
{
if (_xyswap){
_xcoord = constrain(map(_yraw, _vmin, _vmax, 30, _vres-30), 0, _vres);
_ycoord = constrain(map(_xraw, _hmin, _hmax, 30, _hres-30), 0, _hres);
if(_xflip) _xcoord = _vres - _xcoord;
if(_yflip) _ycoord = _hres - _ycoord;
}
else
{
_xcoord = constrain(map(_xraw, _hmin, _hmax, 30, _hres-30), 0, _hres);
_ycoord = constrain(map(_yraw, _vmin, _vmax, 30, _vres-30), 0, _vres);
if(_xflip) _xcoord = _hres - _xcoord;
if(_yflip) _ycoord = _vres - _ycoord;
}
return true;
}else
{
return false;
}
}
/* Reads one of the axis (XAXIS or YAXIS) raw coordinates. */
int16_t TFT_Touch::_ReadAxis(boolean Axis)
{
int Data;
digitalWrite(_CS, LOW);
if (Axis)
{
_OutputData(0x90);
}else
{
_OutputData(0xD0);
}
digitalWrite(_Clk, HIGH); digitalWrite(_Clk, LOW); //_PulseClock();
Data = _ReadData();
digitalWrite(_Clk, HIGH); digitalWrite(_Clk, LOW); //_PulseClock();
digitalWrite(_Clk, HIGH); digitalWrite(_Clk, LOW); //_PulseClock();
digitalWrite(_Clk, HIGH); digitalWrite(_Clk, LOW); //_PulseClock();
digitalWrite(_CS, HIGH);
digitalWrite(_Din, LOW);
return Data;
}
/* Reads the raw data from the touch screen */
uint16_t TFT_Touch::_ReadData(void)
{
byte index;
int Data;
Data = 0;
for (index = 12; index > 0; index--)
{
Data += digitalRead(_Dout) << (index-1);
digitalWrite(_Clk, HIGH); digitalWrite(_Clk, LOW); //_PulseClock();
}
return Data;
}
/* Writes to the touch screen's configuration register */
void TFT_Touch::_OutputData(byte Data)
{
byte index;
for (index = 8; index > 0; index--)
{
digitalWrite(_Din, (Data >> (index -1)) & 1);
digitalWrite(_Clk, HIGH); digitalWrite(_Clk, LOW); //_PulseClock();
}
}
/* Read the current position from the touch screen and return it as a pixel position. */
uint16_t TFT_Touch::ReadRawX(void)
{
return _ReadAxis(_axis);;
}
/* Read the current position from the touch screen and return it as a pixel position. */
uint16_t TFT_Touch::ReadRawY(void)
{
return _ReadAxis(!_axis);;
}
/* Read the last position from the touch screen and return it as a pixel position. */
uint16_t TFT_Touch::RawX(void)
{
return _xraw;
}
/* Read the last position from the touch screen and return it as a pixel position. */
uint16_t TFT_Touch::RawY(void)
{
return _yraw;
}
/* Read the last position from the touch screen and return it as a pixel position. */
uint16_t TFT_Touch::X(void)
{
return _xcoord;
}
/* Read the last position from the touch screen and return it as a pixel position. */
uint16_t TFT_Touch::Y(void)
{
return _ycoord;
}
/* Read the last zone from the touch screen and return it as a pixel position. */
uint32_t TFT_Touch::Zone(void)
{
return _xcoord + (uint32_t)_ycoord *_hres;
}
/* Set the screen resolution in pixels. */
void TFT_Touch::setResolution(uint16_t hres, uint16_t vres)
{
_hres = hres;
_vres = vres;
}
/* Set the screen calibration values */
void TFT_Touch::setCal(uint16_t hmin, uint16_t hmax,
uint16_t vmin, uint16_t vmax,
uint16_t hres, uint16_t vres,
bool axis)//, bool xflip, bool yflip)
{
_hmin = hmin;
_hmax = hmax;
_vmin = vmin;
_vmax = vmax;
_hres = hres;
_vres = vres;
_axis = axis;
//_Xflip = xflip;
//_Yflip = yflip;
}
int16_t TFT_Touch::ReadCal(byte param)
{
switch(param) {
case 1:
return _hmin;
break;
case 2:
return _hmax;
break;
case 3:
return _vmin;
break;
case 4:
return _vmax;
break;
case 5:
return _hres;
break;
case 6:
return _vres;
break;
case 7:
return _axis;
break;
//case 8:
// return _xflip;
// break;
//case 9:
// return _yflip;
// break;
}
return 0;
}
void TFT_Touch::setRotation(byte rotation)
{
switch(rotation) {
case 1:
_xyswap = 0;
_xflip = 0;//_Xflip;
_yflip = 0;//_Yflip;
break;
case 2:
_xyswap = 1;
_xflip = 0;//_Xflip;
_yflip = 1;//!_Yflip;
break;
case 3:
_xyswap = 0;
_xflip = 1;//!_Xflip;
_yflip = 1;//!_Yflip;
break;
case 0:
_xyswap = 1;
_xflip = 1;//!_Xflip;
_yflip = 0;//_Yflip;
break;
}
}
// Original header
/* FILE: HCTFT_Touch.h
DATE: 10/06/14
VERSION: 0.1
AUTHOR: Andrew Davies
Library for 2046 controller based TFT touch screen.
You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/