From 6b35fc8ddb050dd891352e646044ffcfba80045d Mon Sep 17 00:00:00 2001 From: Arne Meeuw Date: Mon, 18 Dec 2023 11:03:18 +0100 Subject: [PATCH] Add support for CST816S touch interface (#20213) * Add initial version (prints coordinates) * Add CST816S_found * Revert formatting * Add supported gestures (untested) * Correct use of enums * Remove library dependency * Unification of methods * Remove redundant variables and format --- tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 71 +++++++++++++++++-- .../xdsp_17_universal.ino | 16 ++++- 2 files changed, 82 insertions(+), 5 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index 01bbe97ca..44eca227e 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -71,6 +71,7 @@ typedef struct TSGlobal_t { TSGlobal_t TSGlobal; +bool CST816S_found = false; bool FT5206_found = false; bool GT911_found = false; bool XPT2046_found = false; @@ -97,7 +98,7 @@ void Touch_SetStatus(uint8_t touches, uint16_t raw_x, uint16_t raw_y, uint8_t ge // return true if succesful, false if not configured bool Touch_GetStatus(uint8_t* touches, uint16_t* x, uint16_t* y, uint8_t* gesture, uint16_t* raw_x, uint16_t* raw_y) { - if (TSGlobal.external_ts || FT5206_found || XPT2046_found) { + if (TSGlobal.external_ts || CST816S_found || FT5206_found || XPT2046_found) { if (touches) { *touches = TSGlobal.touches; } if (x) { *x = TSGlobal.touch_xp; } if (y) { *y = TSGlobal.touch_yp; } @@ -110,7 +111,7 @@ bool Touch_GetStatus(uint8_t* touches, uint16_t* x, uint16_t* y, uint8_t* gestur } uint32_t Touch_Status(int32_t sel) { - if (TSGlobal.external_ts || FT5206_found || GT911_found || XPT2046_found || SRES_found) { + if (TSGlobal.external_ts || CST816S_found || FT5206_found || GT911_found || XPT2046_found || SRES_found) { switch (sel) { case 0: return TSGlobal.touched; @@ -185,6 +186,62 @@ int16_t SRES_y() { } #endif +#ifdef USE_CST816S +#undef CST816S_address +#define CST816S_address 0x15 + +bool CST816S_event_available = false; +uint8_t CST816S_bus = 0; + +uint8_t CST816S_map_gesture(uint8_t gesture) { + switch (gesture) { + case 0x01: return TS_Gest_Move_Up; // SWIPE_UP + case 0x02: return TS_Gest_Move_Down; // SWIPE_DOWN + case 0x03: return TS_Gest_Move_Left; // SWIPE_LEFT + case 0x04: return TS_Gest_Move_Right; // SWIPE_RIGHT + case 0x05: return TS_Gest_None; // SINGLE_CLICK + case 0x0B: return TS_Gest_None; // DOUBLE_CLICK + case 0x0C: return TS_Gest_None; // LONG_PRESS + default: return TS_Gest_None; // NONE + } +} + +bool CST816S_available() { + if (CST816S_event_available) { + byte data_raw[8]; + I2cReadBuffer(CST816S_address, 0x01, data_raw, 6, CST816S_bus); + TSGlobal.raw_touch_xp = ((data_raw[2] & 0xF) << 8) + data_raw[3]; + TSGlobal.raw_touch_yp = ((data_raw[4] & 0xF) << 8) + data_raw[5]; + TSGlobal.gesture = CST816S_map_gesture(data_raw[0]); + CST816S_event_available = false; + return true; + } + return false; +} + +bool CST816S_Touch_Init(uint8_t bus, int8_t irq_pin, int8_t rst_pin, int interrupt = RISING) { + CST816S_found = false; + CST816S_bus = bus; + pinMode(irq_pin, INPUT); + pinMode(rst_pin, OUTPUT); + digitalWrite(rst_pin, HIGH); + delay(50); + digitalWrite(rst_pin, LOW); + delay(5); + digitalWrite(rst_pin, HIGH); + delay(50); + uint8_t version; + I2cReadBuffer(CST816S_address, 0x15, &version, 1, CST816S_bus); + delay(5); + uint8_t versionInfo[3]; + I2cReadBuffer(CST816S_address, 0xA7, versionInfo, 3, CST816S_bus); + attachInterrupt(irq_pin, []{ CST816S_event_available = true; }, interrupt); + CST816S_found = true; + AddLog(LOG_LEVEL_INFO, PSTR("TI: CST816S, version: %d, versionInfo: %d.%d.%d"), version, versionInfo[0], versionInfo[1], versionInfo[2]); + return CST816S_found; +} +#endif // USE_CST816S + #ifdef USE_FT5206 #include // touch panel controller @@ -307,6 +364,12 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { } #endif +#ifdef USE_CST816S + if (CST816S_found) { + TSGlobal.touched = CST816S_available(); + } +#endif // USE_CST816S + #ifdef USE_FT5206 if (FT5206_found) { TSGlobal.touched = FT5206_touched(); @@ -360,7 +423,7 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { #endif // USE_TOUCH_BUTTONS rotconvert(&TSGlobal.touch_xp, &TSGlobal.touch_yp); - AddLog(LOG_LEVEL_DEBUG_MORE, "TS : TSGlobal.touched x=%i y=%i (raw x=%i y=%i)", TSGlobal.touch_xp, TSGlobal.touch_yp, TSGlobal.raw_touch_xp, TSGlobal.raw_touch_yp); + AddLog(LOG_LEVEL_DEBUG_MORE, "TS : TSGlobal.touched x=%i y=%i gest=0x%02x (raw x=%i y=%i)", TSGlobal.touch_xp, TSGlobal.touch_yp, TSGlobal.gesture, TSGlobal.raw_touch_xp, TSGlobal.raw_touch_yp); #ifdef USE_TOUCH_BUTTONS CheckTouchButtons(TSGlobal.touched, TSGlobal.touch_xp, TSGlobal.touch_yp); @@ -522,7 +585,7 @@ bool Xdrv55(uint32_t function) { case FUNC_INIT: break; case FUNC_EVERY_100_MSECOND: - if (FT5206_found || XPT2046_found || GT911_found || SRES_found) { + if (CST816S_found || FT5206_found || XPT2046_found || GT911_found || SRES_found) { Touch_Check(TS_RotConvert); } break; diff --git a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino index c0a1a377f..b9213ed17 100644 --- a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino +++ b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino @@ -35,6 +35,8 @@ extern FS *ffsp; #undef GT911_address #define GT911_address 0x5D +#undef CST816S_address +#define CST816S_address 0x15 enum {GPIO_DP_RES=GPIO_SENSOR_END-1,GPIO_DP_CS,GPIO_DP_RS,GPIO_DP_WR,GPIO_DP_RD,GPIO_DPAR0,GPIO_DPAR1,GPIO_DPAR2,GPIO_DPAR3,GPIO_DPAR4,GPIO_DPAR5,GPIO_DPAR6,GPIO_DPAR7,GPIO_DPAR8,GPIO_DPAR9,GPIO_DPAR10,GPIO_DPAR11,GPIO_DPAR12,GPIO_DPAR13,GPIO_DPAR14,GPIO_DPAR15}; @@ -312,7 +314,7 @@ int8_t cs; udisp = new uDisplay(ddesc); // checck for touch option TI1 or TI2 -#if defined(USE_FT5206) || defined(USE_GT911) +#if defined (USE_CST816S) || defined(USE_FT5206) || defined(USE_GT911) cp = strstr(ddesc, ":TI"); if (cp) { uint8_t wire_n = 1; @@ -350,6 +352,8 @@ int8_t cs; if (I2cSetDevice(i2caddr, wire_n)) { if (i2caddr == GT911_address) { I2cSetActiveFound(i2caddr, "GT911", wire_n); + } else if (i2caddr == CST816S_address) { + I2cSetActiveFound(i2caddr, "CST816S", wire_n); } else { I2cSetActiveFound(i2caddr, "FT5206", wire_n); } @@ -361,6 +365,8 @@ int8_t cs; if (I2cSetDevice(i2caddr)) { if (i2caddr == GT911_address) { I2cSetActiveFound(i2caddr, "GT911"); + } else if (i2caddr == CST816S_address) { + I2cSetActiveFound(i2caddr, "CST816S"); } else { I2cSetActiveFound(i2caddr, "FT5206"); } @@ -373,6 +379,10 @@ int8_t cs; #ifdef USE_GT911 if (!wire_n) GT911_Touch_Init(&Wire, irq, rst, xs, ys); else GT911_Touch_Init(&Wire1, irq, rst, xs, ys); +#endif + } else if (i2caddr == CST816S_address) { +#ifdef USE_CST816S + CST816S_Touch_Init(wire_n, irq, rst); #endif } else { #ifdef USE_FT5206 @@ -386,6 +396,10 @@ int8_t cs; if (i2caddr == GT911_address) { #ifdef USE_GT911 if (!wire_n) GT911_Touch_Init(&Wire, irq, rst, xs, ys); +#endif + } else if (i2caddr == CST816S_address) { +#ifdef USE_CST816S + CST816S_Touch_Init(wire_n, irq, rst); #endif } else { #ifdef USE_FT5206