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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/* ChibiOS/GFX - Copyright (C) 2012
Joel Bodenmann aka Tectu <joel@unormal.org>
This file is part of ChibiOS/GFX.
ChibiOS/GFX is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS/GFX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file src/touchscreen.c
* @brief Touchscreen Driver code.
*
* @addtogroup TOUCHSCREEN
* @{
*/
#include "ch.h"
#include "hal.h"
#include "gdisp.h"
#include "touchscreen.h"
#if GFX_USE_TOUCHSCREEN || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver local variables. */
/*===========================================================================*/
static struct cal_t *cal;
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
static coord_t _tsReadRealX(void) {
int32_t results = 0;
int16_t i;
coord_t x;
for(i = 0; i < TOUCHSCREEN_CONVERSIONS; i++) {
results += ts_lld_read_x();
}
/* Take the average of the readings */
x = results / TOUCHSCREEN_CONVERSIONS;
return x;
}
static coord_t _tsReadRealY(void) {
int32_t results = 0;
int16_t i;
coord_t y;
for(i = 0; i < TOUCHSCREEN_CONVERSIONS; i++) {
results += ts_lld_read_y();
}
/* Take the average of the readings */
y = results / TOUCHSCREEN_CONVERSIONS;
return y;
}
static void _tsDrawCross(uint16_t x, uint16_t y) {
gdispDrawLine(x-15, y, x-2, y, White);
gdispDrawLine(x+2, y, x+15, y, White);
gdispDrawLine(x, y-15, x, y-2, White);
gdispDrawLine(x, y+2, x, y+15, White);
gdispDrawLine(x-15, y+15, x-7, y+15, RGB2COLOR(184,158,131));
gdispDrawLine(x-15, y+7, x-15, y+15, RGB2COLOR(184,158,131));
gdispDrawLine(x-15, y-15, x-7, y-15, RGB2COLOR(184,158,131));
gdispDrawLine(x-15, y-7, x-15, y-15, RGB2COLOR(184,158,131));
gdispDrawLine(x+7, y+15, x+15, y+15, RGB2COLOR(184,158,131));
gdispDrawLine(x+15, y+7, x+15, y+15, RGB2COLOR(184,158,131));
gdispDrawLine(x+7, y-15, x+15, y-15, RGB2COLOR(184,158,131));
gdispDrawLine(x+15, y-15, x+15, y-7, RGB2COLOR(184,158,131));
}
static void _tsTransform(coord_t *x, coord_t *y) {
*x = (coord_t) (cal->ax * (*x) + cal->bx * (*y) + cal->cx);
*y = (coord_t) (cal->ay * (*x) + cal->by * (*y) + cal->cy);
}
static void _tsDo3PointCalibration(const coord_t (*cross)[2], coord_t (*points)[2], cal_t *c) {
float dx, dx0, dx1, dx2, dy0, dy1, dy2;
/* Compute all the required determinants */
dx = ((float)(points[0][0] - points[2][0])) * ((float)(points[1][1] - points[2][1]))
- ((float)(points[1][0] - points[2][0])) * ((float)(points[0][1] - points[2][1]));
dx0 = ((float)(cross[0][0] - cross[2][0])) * ((float)(points[1][1] - points[2][1]))
- ((float)(cross[1][0] - cross[2][0])) * ((float)(points[0][1] - points[2][1]));
dx1 = ((float)(points[0][0] - points[2][0])) * ((float)(cross[1][0] - cross[2][0]))
- ((float)(points[1][0] - points[2][0])) * ((float)(cross[0][0] - cross[2][0]));
dx2 = cross[0][0] * ((float)points[1][0] * (float)points[2][1] - (float)points[2][0] * (float)points[1][1]) -
cross[1][0] * ((float)points[0][0] * (float)points[2][1] - (float)points[2][0] * (float)points[0][1]) +
cross[2][0] * ((float)points[0][0] * (float)points[1][1] - (float)points[1][0] * (float)points[0][1]);
dy0 = ((float)(cross[0][1] - cross[2][1])) * ((float)(points[1][1] - points[2][1]))
- ((float)(cross[1][1] - cross[2][1])) * ((float)(points[0][1] - points[2][1]));
dy1 = ((float)(points[0][0] - points[2][0])) * ((float)(cross[1][1] - cross[2][1]))
- ((float)(points[1][0] - points[2][0])) * ((float)(cross[0][1] - cross[2][1]));
dy2 = cross[0][1] * ((float)points[1][0] * (float)points[2][1] - (float)points[2][0] * (float)points[1][1]) -
cross[1][1] * ((float)points[0][0] * (float)points[2][1] - (float)points[2][0] * (float)points[0][1]) +
cross[2][1] * ((float)points[0][0] * (float)points[1][1] - (float)points[1][0] * (float)points[0][1]);
/* Now, calculate all the required coefficients */
c->ax = dx0 / dx;
c->bx = dx1 / dx;
c->cx = dx2 / dx;
c->ay = dy0 / dx;
c->by = dy1 / dx;
c->cy = dy2 / dx;
}
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Touchscreen Driver initialization.
* @note Calling this function automatically invokes a tsCalibration()
*
* @param[in] ts The touchscreen driver struct
*
* @api
*/
void tsInit(const TouchscreenDriver *ts) {
/* Initialise Mutex */
//MUTEX_INIT
/* Initialise driver */
//MUTEX_ENTER
ts_lld_init(ts);
//MUTEX_EXIT
#if TOUCHSCREEN_STORE_CALIBRATION
cal = ts_restore_calibration_lld();
if(cal != NULL)
return; // All done
#endif
cal = (struct cal_t*)chHeapAlloc(NULL, sizeof(struct cal_t));
if(cal == NULL)
return;
tsCalibrate();
}
/**
* @brief Get the X-Coordinate, relative to screen zero point.
*
* @return The X position in pixels.
*
* @api
*/
coord_t tsReadX(void) {
coord_t x, y;
#if TOUCHSCREEN_XY_INVERTED
x = _tsReadRealY();
y = _tsReadRealX();
#else
x = _tsReadRealX();
y = _tsReadRealY();
#endif
//_tsTransform(&x, &y);
switch(gdispGetOrientation()) {
case GDISP_ROTATE_0:
return x;
case GDISP_ROTATE_90:
return y;
case GDISP_ROTATE_180:
return gdispGetWidth() - x - 1;
case GDISP_ROTATE_270:
return gdispGetWidth() - y - 1;
}
return 0;
}
/**
* @brief Get the X-Coordinate, relative to screen zero point.
*
* @return The Y position in pixels.
*
* @api
*/
coord_t tsReadY(void) {
coord_t x, y;
#if TOUCHSCREEN_XY_INVERTED
x = _tsReadRealY();
y = _tsReadRealX();
#else
x = _tsReadRealX();
y = _tsReadRealY();
#endif
//_tsTransform(&x, &y);
switch(gdispGetOrientation()) {
case GDISP_ROTATE_0:
return y;
case GDISP_ROTATE_90:
return gdispGetHeight() - x - 1;
case GDISP_ROTATE_180:
return gdispGetHeight() - y - 1;
case GDISP_ROTATE_270:
return x;
}
return 0;
}
/**
* @brief Get the pressure.
*
* @return The pressure.
*
* @api
*/
#if TOUCHSCREEN_HAS_PRESSURE || defined(__DOXYGEN__)
uint16_t tsReadZ(void) {
/* ToDo */
return (ts_lld_read_z());
}
#endif
/**
* @brief Returns if touchscreen is pressed or not
*
* @return TRUE if pressed, FALSE otherwise
*
* @api
*/
#if TOUCHSCREEN_HAS_PRESSED || defined(__DOXYGEN__)
bool_t tsPressed(void) {
return ts_lld_pressed();
}
#endif
/* Define maximum no. of times to sample the calibration point */
#define MAX_CAL_SAMPLES 10
/**
* @brief Function to calibrate touchscreen
* @details This function interactively performs calibration of the touchscreen
* using 3-point calibration algorithm. Optionally, it also verifies
* the accuracy of the calibration coefficients obtained if the symbol
* TOUCHSCREEN_VERIFY_CALIBRATION is defined in the configuration.
*
* @note You don't have to call this function manually. It gets invoked by tsInit()
*
* @api
*/
void tsCalibrate(void) {
#if 0
const uint16_t height = gdispGetHeight();
const uint16_t width = gdispGetWidth();
const coord_t cross[][2] = {{(width / 4), (height / 4)},
{(width - (width / 4)) , (height / 4)},
{(width - (width / 4)) , (height - (height / 4))},
{(width / 2), (height / 2)}}; /* Check point */
coord_t points[4][2];
int32_t px, py;
uint8_t i, j;
#if GDISP_NEED_CONTROL
gdispSetOrientation(GDISP_ROTATE_0);
#endif
gdispClear(Blue);
gdispFillStringBox(0, 5, gdispGetWidth(), 30, "Calibration", &fontUI2Double, White, Blue, justifyCenter);
#if TOUCHSCREEN_VERIFY_CALIBRATION
calibrate:
for(i = 0; i < 4; i++) {
#else
for(i = 0; i < 3; i++) {
#endif
_tsDrawCross(cross[i][0], cross[i][1]);
while(!tsPressed())
chThdSleepMilliseconds(2); /* Be nice to other threads*/
chThdSleepMilliseconds(20); /* Allow screen to settle */
/* Take a little more samples per point and their average
* for precise calibration */
px = py = 0;
j = 0;
while(j < MAX_CAL_SAMPLES) {
if(tsPressed()) { /* We have valid pointer data */
#if TOUCHSCREEN_XY_INVERTED
py += _tsReadRealX();
px += _tsReadRealY();
#else
px += _tsReadRealX();
py += _tsReadRealY();
#endif
j++;
}
}
points[i][0] = px / j;
points[i][1] = py / j;
chThdSleepMilliseconds(100);
while(tsPressed())
chThdSleepMilliseconds(2); /* Be nice to other threads*/
gdispFillArea(cross[i][0] - 15, cross[i][1] - 15, 42, 42, Blue);
}
/* Apply 3 point calibration algorithm */
_tsDo3PointCalibration(cross, points, cal);
#if TOUCHSCREEN_VERIFY_CALIBRATION
/* Verification of correctness of calibration (optional) :
* See if the 4th point (Middle of the screen) coincides with the calibrated
* result. If point is with +/- 2 pixel margin, then successful calibration
* Else, start from the beginning.
*/
/* Transform the co-ordinates */
_tpTransform(&points[3][0], &points[3][1]);
/* Calculate the delta */
px = (points[3][0] - cross[3][0]) * (points[3][0] - cross[3][0]) +
(points[3][1] - cross[3][1]) * (points[3][1] - cross[3][1]);
if(px > 4)
goto calibrate;
#endif
/* If enabled, serialize the calibration values for storage */
#if TOUCHSCREEN_STORE_CALIBRATION
ts_store_calibration_lld(cal);
#endif
#endif
}
#endif /* GFX_USE_TOUCHSCREEN */
/** @} */
|