From ebcc53e8e7e124d3e96af95c9f64b4be79cbcae1 Mon Sep 17 00:00:00 2001 From: Tectu Date: Thu, 9 Aug 2012 00:44:12 +0200 Subject: some more touchpad stuff --- halext/src/touchpad.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 halext/src/touchpad.c (limited to 'halext/src') diff --git a/halext/src/touchpad.c b/halext/src/touchpad.c new file mode 100644 index 00000000..69d47f42 --- /dev/null +++ b/halext/src/touchpad.c @@ -0,0 +1,130 @@ +/* + ChibiOS-LCD-Driver - Copyright (C) 2012 + Joel Bodenmann aka Tectu + + This file is part of ChibiOS-LCD-Driver. + + ChibiOS-LCD-Driver 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-LCD-Driver 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 . +*/ + +/** + * @file touchpad.c + * @brief Touchpad Driver code. + * + * @addgroup TOUCHPAD + * @{ + */ +#include "ch.h" +#include "hal.h" +#include "touchpad.h" + +#if HAL_USE_TOUCHPAD || defined(__DOXYGEN__) + +#if GDISP_NEED_MULTITHREAD + #warning "GDISP: Multithread support not complete" + #define MUTEX_INIT /* Not defined yet */ + #define MUTEX_ENTER /* Not defined yet */ + #define MUTEX_EXIT /* Not defined yet */ +#endif + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +#ifdef UNUSED +#elif defined(__GNUC__) +# define UNUSED(x) UNUSED_ ## x __attribute__((unused)) +#elif defined(__LCLINT__) +# define UNUSED(x) /*@unused@*/ x +#else +# define UNUSED(x) x +#endif + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +#if TOUCHPAD_NEED_MULTITHREAD || defined(__DOXYGEN__) + /** + * @brief Touchpad Driver initialization. + * @note This function is NOT currently implicitly invoked by @p halInit(). + * It must be called manually. + * + * @init + */ + void tpInit(TOUCHPADDriver * UNUSED(tp)) { + /* Initialise Mutex */ + MUTEX_INIT + + /* Initialise driver */ + MUTEX_ENTER + tp_lld_init(tp); + MUTEX_EXIT + } +#endif + +#if TOUCHPAD_NEED_MULTITHREAD || defined(__DOXYGEN__) + /** + * @brief Get the X-Coordinate, relative to screen zero point. + * + * @return The X position in pixels. + * + * @api + */ + uint16_t tpReadX(void) { + return (tp_lld_read_x()); + } +#endif + +#if TOUCHPAD_NEED_MULTITHREAD || defined(__DOXYGEN__) + /** + * @brief Get the X-Coordinate, relative to screen zero point. + * + * @return The Y position in pixels. + * + * @api + */ + uint16_t tpReadY(void) { + return (tp_lld_read_y()); + } +#endif + +#if TOUCHPAD_NEED_MULTITHREAD || defined(__DOXYGEN__) + /** + * @brief Get the pressure. + * + * @return The pressure. + * + * @api + */ + uint16_t tpReadZ(void) { + return (tp_lld_read_z()); + } +#endif + +#endif /* HAL_USE_TOUCHPAD */ +/** @} */ + -- cgit v1.2.3 From ed29707da6ba034db1fe7fbf11abe98f530b3647 Mon Sep 17 00:00:00 2001 From: Tectu Date: Thu, 9 Aug 2012 01:00:09 +0200 Subject: fixed a few TOUCHPAD_MULTITHREAD stuffs --- halext/src/touchpad.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'halext/src') diff --git a/halext/src/touchpad.c b/halext/src/touchpad.c index 69d47f42..29ff9eb5 100644 --- a/halext/src/touchpad.c +++ b/halext/src/touchpad.c @@ -32,7 +32,7 @@ #if HAL_USE_TOUCHPAD || defined(__DOXYGEN__) #if GDISP_NEED_MULTITHREAD - #warning "GDISP: Multithread support not complete" + #warning "TOUCHPAD: Multithread support not complete" #define MUTEX_INIT /* Not defined yet */ #define MUTEX_ENTER /* Not defined yet */ #define MUTEX_EXIT /* Not defined yet */ -- cgit v1.2.3 From 79c053567816521b601e44981bf3af5b0e40f752 Mon Sep 17 00:00:00 2001 From: Tectu Date: Thu, 9 Aug 2012 01:31:24 +0200 Subject: more touchpad stuff --- halext/src/touchpad.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'halext/src') diff --git a/halext/src/touchpad.c b/halext/src/touchpad.c index 29ff9eb5..f65ca0cd 100644 --- a/halext/src/touchpad.c +++ b/halext/src/touchpad.c @@ -77,12 +77,12 @@ */ void tpInit(TOUCHPADDriver * UNUSED(tp)) { /* Initialise Mutex */ - MUTEX_INIT + //MUTEX_INIT /* Initialise driver */ - MUTEX_ENTER - tp_lld_init(tp); - MUTEX_EXIT + //MUTEX_ENTER + tp_lld_init(); + //MUTEX_EXIT } #endif -- cgit v1.2.3 From 4c3e1847dede93bf3f1671a00e86c57c0c1387ac Mon Sep 17 00:00:00 2001 From: Tectu Date: Thu, 9 Aug 2012 02:22:10 +0200 Subject: removed TOUCHPAD_NEED_MULTITASKING --- halext/src/touchpad.c | 107 +++++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 50 deletions(-) (limited to 'halext/src') diff --git a/halext/src/touchpad.c b/halext/src/touchpad.c index f65ca0cd..4adb25fe 100644 --- a/halext/src/touchpad.c +++ b/halext/src/touchpad.c @@ -27,17 +27,11 @@ */ #include "ch.h" #include "hal.h" +#include "gdisp.h" #include "touchpad.h" #if HAL_USE_TOUCHPAD || defined(__DOXYGEN__) -#if GDISP_NEED_MULTITHREAD - #warning "TOUCHPAD: Multithread support not complete" - #define MUTEX_INIT /* Not defined yet */ - #define MUTEX_ENTER /* Not defined yet */ - #define MUTEX_EXIT /* Not defined yet */ -#endif - /*===========================================================================*/ /* Driver local definitions. */ /*===========================================================================*/ @@ -58,6 +52,9 @@ /*===========================================================================*/ /* Driver local variables. */ /*===========================================================================*/ +volatile static struct cal cal = { + 1, 1, 0, 0 +}; /*===========================================================================*/ /* Driver local functions. */ @@ -67,52 +64,62 @@ /* Driver exported functions. */ /*===========================================================================*/ -#if TOUCHPAD_NEED_MULTITHREAD || defined(__DOXYGEN__) - /** - * @brief Touchpad Driver initialization. - * @note This function is NOT currently implicitly invoked by @p halInit(). - * It must be called manually. - * - * @init - */ - void tpInit(TOUCHPADDriver * UNUSED(tp)) { - /* Initialise Mutex */ - //MUTEX_INIT - - /* Initialise driver */ - //MUTEX_ENTER - tp_lld_init(); - //MUTEX_EXIT - } -#endif +/** + * @brief Touchpad Driver initialization. + * @note This function is NOT currently implicitly invoked by @p halInit(). + * It must be called manually. + * + * @init + */ +void tpInit(TOUCHPADDriver *tp) { + /* Initialise Mutex */ + //MUTEX_INIT -#if TOUCHPAD_NEED_MULTITHREAD || defined(__DOXYGEN__) - /** - * @brief Get the X-Coordinate, relative to screen zero point. - * - * @return The X position in pixels. - * - * @api - */ - uint16_t tpReadX(void) { - return (tp_lld_read_x()); - } -#endif + /* Initialise driver */ + //MUTEX_ENTER + tp_lld_init(tp); + //MUTEX_EXIT +} -#if TOUCHPAD_NEED_MULTITHREAD || defined(__DOXYGEN__) - /** - * @brief Get the X-Coordinate, relative to screen zero point. - * - * @return The Y position in pixels. - * - * @api - */ - uint16_t tpReadY(void) { - return (tp_lld_read_y()); - } -#endif +/** + * @brief Get the X-Coordinate, relative to screen zero point. + * + * @return The X position in pixels. + * + * @api + */ +uint16_t tpReadX(void) { + uint16_t x, y; + + x = cal.xm * _tpReadRealX() + cal.xn; + y = cal.ym * _tpReadRealY() + cal.yn; + + //switch(gdispGetOrientation()) { + switch(portrait) { // implement gdispGetOrientation() + case portrait: + return x; + case landscape: + return SCREEN_HEIGHT - y; + case portraitInv: + return SCREEN_WIDTH - x; + case landscapeInv: + return y; + } + return x; +} + +/** + * @brief Get the X-Coordinate, relative to screen zero point. + * + * @return The Y position in pixels. + * + * @api + */ +uint16_t tpReadY(void) { + return (tp_lld_read_y()); +} -#if TOUCHPAD_NEED_MULTITHREAD || defined(__DOXYGEN__) +#if TOUCHPAD_PRESSURE || defined(__DOXYGEN__) /** * @brief Get the pressure. * -- cgit v1.2.3 From 55c8323950397d7bb6829f9fbd1a50a61d4913ce Mon Sep 17 00:00:00 2001 From: Tectu Date: Thu, 9 Aug 2012 02:31:42 +0200 Subject: more touchpad stuff --- halext/src/touchpad.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) (limited to 'halext/src') diff --git a/halext/src/touchpad.c b/halext/src/touchpad.c index 4adb25fe..912e2256 100644 --- a/halext/src/touchpad.c +++ b/halext/src/touchpad.c @@ -60,6 +60,44 @@ volatile static struct cal cal = { /* Driver local functions. */ /*===========================================================================*/ +/** + * @brief returns the uncalibrated readout of the X direction from the controller + * + * @noapi + */ +static uint16_t _tpReadRealX(void) { + uint32_t results = 0; + uint16_t i, x; + + for(i = 0; i < CONVERSIONS; i++) { + tp_lld_read_x(); /* dummy, reduce noise on SPI */ + results += tp_lld_read_x(); + } + + x = (((SCREEN_WIDTH-1) * (results/CONVERSIONS)) / 2048); + + return x; +} + +/** + * @brief return the uncalibrated readout of the Y-direction from the controller + * + * @noapi + */ +static uint16_t _tpReadRealY(void) { + uint32_t results = 0; + uint16_t i, y; + + for(i = 0; i < CONVERSIONS; i++) { + tp_lld_read_y(); /* dummy, reduce noise on SPI */ + results += tp_lld_read_y(); + } + + y = (((SCREEN_HEIGHT-1) * (results/CONVERSIONS)) / 2048); + + return y; +} + /*===========================================================================*/ /* Driver exported functions. */ /*===========================================================================*/ @@ -105,7 +143,8 @@ uint16_t tpReadX(void) { case landscapeInv: return y; } - return x; + + return x; } /** @@ -116,7 +155,24 @@ uint16_t tpReadX(void) { * @api */ uint16_t tpReadY(void) { - return (tp_lld_read_y()); + uint16_t x, y; + + x = cal.xm * _tpReadRealX() + cal.xn; + y = cal.ym * _tpReadRealY() + cal.yn; + + //switch(gdispGetOrientation()) { + switch(portrait) { // implement gdispGetOrientation() + case portrait: + return y; + case landscape: + return x; + case portraitInv: + return SCREEN_HEIGHT - y; + case landscapeInv: + return SCREEN_WIDTH - x; + } + + return y; } #if TOUCHPAD_PRESSURE || defined(__DOXYGEN__) @@ -128,6 +184,7 @@ uint16_t tpReadY(void) { * @api */ uint16_t tpReadZ(void) { + /* ToDo */ return (tp_lld_read_z()); } #endif -- cgit v1.2.3 From 385798b21217742ae63380bfdf623e9a5b2c9c36 Mon Sep 17 00:00:00 2001 From: Tectu Date: Thu, 9 Aug 2012 03:07:07 +0200 Subject: touchpad fix --- halext/src/touchpad.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'halext/src') diff --git a/halext/src/touchpad.c b/halext/src/touchpad.c index 912e2256..7f7c87f6 100644 --- a/halext/src/touchpad.c +++ b/halext/src/touchpad.c @@ -132,8 +132,8 @@ uint16_t tpReadX(void) { x = cal.xm * _tpReadRealX() + cal.xn; y = cal.ym * _tpReadRealY() + cal.yn; - //switch(gdispGetOrientation()) { - switch(portrait) { // implement gdispGetOrientation() + /* + switch(gdispGetOrientation()) { // implement gdispGetOrientation() case portrait: return x; case landscape: @@ -143,6 +143,7 @@ uint16_t tpReadX(void) { case landscapeInv: return y; } + */ return x; } @@ -160,8 +161,8 @@ uint16_t tpReadY(void) { x = cal.xm * _tpReadRealX() + cal.xn; y = cal.ym * _tpReadRealY() + cal.yn; - //switch(gdispGetOrientation()) { - switch(portrait) { // implement gdispGetOrientation() + /* + switch(gdispGetOrientation()) { // implement gdispGetOrientation() case portrait: return y; case landscape: @@ -171,6 +172,7 @@ uint16_t tpReadY(void) { case landscapeInv: return SCREEN_WIDTH - x; } + */ return y; } -- cgit v1.2.3 From 03c27adb1c86d37548e695fd89937f01ed528954 Mon Sep 17 00:00:00 2001 From: Tectu Date: Thu, 9 Aug 2012 03:30:45 +0200 Subject: added tpIRQ() --- halext/src/touchpad.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'halext/src') diff --git a/halext/src/touchpad.c b/halext/src/touchpad.c index 7f7c87f6..2b51504a 100644 --- a/halext/src/touchpad.c +++ b/halext/src/touchpad.c @@ -107,7 +107,7 @@ static uint16_t _tpReadRealY(void) { * @note This function is NOT currently implicitly invoked by @p halInit(). * It must be called manually. * - * @init + * @api */ void tpInit(TOUCHPADDriver *tp) { /* Initialise Mutex */ @@ -177,7 +177,20 @@ uint16_t tpReadY(void) { return y; } -#if TOUCHPAD_PRESSURE || defined(__DOXYGEN__) +#if TOUCHPAD_HAS_IRQ || defined(__DOXYGEN__) + /** + * @brief returns if touchpad is pressed or not + * + * @return 1 if pressed, 0 otherwise + * + * @api + */ + uint8_t tpIRQ(void) { + return tp_lld_irq(); + } +#endif + +#if TOUCHPAD_HAS_PRESSURE || defined(__DOXYGEN__) /** * @brief Get the pressure. * -- cgit v1.2.3 From b8e412e066db3f3f231a6873e5f4da15ff297c2d Mon Sep 17 00:00:00 2001 From: Tectu Date: Thu, 9 Aug 2012 03:43:30 +0200 Subject: implemented tpCalibrate() --- halext/src/touchpad.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'halext/src') diff --git a/halext/src/touchpad.c b/halext/src/touchpad.c index 2b51504a..5274e350 100644 --- a/halext/src/touchpad.c +++ b/halext/src/touchpad.c @@ -98,6 +98,30 @@ static uint16_t _tpReadRealY(void) { return y; } +/** + * @brief draws a cross. Used for calibration. + * + * @noapi + */ +static void _tpDrawCross(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)); +} + /*===========================================================================*/ /* Driver exported functions. */ /*===========================================================================*/ @@ -177,6 +201,32 @@ uint16_t tpReadY(void) { return y; } +void tpCalibrate(void) { + uint16_t cross[2][2] = {{40,50}, {200, 280}}; + uint16_t points[2][2]; + uint8_t i; + + //gdispSetOrientation(portrait); + gdispClear(Red); + gdispDrawString(40, 10, "Calibration", &fontUI1Double, White); + + for(i = 0; i < 2; i++) { + _tpDrawCross(cross[i][0], cross[i][1]); + while(!tpIRQ()); + points[i][0] = _tpReadRealX(); + points[i][1] = _tpReadRealY(); + chThdSleepMilliseconds(100); + while(tpIRQ()); + gdispFillArea(cross[i][0]-15, cross[i][1]-15, 42, 42, Red); + } + + cal.xm = ((float)cross[1][0] - (float)cross[0][0]) / ((float)points[1][0] - (float)points[0][0]); + cal.ym = ((float)cross[1][1] - (float)cross[0][1]) / ((float)points[1][1] - (float)points[0][1]); + + cal.xn = (float)cross[0][0] - cal.xm * (float)points[0][0]; + cal.yn = (float)cross[0][1] - cal.ym * (float)points[0][1]; +} + #if TOUCHPAD_HAS_IRQ || defined(__DOXYGEN__) /** * @brief returns if touchpad is pressed or not -- cgit v1.2.3 From fda647c6c2111857cec188a7f6f104c459d87684 Mon Sep 17 00:00:00 2001 From: Tectu Date: Thu, 9 Aug 2012 09:39:22 +0200 Subject: doc --- halext/src/touchpad.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'halext/src') diff --git a/halext/src/touchpad.c b/halext/src/touchpad.c index 5274e350..3539ad32 100644 --- a/halext/src/touchpad.c +++ b/halext/src/touchpad.c @@ -74,6 +74,7 @@ static uint16_t _tpReadRealX(void) { results += tp_lld_read_x(); } + // 12-bit x = (((SCREEN_WIDTH-1) * (results/CONVERSIONS)) / 2048); return x; @@ -93,6 +94,7 @@ static uint16_t _tpReadRealY(void) { results += tp_lld_read_y(); } + // 12-bit y = (((SCREEN_HEIGHT-1) * (results/CONVERSIONS)) / 2048); return y; -- cgit v1.2.3