XPT2046: Handle MKS touchscreen w/out PenIRQ pin (#14640)

This commit is contained in:
Tanguy Pruvot 2019-07-17 11:41:10 +02:00 committed by Scott Lahteine
parent f2c5740d06
commit 9dfa5ba3a5
3 changed files with 28 additions and 7 deletions

View file

@ -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];

View file

@ -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);
};

View file

@ -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