renamed touchpad into touchscreen

ugfx_release_2.6
Joel Bodenmann 2012-11-10 00:05:01 +01:00
parent 995c9835c2
commit 87b6d98055
19 changed files with 153 additions and 806 deletions

View File

@ -21,7 +21,7 @@
#include "ch.h"
#include "hal.h"
#include "gdisp.h"
#include "touchpad.h"
#include "touchscreen.h"
#define COLOR_SIZE 20
#define PEN_SIZE 20
@ -43,7 +43,7 @@ static const SPIConfig spicfg = {
/* SPI_CR1_BR_2 | */ SPI_CR1_BR_1 | SPI_CR1_BR_0,
};
TOUCHPADDriver TOUCHPADD1 = {
TouchscreenDriver TOUCHPADD1 = {
&SPID1,
&spicfg,
TP_IRQ_PORT,
@ -85,14 +85,14 @@ int main(void) {
chSysInit();
gdispInit();
tpInit(&TOUCHPADD1);
tpCalibrate();
tsInit(&TOUCHPADD1);
tsCalibrate();
drawScreen();
while (TRUE) {
x = tpReadX();
y = tpReadY();
x = tsReadX();
y = tsReadY();
/* inside color box ? */
if(y >= OFFSET && y <= COLOR_SIZE) {

View File

@ -1,26 +0,0 @@
/*
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 TOUCHPAD
* @details The TOUCHPAD module provides high level abstraction to interface
* touchscreens.
*/

View File

@ -1,8 +0,0 @@
To use this driver:
1. Add in your halconf.h:
a) #define GFX_USE_TOUCHPAD TRUE
2. To your makefile add the following lines:
include $(GFXLIB)/drivers/touchpadADS7843/touchpad_lld.mk

View File

@ -1,6 +0,0 @@
# List the required driver.
GFXSRC += $(GFXLIB)/drivers/touchpad/ADS7843/touchpad_lld.c
# Required include directories
GFXINC += $(GFXLIB)/drivers/touchpad/ADS7843

View File

@ -1,8 +0,0 @@
To use this driver:
1. Add in your halconf.h:
a) #define GFX_USE_TOUCHPAD TRUE
2. To your makefile add the following lines:
include $(GFXLIB)/drivers/touchpad/XPT2046/touchpad_lld.mk

View File

@ -1,6 +0,0 @@
# List the required driver.
GFXSRC += $(GFXLIB)/drivers/touchpad/XPT2046/touchpad_lld.c
# Required include directories
GFXINC += $(GFXLIB)/drivers/touchpad/XPT2046

View File

@ -0,0 +1,8 @@
To use this driver:
1. Add in your halconf.h:
a) #define GFX_USE_TOUCHSCREEN TRUE
2. To your makefile add the following lines:
include $(GFXLIB)/drivers/touchscreen/ADS7843/touchscreen_lld.mk

View File

@ -19,18 +19,18 @@
*/
/**
* @file drivers/touchpad/XPT2046/touchpad_lld.c
* @brief Touchpad Driver subsystem low level driver source.
* @file drivers/touchscreen/ADS7843/touchscreen_lld.c
* @brief Touchscreen Driver subsystem low level driver source.
*
* @addtogroup TOUCHPAD
* @addtogroup TOUCHSCREEN
* @{
*/
#include "ch.h"
#include "hal.h"
#include "touchpad.h"
#include "touchscreen.h"
#if GFX_USE_TOUCHPAD /*|| defined(__DOXYGEN__)*/
#if GFX_USE_TOUCHSCREEN /*|| defined(__DOXYGEN__)*/
/*===========================================================================*/
/* Driver local definitions. */
@ -44,8 +44,8 @@
/* Driver local variables. */
/*===========================================================================*/
#if !defined(__DOXYGEN__)
/* Local copy of the current touchpad driver */
static const TOUCHPADDriver *tpDriver;
/* Local copy of the current touchscreen driver */
static const TouchscreenDriver *tsDriver;
static uint16_t sampleBuf[7];
#endif
@ -65,24 +65,24 @@
/* ---- Required Routines ---- */
/**
* @brief Low level Touchpad driver initialization.
* @brief Low level touchscreen driver initialization.
*
* @param[in] tp The touchpad driver struct
* @param[in] ts The touchscreen driver
*
* @notapi
*/
void tp_lld_init(const TOUCHPADDriver *tp) {
tpDriver = tp;
void ts_lld_init(const TouchscreenDriver *ts) {
tsDriver = ts;
if(tpDriver->direct_init)
spiStart(tpDriver->spip, tpDriver->spicfg);
if(tsDriver->direct_init)
spiStart(tsDriver->spip, tsDriver->spicfg);
}
/**
* @brief Reads a conversion from the touchpad
* @brief Reads a conversion from the touchscreen
*
* @param[in] cmd The command bits to send to the touchpad
* @param[in] cmd The command bits to send to the touchscreen
*
* @return The read value 12-bit right-justified
*
@ -91,14 +91,14 @@ void tp_lld_init(const TOUCHPADDriver *tp) {
*
* @notapi
*/
uint16_t tp_lld_read_value(uint8_t cmd) {
uint16_t ts_lld_read_value(uint8_t cmd) {
static uint8_t txbuf[3] = {0};
static uint8_t rxbuf[3] = {0};
uint16_t ret;
txbuf[0] = cmd;
spiExchange(tpDriver->spip, 3, txbuf, rxbuf);
spiExchange(tsDriver->spip, 3, txbuf, rxbuf);
ret = (rxbuf[1] << 5) | (rxbuf[2] >> 3);
@ -106,18 +106,18 @@ uint16_t tp_lld_read_value(uint8_t cmd) {
}
/**
* @brief 7-point median filtering code for touchpad samples
* @brief 7-point median filtering code for touchscreen samples
*
* @note This is an internally used routine only.
*
* @notapi
*/
static void tp_lld_filter(void) {
static void ts_lld_filter(void) {
uint16_t temp;
int i,j;
for(i = 0; i < 4; i++) {
for(j=i; j < 7; j++) {
for(j = i; j < 7; j++) {
if(sampleBuf[i] > sampleBuf[j]) {
/* Swap the values */
temp = sampleBuf[i];
@ -135,37 +135,37 @@ static void tp_lld_filter(void) {
*
* @notapi
*/
uint16_t tp_lld_read_x(void) {
uint16_t ts_lld_read_x(void) {
int i;
#if defined(SPI_USE_MUTUAL_EXCLUSION)
spiAcquireBus(tpDriver->spip);
spiAcquireBus(tsDriver->spip);
#endif
TOUCHPAD_SPI_PROLOGUE();
palClearPad(tpDriver->spicfg->ssport, tpDriver->spicfg->sspad);
TOUCHSCREEN_SPI_PROLOGUE();
palClearPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
/* Discard the first conversion - very noisy and keep the ADC on hereafter
* till we are done with the sampling. Note that PENIRQ is disabled.
*/
tp_lld_read_value(0xD1);
ts_lld_read_value(0xD1);
for(i = 0; i < 7; i++) {
sampleBuf[i]=tp_lld_read_value(0xD1);
sampleBuf[i] = ts_lld_read_value(0xD1);
}
/* Switch on PENIRQ once again - perform a dummy read */
tp_lld_read_value(0xD0);
ts_lld_read_value(0xD0);
palSetPad(tpDriver->spicfg->ssport, tpDriver->spicfg->sspad);
TOUCHPAD_SPI_EPILOGUE();
palSetPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
TOUCHSCREEN_SPI_EPILOGUE();
#if defined(SPI_USE_MUTUAL_EXCLUSION)
spiReleaseBus(tpDriver->spip);
spiReleaseBus(tsDriver->spip);
#endif
/* Find the median - use selection sort */
tp_lld_filter();
ts_lld_filter();
return sampleBuf[3];
}
@ -175,43 +175,43 @@ uint16_t tp_lld_read_x(void) {
*
* @notapi
*/
uint16_t tp_lld_read_y(void) {
uint16_t ts_lld_read_y(void) {
int i;
#if defined(SPI_USE_MUTUAL_EXCLUSION)
spiAcquireBus(tpDriver->spip);
spiAcquireBus(tsDriver->spip);
#endif
TOUCHPAD_SPI_PROLOGUE();
palClearPad(tpDriver->spicfg->ssport, tpDriver->spicfg->sspad);
TOUCHSCREEN_SPI_PROLOGUE();
palClearPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
/* Discard the first conversion - very noisy and keep the ADC on hereafter
* till we are done with the sampling. Note that PENIRQ is disabled.
*/
tp_lld_read_value(0x91);
ts_lld_read_value(0x91);
for(i = 0; i < 7; i++) {
sampleBuf[i] = tp_lld_read_value(0x91);
sampleBuf[i] = ts_lld_read_value(0x91);
}
/* Switch on PENIRQ once again - perform a dummy read */
tp_lld_read_value(0x90);
ts_lld_read_value(0x90);
palSetPad(tpDriver->spicfg->ssport, tpDriver->spicfg->sspad);
TOUCHPAD_SPI_EPILOGUE();
palSetPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
TOUCHSCREEN_SPI_EPILOGUE();
#ifdef SPI_USE_MUTUAL_EXCLUSION
spiReleaseBus(tpDriver->spip);
spiReleaseBus(tsDriver->spip);
#endif
/* Find the median - use selection sort */
tp_lld_filter();
ts_lld_filter();
return sampleBuf[3];
}
/* ---- Optional Routines ---- */
#if TOUCHPAD_HAS_IRQ || defined(__DOXYGEN__)
#if TOUCHSCREEN_HAS_IRQ || defined(__DOXYGEN__)
/*
* @brief for checking if touchpad is pressed or not.
*
@ -219,23 +219,23 @@ uint16_t tp_lld_read_y(void) {
*
* @notapi
*/
uint8_t tp_lld_irq(void) {
return (!palReadPad(tpDriver->tpIRQPort, tpDriver->tpIRQPin));
uint8_t ts_lld_irq(void) {
return (!palReadPad(tsDriver->tsIRQPort, tsDriver->tsIRQPin));
}
#endif
#if TOUCHPAD_HAS_PRESSURE || defined(__DOXYGEN__)
#if TOUCHSCREEN_HAS_PRESSURE || defined(__DOXYGEN__)
/*
* @brief Reads out the Z direction / pressure.
*
* @notapi
*/
uint16_t tp_lld_read_z(void) {
uint16_t ts_lld_read_z(void) {
/* ToDo */
return 42;
}
#endif
#endif /* GFX_USE_TOUCHPAD */
#endif /* GFX_USE_TOUCHSCREEN */
/** @} */

View File

@ -0,0 +1,6 @@
# List the required driver.
GFXSRC += $(GFXLIB)/drivers/touchscreen/ADS7843/touchscreen_lld.c
# Required include directories
GFXINC += $(GFXLIB)/drivers/touchscreen/ADS7843

View File

@ -19,27 +19,27 @@
*/
/**
* @file drivers/touchpad/XPT2046/touchpad_lld_config.h
* @brief Touchppad Driver subsystem low level driver.
* @file drivers/touchscreen/ADS7843/touchscreen_lld_config.h
* @brief Touchscreen Driver subsystem low level driver.
*
* @addtogroup TOUCHPAD
* @addtogroup TOUCHSCREEN
* @{
*/
#ifndef _TOUCHPAD_LLD_CONFIG_H
#define _TOUCHPAD_LLD_CONFIG_H
#ifndef TOUCHSCREEN_LLD_CONFIG_H
#define TOUCHSCREEN_LLD_CONFIG_H
#if GFX_USE_TOUCHPAD /*|| defined(__DOXYGEN__)*/
#if GFX_USE_TOUCHSCREEN /*|| defined(__DOXYGEN__)*/
/*===========================================================================*/
/* Driver hardware support. */
/*===========================================================================*/
#define TOUCHPAD_HAS_IRQ TRUE
#define TOUCHPAD_HAS_PRESSURE TRUE
#define TOUCHSCREEN_HAS_IRQ TRUE
#define TOUCHSCREEN_HAS_PRESSURE FALSE
#endif /* GFX_USE_TOUCHPAD */
#endif /* GFX_USE_TOUCHSCREEN */
#endif /* _TOUCHPAD_LLD_CONFIG_H */
#endif /* TOUCHSCREEN_LLD_CONFIG_H */
/** @} */

View File

@ -0,0 +1,8 @@
To use this driver:
1. Add in your halconf.h:
a) #define GFX_USE_TOUCHSCREEN TRUE
2. To your makefile add the following lines:
include $(GFXLIB)/drivers/touchscreen/XPT2046/touchscreen_lld.mk

View File

@ -19,18 +19,18 @@
*/
/**
* @file drivers/touchpad/ADS7843/touchpad_lld.c
* @brief Touchpad Driver subsystem low level driver source.
* @file drivers/touchscreen/XPT2046/touchscreen_lld.c
* @brief Touchscreen Driver subsystem low level driver source.
*
* @addtogroup TOUCHPAD
* @addtogroup TOUCHSCREEN
* @{
*/
#include "ch.h"
#include "hal.h"
#include "touchpad.h"
#include "touchscreen.h"
#if GFX_USE_TOUCHPAD /*|| defined(__DOXYGEN__)*/
#if GFX_USE_TOUCHSCREEN /*|| defined(__DOXYGEN__)*/
/*===========================================================================*/
/* Driver local definitions. */
@ -45,7 +45,7 @@
/*===========================================================================*/
#if !defined(__DOXYGEN__)
/* Local copy of the current touchpad driver */
static const TOUCHPADDriver *tpDriver;
static const TouchscreenDriver *tsDriver;
static uint16_t sampleBuf[7];
#endif
@ -65,24 +65,24 @@
/* ---- Required Routines ---- */
/**
* @brief Low level Touchpad driver initialization.
* @brief Low level Touchscreen driver initialization.
*
* @param[in] tp The touchpad driver
* @param[in] ts The touchscreen driver struct
*
* @notapi
*/
void tp_lld_init(const TOUCHPADDriver *tp) {
tpDriver = tp;
void ts_lld_init(const TouchscreenDriver *ts) {
tsDriver = ts;
if(tpDriver->direct_init)
spiStart(tpDriver->spip, tpDriver->spicfg);
if(tsDriver->direct_init)
spiStart(tsDriver->spip, tsDriver->spicfg);
}
/**
* @brief Reads a conversion from the touchpad
* @brief Reads a conversion from the touchscreen
*
* @param[in] cmd The command bits to send to the touchpad
* @param[in] cmd The command bits to send to the touchscreen
*
* @return The read value 12-bit right-justified
*
@ -91,14 +91,14 @@ void tp_lld_init(const TOUCHPADDriver *tp) {
*
* @notapi
*/
uint16_t tp_lld_read_value(uint8_t cmd) {
uint16_t ts_lld_read_value(uint8_t cmd) {
static uint8_t txbuf[3] = {0};
static uint8_t rxbuf[3] = {0};
uint16_t ret;
txbuf[0] = cmd;
spiExchange(tpDriver->spip, 3, txbuf, rxbuf);
spiExchange(tsDriver->spip, 3, txbuf, rxbuf);
ret = (rxbuf[1] << 5) | (rxbuf[2] >> 3);
@ -106,18 +106,18 @@ uint16_t tp_lld_read_value(uint8_t cmd) {
}
/**
* @brief 7-point median filtering code for touchpad samples
* @brief 7-point median filtering code for touchscreen samples
*
* @note This is an internally used routine only.
*
* @notapi
*/
static void tp_lld_filter(void) {
static void ts_lld_filter(void) {
uint16_t temp;
int i,j;
for(i = 0; i < 4; i++) {
for(j=i; j < 7; j++) {
for(j = i; j < 7; j++) {
if(sampleBuf[i] > sampleBuf[j]) {
/* Swap the values */
temp = sampleBuf[i];
@ -135,37 +135,37 @@ static void tp_lld_filter(void) {
*
* @notapi
*/
uint16_t tp_lld_read_x(void) {
uint16_t ts_lld_read_x(void) {
int i;
#if defined(SPI_USE_MUTUAL_EXCLUSION)
spiAcquireBus(tpDriver->spip);
spiAcquireBus(tsDriver->spip);
#endif
TOUCHPAD_SPI_PROLOGUE();
palClearPad(tpDriver->spicfg->ssport, tpDriver->spicfg->sspad);
TOUCHSCREEN_SPI_PROLOGUE();
palClearPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
/* Discard the first conversion - very noisy and keep the ADC on hereafter
* till we are done with the sampling. Note that PENIRQ is disabled.
*/
tp_lld_read_value(0xD1);
ts_lld_read_value(0xD1);
for(i = 0; i < 7; i++) {
sampleBuf[i]=tp_lld_read_value(0xD1);
sampleBuf[i] = ts_lld_read_value(0xD1);
}
/* Switch on PENIRQ once again - perform a dummy read */
tp_lld_read_value(0xD0);
ts_lld_read_value(0xD0);
palSetPad(tpDriver->spicfg->ssport, tpDriver->spicfg->sspad);
TOUCHPAD_SPI_EPILOGUE();
palSetPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
TOUCHSCREEN_SPI_EPILOGUE();
#if defined(SPI_USE_MUTUAL_EXCLUSION)
spiReleaseBus(tpDriver->spip);
spiReleaseBus(tsDriver->spip);
#endif
/* Find the median - use selection sort */
tp_lld_filter();
ts_lld_filter();
return sampleBuf[3];
}
@ -175,67 +175,67 @@ uint16_t tp_lld_read_x(void) {
*
* @notapi
*/
uint16_t tp_lld_read_y(void) {
uint16_t ts_lld_read_y(void) {
int i;
#if defined(SPI_USE_MUTUAL_EXCLUSION)
spiAcquireBus(tpDriver->spip);
spiAcquireBus(tsDriver->spip);
#endif
TOUCHPAD_SPI_PROLOGUE();
palClearPad(tpDriver->spicfg->ssport, tpDriver->spicfg->sspad);
TOUCHSCREEN_SPI_PROLOGUE();
palClearPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
/* Discard the first conversion - very noisy and keep the ADC on hereafter
* till we are done with the sampling. Note that PENIRQ is disabled.
*/
tp_lld_read_value(0x91);
ts_lld_read_value(0x91);
for(i = 0; i < 7; i++) {
sampleBuf[i] = tp_lld_read_value(0x91);
sampleBuf[i] = ts_lld_read_value(0x91);
}
/* Switch on PENIRQ once again - perform a dummy read */
tp_lld_read_value(0x90);
ts_lld_read_value(0x90);
palSetPad(tpDriver->spicfg->ssport, tpDriver->spicfg->sspad);
TOUCHPAD_SPI_EPILOGUE();
palSetPad(tsDriver->spicfg->ssport, tsDriver->spicfg->sspad);
TOUCHSCREEN_SPI_EPILOGUE();
#ifdef SPI_USE_MUTUAL_EXCLUSION
spiReleaseBus(tpDriver->spip);
spiReleaseBus(tsDriver->spip);
#endif
/* Find the median - use selection sort */
tp_lld_filter();
ts_lld_filter();
return sampleBuf[3];
}
/* ---- Optional Routines ---- */
#if TOUCHPAD_HAS_IRQ || defined(__DOXYGEN__)
#if TOUCHSCREEN_HAS_IRQ || defined(__DOXYGEN__)
/*
* @brief for checking if touchpad is pressed or not.
* @brief for checking if touchscreen is pressed or not.
*
* @return 1 if pressed / 0 if not pressed
*
* @notapi
*/
uint8_t tp_lld_irq(void) {
return (!palReadPad(tpDriver->tpIRQPort, tpDriver->tpIRQPin));
uint8_t ts_lld_irq(void) {
return (!palReadPad(tsDriver->tsIRQPort, tsDriver->tsIRQPin));
}
#endif
#if TOUCHPAD_HAS_PRESSURE || defined(__DOXYGEN__)
#if TOUCHSCREEN_HAS_PRESSURE || defined(__DOXYGEN__)
/*
* @brief Reads out the Z direction / pressure.
*
* @notapi
*/
uint16_t tp_lld_read_z(void) {
uint16_t ts_lld_read_z(void) {
/* ToDo */
return 42;
}
#endif
#endif /* GFX_USE_TOUCHPAD */
#endif /* GFX_USE_TOUCHSCREEN */
/** @} */

View File

@ -0,0 +1,6 @@
# List the required driver.
GFXSRC += $(GFXLIB)/drivers/touchscreen/XPT2046/touchscreen_lld.c
# Required include directories
GFXINC += $(GFXLIB)/drivers/touchscreen/XPT2046

View File

@ -19,27 +19,27 @@
*/
/**
* @file drivers/touchpad/ADS7843/touchpad_lld_config.h
* @brief Touchpad Driver subsystem low level driver.
* @file drivers/touchscreen/XPT2046/touchscreen_lld_config.h
* @brief Touchscreen Driver subsystem low level driver.
*
* @addtogroup TOUCHPAD
* @addtogroup TOUCHSCREEN
* @{
*/
#ifndef _TOUCHPAD_LLD_CONFIG_H
#define _TOUCHPAD_LLD_CONFIG_H
#ifndef TOUCHSCREEN_LLD_CONFIG_H
#define TOUCHSCREEN_LLD_CONFIG_H
#if GFX_USE_TOUCHPAD /*|| defined(__DOXYGEN__)*/
#if GFX_USE_TOUCHSCREEN /*|| defined(__DOXYGEN__)*/
/*===========================================================================*/
/* Driver hardware support. */
/*===========================================================================*/
#define TOUCHPAD_HAS_IRQ TRUE
#define TOUCHPAD_HAS_PRESSURE FALSE
#define TOUCHSCREEN_HAS_IRQ TRUE
#define TOUCHSCREEN_HAS_PRESSURE TRUE
#endif /* GFX_USE_TOUCHPAD */
#endif /* GFX_USE_TOUCHSCREEN */
#endif /* _TOUCHPAD_LLD_CONFIG_H */
#endif /* TOUCHSCREEN_LLD_CONFIG_H */
/** @} */

2
gfx.mk
View File

@ -6,7 +6,7 @@ endif
GFXSRC += $(GFXLIB)/src/gdisp.c \
$(GFXLIB)/src/gdisp_fonts.c \
$(GFXLIB)/src/gwin.c \
$(GFXLIB)/src/touchpad.c \
$(GFXLIB)/src/touchscreen.c \
$(GFXLIB)/src/console.c \
$(GFXLIB)/src/graph.c \

View File

@ -1,100 +0,0 @@
/*
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/touchpad.h
* @brief TOUCHPAD Touchpad Driver subsystem header file.
*
* @addtogroup TOUCHPAD
* @{
*/
#ifndef _TOUCHPAD_H
#define _TOUCHPAD_H
#if GFX_USE_TOUCHPAD || 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 "touchpad_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 tpInit(const TOUCHPADDriver *tp);
coord_t tpReadX(void);
coord_t tpReadY(void);
void tpCalibrate(void);
#if TOUCHPAD_HAS_IRQ
bool_t tpIRQ(void);
#endif
#if TOUCHPAD_HAS_PRESSURE
uint16_t tpReadZ(void);
#endif
#ifdef __cplusplus
}
#endif
#endif /* GFX_USE_TOUCHPAD */
#endif /* _TOUCHPAD_H */
/** @} */

View File

@ -1,155 +0,0 @@
/*
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/touchpad_lld.h
* @brief TOUCHPAD Driver subsystem low level driver header.
*
* @addtogroup TOUCHPAD
* @{
*/
#ifndef _TOUCHPAD_LLD_H
#define _TOUCHPAD_LLD_H
#if GFX_USE_TOUCHPAD || defined(__DOXYGEN__)
/*===========================================================================*/
/* Include the low level driver configuration information */
/*===========================================================================*/
#include "touchpad_lld_config.h"
/*===========================================================================*/
/* Error checks. */
/*===========================================================================*/
#ifndef TOUCHPAD_NEED_MULTITHREAD
#define TOUCHPAD_NEED_MULTITHREAD FALSE
#endif
#ifndef TOUCHPAD_XY_INVERTED
#define TOUCHPAD_XY_INVERTED FALSE
#endif
#ifndef TOUCHPAD_STORE_CALIBRATION
#define TOUCHPAD_STORE_CALIBRATION FALSE
#endif
#ifndef TOUCHPAD_VERIFY_CALIBRATION
#define TOUCHPAD_VERIFY_CALIBRATION FALSE
#endif
#ifndef TOUCHPAD_HAS_IRQ
#define TOUCHPAD_HAS_IRQ FALSE
#endif
#ifndef TOUCHPAD_HAS_PRESSURE
#define TOUCHPAD_HAS_PRESSURE FALSE
#endif
#ifndef TOUCHPAD_SPI_PROLOGUE
#define TOUCHPAD_SPI_PROLOGUE()
#endif
#ifndef TOUCHPAD_SPI_EPILOGUE
#define TOUCHPAD_SPI_EPILOGUE()
#endif
/*===========================================================================*/
/* Driver types. */
/*===========================================================================*/
/**
* @brief Structure representing a Touchpad driver.
*/
typedef struct TOUCHPADDriver {
/*
* @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 tpIRQPort;
/*
* @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 tpIRQPin;
/*
* @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 TOUCHPAD_SPI_PROLOGUE() and TOUCHPAD_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;
} TOUCHPADDriver;
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#ifdef __cplusplus
extern "C" {
#endif
/* Core functions */
void tp_lld_init(const TOUCHPADDriver *tp);
uint16_t tp_lld_read_value(uint8_t cmd);
uint16_t tp_lld_read_x(void);
uint16_t tp_lld_read_y(void);
#if TOUCHPAD_HAS_IRQ
uint8_t tp_lld_irq(void);
#endif
#if TOUCHPAD_HAS_PRESSURE
uint16_t tp_lld_read_z(void);
#endif
#ifdef __cplusplus
}
#endif
#endif /* GFX_USE_TOUCHPAD */
#endif /* _TOUCHPAD_LLD_H */
/** @} */

View File

@ -6,6 +6,8 @@ current stable: 1.4
*** changes after 1.4 ***
FEATURE: Added three point calibration
FIX: Touchpad renamed into Touchscreen
*** changes after 1.3 ***

View File

@ -1,374 +0,0 @@
/* 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/touchpad.c
* @brief Touchpad Driver code.
*
* @addtogroup TOUCHPAD
* @{
*/
#include "ch.h"
#include "hal.h"
#include "gdisp.h"
#include "touchpad.h"
#if GFX_USE_TOUCHPAD || defined(__DOXYGEN__)
#if TOUCHPAD_STORE_CALIBRATION
extern void tp_store_calibration_lld(struct cal_t *cal);
extern struct cal_t *tp_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 _tpReadRealX(void) {
int32_t results = 0;
int16_t i;
coord_t x;
for(i = 0; i < CONVERSIONS; i++) {
results += tp_lld_read_x();
}
/* Take the average of the readings */
x = results / CONVERSIONS;
return x;
}
static coord_t _tpReadRealY(void) {
int32_t results = 0;
int16_t i;
coord_t y;
for(i = 0; i < CONVERSIONS; i++) {
results += tp_lld_read_y();
}
/* Take the average of the readings */
y = results / CONVERSIONS;
return y;
}
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));
}
static void _tpTransform(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 _tpDo3PointCalibration(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 Touchpad Driver initialization.
* @note This function is NOT currently implicitly invoked by @p halInit().
* It must be called manually.
*
* @param[in] tp The touchpad driver struct
*
* @api
*/
void tpInit(const TOUCHPADDriver *tp) {
cal = (struct cal_t*)chHeapAlloc(NULL, sizeof(struct cal_t));
if(cal == NULL)
return;
/* Initialise Mutex */
//MUTEX_INIT
/* Initialise driver */
//MUTEX_ENTER
tp_lld_init(tp);
//MUTEX_EXIT
#if TOUCHPAD_STORE_CALIBRATION
cal = tp_restore_calibration_lld();
if(cal == NULL) {
cal = (struct cal_t*)chHeapAlloc(NULL, sizeof(struct cal_t));
tpCalibrate();
}
#endif
}
/**
* @brief Get the X-Coordinate, relative to screen zero point.
*
* @return The X position in pixels.
*
* @api
*/
coord_t tpReadX(void) {
coord_t x, y;
#if TOUCHPAD_XY_INVERTED == TRUE
x = _tpReadRealY();
y = _tpReadRealX();
#else
x = _tpReadRealX();
y = _tpReadRealY();
#endif
_tpTransform(&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 tpReadY(void) {
coord_t x, y;
#if TOUCHPAD_XY_INVERTED == TRUE
x = _tpReadRealY();
y = _tpReadRealX();
#else
x = _tpReadRealX();
y = _tpReadRealY();
#endif
_tpTransform(&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 TOUCHPAD_HAS_PRESSURE || defined(__DOXYGEN__)
uint16_t tpReadZ(void) {
/* ToDo */
return (tp_lld_read_z());
}
#endif
/**
* @brief Returns if touchpad is pressed or not
*
* @return TRUE if pressed, FALSE otherwise
*
* @api
*/
#if TOUCHPAD_HAS_IRQ || defined(__DOXYGEN__)
bool_t tpIRQ(void) {
return tp_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
* TOUCHPAD_VERIFY_CALIBRATION is defined in the configuration.
*
* @api
*/
void tpCalibrate(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 TOUCHPAD_VERIFY_CALIBRATION
calibrate:
for(i = 0; i < 4; i++) {
#else
for(i = 0; i < 3; i++) {
#endif
_tpDrawCross(cross[i][0], cross[i][1]);
while(!tpIRQ())
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 (tpIRQ()) {
/* We have valid pointer data */
px += _tpReadRealX();
py += _tpReadRealY();
j++;
}
}
points[i][0] = px / j;
points[i][1] = py / j;
chThdSleepMilliseconds(100);
while(tpIRQ())
chThdSleepMilliseconds(2); /* Be nice to other threads*/
gdispFillArea(cross[i][0] - 15, cross[i][1] - 15, 42, 42, Blue);
}
/* Apply 3 point calibration algorithm */
_tpDo3PointCalibration(cross, points, cal);
#if TOUCHPAD_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 TOUCHPAD_STORE_CALIBRATION
tp_store_calibration_lld(cal);
#endif
}
#endif /* GFX_USE_TOUCHPAD */
/** @} */