ugfx/src/touchscreen.c

370 lines
10 KiB
C
Raw Normal View History

2012-11-09 23:13:42 +00:00
/* 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++) {
2012-11-09 23:13:42 +00:00
results += ts_lld_read_x();
}
/* Take the average of the readings */
x = results / TOUCHSCREEN_CONVERSIONS;
2012-11-09 23:13:42 +00:00
return x;
}
static coord_t _tsReadRealY(void) {
int32_t results = 0;
int16_t i;
coord_t y;
for(i = 0; i < TOUCHSCREEN_CONVERSIONS; i++) {
2012-11-09 23:13:42 +00:00
results += ts_lld_read_y();
}
/* Take the average of the readings */
y = results / TOUCHSCREEN_CONVERSIONS;
2012-11-09 23:13:42 +00:00
return y;
}
static void _tsDrawCross(uint16_t x, uint16_t y) {
2012-11-10 17:39:00 +00:00
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);
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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));
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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));
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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));
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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));
2012-11-09 23:13:42 +00:00
}
static void _tsTransform(coord_t *x, coord_t *y) {
2012-11-10 17:39:00 +00:00
*x = (coord_t) (cal->ax * (*x) + cal->bx * (*y) + cal->cx);
*y = (coord_t) (cal->ay * (*x) + cal->by * (*y) + cal->cy);
2012-11-09 23:13:42 +00:00
}
static void _tsDo3PointCalibration(const coord_t (*cross)[2], coord_t (*points)[2], cal_t *c) {
2012-11-10 17:39:00 +00:00
float dx, dx0, dx1, dx2, dy0, dy1, dy2;
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
/* 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]));
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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]));
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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]));
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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]);
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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]));
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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]));
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
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]);
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
/* Now, calculate all the required coefficients */
c->ax = dx0 / dx;
c->bx = dx1 / dx;
c->cx = dx2 / dx;
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
c->ay = dy0 / dx;
c->by = dy1 / dx;
c->cy = dy2 / dx;
2012-11-09 23:13:42 +00:00
}
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Touchscreen Driver initialization.
2012-11-10 16:37:44 +00:00
* @note Calling this function automatically invokes a tsCalibration()
2012-11-09 23:13:42 +00:00
*
* @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
2012-11-09 23:13:42 +00:00
#endif
cal = (struct cal_t*)chHeapAlloc(NULL, sizeof(struct cal_t));
if(cal == NULL)
return;
2012-11-10 16:04:20 +00:00
tsCalibrate();
2012-11-09 23:13:42 +00:00
}
/**
* @brief Get the X-Coordinate, relative to screen zero point.
*
* @return The X position in pixels.
*
* @api
*/
coord_t tsReadX(void) {
2012-11-10 17:39:00 +00:00
coord_t x, y;
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
#if TOUCHSCREEN_XY_INVERTED
x = _tsReadRealY();
y = _tsReadRealX();
2012-11-09 23:13:42 +00:00
#else
2012-11-10 17:39:00 +00:00
x = _tsReadRealX();
y = _tsReadRealY();
2012-11-09 23:13:42 +00:00
#endif
2012-11-25 23:35:02 +00:00
_tsTransform(&x, &y);
2012-11-09 23:13:42 +00:00
switch(gdispGetOrientation()) {
case GDISP_ROTATE_0:
return x;
case GDISP_ROTATE_90:
return y;
case GDISP_ROTATE_180:
return gdispGetWidth() - x - 1;
2012-11-09 23:13:42 +00:00
case GDISP_ROTATE_270:
return gdispGetWidth() - y - 1;
2012-11-09 23:13:42 +00:00
}
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;
2012-11-10 17:39:00 +00:00
#if TOUCHSCREEN_XY_INVERTED
2012-11-09 23:13:42 +00:00
x = _tsReadRealY();
y = _tsReadRealX();
#else
x = _tsReadRealX();
y = _tsReadRealY();
#endif
2012-11-25 23:35:02 +00:00
_tsTransform(&x, &y);
2012-11-09 23:13:42 +00:00
switch(gdispGetOrientation()) {
case GDISP_ROTATE_0:
return y;
case GDISP_ROTATE_90:
return gdispGetHeight() - x - 1;
2012-11-09 23:13:42 +00:00
case GDISP_ROTATE_180:
return gdispGetHeight() - y - 1;
2012-11-09 23:13:42 +00:00
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__)
2012-11-10 15:46:02 +00:00
bool_t tsPressed(void) {
return ts_lld_pressed();
2012-11-09 23:13:42 +00:00
}
#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.
*
2012-11-10 16:37:44 +00:00
* @note You don't have to call this function manually. It gets invoked by tsInit()
*
2012-11-09 23:13:42 +00:00
* @api
*/
void tsCalibrate(void) {
2012-11-10 17:39:00 +00:00
const uint16_t height = gdispGetHeight();
const uint16_t width = gdispGetWidth();
const coord_t cross[][2] = {{(width / 4), (height / 4)},
2012-11-09 23:13:42 +00:00
{(width - (width / 4)) , (height / 4)},
{(width - (width / 4)) , (height - (height / 4))},
{(width / 2), (height / 2)}}; /* Check point */
2012-11-10 17:39:00 +00:00
coord_t points[4][2];
int32_t px, py;
uint8_t i, j;
2012-11-09 23:13:42 +00:00
#if GDISP_NEED_CONTROL
2012-11-10 17:39:00 +00:00
gdispSetOrientation(GDISP_ROTATE_0);
#endif
2012-11-10 17:39:00 +00:00
gdispClear(Blue);
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
gdispFillStringBox(0, 5, gdispGetWidth(), 30, "Calibration", &fontUI2Double, White, Blue, justifyCenter);
2012-11-09 23:13:42 +00:00
#if TOUCHSCREEN_VERIFY_CALIBRATION
calibrate:
2012-11-10 17:39:00 +00:00
for(i = 0; i < 4; i++) {
#else
2012-11-10 17:39:00 +00:00
for(i = 0; i < 3; i++) {
#endif
2012-11-10 17:39:00 +00:00
_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);
}
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
/* Apply 3 point calibration algorithm */
_tsDo3PointCalibration(cross, points, cal);
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
#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.
*/
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
/* Transform the co-ordinates */
_tpTransform(&points[3][0], &points[3][1]);
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
/* 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]);
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
if(px > 4)
goto calibrate;
#endif
2012-11-09 23:13:42 +00:00
2012-11-10 17:39:00 +00:00
/* If enabled, serialize the calibration values for storage */
#if TOUCHSCREEN_STORE_CALIBRATION
ts_store_calibration_lld(cal);
#endif
2012-11-09 23:13:42 +00:00
}
#endif /* GFX_USE_TOUCHSCREEN */
/** @} */