From 2e84218ecbd5105dd0007268d87de70f9d3529fc Mon Sep 17 00:00:00 2001 From: Bodmer Date: Thu, 21 Dec 2017 23:16:11 +0000 Subject: [PATCH] Add touch pressure hysterisis and user defined sensitivity getTouch accepts a third optional pressure threshold, default is 600, increasing means harder presses are needed. getTouch(uint16_t *x, uint16_t *y, uint16_t threshold = 600); --- TFT_eSPI.cpp | 26 ++++++++++++++++++++------ TFT_eSPI.h | 4 +++- library.json | 2 +- library.properties | 2 +- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/TFT_eSPI.cpp b/TFT_eSPI.cpp index 641874a..15578d5 100644 --- a/TFT_eSPI.cpp +++ b/TFT_eSPI.cpp @@ -4114,10 +4114,22 @@ uint8_t TFT_eSPI::validTouch(uint16_t *x, uint16_t *y, uint16_t threshold){ ** Description: read callibrated position. Return false if not pressed. ***************************************************************************************/ #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 threshold){ uint16_t x_tmp, y_tmp, xx, yy; - if(!validTouch(&x_tmp, &y_tmp, Z_THRESHOLD)) return false; + if (threshold<20) threshold = 20; + if (_pressTime > millis()) threshold=20; + + byte n = 5; + byte valid = 0; + while (n--) + { + if (validTouch(&x_tmp, &y_tmp, threshold)) valid++;; + } + + if (valid<1) { _pressTime = 0; return false; } + + _pressTime = millis() + 50; if(!touchCalibration_rotate){ xx=(x_tmp-touchCalibration_x0)*_width/touchCalibration_x1; @@ -4135,11 +4147,13 @@ uint8_t TFT_eSPI::getTouch(uint16_t *x, uint16_t *y){ yy = _height - yy; } - if (xx >= _width || yy >= _height) return false; + if (xx >= _width || yy >= _height) return valid; - *x = xx; - *y = yy; - return true; + _pressX = xx; + _pressY = yy; + *x = _pressX; + *y = _pressY; + return valid; } /*************************************************************************************** diff --git a/TFT_eSPI.h b/TFT_eSPI.h index ae7f3c0..c291c92 100644 --- a/TFT_eSPI.h +++ b/TFT_eSPI.h @@ -458,7 +458,7 @@ class TFT_eSPI : public Print { // These are associated with the Touch Screen handlers uint8_t getTouchRaw(uint16_t *x, uint16_t *y); uint16_t getTouchRawZ(void); - uint8_t getTouch(uint16_t *x, uint16_t *y); + uint8_t getTouch(uint16_t *x, uint16_t *y, uint16_t threshold = 600); void calibrateTouch(uint16_t *data, uint32_t color_fg, uint32_t color_bg, uint8_t size); void setTouch(uint16_t *data); @@ -488,6 +488,8 @@ class TFT_eSPI : public Print { // Initialise with example calibration values so processor does not crash if setTouch() not called in setup() uint16_t touchCalibration_x0 = 300, touchCalibration_x1 = 3600, touchCalibration_y0 = 300, touchCalibration_y1 = 3600; uint8_t touchCalibration_rotate = 1, touchCalibration_invert_x = 2, touchCalibration_invert_y = 0; + uint32_t _pressTime; + uint16_t _pressX, _pressY; protected: diff --git a/library.json b/library.json index 1de946d..d189858 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "TFT_eSPI", - "version": "0.17.19", + "version": "0.17.20", "keywords": "TFT, ESP8266, NodeMCU, ESP32, ILI9341, ST7735, ILI9163, S6D02A1, ILI9486", "description": "A TFT SPI graphics library for ESP8266", "repository": diff --git a/library.properties b/library.properties index 59804c6..61ea335 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TFT_eSPI -version=0.17.19 +version=0.17.20 author=Bodmer maintainer=Bodmer sentence=A fast TFT library for ESP8266 processors and the Arduino IDE