From 9dfa5ba3a58d7e2c5be1ef19d9b3a47497671a67 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Wed, 17 Jul 2019 11:41:10 +0200 Subject: [PATCH] XPT2046: Handle MKS touchscreen w/out PenIRQ pin (#14640) --- Marlin/src/feature/touch/xpt2046.cpp | 23 ++++++++++++++++++----- Marlin/src/feature/touch/xpt2046.h | 9 +++++++-- Marlin/src/pins/pinsDebug_list.h | 3 +++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Marlin/src/feature/touch/xpt2046.cpp b/Marlin/src/feature/touch/xpt2046.cpp index d93e99681..a080e880c 100644 --- a/Marlin/src/feature/touch/xpt2046.cpp +++ b/Marlin/src/feature/touch/xpt2046.cpp @@ -44,13 +44,16 @@ XPT2046 touch; extern int8_t encoderDiff; void XPT2046::init(void) { - SET_INPUT(TOUCH_INT_PIN); // Pendrive interrupt pin, used as polling in getInTouch SET_INPUT(TOUCH_MISO_PIN); SET_OUTPUT(TOUCH_MOSI_PIN); - - OUT_WRITE(TOUCH_SCK_PIN, 0); + SET_OUTPUT(TOUCH_SCK_PIN); OUT_WRITE(TOUCH_CS_PIN, 1); + #if PIN_EXISTS(TOUCH_INT) + // Optional Pendrive interrupt pin + SET_INPUT(TOUCH_INT_PIN); + #endif + // Read once to enable pendrive status pin getInTouch(XPT2046_X); } @@ -74,10 +77,10 @@ uint8_t XPT2046::read_buttons() { // We rely on XPT2046 compatible mode to ADS7843, hence no Z1 and Z2 measurements possible. - if (READ(TOUCH_INT_PIN)) return 0; // If HIGH there are no screen presses. + if (!isTouched()) return 0; const uint16_t x = uint16_t(((uint32_t(getInTouch(XPT2046_X))) * tsoffsets[0]) >> 16) + tsoffsets[1], y = uint16_t(((uint32_t(getInTouch(XPT2046_Y))) * tsoffsets[2]) >> 16) + tsoffsets[3]; - if (READ(TOUCH_INT_PIN)) return 0; // Fingers must still be on the TS for a valid read. + if (!isTouched()) return 0; // Fingers must still be on the TS for a valid read. if (y < 185 || y > 224) return 0; @@ -88,6 +91,16 @@ uint8_t XPT2046::read_buttons() { return 0; } +bool XPT2046::isTouched() { + return ( + #if PIN_EXISTS(TOUCH_INT) + READ(TOUCH_INT_PIN) != HIGH + #else + getInTouch(XPT2046_Z1) >= XPT2046_Z1_TRESHHOLD + #endif + ); +} + uint16_t XPT2046::getInTouch(const XPTCoordinate coordinate) { uint16_t data[3]; diff --git a/Marlin/src/feature/touch/xpt2046.h b/Marlin/src/feature/touch/xpt2046.h index 26814926a..901c3c4a4 100644 --- a/Marlin/src/feature/touch/xpt2046.h +++ b/Marlin/src/feature/touch/xpt2046.h @@ -28,15 +28,20 @@ #define XPT2046_CONTROL 0x80 enum XPTCoordinate : uint8_t { - XPT2046_X = 0x10, - XPT2046_Y = 0x50 + XPT2046_X = 0x10, + XPT2046_Y = 0x50, + XPT2046_Z1 = 0x30, + XPT2046_Z2 = 0x40 }; +#define XPT2046_Z1_TRESHHOLD 10 + class XPT2046 { public: static void init(void); static uint8_t read_buttons(); private: + static bool isTouched(); static uint16_t getInTouch(const XPTCoordinate coordinate); }; diff --git a/Marlin/src/pins/pinsDebug_list.h b/Marlin/src/pins/pinsDebug_list.h index 19bdb0aad..3d3c9bb82 100644 --- a/Marlin/src/pins/pinsDebug_list.h +++ b/Marlin/src/pins/pinsDebug_list.h @@ -1172,3 +1172,6 @@ #if PIN_EXISTS(TOUCH_CS) REPORT_NAME_DIGITAL(__LINE__, TOUCH_CS_PIN) #endif +#if PIN_EXISTS(TOUCH_INT) + REPORT_NAME_DIGITAL(__LINE__, TOUCH_INT_PIN) +#endif