aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/src/touchscreen.dox26
-rw-r--r--include/touchscreen.h100
-rw-r--r--include/touchscreen_lld.h155
-rw-r--r--src/touchscreen.c374
4 files changed, 655 insertions, 0 deletions
diff --git a/docs/src/touchscreen.dox b/docs/src/touchscreen.dox
new file mode 100644
index 00000000..0a1819b9
--- /dev/null
+++ b/docs/src/touchscreen.dox
@@ -0,0 +1,26 @@
+/*
+ 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/>.
+*/
+
+/**
+ * @addtogroup TOUCHSCREEN
+ * @details The TOUCHSCREEN module provides high level abstraction to interface
+ * touchscreens.
+ */
+
diff --git a/include/touchscreen.h b/include/touchscreen.h
new file mode 100644
index 00000000..4d48e38d
--- /dev/null
+++ b/include/touchscreen.h
@@ -0,0 +1,100 @@
+/*
+ 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 include/touchscreen.h
+ * @brief TOUCHSCREEN Touchscreen driver subsystem header file.
+ *
+ * @addtogroup TOUCHSCREEN
+ * @{
+ */
+
+#ifndef TOUCHSCREEN_H
+#define TOUCHSCREEN_H
+
+#if GFX_USE_TOUCHSCREEN || defined(__DOXYGEN__)
+
+/**
+ * @brief specifies how many conversions are made for a readout.
+ *
+ * @note higher is more accurate, but takes more time
+ */
+#define CONVERSIONS 3
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Low Level Driver details and error checks. */
+/*===========================================================================*/
+
+/* Include the low level driver information */
+#include "touchscreen_lld.h"
+
+/* For definitions of coord_t, we require gdisp.h */
+#include "gdisp.h"
+
+/*===========================================================================*/
+/* Type definitions */
+/*===========================================================================*/
+
+/**
+ * @brief Struct used for calibration
+ */
+typedef struct cal_t {
+ float ax;
+ float bx;
+ float cx;
+ float ay;
+ float by;
+ float cy;
+} cal_t;
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void tsInit(const TouchscreenDriver *ts);
+coord_t tsReadX(void);
+coord_t tsReadY(void);
+void tsCalibrate(void);
+
+#if TOUCHSCREEN_HAS_IRQ
+ bool_t tsIRQ(void);
+#endif
+
+#if TOUCHSCREEN_HAS_PRESSURE
+ uint16_t tsReadZ(void);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GFX_USE_TOUCHSCREEN */
+
+#endif /* TOUCHSCREEN_H */
+/** @} */
+
diff --git a/include/touchscreen_lld.h b/include/touchscreen_lld.h
new file mode 100644
index 00000000..53c66b53
--- /dev/null
+++ b/include/touchscreen_lld.h
@@ -0,0 +1,155 @@
+/*
+ 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 include/touchscreen_lld.h
+ * @brief TOUCHSCREEN Driver subsystem low level driver header.
+ *
+ * @addtogroup TOUCHSCREEN
+ * @{
+ */
+
+#ifndef TOUCHSCREEN_LLD_H
+#define TOUCHSCREEN_LLD_H
+
+#if GFX_USE_TOUCHSCREEN || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Include the low level driver configuration information */
+/*===========================================================================*/
+
+#include "touchscreen_lld_config.h"
+
+/*===========================================================================*/
+/* Error checks. */
+/*===========================================================================*/
+
+#ifndef TOUCHSCREEN_NEED_MULTITHREAD
+ #define TOUCHSCREEN_NEED_MULTITHREAD FALSE
+#endif
+
+#ifndef TOUCHSCREEN_XY_INVERTED
+ #define TOUCHSCREEN_XY_INVERTED FALSE
+#endif
+
+#ifndef TOUCHSCREEN_STORE_CALIBRATION
+ #define TOUCHSCREEN_STORE_CALIBRATION FALSE
+#endif
+
+#ifndef TOUCHSCREEN_VERIFY_CALIBRATION
+ #define TOUCHSCREEN_VERIFY_CALIBRATION FALSE
+#endif
+
+#ifndef TOUCHSCREEN_HAS_IRQ
+ #define TOUCHSCREEN_HAS_IRQ FALSE
+#endif
+
+#ifndef TOUCHSCREEN_HAS_PRESSURE
+ #define TOUCHSCREEN_HAS_PRESSURE FALSE
+#endif
+
+#ifndef TOUCHSCREEN_SPI_PROLOGUE
+ #define TOUCHSCREEN_SPI_PROLOGUE()
+#endif
+
+#ifndef TOUCHSCREEN_SPI_EPILOGUE
+ #define TOUCHSCREEN_SPI_EPILOGUE()
+#endif
+
+/*===========================================================================*/
+/* Driver types. */
+/*===========================================================================*/
+
+/**
+ * @brief Structure representing a touchscreen driver.
+ */
+typedef struct TouchscreenDriver {
+ /*
+ * @brief Pointer to SPI driver.
+ * @note SPI driver must be enabled in mcuconf.h and halconf.h
+ */
+ SPIDriver *spip;
+
+ /*
+ * @brief Pointer to the SPI configuration structure.
+ * @note The lowest possible speed ~ 1-2MHz is to be used, otherwise
+ * will result in a lot of noise
+ */
+ const SPIConfig *spicfg;
+
+ /*
+ * @brief Touchscreen controller TPIRQ pin GPIO port
+ */
+ ioportid_t tsIRQPort;
+
+ /*
+ * @brief Touchscreen controller TPIRQ GPIO pin
+ * @note The interface is polled as of now, interrupt support is
+ * to be implemented in the future.
+ */
+ ioportmask_t tsIRQPin;
+
+ /*
+ * @brief Initialize the SPI with the configuration struct given or not
+ * If TRUE, spiStart is called by the init, otherwise not
+ * @note This is provided in such a case when SPI port is being shared
+ * across multiple peripherals, so not to disturb the SPI bus.
+ * You can use TOUCHSCREEN_SPI_PROLOGUE() and TOUCHSCREEN_SPI_EPILOGUE()
+ * macros to change the SPI configuration or speed before and
+ * after using the touchpad. An example case would be sharing the
+ * bus with a fast flash memory chip.
+ */
+ bool_t direct_init;
+} TouchscreenDriver;
+
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ /* Core functions */
+ void ts_lld_init(const TouchscreenDriver *ts);
+
+ uint16_t ts_lld_read_value(uint8_t cmd);
+ uint16_t ts_lld_read_x(void);
+ uint16_t ts_lld_read_y(void);
+
+ #if TOUCHSCREEN_HAS_IRQ
+ uint8_t ts_lld_irq(void);
+ #endif
+
+ #if TOUCHSCREEN_HAS_PRESSURE
+ uint16_t ts_lld_read_z(void);
+ #endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GFX_USE_TOUCHSCREEN */
+
+#endif /* _TOUCHSCREEN_LLD_H */
+/** @} */
+
diff --git a/src/touchscreen.c b/src/touchscreen.c
new file mode 100644
index 00000000..e226eaea
--- /dev/null
+++ b/src/touchscreen.c
@@ -0,0 +1,374 @@
+/* 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__)
+
+#if TOUCHSCREEN_STORE_CALIBRATION
+extern void ts_store_calibration_lld(struct cal_t *cal);
+extern struct cal_t *ts_restore_calibration_lld(void);
+#endif
+
+/*===========================================================================*/
+/* Driver local definitions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* 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 < CONVERSIONS; i++) {
+ results += ts_lld_read_x();
+ }
+
+ /* Take the average of the readings */
+ x = results / CONVERSIONS;
+
+ return x;
+}
+
+static coord_t _tsReadRealY(void) {
+ int32_t results = 0;
+ int16_t i;
+ coord_t y;
+
+ for(i = 0; i < CONVERSIONS; i++) {
+ results += ts_lld_read_y();
+ }
+
+ /* Take the average of the readings */
+ y = results / CONVERSIONS;
+
+ return y;
+}
+
+static void _tsDrawCross(uint16_t x, uint16_t y) {
+ gdispDrawLine(x-15, y, x-2, y, 0xffff);
+ gdispDrawLine(x+2, y, x+15, y, 0xffff);
+ gdispDrawLine(x, y-15, x, y-2, 0xffff);
+ gdispDrawLine(x, y+2, x, y+15, 0xffff);
+
+ gdispDrawLine(x-15, y+15, x-7, y+15, RGB565CONVERT(184,158,131));
+ gdispDrawLine(x-15, y+7, x-15, y+15, RGB565CONVERT(184,158,131));
+
+ gdispDrawLine(x-15, y-15, x-7, y-15, RGB565CONVERT(184,158,131));
+ gdispDrawLine(x-15, y-7, x-15, y-15, RGB565CONVERT(184,158,131));
+
+ gdispDrawLine(x+7, y+15, x+15, y+15, RGB565CONVERT(184,158,131));
+ gdispDrawLine(x+15, y+7, x+15, y+15, RGB565CONVERT(184,158,131));
+
+ gdispDrawLine(x+7, y-15, x+15, y-15, RGB565CONVERT(184,158,131));
+ gdispDrawLine(x+15, y-15, x+15, y-7, RGB565CONVERT(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 This function is NOT currently implicitly invoked by @p halInit().
+ * It must be called manually.
+ *
+ * @param[in] ts The touchscreen driver struct
+ *
+ * @api
+ */
+void tsInit(const TouchscreenDriver *ts) {
+ cal = (struct cal_t*)chHeapAlloc(NULL, sizeof(struct cal_t));
+ if(cal == NULL)
+ return;
+
+ /* 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) {
+ cal = (struct cal_t*)chHeapAlloc(NULL, sizeof(struct cal_t));
+ tsCalibrate();
+ }
+ #endif
+}
+
+/**
+ * @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 == TRUE
+ 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 GDISP_SCREEN_WIDTH - x - 1;
+ case GDISP_ROTATE_270:
+ return GDISP_SCREEN_HEIGHT - 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 == TRUE
+ 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 GDISP_SCREEN_WIDTH - x - 1;
+ case GDISP_ROTATE_180:
+ return GDISP_SCREEN_HEIGHT - 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_IRQ || defined(__DOXYGEN__)
+ bool_t tsIRQ(void) {
+ return ts_lld_irq();
+ }
+#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.
+ *
+ * @api
+ */
+void tsCalibrate(void) {
+ 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;
+
+ gdispSetOrientation(GDISP_ROTATE_0);
+ 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(!tsIRQ())
+ 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 (tsIRQ()) {
+ /* We have valid pointer data */
+ px += _tsReadRealX();
+ py += _tsReadRealY();
+
+ j++;
+ }
+ }
+
+ points[i][0] = px / j;
+ points[i][1] = py / j;
+
+ chThdSleepMilliseconds(100);
+
+ while(tsIRQ())
+ 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 /* GFX_USE_TOUCHSCREEN */
+/** @} */
+