-
Notifications
You must be signed in to change notification settings - Fork 2
/
touch.cpp
278 lines (233 loc) · 7.93 KB
/
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
/* handle the touch screen
*/
#include "HamClock.h"
#include "calibrate.h"
#define TOUCH_HOLDT 1500 // hold time, millis()
static CAL_MATRIX touch_matrix; // touch-to-display transformation
/* insert new_value into a[] in increasing order.
* N.B. we do not check for overflowing a[]
*/
static void insertList (uint16_t a[], uint8_t n_used, uint16_t new_value)
{
while (n_used > 0 && a[n_used-1] > new_value) {
a[n_used] = a[n_used-1];
n_used -= 1;
}
a[n_used] = new_value;
}
/* read the touch screen and return raw uncalibrated coordinates.
*/
static TouchType readRawTouch (uint16_t &x, uint16_t &y)
{
// fast return if none
if (!tft.touched())
return (TT_NONE);
// sums for means until released
uint32_t xsum = 0, ysum = 0;
uint16_t nsum = 0;
// collect and determine duration until released
uint32_t t0 = millis();
while (tft.touched()) {
uint16_t tx, ty;
tft.touchRead (&tx, &ty);
xsum += tx;
ysum += ty;
nsum++;
wdDelay(10);
}
// set location from means
x = xsum/nsum;
y = ysum/nsum;
// return hold or tap
return (millis() - t0 >= TOUCH_HOLDT ? TT_HOLD : TT_TAP);
}
#if defined(_IS_ESP8266)
/* given values return from tft.touchRead(), return screen location.
* N.B. assumes calibrateTouch() has already been called.
*/
static void touch2Screen (uint16_t tx, uint16_t ty, SCoord &s)
{
CAL_POINT p, q;
p.x = tx;
p.y = ty;
getDisplayPoint (&q, &p, &touch_matrix);
s.x = q.x;
s.y = q.y;
}
#endif
/* calibrate the touch screen.
* use values from NVRAM if available unless force, and give op chance to redo.
* goal is to set touch_matrix.
*/
void calibrateTouch (bool force)
{
#if !defined(_IS_ESP8266)
return;
#endif
# define N_TAPS 5 // number of taps to average for each point
# define TAP_RAD 5 // symbol radius
# define EDGE 50U // distance from edge
# define OP_WAIT 10 // seconds to wait for op to decide
# define N_CALPTS 3 // number of calibration points
# define DBOUNCE 100 // millis() debounce
CAL_POINT display[N_CALPTS] = { // well-dispersed display coords
{EDGE, EDGE},
{tft.width()-EDGE, tft.height()/2U},
{tft.width()/2U, tft.height()-EDGE},
};
CAL_POINT touch[N_CALPTS]; // corresponding touch coords
uint16_t tx[N_TAPS], ty[N_TAPS]; // raw touch values
uint16_t counter_x;
// check whether values are already in NVRAM
bool nvok = !force &&
NVReadUInt32 (NV_TOUCH_CAL_A, &touch_matrix.An) &&
NVReadUInt32 (NV_TOUCH_CAL_B, &touch_matrix.Bn) &&
NVReadUInt32 (NV_TOUCH_CAL_C, &touch_matrix.Cn) &&
NVReadUInt32 (NV_TOUCH_CAL_D, &touch_matrix.Dn) &&
NVReadUInt32 (NV_TOUCH_CAL_E, &touch_matrix.En) &&
NVReadUInt32 (NV_TOUCH_CAL_F, &touch_matrix.Fn) &&
NVReadUInt32 (NV_TOUCH_CAL_DIV, &touch_matrix.Divider);
// set up screen, give op chance to redo calibration if already in NVRAM
tft.graphicsMode();
eraseScreen();
if (nvok) {
selectFontStyle (LIGHT_FONT, SMALL_FONT);
tft.setTextColor (RA8875_WHITE);
// give a way out
drawStringInBox ("Skip", skip_b, false, RA8875_WHITE);
// appropriate prompt
tft.setCursor (tft.width()/6, tft.height()/5);
#if defined(_IS_ESP8266)
tft.print (F("Tap anywhere to recalibrate touch screen ... "));
#else
tft.print (F("Click anywhere to recalibrate touch screen ... "));
#endif
int16_t x = tft.getCursorX();
int16_t y = tft.getCursorY();
// spin until time out or tapped
for (uint16_t s = OP_WAIT*10; !skip_skip && s > 0; --s) {
resetWatchdog();
if ((s+9)/10 != (s+10)/10) {
tft.fillRect (x, y-30, 80, 40, RA8875_BLACK);
tft.setCursor (x, y);
tft.print((s+9)/10);
}
if (tft.touched()) {
SCoord ss;
(void) readCalTouch (ss);
if (inBox(ss, skip_b))
return;
goto proceed;
}
wdDelay(100);
}
return;
}
proceed:
// collect each target
eraseScreen();
selectFontStyle (LIGHT_FONT, SMALL_FONT);
tft.setTextColor (RA8875_WHITE);
tft.setCursor (tft.width()/6, tft.height()/3);
tft.print (F("Tip: use a pencil or stylus, not your finger"));
for (uint8_t i = 0; i < N_CALPTS; i++) {
char buf[100];
resetWatchdog();
// appropriate prompt
#if defined(_IS_ESP8266)
const char *prompt = "Tap";
#else
const char *prompt = "Click";
#endif
// display touch location and instructions
tft.drawCircle (display[i].x, display[i].y, TAP_RAD, RA8875_WHITE);
if (display[i].x < tft.width()/2) {
tft.setCursor (display[i].x+20, display[i].y+10);
sprintf (buf, _FX("< %s here carefully %d times "), prompt, N_TAPS);
tft.print (buf);
counter_x = display[i].x-35;
} else {
tft.setCursor (display[i].x-365, display[i].y+10);
sprintf (buf, _FX("%s here carefully %d times > "), prompt, N_TAPS);
tft.print (buf);
counter_x = tft.getCursorX() + 35;
}
// collect N_TAPS
for (uint8_t j = 0; j < N_TAPS; j++) {
drainTouch();
uint16_t x, y;
uint32_t t0 = millis();
while (readRawTouch (x, y) == TT_NONE || millis() - t0 < DBOUNCE)
resetWatchdog();
// Serial.printf(_FX("Raw Touch:\t%4d %4d\n"), x, y);
tft.fillRect (counter_x, display[i].y-20, 20, 40, RA8875_BLACK);
tft.setCursor (counter_x, display[i].y+10);
tft.print(j+1);
insertList (tx, j, x);
insertList (ty, j, y);
}
// compute estimate as mean of center 3
touch[i].x = (tx[N_TAPS/2-1] + tx[N_TAPS/2] + tx[N_TAPS/2+1])/3;
touch[i].y = (ty[N_TAPS/2-1] + ty[N_TAPS/2] + ty[N_TAPS/2+1])/3;
}
// progress
tft.setCursor (tft.width()/3, 2*tft.height()/3);
tft.print('.');
// find conversion mapping
setCalibrationMatrix (display, touch, &touch_matrix);
tft.print('.');
// store in NVRAM
NVWriteUInt32 (NV_TOUCH_CAL_A, touch_matrix.An);
tft.print('.');
NVWriteUInt32 (NV_TOUCH_CAL_B, touch_matrix.Bn);
tft.print('.');
NVWriteUInt32 (NV_TOUCH_CAL_C, touch_matrix.Cn);
tft.print('.');
NVWriteUInt32 (NV_TOUCH_CAL_D, touch_matrix.Dn);
tft.print('.');
NVWriteUInt32 (NV_TOUCH_CAL_E, touch_matrix.En);
tft.print('.');
NVWriteUInt32 (NV_TOUCH_CAL_F, touch_matrix.Fn);
tft.print('.');
NVWriteUInt32 (NV_TOUCH_CAL_DIV, touch_matrix.Divider);
}
/* read the touch screen and return calibrated screen coordinate.
*/
TouchType readCalTouch (SCoord &s)
{
// fast return if none
if (!tft.touched())
return (TT_NONE);
// read raw
uint16_t x = 0, y = 0;
TouchType tt = readRawTouch (x, y);
#if defined(_IS_ESP8266)
// convert to screen coords via calibration matrix
touch2Screen (x, y, s);
#else
// no mapping required
s.x = x;
s.y = y;
#endif
Serial.printf(_FX("Touch:\t%4d %4d\ttype %d\n"), s.x, s.y, (int)tt);
// return hold or tap
return (tt);
}
/* wait for no touch events, need time also since the resistance film seems to be sticky
*/
void drainTouch()
{
#if defined(_IS_ESP8266)
resetWatchdog();
uint32_t t0 = millis();
bool touched = false;
while (millis() - t0 < 100 || touched) {
if ((touched = tft.touched()) == true) {
uint16_t tx, ty;
tft.touchRead (&tx, &ty);
}
}
// Serial.println (F("Drain complete"));
resetWatchdog();
#endif // defined(_IS_ESP8266)
}