2013-05-24 15:26:52 +00:00
|
|
|
/*
|
2013-06-15 11:37:22 +00:00
|
|
|
* This file is subject to the terms of the GFX License. If a copy of
|
2013-05-24 15:26:52 +00:00
|
|
|
* the license was not distributed with this file, you can obtain one at:
|
|
|
|
*
|
2013-07-21 20:20:37 +00:00
|
|
|
* http://ugfx.org/license.html
|
2013-05-24 15:26:52 +00:00
|
|
|
*/
|
2013-02-11 08:25:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file drivers/ginput/touch/MCU/ginput_lld_mouse.c
|
|
|
|
* @brief GINPUT Touch low level driver source for the MCU.
|
|
|
|
*
|
|
|
|
* @defgroup Mouse Mouse
|
|
|
|
* @ingroup GINPUT
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gfx.h"
|
|
|
|
|
|
|
|
#if (GFX_USE_GINPUT && GINPUT_NEED_MOUSE) /*|| defined(__DOXYGEN__)*/
|
|
|
|
|
|
|
|
#include "ginput/lld/mouse.h"
|
|
|
|
|
2013-10-28 10:04:03 +00:00
|
|
|
#include "ginput_lld_mouse_board.h"
|
2013-02-11 08:25:45 +00:00
|
|
|
|
|
|
|
static uint16_t sampleBuf[7];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief 7-point median filtering code for touchscreen samples
|
|
|
|
*
|
|
|
|
* @note This is an internally used routine only.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
static void filter(void) {
|
|
|
|
uint16_t temp;
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
for(i = 0; i < 4; i++) {
|
|
|
|
for(j = i; j < 7; j++) {
|
|
|
|
if(sampleBuf[i] > sampleBuf[j]) {
|
|
|
|
/* Swap the values */
|
|
|
|
temp = sampleBuf[i];
|
|
|
|
sampleBuf[i] = sampleBuf[j];
|
|
|
|
sampleBuf[j] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialise the mouse/touch.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void ginput_lld_mouse_init(void) {
|
|
|
|
init_board();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read the mouse/touch position.
|
|
|
|
*
|
|
|
|
* @param[in] pt A pointer to the structure to fill
|
|
|
|
*
|
2013-11-23 16:23:21 +00:00
|
|
|
* @note We use a 7 sample medium filter on each coordinate to remove analogue noise.
|
|
|
|
* @note During touch transition the ADC can return some very strange
|
|
|
|
* results. To fix this behaviour we don't return until
|
|
|
|
* we have tested the touch is in the same state at both the beginning
|
|
|
|
* and the end of the reading.
|
|
|
|
* @note Whilst x and y can return readings in any range so long as it fits in 16 bits,
|
|
|
|
* the z value must be ranged by the board file to be a rough percentage. Anything
|
|
|
|
* greater than 80% pressure is a touch.
|
2013-02-11 08:25:45 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void ginput_lld_mouse_get_reading(MouseReading *pt) {
|
|
|
|
uint16_t i;
|
|
|
|
|
2013-11-23 16:23:21 +00:00
|
|
|
// Obtain access to the bus
|
2013-02-11 08:25:45 +00:00
|
|
|
aquire_bus();
|
|
|
|
|
2013-11-23 16:23:21 +00:00
|
|
|
// Read the ADC for z, x, y and then z again
|
|
|
|
while(1) {
|
|
|
|
setup_z();
|
|
|
|
for(i = 0; i < 7; i++)
|
|
|
|
sampleBuf[i] = read_z();
|
|
|
|
filter();
|
|
|
|
pt->z = (coord_t)sampleBuf[3];
|
|
|
|
|
|
|
|
setup_x();
|
|
|
|
for(i = 0; i < 7; i++)
|
|
|
|
sampleBuf[i] = read_x();
|
|
|
|
filter();
|
|
|
|
pt->x = (coord_t)sampleBuf[3];
|
|
|
|
|
|
|
|
setup_y();
|
|
|
|
for(i = 0; i < 7; i++)
|
|
|
|
sampleBuf[i] = read_y();
|
|
|
|
filter();
|
|
|
|
pt->y = (coord_t)sampleBuf[3];
|
|
|
|
|
|
|
|
pt->buttons = pt->z >= 80 ? GINPUT_TOUCH_PRESSED : 0;
|
|
|
|
|
|
|
|
setup_z();
|
|
|
|
for(i = 0; i < 7; i++)
|
|
|
|
sampleBuf[i] = read_z();
|
|
|
|
filter();
|
|
|
|
i = (coord_t)sampleBuf[3] >= 80 ? GINPUT_TOUCH_PRESSED : 0;
|
|
|
|
|
|
|
|
if (pt->buttons == i)
|
|
|
|
break;
|
|
|
|
}
|
2013-02-11 08:25:45 +00:00
|
|
|
|
|
|
|
// Release the bus
|
|
|
|
release_bus();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* GFX_USE_GINPUT && GINPUT_NEED_MOUSE */
|
|
|
|
/** @} */
|