-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathURTouch.cpp
266 lines (231 loc) · 5.85 KB
/
URTouch.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
/*
URTouch.cpp - Arduino/chipKit library support for Color TFT LCD Touch screens
Copyright (C)2016 Rinky-Dink Electronics, Henning Karlsen. All right reserved
Basic functionality of this library are based on the demo-code provided by
ITead studio.
You can find the latest version of the library at
http://www.RinkyDinkElectronics.com/
This library is free software; you can redistribute it and/or
modify it under the terms of the CC BY-NC-SA 3.0 license.
Please see the included documents for further information.
Commercial use of this library requires you to buy a license that
will allow commercial use. This includes using the library,
modified or not, as a tool to sell products.
The license applies to all part of the library including the
examples and tools supplied with the library.
*/
#include "URTouch.h"
#if defined(__AVR__)
#include "hardware/avr/HW_AVR.inc"
#elif defined(__PIC32MX__)
#include "hardware/pic32/HW_PIC32.inc"
#elif defined(__arm__)
#include "hardware/arm/HW_ARM.inc"
#endif
URTouch::URTouch(byte tclk, byte tcs, byte din, byte dout, byte irq)
{
T_CLK = tclk;
T_CS = tcs;
T_DIN = din;
T_DOUT = dout;
T_IRQ = irq;
}
void URTouch::InitTouch(byte orientation, uint32_t cal_x, uint32_t cal_y, uint32_t cal_s)
{
orient = orientation;
setCalibration(cal_x, cal_y, cal_s);
prec = 10;
P_CLK = portOutputRegister(digitalPinToPort(T_CLK));
B_CLK = digitalPinToBitMask(T_CLK);
P_CS = portOutputRegister(digitalPinToPort(T_CS));
B_CS = digitalPinToBitMask(T_CS);
P_DIN = portOutputRegister(digitalPinToPort(T_DIN));
B_DIN = digitalPinToBitMask(T_DIN);
P_DOUT = portInputRegister(digitalPinToPort(T_DOUT));
B_DOUT = digitalPinToBitMask(T_DOUT);
P_IRQ = portInputRegister(digitalPinToPort(T_IRQ));
B_IRQ = digitalPinToBitMask(T_IRQ);
pinMode(T_CLK, OUTPUT);
pinMode(T_CS, OUTPUT);
pinMode(T_DIN, OUTPUT);
pinMode(T_DOUT, INPUT);
pinMode(T_IRQ, OUTPUT);
sbi(P_CS, B_CS);
sbi(P_CLK, B_CLK);
sbi(P_DIN, B_DIN);
sbi(P_IRQ, B_IRQ);
}
void URTouch::read()
{
unsigned long tx=0, temp_x=0;
unsigned long ty=0, temp_y=0;
unsigned long minx=99999, maxx=0;
unsigned long miny=99999, maxy=0;
int datacount=0;
cbi(P_CS, B_CS);
pinMode(T_IRQ, INPUT);
for (int i=0; i<prec; i++)
{
if (!rbi(P_IRQ, B_IRQ))
{
touch_WriteData(0x90);
pulse_high(P_CLK, B_CLK);
temp_x=touch_ReadData();
if (!rbi(P_IRQ, B_IRQ))
{
touch_WriteData(0xD0);
pulse_high(P_CLK, B_CLK);
temp_y=touch_ReadData();
if ((temp_x>0) and (temp_x<4096) and (temp_y>0) and (temp_y<4096))
{
tx+=temp_x;
ty+=temp_y;
if (prec>5)
{
if (temp_x<minx)
minx=temp_x;
if (temp_x>maxx)
maxx=temp_x;
if (temp_y<miny)
miny=temp_y;
if (temp_y>maxy)
maxy=temp_y;
}
datacount++;
}
}
}
}
pinMode(T_IRQ, OUTPUT);
if (prec>5)
{
tx = tx-(minx+maxx);
ty = ty-(miny+maxy);
datacount -= 2;
}
sbi(P_CS, B_CS);
if ((datacount==(prec-2)) or (datacount==PREC_LOW))
{
if (orient == _default_orientation)
{
TP_X=ty/datacount;
TP_Y=tx/datacount;
}
else
{
TP_X=tx/datacount;
TP_Y=ty/datacount;
}
}
else
{
TP_X=-1;
TP_Y=-1;
}
}
bool URTouch::dataAvailable()
{
bool avail;
pinMode(T_IRQ, INPUT);
avail = !(rbi(P_IRQ, B_IRQ));
pinMode(T_IRQ, OUTPUT);
return avail;
}
int16_t URTouch::getX()
{
long c;
if ((TP_X==-1) or (TP_Y==-1))
return -1;
if (orient == _default_orientation)
{
c = long(long(TP_X - touch_x_left) * (disp_x_size)) / long(touch_x_right - touch_x_left);
if (c<0)
c = 0;
if (c>disp_x_size)
c = disp_x_size;
}
else
{
if (_default_orientation == PORTRAIT)
c = long(long(TP_X - touch_y_top) * (-disp_y_size)) / long(touch_y_bottom - touch_y_top) + long(disp_y_size);
else
c = long(long(TP_X - touch_y_top) * (disp_y_size)) / long(touch_y_bottom - touch_y_top);
if (c<0)
c = 0;
if (c>disp_y_size)
c = disp_y_size;
}
return c;
}
int16_t URTouch::getY()
{
int c;
if ((TP_X==-1) or (TP_Y==-1))
return -1;
if (orient == _default_orientation)
{
c = long(long(TP_Y - touch_y_top) * (disp_y_size)) / long(touch_y_bottom - touch_y_top);
if (c<0)
c = 0;
if (c>disp_y_size)
c = disp_y_size;
}
else
{
if (_default_orientation == PORTRAIT)
c = long(long(TP_Y - touch_x_left) * (disp_x_size)) / long(touch_x_right - touch_x_left);
else
c = long(long(TP_Y - touch_x_left) * (-disp_x_size)) / long(touch_x_right - touch_x_left) + long(disp_x_size);
if (c<0)
c = 0;
if (c>disp_x_size)
c = disp_x_size;
}
return c;
}
void URTouch::setPrecision(byte precision)
{
switch (precision)
{
case PREC_LOW:
prec=1; // DO NOT CHANGE!
break;
case PREC_MEDIUM:
prec=12; // Iterations + 2
break;
case PREC_HI:
prec=27; // Iterations + 2
break;
case PREC_EXTREME:
prec=102; // Iterations + 2
break;
default:
prec=12; // Iterations + 2
break;
}
}
void URTouch::setCalibration(uint32_t calx, uint32_t caly, uint32_t cals)
{
_default_orientation = cals>>31;
touch_x_left = (calx>>14) & 0x3FFF;
touch_x_right = calx & 0x3FFF;
touch_y_top = (caly>>14) & 0x3FFF;
touch_y_bottom = caly & 0x3FFF;
disp_x_size = (cals>>12) & 0x0FFF;
disp_y_size = cals & 0x0FFF;
}
void URTouch::calibrateRead()
{
unsigned long tx=0;
unsigned long ty=0;
cbi(P_CS, B_CS);
touch_WriteData(0x90);
pulse_high(P_CLK, B_CLK);
tx=touch_ReadData();
touch_WriteData(0xD0);
pulse_high(P_CLK, B_CLK);
ty=touch_ReadData();
sbi(P_CS, B_CS);
TP_X=ty;
TP_Y=tx;
}