mirror of
https://github.com/Bodmer/TFT_eSPI.git
synced 2025-08-05 05:34:43 +02:00
Improved touch detection and position reliability
This commit is contained in:
51
TFT_eSPI.cpp
51
TFT_eSPI.cpp
@@ -3735,24 +3735,29 @@ uint16_t TFT_eSPI::getTouchRawZ(void){
|
|||||||
uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
|
uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
|
||||||
uint16_t x_tmp, y_tmp, x_tmp2, y_tmp2;
|
uint16_t x_tmp, y_tmp, x_tmp2, y_tmp2;
|
||||||
|
|
||||||
|
// Wait until pressure stops increasing
|
||||||
|
uint16_t z1 = 1;
|
||||||
|
uint16_t z2 = 0;
|
||||||
|
while (z1 > z2)
|
||||||
|
{
|
||||||
|
z2 = z1;
|
||||||
|
z1 = getTouchRawZ();
|
||||||
|
delay(1);
|
||||||
|
}
|
||||||
|
|
||||||
// uint16_t z_tmp = getTouchRawZ(); // Save value for debug message
|
// Serial.print("Z = ");Serial.println(z1);
|
||||||
// Debug messages used for tuning
|
|
||||||
// Serial.print("Z = ");Serial.println(z_tmp);
|
if (z1 <= threshold) return false;
|
||||||
|
|
||||||
if (getTouchRawZ() <= threshold) return false;
|
|
||||||
|
|
||||||
delay(2); // Small debounce delay to the next sample
|
|
||||||
getTouchRaw(&x_tmp,&y_tmp);
|
getTouchRaw(&x_tmp,&y_tmp);
|
||||||
|
|
||||||
// z_tmp = getTouchRawZ();
|
|
||||||
// Serial.print("Sample 1 x,y = "); Serial.print(x_tmp);Serial.print(",");Serial.print(y_tmp);
|
// Serial.print("Sample 1 x,y = "); Serial.print(x_tmp);Serial.print(",");Serial.print(y_tmp);
|
||||||
// Serial.print(", Z = ");Serial.println(z_tmp);
|
// Serial.print(", Z = ");Serial.println(z1);
|
||||||
|
|
||||||
delay(2); // Small debounce delay to the next sample
|
delay(1); // Small delay to the next sample
|
||||||
if (getTouchRawZ() <= threshold) return false;
|
if (getTouchRawZ() <= threshold) return false;
|
||||||
|
|
||||||
delay(2); // Small debounce delay to the next sample
|
delay(2); // Small delay to the next sample
|
||||||
getTouchRaw(&x_tmp2,&y_tmp2);
|
getTouchRaw(&x_tmp2,&y_tmp2);
|
||||||
|
|
||||||
// Serial.print("Sample 2 x,y = "); Serial.print(x_tmp2);Serial.print(",");Serial.println(y_tmp2);
|
// Serial.print("Sample 2 x,y = "); Serial.print(x_tmp2);Serial.print(",");Serial.println(y_tmp2);
|
||||||
@@ -3771,28 +3776,32 @@ uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){
|
|||||||
** Function name: getTouch
|
** Function name: getTouch
|
||||||
** Description: read callibrated position. Return false if not pressed.
|
** Description: read callibrated position. Return false if not pressed.
|
||||||
***************************************************************************************/
|
***************************************************************************************/
|
||||||
#define Z_THRESHOLD 0x200 // Touch pressure threshold for validating touches
|
#define Z_THRESHOLD 350 // Touch pressure threshold for validating touches
|
||||||
uint8_t TFT_eSPI::getTouch(uint16_t *x, uint16_t *y){
|
uint8_t TFT_eSPI::getTouch(uint16_t *x, uint16_t *y){
|
||||||
uint16_t x_tmp, y_tmp;
|
uint16_t x_tmp, y_tmp, xx, yy;
|
||||||
|
|
||||||
if(!validTouch(&x_tmp, &y_tmp, Z_THRESHOLD)) return false;
|
if(!validTouch(&x_tmp, &y_tmp, Z_THRESHOLD)) return false;
|
||||||
|
|
||||||
if(!touchCalibration_rotate){
|
if(!touchCalibration_rotate){
|
||||||
*x=(x_tmp-touchCalibration_x0)*_width/touchCalibration_x1;
|
xx=(x_tmp-touchCalibration_x0)*_width/touchCalibration_x1;
|
||||||
*y=(y_tmp-touchCalibration_y0)*_height/touchCalibration_y1;
|
yy=(y_tmp-touchCalibration_y0)*_height/touchCalibration_y1;
|
||||||
if(touchCalibration_invert_x)
|
if(touchCalibration_invert_x)
|
||||||
*x = _width - *x;
|
xx = _width - xx;
|
||||||
if(touchCalibration_invert_y)
|
if(touchCalibration_invert_y)
|
||||||
*y = _height - *y;
|
yy = _height - yy;
|
||||||
} else {
|
} else {
|
||||||
*y=(x_tmp-touchCalibration_x0)*_height/touchCalibration_x1;
|
yy=(x_tmp-touchCalibration_x0)*_height/touchCalibration_x1;
|
||||||
*x=(y_tmp-touchCalibration_y0)*_width/touchCalibration_y1;
|
xx=(y_tmp-touchCalibration_y0)*_width/touchCalibration_y1;
|
||||||
if(touchCalibration_invert_x)
|
if(touchCalibration_invert_x)
|
||||||
*x = _width - *x;
|
xx = _width - xx;
|
||||||
if(touchCalibration_invert_y)
|
if(touchCalibration_invert_y)
|
||||||
*y = _height - *y;
|
yy = _height - yy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (xx >= _width || yy >= _height) return false;
|
||||||
|
|
||||||
|
*x = xx;
|
||||||
|
*y = yy;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user