From 2f4b98ea2a4391df2353ef7e250c792e4fb5877d Mon Sep 17 00:00:00 2001 From: Arne Meeuw Date: Mon, 11 Dec 2023 23:33:26 +0100 Subject: [PATCH 1/8] Add initial version (prints coordinates) --- lib/lib_display/CST816S/CST816S.cpp | 206 +++++++ lib/lib_display/CST816S/CST816S.h | 80 +++ tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 514 ++++++++++++------ .../xdsp_17_universal.ino | 466 ++++++++++------ 4 files changed, 921 insertions(+), 345 deletions(-) create mode 100644 lib/lib_display/CST816S/CST816S.cpp create mode 100644 lib/lib_display/CST816S/CST816S.h diff --git a/lib/lib_display/CST816S/CST816S.cpp b/lib/lib_display/CST816S/CST816S.cpp new file mode 100644 index 000000000000..f879d857e668 --- /dev/null +++ b/lib/lib_display/CST816S/CST816S.cpp @@ -0,0 +1,206 @@ +/* + MIT License + + Copyright (c) 2021 Felix Biego + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#include "Arduino.h" +#include +#include + +#include "CST816S.h" + +/*! + @brief Constructor for CST816S + @param port + i2c port + @param rst + touch reset pin + @param irq + touch interrupt pin +*/ +CST816S::CST816S(TwoWire *use_wire, int8_t irq, int8_t rst) +{ + wire = use_wire; + _rst = rst; + _irq = irq; +} + +/*! + @brief read touch data +*/ +void CST816S::read_touch() +{ + byte data_raw[8]; + i2c_read(CST816S_address, 0x01, data_raw, 6); + + data.gestureID = data_raw[0]; + data.points = data_raw[1]; + data.event = data_raw[2] >> 6; + data.x = ((data_raw[2] & 0xF) << 8) + data_raw[3]; + data.y = ((data_raw[4] & 0xF) << 8) + data_raw[5]; +} + +/*! + @brief handle interrupts +*/ +void IRAM_ATTR CST816S::handleISR(void) +{ + _event_available = true; +} + +/*! + @brief initialize the touch screen + @param interrupt + type of interrupt FALLING, RISING.. +*/ +void CST816S::begin(int interrupt) +{ + pinMode(_irq, INPUT); + pinMode(_rst, OUTPUT); + + digitalWrite(_rst, HIGH); + delay(50); + digitalWrite(_rst, LOW); + delay(5); + digitalWrite(_rst, HIGH); + delay(50); + + i2c_read(CST816S_address, 0x15, &data.version, 1); + delay(5); + i2c_read(CST816S_address, 0xA7, data.versionInfo, 3); + + attachInterrupt(_irq, std::bind(&CST816S::handleISR, this), interrupt); +} + +/*! + @brief check for a touch event +*/ +bool CST816S::available() +{ + if (_event_available) + { + read_touch(); + _event_available = false; + return true; + } + return false; +} + +/*! + @brief put the touch screen in standby mode +*/ +void CST816S::sleep() +{ + digitalWrite(_rst, LOW); + delay(5); + digitalWrite(_rst, HIGH); + delay(50); + byte standby_value = 0x03; + i2c_write(CST816S_address, 0xA5, &standby_value, 1); +} + +/*! + @brief get the gesture event name +*/ +String CST816S::gesture() +{ + switch (data.gestureID) + { + case NONE: + return "NONE"; + break; + case SWIPE_DOWN: + return "SWIPE DOWN"; + break; + case SWIPE_UP: + return "SWIPE UP"; + break; + case SWIPE_LEFT: + return "SWIPE LEFT"; + break; + case SWIPE_RIGHT: + return "SWIPE RIGHT"; + break; + case SINGLE_CLICK: + return "SINGLE CLICK"; + break; + case DOUBLE_CLICK: + return "DOUBLE CLICK"; + break; + case LONG_PRESS: + return "LONG PRESS"; + break; + default: + return "UNKNOWN"; + break; + } +} + +/*! + @brief read data from i2c + @param addr + i2c device address + @param reg_addr + device register address + @param reg_data + array to copy the read data + @param length + length of data +*/ +uint8_t CST816S::i2c_read(uint16_t addr, uint8_t reg_addr, uint8_t *reg_data, size_t length) +{ + wire->beginTransmission(addr); + wire->write(reg_addr); + if (wire->endTransmission(true)) + return -1; + wire->requestFrom(addr, length, true); + for (int i = 0; i < length; i++) + { + *reg_data++ = wire->read(); + } + return 0; +} + +/*! + @brief write data to i2c + @brief read data from i2c + @param addr + i2c device address + @param reg_addr + device register address + @param reg_data + data to be sent + @param length + length of data +*/ +uint8_t CST816S::i2c_write(uint8_t addr, uint8_t reg_addr, const uint8_t *reg_data, size_t length) +{ + wire->beginTransmission(addr); + wire->write(reg_addr); + for (int i = 0; i < length; i++) + { + wire->write(*reg_data++); + } + if (wire->endTransmission(true)) + return -1; + return 0; +} \ No newline at end of file diff --git a/lib/lib_display/CST816S/CST816S.h b/lib/lib_display/CST816S/CST816S.h new file mode 100644 index 000000000000..4ed48b74e9eb --- /dev/null +++ b/lib/lib_display/CST816S/CST816S.h @@ -0,0 +1,80 @@ +/* + MIT License + + Copyright (c) 2021 Felix Biego + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#ifndef CST816S_H +#define CST816S_H + +#include + +#define CST816S_address 0x15 + +enum GESTURE +{ + NONE = 0x00, + SWIPE_UP = 0x01, + SWIPE_DOWN = 0x02, + SWIPE_LEFT = 0x03, + SWIPE_RIGHT = 0x04, + SINGLE_CLICK = 0x05, + DOUBLE_CLICK = 0x0B, + LONG_PRESS = 0x0C + +}; + +struct data_struct +{ + byte gestureID; // Gesture ID + byte points; // Number of touch points + byte event; // Event (0 = Down, 1 = Up, 2 = Contact) + int x; + int y; + uint8_t version; + uint8_t versionInfo[3]; +}; + +class CST816S +{ + +public: + CST816S(TwoWire *use_wire, int8_t irq = 0, int8_t rst = 1); + void begin(int interrupt = RISING); + void sleep(); + bool available(); + data_struct data; + String gesture(); + +private: + TwoWire *wire; + int8_t _rst; + int8_t _irq; + bool _event_available; + + void IRAM_ATTR handleISR(); + void read_touch(); + + uint8_t i2c_read(uint16_t addr, uint8_t reg_addr, uint8_t *reg_data, size_t length); + uint8_t i2c_write(uint8_t addr, uint8_t reg_addr, const uint8_t *reg_data, size_t length); +}; + +#endif \ No newline at end of file diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index 01bbe97cabed..9c56a9c4d413 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -35,7 +35,6 @@ * void TS_RotConvert(int16_t *x, int16_t *y) - calls the renderer's rotation converter \*******************************************************************************************/ - #if defined(USE_LVGL_TOUCHSCREEN) || defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_GT911) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) || defined(SIMPLE_RES_TOUCH) #ifdef USE_DISPLAY_LVGL_ONLY @@ -44,10 +43,11 @@ #include -#define XDRV_55 55 +#define XDRV_55 55 // Codes for gestures, when supported by the Touch Screen controller -enum TS_Gesture { +enum TS_Gesture +{ TS_Gest_None = 0, TS_Gest_Move_Up = 0x10, TS_Gest_Move_Down = 0x11, @@ -57,13 +57,14 @@ enum TS_Gesture { TS_Gest_Zoom_Out = 0x21, }; -typedef struct TSGlobal_t { +typedef struct TSGlobal_t +{ int16_t raw_touch_xp = 0; int16_t raw_touch_yp = 0; int16_t touch_xp = 0; int16_t touch_yp = 0; - uint8_t touches = 0; // number of touches for multi-touch - uint8_t gesture = 0; // gesture code + uint8_t touches = 0; // number of touches for multi-touch + uint8_t gesture = 0; // gesture code // multi-point is not yet supported bool touched = false; bool external_ts = false; @@ -73,6 +74,7 @@ TSGlobal_t TSGlobal; bool FT5206_found = false; bool GT911_found = false; +bool CST816S_found = false; bool XPT2046_found = false; bool SRES_found = false; @@ -84,7 +86,8 @@ bool SRES_found = false; VButton *buttons[MAX_TOUCH_BUTTONS]; #endif -void Touch_SetStatus(uint8_t touches, uint16_t raw_x, uint16_t raw_y, uint8_t gesture) { +void Touch_SetStatus(uint8_t touches, uint16_t raw_x, uint16_t raw_y, uint8_t gesture) +{ TSGlobal.external_ts = true; TSGlobal.gesture = gesture; TSGlobal.touches = touches; @@ -95,36 +98,61 @@ 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 (touches) { *touches = TSGlobal.touches; } - if (x) { *x = TSGlobal.touch_xp; } - if (y) { *y = TSGlobal.touch_yp; } - if (raw_x) { *raw_x = TSGlobal.raw_touch_xp; } - if (raw_y) { *raw_y = TSGlobal.raw_touch_yp; } - if (gesture) { *touches = TSGlobal.gesture; } +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 (touches) + { + *touches = TSGlobal.touches; + } + if (x) + { + *x = TSGlobal.touch_xp; + } + if (y) + { + *y = TSGlobal.touch_yp; + } + if (raw_x) + { + *raw_x = TSGlobal.raw_touch_xp; + } + if (raw_y) + { + *raw_y = TSGlobal.raw_touch_yp; + } + if (gesture) + { + *touches = TSGlobal.gesture; + } return true; } return false; } -uint32_t Touch_Status(int32_t sel) { - if (TSGlobal.external_ts || FT5206_found || GT911_found || XPT2046_found || SRES_found) { - switch (sel) { - case 0: - return TSGlobal.touched; - case 1: - return TSGlobal.touch_xp; - case 2: - return TSGlobal.touch_yp; - case -1: // before calibration - return TSGlobal.raw_touch_xp; - case -2: - return TSGlobal.raw_touch_yp; +uint32_t Touch_Status(int32_t sel) +{ + if (TSGlobal.external_ts || FT5206_found || GT911_found || XPT2046_found || SRES_found) + { + switch (sel) + { + case 0: + return TSGlobal.touched; + case 1: + return TSGlobal.touch_xp; + case 2: + return TSGlobal.touch_yp; + case -1: // before calibration + return TSGlobal.raw_touch_xp; + case -2: + return TSGlobal.raw_touch_yp; } return 0; - } else { + } + else + { return 0; } } @@ -133,12 +161,12 @@ uint32_t Touch_Status(int32_t sel) { uint8_t tbstate[3]; #endif // USE_M5STACK_CORE2 - // simple resistive touch pins // with dma it should check for active transfers // so currently dont use dma #ifdef SIMPLE_RES_TOUCH -struct RES_TOUCH { +struct RES_TOUCH +{ int8_t xplus; int8_t xminus; int8_t yplus; @@ -147,10 +175,11 @@ struct RES_TOUCH { uint16_t yp; } sres_touch; -void Simple_ResTouch_Init(int8_t xp, int8_t xm, int8_t yp, int8_t ym) { - sres_touch.xplus = xp; // d1 +void Simple_ResTouch_Init(int8_t xp, int8_t xm, int8_t yp, int8_t ym) +{ + sres_touch.xplus = xp; // d1 sres_touch.xminus = xm; // cs - sres_touch.yplus = yp; // rs + sres_touch.yplus = yp; // rs sres_touch.yminus = ym; // d0 SRES_found = true; AddLog(LOG_LEVEL_INFO, PSTR("TS: simple resistive touch init")); @@ -158,33 +187,70 @@ void Simple_ResTouch_Init(int8_t xp, int8_t xm, int8_t yp, int8_t ym) { #define SRES_THRESHOLD 500 -bool SRES_touched() { +bool SRES_touched() +{ uint32_t val = renderer->get_sr_touch(sres_touch.xplus, sres_touch.xminus, sres_touch.yplus, sres_touch.yminus); - if (val == 0) { + if (val == 0) + { return false; } sres_touch.xp = val >> 16; - sres_touch.yp = val & 0xffff; + sres_touch.yp = val & 0xffff; int16_t xp = sres_touch.xp; int16_t yp = sres_touch.yp; - //AddLog(LOG_LEVEL_INFO, "TS x=%i y=%i)", xp, yp); + // AddLog(LOG_LEVEL_INFO, "TS x=%i y=%i)", xp, yp); - if (xp > SRES_THRESHOLD && yp > SRES_THRESHOLD) { + if (xp > SRES_THRESHOLD && yp > SRES_THRESHOLD) + { return 1; } return 0; } -int16_t SRES_x() { +int16_t SRES_x() +{ return sres_touch.xp; } -int16_t SRES_y() { +int16_t SRES_y() +{ return sres_touch.yp; } #endif +#ifdef USE_CST816S +#include +// touch panel controller +#undef CST816S_address +#define CST816S_address 0x15 + +CST816S *CST816S_touchp; + +bool CST816S_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin) +{ + CST816S_found = false; + CST816S_touchp = new CST816S(i2c, irq_pin, rst_pin); + CST816S_touchp->begin(); + CST816S_found = true; + AddLog(LOG_LEVEL_INFO, PSTR("TI: CST816S")); + return CST816S_found; +} + +bool CST816S_touched() +{ + return CST816S_touchp->available(); +} +int16_t CST816S_x() +{ + return CST816S_touchp->data.x; +} +int16_t CST816S_y() +{ + return CST816S_touchp->data.y; +} +#endif // USE_CST816S + #ifdef USE_FT5206 #include // touch panel controller @@ -193,124 +259,157 @@ int16_t SRES_y() { FT5206_Class *FT5206_touchp; -bool FT5206_Touch_Init(TwoWire &i2c) { +bool FT5206_Touch_Init(TwoWire &i2c) +{ FT5206_found = false; FT5206_touchp = new FT5206_Class(); - if (FT5206_touchp->begin(i2c, FT5206_address)) { + if (FT5206_touchp->begin(i2c, FT5206_address)) + { AddLog(LOG_LEVEL_INFO, PSTR("TI: FT5206")); FT5206_found = true; } - //AddLog(LOG_LEVEL_INFO, PSTR("TS: FT5206 %d"),FT5206_found); + // AddLog(LOG_LEVEL_INFO, PSTR("TS: FT5206 %d"),FT5206_found); return FT5206_found; } -bool FT5206_touched() { +bool FT5206_touched() +{ return FT5206_touchp->touched(); } -int16_t FT5206_x() { +int16_t FT5206_x() +{ TP_Point pLoc = FT5206_touchp->getPoint(0); return pLoc.x; } -int16_t FT5206_y() { +int16_t FT5206_y() +{ TP_Point pLoc = FT5206_touchp->getPoint(0); return pLoc.y; } -#endif // USE_FT5206 +#endif // USE_FT5206 #ifdef USE_GT911 #include // touch panel controller GT911 *GT911_touchp; -bool GT911_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin, uint16_t xs, uint16_t ys) { +bool GT911_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin, uint16_t xs, uint16_t ys) +{ GT911_found = false; GT911_touchp = new GT911(); - if (ESP_OK == GT911_touchp->begin(i2c, irq_pin, rst_pin, xs, ys)) { + if (ESP_OK == GT911_touchp->begin(i2c, irq_pin, rst_pin, xs, ys)) + { AddLog(LOG_LEVEL_INFO, PSTR("TI: GT911")); GT911_found = true; - } else { + } + else + { AddLog(LOG_LEVEL_INFO, PSTR("TI: GT911 failed")); } return GT911_found; } -void GT911_CheckTouch(void) { +void GT911_CheckTouch(void) +{ GT911_touchp->update(); TSGlobal.touched = !GT911_touchp->isFingerUp(); - if (TSGlobal.touched) { + if (TSGlobal.touched) + { TSGlobal.raw_touch_xp = GT911_touchp->readFingerX(0); TSGlobal.raw_touch_yp = GT911_touchp->readFingerY(0); } } -#endif // USE_GT911 - +#endif // USE_GT911 #ifdef USE_XPT2046 #include XPT2046_Touchscreen *XPT2046_touchp; -bool XPT2046_Touch_Init(uint16_t CS, int8_t irqpin, uint8_t bus) { +bool XPT2046_Touch_Init(uint16_t CS, int8_t irqpin, uint8_t bus) +{ int8_t sclk = -1; int8_t mosi = -1; int8_t miso = -1; uint8_t xbus = bus; bus &= 1; - #ifdef ESP32 - if (PinUsed(GPIO_SPI_CLK, bus) && PinUsed(GPIO_SPI_MISO, bus) && PinUsed(GPIO_SPI_MOSI, bus)) { +#ifdef ESP32 + if (PinUsed(GPIO_SPI_CLK, bus) && PinUsed(GPIO_SPI_MISO, bus) && PinUsed(GPIO_SPI_MOSI, bus)) + { // must init SPI with pins sclk = Pin(GPIO_SPI_CLK, bus); miso = Pin(GPIO_SPI_MISO, bus); mosi = Pin(GPIO_SPI_MOSI, bus); - } - #endif // ESP32 + } +#endif // ESP32 - #ifdef ESP8266 - if (PinUsed(GPIO_SPI_CLK) && PinUsed(GPIO_SPI_MISO) && PinUsed(GPIO_SPI_MOSI)) { +#ifdef ESP8266 + if (PinUsed(GPIO_SPI_CLK) && PinUsed(GPIO_SPI_MISO) && PinUsed(GPIO_SPI_MOSI)) + { // must init SPI with pins sclk = Pin(GPIO_SPI_CLK); miso = Pin(GPIO_SPI_MISO); mosi = Pin(GPIO_SPI_MOSI); - } - #endif // ESP8266 + } +#endif // ESP8266 XPT2046_touchp = new XPT2046_Touchscreen(CS, irqpin, xbus, sclk, miso, mosi); XPT2046_found = XPT2046_touchp->begin(); - if (XPT2046_found) { - AddLog(LOG_LEVEL_INFO, PSTR("TS: XPT2046")); + if (XPT2046_found) + { + AddLog(LOG_LEVEL_INFO, PSTR("TS: XPT2046")); } return XPT2046_found; } -bool XPT2046_touched() { +bool XPT2046_touched() +{ return XPT2046_touchp->touched(); } -int16_t XPT2046_x() { +int16_t XPT2046_x() +{ TS_Point pLoc = XPT2046_touchp->getPoint(); return pLoc.x; } -int16_t XPT2046_y() { +int16_t XPT2046_y() +{ TS_Point pLoc = XPT2046_touchp->getPoint(); return pLoc.y; } -#endif // USE_XPT2046 - -void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { - static bool was_touched = false; // flag used to log the data sent when the screen was just released +#endif // USE_XPT2046 +void Touch_Check(void (*rotconvert)(int16_t *x, int16_t *y)) +{ + static bool was_touched = false; // flag used to log the data sent when the screen was just released #ifdef SIMPLE_RES_TOUCH - if (SRES_found) { + if (SRES_found) + { TSGlobal.touched = SRES_touched(); - if (TSGlobal.touched) { + if (TSGlobal.touched) + { TSGlobal.raw_touch_xp = SRES_x(); TSGlobal.raw_touch_yp = SRES_y(); } } #endif +#ifdef USE_CST816S + if (CST816S_found) + { + TSGlobal.touched = CST816S_touched(); + if (TSGlobal.touched) + { + TSGlobal.raw_touch_xp = CST816S_x(); + TSGlobal.raw_touch_yp = CST816S_y(); + } + } +#endif // USE_CST816S + #ifdef USE_FT5206 - if (FT5206_found) { + if (FT5206_found) + { TSGlobal.touched = FT5206_touched(); - if (TSGlobal.touched) { + if (TSGlobal.touched) + { TSGlobal.raw_touch_xp = FT5206_x(); TSGlobal.raw_touch_yp = FT5206_y(); } @@ -318,15 +417,18 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { #endif // USE_FT5206 #ifdef USE_GT911 - if (GT911_found) { + if (GT911_found) + { GT911_CheckTouch(); } #endif // USE_FT5206 #ifdef USE_XPT2046 - if (XPT2046_found) { + if (XPT2046_found) + { TSGlobal.touched = XPT2046_touched(); - if (TSGlobal.touched) { + if (TSGlobal.touched) + { TSGlobal.raw_touch_xp = XPT2046_x(); TSGlobal.raw_touch_yp = XPT2046_y(); } @@ -336,7 +438,8 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { TSGlobal.touch_xp = TSGlobal.raw_touch_xp; TSGlobal.touch_yp = TSGlobal.raw_touch_yp; - if (TSGlobal.touched) { + if (TSGlobal.touched) + { was_touched = true; #ifdef USE_TOUCH_BUTTONS #ifdef USE_M5STACK_CORE2 @@ -344,19 +447,22 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { uint16_t xcenter = 80; #define TDELTA 30 #define TYPOS 275 - for (uint32_t tbut = 0; tbut < 3; tbut++) { - if (TSGlobal.touch_xp > (xcenter - TDELTA) && TSGlobal.touch_xp < (xcenter + TDELTA) && TSGlobal.touch_yp > (TYPOS - TDELTA) && TSGlobal.touch_yp < (TYPOS + TDELTA)) { + for (uint32_t tbut = 0; tbut < 3; tbut++) + { + if (TSGlobal.touch_xp > (xcenter - TDELTA) && TSGlobal.touch_xp < (xcenter + TDELTA) && TSGlobal.touch_yp > (TYPOS - TDELTA) && TSGlobal.touch_yp < (TYPOS + TDELTA)) + { // hit a button - if (!(tbstate[tbut] & 1)) { + if (!(tbstate[tbut] & 1)) + { // pressed tbstate[tbut] |= 1; - //AddLog(LOG_LEVEL_INFO, PSTR("tbut: %d pressed"), tbut); + // AddLog(LOG_LEVEL_INFO, PSTR("tbut: %d pressed"), tbut); Touch_MQTT(tbut, "BIB", tbstate[tbut] & 1); } } xcenter += 100; } -#endif // USE_M5STACK_CORE2 +#endif // USE_M5STACK_CORE2 #endif // USE_TOUCH_BUTTONS rotconvert(&TSGlobal.touch_xp, &TSGlobal.touch_yp); @@ -365,138 +471,178 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { #ifdef USE_TOUCH_BUTTONS CheckTouchButtons(TSGlobal.touched, TSGlobal.touch_xp, TSGlobal.touch_yp); #endif // USE_TOUCH_BUTTONS - - } else { + } + else + { #ifdef USE_M5STACK_CORE2 - for (uint32_t tbut = 0; tbut < 3; tbut++) { - if (tbstate[tbut] & 1) { + for (uint32_t tbut = 0; tbut < 3; tbut++) + { + if (tbstate[tbut] & 1) + { // released tbstate[tbut] &= 0xfe; Touch_MQTT(tbut, "BIB", tbstate[tbut] & 1); - //AddLog(LOG_LEVEL_INFO, PSTR("tbut: %d released"), tbut); + // AddLog(LOG_LEVEL_INFO, PSTR("tbut: %d released"), tbut); } } -#endif // USE_M5STACK_CORE2 +#endif // USE_M5STACK_CORE2 - rotconvert(&TSGlobal.touch_xp, &TSGlobal.touch_yp); // still do rot convert if not TSGlobal.touched - if (was_touched) { + rotconvert(&TSGlobal.touch_xp, &TSGlobal.touch_yp); // still do rot convert if not TSGlobal.touched + if (was_touched) + { AddLog(LOG_LEVEL_DEBUG_MORE, "TS : released x=%i y=%i (raw x=%i y=%i)", TSGlobal.touch_xp, TSGlobal.touch_yp, TSGlobal.raw_touch_xp, TSGlobal.raw_touch_yp); was_touched = false; } #ifdef USE_TOUCH_BUTTONS CheckTouchButtons(TSGlobal.touched, TSGlobal.touch_xp, TSGlobal.touch_yp); #endif // USE_TOUCH_BUTTONS - } } - #ifdef USE_TOUCH_BUTTONS -void Touch_MQTT(uint8_t index, const char *cp, uint32_t val) { +void Touch_MQTT(uint8_t index, const char *cp, uint32_t val) +{ #ifdef USE_FT5206 - if (FT5206_found) ResponseTime_P(PSTR(",\"FT5206\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); + if (FT5206_found) + ResponseTime_P(PSTR(",\"FT5206\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); #endif #ifdef USE_XPT2046 - if (XPT2046_found) ResponseTime_P(PSTR(",\"XPT2046\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); -#endif // USE_XPT2046 + if (XPT2046_found) + ResponseTime_P(PSTR(",\"XPT2046\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); +#endif // USE_XPT2046 #ifdef USE_GT911 - if (GT911_found) ResponseTime_P(PSTR(",\"GT911\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); -#endif // USE_XPT2046 + if (GT911_found) + ResponseTime_P(PSTR(",\"GT911\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); +#endif // USE_XPT2046 MqttPublishTeleSensor(); } -void EP_Drawbutton(uint32_t count) { +void EP_Drawbutton(uint32_t count) +{ renderer->ep_update_area(buttons[count]->spars.xp, buttons[count]->spars.yp, buttons[count]->spars.xs, buttons[count]->spars.ys, 3); } -void Touch_RDW_BUTT(uint32_t count, uint32_t pwr) { +void Touch_RDW_BUTT(uint32_t count, uint32_t pwr) +{ buttons[count]->xdrawButton(pwr); EP_Drawbutton(count); - if (pwr) buttons[count]->vpower.on_off = 1; - else buttons[count]->vpower.on_off = 0; + if (pwr) + buttons[count]->vpower.on_off = 1; + else + buttons[count]->vpower.on_off = 0; } -void CheckTouchButtons(bool touched, int16_t touch_x, int16_t touch_y) { +void CheckTouchButtons(bool touched, int16_t touch_x, int16_t touch_y) +{ uint16_t temp; - uint8_t rbutt=0; - uint8_t vbutt=0; - - if (!renderer) return; - if (touched) { - //AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("touch after convert %d - %d"), touch_x, touch_y); - // now must compare with defined buttons - for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) { - if (buttons[count]) { - if (!buttons[count]->vpower.slider) { - if (!buttons[count]->vpower.disable) { - if (buttons[count]->contains(touch_x, touch_y)) { - // did hit - buttons[count]->press(true); - if (buttons[count]->justPressed()) { - if (!buttons[count]->vpower.is_virtual) { - uint8_t pwr=bitRead(TasmotaGlobal.power, rbutt); - if (!SendKey(KEY_BUTTON, rbutt+1, POWER_TOGGLE)) { - Touch_RDW_BUTT(count, !pwr); - ExecuteCommandPower(rbutt+1, POWER_TOGGLE, SRC_BUTTON); - } - } else { - // virtual button - const char *cp; - if (!buttons[count]->vpower.is_pushbutton) { - // toggle button - buttons[count]->vpower.on_off ^= 1; - cp="TBT"; - } else { - // push button - buttons[count]->vpower.on_off = 1; - cp="PBT"; - } - buttons[count]->xdrawButton(buttons[count]->vpower.on_off); - EP_Drawbutton(count); - Touch_MQTT(count, cp, buttons[count]->vpower.on_off); - + uint8_t rbutt = 0; + uint8_t vbutt = 0; + + if (!renderer) + return; + if (touched) + { + // AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("touch after convert %d - %d"), touch_x, touch_y); + // now must compare with defined buttons + for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) + { + if (buttons[count]) + { + if (!buttons[count]->vpower.slider) + { + if (!buttons[count]->vpower.disable) + { + if (buttons[count]->contains(touch_x, touch_y)) + { + // did hit + buttons[count]->press(true); + if (buttons[count]->justPressed()) + { + if (!buttons[count]->vpower.is_virtual) + { + uint8_t pwr = bitRead(TasmotaGlobal.power, rbutt); + if (!SendKey(KEY_BUTTON, rbutt + 1, POWER_TOGGLE)) + { + Touch_RDW_BUTT(count, !pwr); + ExecuteCommandPower(rbutt + 1, POWER_TOGGLE, SRC_BUTTON); } } - } - if (!buttons[count]->vpower.is_virtual) { - rbutt++; - } else { - vbutt++; + else + { + // virtual button + const char *cp; + if (!buttons[count]->vpower.is_pushbutton) + { + // toggle button + buttons[count]->vpower.on_off ^= 1; + cp = "TBT"; + } + else + { + // push button + buttons[count]->vpower.on_off = 1; + cp = "PBT"; + } + buttons[count]->xdrawButton(buttons[count]->vpower.on_off); + EP_Drawbutton(count); + Touch_MQTT(count, cp, buttons[count]->vpower.on_off); + } } } - } else { - // slider - if (buttons[count]->didhit(touch_x, touch_y)) { - uint16_t value = buttons[count]->UpdateSlider(touch_x, touch_y); - EP_Drawbutton(count); - Touch_MQTT(count, "SLD", value); + if (!buttons[count]->vpower.is_virtual) + { + rbutt++; } + else + { + vbutt++; + } + } + } + else + { + // slider + if (buttons[count]->didhit(touch_x, touch_y)) + { + uint16_t value = buttons[count]->UpdateSlider(touch_x, touch_y); + EP_Drawbutton(count); + Touch_MQTT(count, "SLD", value); } } } - - } else { + } + } + else + { // no hit - for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) { - if (buttons[count]) { - if (!buttons[count]->vpower.slider) { + for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) + { + if (buttons[count]) + { + if (!buttons[count]->vpower.slider) + { buttons[count]->press(false); - if (buttons[count]->justReleased()) { - if (buttons[count]->vpower.is_virtual) { - if (buttons[count]->vpower.is_pushbutton) { + if (buttons[count]->justReleased()) + { + if (buttons[count]->vpower.is_virtual) + { + if (buttons[count]->vpower.is_pushbutton) + { // push button buttons[count]->vpower.on_off = 0; - Touch_MQTT(count,"PBT", buttons[count]->vpower.on_off); + Touch_MQTT(count, "PBT", buttons[count]->vpower.on_off); buttons[count]->xdrawButton(buttons[count]->vpower.on_off); EP_Drawbutton(count); } } } - if (!buttons[count]->vpower.is_virtual) { + if (!buttons[count]->vpower.is_virtual) + { // check if power button stage changed uint8_t pwr = bitRead(TasmotaGlobal.power, rbutt); uint8_t vpwr = buttons[count]->vpower.on_off; - if (pwr != vpwr) { + if (pwr != vpwr) + { Touch_RDW_BUTT(count, pwr); } rbutt++; @@ -508,33 +654,39 @@ void CheckTouchButtons(bool touched, int16_t touch_x, int16_t touch_y) { } #endif // USE_TOUCH_BUTTONS -void TS_RotConvert(int16_t *x, int16_t *y) { - if (renderer) renderer->TS_RotConvert(x, y); +void TS_RotConvert(int16_t *x, int16_t *y) +{ + if (renderer) + renderer->TS_RotConvert(x, y); } /*********************************************************************************************\ * Interface \*********************************************************************************************/ -bool Xdrv55(uint32_t function) { +bool Xdrv55(uint32_t function) +{ bool result = false; - switch (function) { - case FUNC_INIT: - break; - case FUNC_EVERY_100_MSECOND: - if (FT5206_found || XPT2046_found || GT911_found || SRES_found) { - Touch_Check(TS_RotConvert); - } - break; + switch (function) + { + case FUNC_INIT: + break; + case FUNC_EVERY_100_MSECOND: + if (FT5206_found || XPT2046_found || GT911_found || CST816S_found || SRES_found) + { + Touch_Check(TS_RotConvert); + } + break; } return result; } -#else // #if defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) +#else // #if defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) // dummy for LVGL without a touch controller -uint32_t Touch_Status(int32_t sel) { +uint32_t Touch_Status(int32_t sel) +{ return 0; } -#endif // #if defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) +#endif // #if defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) diff --git a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino index c0a1a377ff33..3297df52bf8d 100644 --- a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino +++ b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino @@ -17,27 +17,49 @@ along with this program. If not, see . */ - #if defined(USE_DISPLAY) #ifdef USE_UNIVERSAL_DISPLAY -#define XDSP_17 17 +#define XDSP_17 17 #include bool udisp_init_done = false; uint8_t ctouch_counter; - #ifdef USE_UFILESYS extern FS *ffsp; #endif #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}; - +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 +}; #ifndef USE_DISPLAY uint8_t color_type; @@ -57,10 +79,10 @@ extern uint16_t bg_color; void Core2DisplayPower(uint8_t on); void Core2DisplayDim(uint8_t dim); -//#define DSP_ROM_DESC +// #define DSP_ROM_DESC #ifndef DISP_DESC_FILE -//#define DISP_DESC_FILE "/dispdesc.txt" +// #define DISP_DESC_FILE "/dispdesc.txt" #define DISP_DESC_FILE "/display.ini" #endif // DISP_DESC_FILE @@ -69,39 +91,45 @@ void Core2DisplayDim(uint8_t dim); const char DSP_SAMPLE_DESC[] PROGMEM = DSP_ROM_DESC; #endif // DSP_ROM_DESC /*********************************************************************************************/ -Renderer *Init_uDisplay(const char *desc) { -char *ddesc = 0; -char *fbuff; -uDisplay *udisp; -int8_t cs; +Renderer *Init_uDisplay(const char *desc) +{ + char *ddesc = 0; + char *fbuff; + uDisplay *udisp; + int8_t cs; - if (TasmotaGlobal.gpio_optiona.udisplay_driver || desc) { + if (TasmotaGlobal.gpio_optiona.udisplay_driver || desc) + { Settings->display_model = XDSP_17; + fbuff = (char *)calloc(DISPDESC_SIZE, 1); + if (!fbuff) + return 0; - fbuff = (char*)calloc(DISPDESC_SIZE, 1); - if (!fbuff) return 0; - - if (desc) { + if (desc) + { memcpy_P(fbuff, desc, DISPDESC_SIZE - 1); ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: const char descriptor used")); } - #ifdef USE_UFILESYS - if (ffsp && !TasmotaGlobal.no_autoexec && !ddesc) { + if (ffsp && !TasmotaGlobal.no_autoexec && !ddesc) + { File fp; fp = ffsp->open(DISP_DESC_FILE, "r"); - if (fp > 0) { + if (fp > 0) + { uint32_t size = fp.size(); - if (size > DISPDESC_SIZE - 50) { + if (size > DISPDESC_SIZE - 50) + { free(fbuff); - fbuff = (char*)calloc(size + 50, 1); - if (!fbuff) return 0; + fbuff = (char *)calloc(size + 50, 1); + if (!fbuff) + return 0; } - fp.read((uint8_t*)fbuff, size); + fp.read((uint8_t *)fbuff, size); fp.close(); ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: File descriptor used")); @@ -109,13 +137,15 @@ int8_t cs; } #endif // USE_UFILESYS - #ifdef USE_SCRIPT - if (bitRead(Settings->rule_enabled, 0) && !ddesc) { - uint8_t dfound = Run_Scripter(">d",-2,0); - if (dfound == 99) { + if (bitRead(Settings->rule_enabled, 0) && !ddesc) + { + uint8_t dfound = Run_Scripter(">d", -2, 0); + if (dfound == 99) + { char *lp = glob_script_mem.section_ptr + 2; - while (*lp != '\n') lp++; + while (*lp != '\n') + lp++; memcpy(fbuff, lp + 1, DISPDESC_SIZE - 1); ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: Script descriptor used")); @@ -124,47 +154,57 @@ int8_t cs; #endif // USE_SCRIPT #ifdef USE_RULES - if (!bitRead(Settings->rule_enabled, 2) && !ddesc) { + if (!bitRead(Settings->rule_enabled, 2) && !ddesc) + { // only if rule3 is not enabled for rules char *cp = Settings->rules[2]; - while (*cp == ' ') cp++; + while (*cp == ' ') + cp++; memcpy(fbuff, cp, DISPDESC_SIZE - 1); - if (fbuff[0] == ':' && fbuff[1] == 'H') { + if (fbuff[0] == ':' && fbuff[1] == 'H') + { // assume display descriptor, replace space with line feed - for (uint32_t cnt = 0; cnt < DISPDESC_SIZE; cnt++) { - if (fbuff[cnt] == ' ') fbuff[cnt] = '\n'; + for (uint32_t cnt = 0; cnt < DISPDESC_SIZE; cnt++) + { + if (fbuff[cnt] == ' ') + fbuff[cnt] = '\n'; } ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: Rule 3 descriptor used")); } - } #endif // USE_RULES - #ifdef DSP_ROM_DESC - if (!ddesc) { + if (!ddesc) + { memcpy_P(fbuff, DSP_SAMPLE_DESC, sizeof(DSP_SAMPLE_DESC)); ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: Flash descriptor used")); } #endif // DSP_ROM_DESC - if (!ddesc) { + if (!ddesc) + { AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: No valid descriptor found")); - if (fbuff) free(fbuff); + if (fbuff) + free(fbuff); return 0; } // now replace tasmota vars before passing to driver char *cp = strstr(ddesc, "I2C"); - if (cp) { + if (cp) + { cp += 3; uint8_t wire_n = 1; - if (*cp == '1' || *cp == '2') { + if (*cp == '1' || *cp == '2') + { wire_n = *cp & 3; cp += 2; - } else { + } + else + { cp++; } //,3c,22,21,-1 @@ -174,48 +214,57 @@ int8_t cs; sda = replacepin(&cp, Pin(GPIO_I2C_SDA, wire_n - 1)); replacepin(&cp, Pin(GPIO_OLED_RESET)); - if (wire_n == 1) { - if (!TasmotaGlobal.i2c_enabled) { + if (wire_n == 1) + { + if (!TasmotaGlobal.i2c_enabled) + { I2cBegin(sda, scl); } } #ifdef ESP32 - if (wire_n == 2) { - if (!TasmotaGlobal.i2c_enabled_2) { + if (wire_n == 2) + { + if (!TasmotaGlobal.i2c_enabled_2) + { I2c2Begin(sda, scl); } } - if (I2cSetDevice(i2caddr, wire_n - 1)) { + if (I2cSetDevice(i2caddr, wire_n - 1)) + { I2cSetActiveFound(i2caddr, "DSP-I2C", wire_n - 1); } #endif // ESP32 #ifdef ESP8266 - if (I2cSetDevice(i2caddr)) { + if (I2cSetDevice(i2caddr)) + { I2cSetActiveFound(i2caddr, "DSP-I2C"); } #endif // ESP8266 - //AddLog(LOG_LEVEL_INFO, PSTR("DSP: i2c %x, %d, %d, %d!"), i2caddr, wire_n, scl, sda); + // AddLog(LOG_LEVEL_INFO, PSTR("DSP: i2c %x, %d, %d, %d!"), i2caddr, wire_n, scl, sda); } cp = strstr(ddesc, "SPI,"); - if (cp) { + if (cp) + { cp += 4; //; 7 params nr,cs,sclk,mosi,dc,bl,reset,miso - //SPI,*,*,*,*,*,*,* - switch (*cp) { - case '1': - cs = Pin(GPIO_SPI_CS); - break; - case '2': - cs = Pin(GPIO_SPI_CS, 1); - break; - default: - cs = Pin(GPIO_SSPI_CS); - break; + // SPI,*,*,*,*,*,*,* + switch (*cp) + { + case '1': + cs = Pin(GPIO_SPI_CS); + break; + case '2': + cs = Pin(GPIO_SPI_CS, 1); + break; + default: + cs = Pin(GPIO_SSPI_CS); + break; } - if (*cp == '1') { - cp+=2; + if (*cp == '1') + { + cp += 2; replacepin(&cp, cs); replacepin(&cp, Pin(GPIO_SPI_CLK)); replacepin(&cp, Pin(GPIO_SPI_MOSI)); @@ -223,8 +272,10 @@ int8_t cs; replacepin(&cp, Pin(GPIO_BACKLIGHT)); replacepin(&cp, Pin(GPIO_OLED_RESET)); replacepin(&cp, Pin(GPIO_SPI_MISO)); - } else if (*cp == '2') { - cp+=2; + } + else if (*cp == '2') + { + cp += 2; replacepin(&cp, cs); replacepin(&cp, Pin(GPIO_SPI_CLK, 1)); replacepin(&cp, Pin(GPIO_SPI_MOSI, 1)); @@ -232,9 +283,11 @@ int8_t cs; replacepin(&cp, Pin(GPIO_BACKLIGHT)); replacepin(&cp, Pin(GPIO_OLED_RESET)); replacepin(&cp, Pin(GPIO_SPI_MISO, 1)); - } else { + } + else + { // soft spi pins - cp+=2; + cp += 2; replacepin(&cp, cs); replacepin(&cp, Pin(GPIO_SSPI_SCLK)); replacepin(&cp, Pin(GPIO_SSPI_MOSI)); @@ -248,7 +301,8 @@ int8_t cs; uint16_t xs, ys; // we need screen size for gt911 touch controler cp = strstr(ddesc, ":H,"); - if (cp) { + if (cp) + { cp += 3; cp = strchr(cp, ','); cp++; @@ -261,7 +315,8 @@ int8_t cs; int8_t xp, xm, yp, ym; cp = strstr(ddesc, "PAR,"); - if (cp) { + if (cp) + { cp += 4; // 8 or 16 bus uint8_t mode = strtol(cp, &cp, 10); @@ -284,7 +339,8 @@ int8_t cs; replacepin(&cp, Pin(GPIO_DPAR6)); replacepin(&cp, Pin(GPIO_DPAR7)); - if (mode == 16) { + if (mode == 16) + { replacepin(&cp, Pin(GPIO_DPAR8)); replacepin(&cp, Pin(GPIO_DPAR9)); replacepin(&cp, Pin(GPIO_DPAR10)); @@ -296,25 +352,27 @@ int8_t cs; } } #endif // CONFIG_IDF_TARGET_ESP32S3 -/* - File fp; - fp = ffsp->open("/dump.txt", "w"); - fp.write((uint8_t*)ddesc, DISPDESC_SIZE); - fp.close(); -*/ + /* + File fp; + fp = ffsp->open("/dump.txt", "w"); + fp.write((uint8_t*)ddesc, DISPDESC_SIZE); + fp.close(); + */ // init renderer - if (renderer) { + if (renderer) + { delete renderer; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: reinit")); } - udisp = new uDisplay(ddesc); + udisp = new uDisplay(ddesc); // checck for touch option TI1 or TI2 -#if defined(USE_FT5206) || defined(USE_GT911) +#if defined(USE_FT5206) || defined(USE_GT911) || defined(USE_CST816S) cp = strstr(ddesc, ":TI"); - if (cp) { + if (cp) + { uint8_t wire_n = 1; cp += 3; wire_n = (*cp & 3) - 1; @@ -324,44 +382,71 @@ int8_t cs; int8_t scl, sda, irq = -1, rst = -1; scl = replacepin(&cp, Pin(GPIO_I2C_SCL, wire_n)); sda = replacepin(&cp, Pin(GPIO_I2C_SDA, wire_n)); - if (*(cp - 1) == ',') { + if (*(cp - 1) == ',') + { irq = strtol(cp, &cp, 10); - } else { + } + else + { irq = -1; } - if (*cp == ',') { + if (*cp == ',') + { cp++; rst = strtol(cp, &cp, 10); - } else { + } + else + { rst = -1; } - if (wire_n == 0) { - if (!TasmotaGlobal.i2c_enabled) { + if (wire_n == 0) + { + if (!TasmotaGlobal.i2c_enabled) + { I2cBegin(sda, scl); } } + #ifdef ESP32 - if (wire_n == 1) { - if (!TasmotaGlobal.i2c_enabled_2) { + if (wire_n == 1) + { + if (!TasmotaGlobal.i2c_enabled_2) + { I2c2Begin(sda, scl, 400000); } } - if (I2cSetDevice(i2caddr, wire_n)) { - if (i2caddr == GT911_address) { + if (I2cSetDevice(i2caddr, wire_n)) + { + if (i2caddr == GT911_address) + { I2cSetActiveFound(i2caddr, "GT911", wire_n); - } else { + } + else if (i2caddr == CST816S_address) + { + I2cSetActiveFound(i2caddr, "CST816S", wire_n); + } + else + { I2cSetActiveFound(i2caddr, "FT5206", wire_n); } } #endif // ESP32 #ifdef ESP8266 - //AddLog(LOG_LEVEL_INFO, PSTR("DSP: touch %x, %d, %d, %d!"), i2caddr, wire_n, scl, sda); - if (I2cSetDevice(i2caddr)) { - if (i2caddr == GT911_address) { + // AddLog(LOG_LEVEL_INFO, PSTR("DSP: touch %x, %d, %d, %d!"), i2caddr, wire_n, scl, sda); + if (I2cSetDevice(i2caddr)) + { + if (i2caddr == GT911_address) + { I2cSetActiveFound(i2caddr, "GT911"); - } else { + } + else if (i2caddr == CST816S_address) + { + I2cSetActiveFound(i2caddr, "CST816S"); + } + else + { I2cSetActiveFound(i2caddr, "FT5206"); } } @@ -369,57 +454,89 @@ int8_t cs; // start digitizer #ifdef ESP32 - if (i2caddr == GT911_address) { + if (i2caddr == GT911_address) + { #ifdef USE_GT911 - if (!wire_n) GT911_Touch_Init(&Wire, irq, rst, xs, ys); - else GT911_Touch_Init(&Wire1, irq, rst, xs, ys); + if (!wire_n) + GT911_Touch_Init(&Wire, irq, rst, xs, ys); + else + GT911_Touch_Init(&Wire1, irq, rst, xs, ys); #endif - } else { + } + else if (i2caddr == CST816S_address) + { +#ifdef USE_CST816S + if (!wire_n) + CST816S_Touch_Init(&Wire, irq, rst); + else + CST816S_Touch_Init(&Wire1, irq, rst); +#endif + } + else + { #ifdef USE_FT5206 - if (!wire_n) FT5206_Touch_Init(Wire); - else FT5206_Touch_Init(Wire1); + if (!wire_n) + FT5206_Touch_Init(Wire); + else + FT5206_Touch_Init(Wire1); #endif } #else - if (i2caddr == GT911_address) { + if (i2caddr == GT911_address) + { #ifdef USE_GT911 - if (!wire_n) GT911_Touch_Init(&Wire, irq, rst, xs, ys); + if (!wire_n) + GT911_Touch_Init(&Wire, irq, rst, xs, ys); #endif - } else { + } + else if (i2caddr == CST816S_address) + { +#ifdef USE_CST816S + if (!wire_n) + CST816S_Touch_Init(&Wire, irq, rst); +#endif + } + else + { #ifdef USE_FT5206 - if (!wire_n) FT5206_Touch_Init(Wire); + if (!wire_n) + FT5206_Touch_Init(Wire); #endif } #endif // ESP32 - } -#endif // USE_FT5206 || GT911 +#endif // USE_FT5206 || GT911 || CST816S #ifdef USE_XPT2046 cp = strstr(ddesc, ":TS,"); - if (cp) { + if (cp) + { cp += 4; uint8_t touch_cs = replacepin(&cp, Pin(GPIO_XPT2046_CS)); int8_t irqpin = -1; - if (*(cp - 1) == ',') { + if (*(cp - 1) == ',') + { irqpin = strtol(cp, &cp, 10); } uint8_t bus = 1; - if (*cp == ',') { + if (*cp == ',') + { cp++; bus = strtol(cp, &cp, 10); - if (bus < 1) bus = 1; + if (bus < 1) + bus = 1; } - XPT2046_Touch_Init(touch_cs, irqpin, bus - 1); + XPT2046_Touch_Init(touch_cs, irqpin, bus - 1); } #endif // USE_XPT2046 #ifdef CONFIG_IDF_TARGET_ESP32S3 #ifdef SIMPLE_RES_TOUCH cp = strstr(ddesc, ":TR,"); - if (cp) { + if (cp) + { cp += 4; Simple_ResTouch_Init(xp, xm, yp, ym); } @@ -429,16 +546,19 @@ int8_t cs; uint8_t inirot = Settings->display_rotate; cp = strstr(ddesc, ":r,"); - if (cp) { - cp+=3; + if (cp) + { + cp += 3; inirot = strtol(cp, &cp, 10); } // release desc buffer - if (fbuff) free(fbuff); + if (fbuff) + free(fbuff); renderer = udisp->Init(); - if (!renderer) return 0; + if (!renderer) + return 0; fg_color = renderer->fgcol(); bg_color = renderer->bgcol(); @@ -456,8 +576,9 @@ int8_t cs; bool iniinv = Settings->display_options.invert; cp = strstr(ddesc, ":n,"); - if (cp) { - cp+=3; + if (cp) + { + cp += 3; iniinv = strtol(cp, &cp, 10); Settings->display_options.invert = iniinv; } @@ -466,7 +587,8 @@ int8_t cs; ApplyDisplayDimmer(); #ifdef SHOW_SPLASH - if (!Settings->flag5.display_no_splash) { + if (!Settings->flag5.display_no_splash) + { renderer->Splash(); } #endif // SHOW_SPLASH @@ -481,21 +603,25 @@ int8_t cs; /*********************************************************************************************/ -int8_t replacepin(char **cp, uint16_t pin) { +int8_t replacepin(char **cp, uint16_t pin) +{ int8_t res = 0; char *lp = *cp; - if (*lp == ',') lp++; - if (*lp == '*') { + if (*lp == ',') + lp++; + if (*lp == '*') + { char val[8]; itoa(pin, val, 10); uint16_t slen = strlen(val); - //AddLog(LOG_LEVEL_INFO, PSTR("replace pin: %d"), pin); + // AddLog(LOG_LEVEL_INFO, PSTR("replace pin: %d"), pin); memmove(lp + slen, lp + 1, strlen(lp)); memmove(lp, val, slen); } - res= strtol(lp, 0, 10); + res = strtol(lp, 0, 10); char *np = strchr(lp, ','); - if (np) { + if (np) + { *cp = np + 1; } return res; @@ -506,19 +632,25 @@ int8_t replacepin(char **cp, uint16_t pin) { void UDISP_PrintLog(void) { disp_refresh--; - if (!disp_refresh) { + if (!disp_refresh) + { disp_refresh = Settings->display_refresh; - if (!disp_screen_buffer_cols) { DisplayAllocScreenBuffer(); } + if (!disp_screen_buffer_cols) + { + DisplayAllocScreenBuffer(); + } - char* txt = DisplayLogBuffer('\370'); - if (txt != NULL) { - uint8_t last_row = Settings->display_rows -1; + char *txt = DisplayLogBuffer('\370'); + if (txt != NULL) + { + uint8_t last_row = Settings->display_rows - 1; renderer->clearDisplay(); renderer->setTextSize(Settings->display_size); - renderer->setCursor(0,0); - for (byte i = 0; i < last_row; i++) { - strlcpy(disp_screen_buffer[i], disp_screen_buffer[i +1], disp_screen_buffer_cols); + renderer->setCursor(0, 0); + for (byte i = 0; i < last_row; i++) + { + strlcpy(disp_screen_buffer[i], disp_screen_buffer[i + 1], disp_screen_buffer_cols); renderer->println(disp_screen_buffer[i]); } strlcpy(disp_screen_buffer[last_row], txt, disp_screen_buffer_cols); @@ -540,60 +672,66 @@ void UDISP_Time(void) renderer->setTextSize(Settings->display_size); renderer->setTextFont(Settings->display_font); renderer->setCursor(0, 0); - snprintf_P(line, sizeof(line), PSTR(" %02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second); // [ 12:34:56 ] + snprintf_P(line, sizeof(line), PSTR(" %02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second); // [ 12:34:56 ] renderer->println(line); renderer->println(); - snprintf_P(line, sizeof(line), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%04d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year); // [01-02-2018] + snprintf_P(line, sizeof(line), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%04d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year); // [01-02-2018] renderer->println(line); renderer->Updateframe(); } -void UDISP_Refresh(void) // Every second +void UDISP_Refresh(void) // Every second { - if (!renderer) return; - if (Settings->display_mode) { // Mode 0 is User text - switch (Settings->display_mode) { - case 1: // Time - UDISP_Time(); - break; - case 2: // Local - case 3: // Local - case 4: // Mqtt - case 5: // Mqtt - UDISP_PrintLog(); - break; + if (!renderer) + return; + if (Settings->display_mode) + { // Mode 0 is User text + switch (Settings->display_mode) + { + case 1: // Time + UDISP_Time(); + break; + case 2: // Local + case 3: // Local + case 4: // Mqtt + case 5: // Mqtt + UDISP_PrintLog(); + break; } } } -#endif // USE_DISPLAY_MODES1TO5 +#endif // USE_DISPLAY_MODES1TO5 /*********************************************************************************************\ * Interface \*********************************************************************************************/ -bool Xdsp17(uint32_t function) { +bool Xdsp17(uint32_t function) +{ bool result = false; - if (FUNC_DISPLAY_INIT_DRIVER == function) { + if (FUNC_DISPLAY_INIT_DRIVER == function) + { Init_uDisplay(nullptr); } - else if (udisp_init_done && (XDSP_17 == Settings->display_model)) { - switch (function) { - case FUNC_DISPLAY_MODEL: - result = true; - break; + else if (udisp_init_done && (XDSP_17 == Settings->display_model)) + { + switch (function) + { + case FUNC_DISPLAY_MODEL: + result = true; + break; #ifdef USE_DISPLAY_MODES1TO5 - case FUNC_DISPLAY_EVERY_SECOND: - UDISP_Refresh(); - break; -#endif // USE_DISPLAY_MODES1TO5 - + case FUNC_DISPLAY_EVERY_SECOND: + UDISP_Refresh(); + break; +#endif // USE_DISPLAY_MODES1TO5 } } return result; } -#endif // USE_UNIVERSAL_DISPLAY -#endif // USE_DISPLAY +#endif // USE_UNIVERSAL_DISPLAY +#endif // USE_DISPLAY From 064cd8fade3dad486bdd2dcebd084b9b927f2c33 Mon Sep 17 00:00:00 2001 From: Arne Meeuw Date: Tue, 12 Dec 2023 08:08:39 +0100 Subject: [PATCH 2/8] Add CST816S_found --- tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index 9c56a9c4d413..549eb8b37194 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -72,9 +72,9 @@ typedef struct TSGlobal_t TSGlobal_t TSGlobal; +bool CST816S_found = false; bool FT5206_found = false; bool GT911_found = false; -bool CST816S_found = false; bool XPT2046_found = false; bool SRES_found = false; @@ -101,7 +101,7 @@ void Touch_SetStatus(uint8_t touches, uint16_t raw_x, uint16_t raw_y, uint8_t ge 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 || FT5206_found || XPT2046_found || CST816S_found) { if (touches) { @@ -134,7 +134,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 || FT5206_found || GT911_found || XPT2046_found || CST816S_found || SRES_found) { switch (sel) { From 53450f83264121ceaa95714426e8ccc8def48557 Mon Sep 17 00:00:00 2001 From: Arne Meeuw Date: Tue, 12 Dec 2023 16:02:08 +0100 Subject: [PATCH 3/8] Revert formatting --- lib/lib_display/CST816S/CST816S.cpp | 45 +- lib/lib_display/CST816S/CST816S.h | 10 +- tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 492 +++++++----------- .../xdsp_17_universal.ino | 465 ++++++----------- 4 files changed, 378 insertions(+), 634 deletions(-) diff --git a/lib/lib_display/CST816S/CST816S.cpp b/lib/lib_display/CST816S/CST816S.cpp index f879d857e668..e3b7c07f76b2 100644 --- a/lib/lib_display/CST816S/CST816S.cpp +++ b/lib/lib_display/CST816S/CST816S.cpp @@ -37,8 +37,7 @@ @param irq touch interrupt pin */ -CST816S::CST816S(TwoWire *use_wire, int8_t irq, int8_t rst) -{ +CST816S::CST816S(TwoWire *use_wire, int8_t irq, int8_t rst) { wire = use_wire; _rst = rst; _irq = irq; @@ -47,8 +46,7 @@ CST816S::CST816S(TwoWire *use_wire, int8_t irq, int8_t rst) /*! @brief read touch data */ -void CST816S::read_touch() -{ +void CST816S::read_touch() { byte data_raw[8]; i2c_read(CST816S_address, 0x01, data_raw, 6); @@ -62,8 +60,7 @@ void CST816S::read_touch() /*! @brief handle interrupts */ -void IRAM_ATTR CST816S::handleISR(void) -{ +void IRAM_ATTR CST816S::handleISR(void) { _event_available = true; } @@ -72,8 +69,7 @@ void IRAM_ATTR CST816S::handleISR(void) @param interrupt type of interrupt FALLING, RISING.. */ -void CST816S::begin(int interrupt) -{ +void CST816S::begin(int interrupt) { pinMode(_irq, INPUT); pinMode(_rst, OUTPUT); @@ -94,10 +90,8 @@ void CST816S::begin(int interrupt) /*! @brief check for a touch event */ -bool CST816S::available() -{ - if (_event_available) - { +bool CST816S::available() { + if (_event_available) { read_touch(); _event_available = false; return true; @@ -108,8 +102,7 @@ bool CST816S::available() /*! @brief put the touch screen in standby mode */ -void CST816S::sleep() -{ +void CST816S::sleep() { digitalWrite(_rst, LOW); delay(5); digitalWrite(_rst, HIGH); @@ -121,10 +114,8 @@ void CST816S::sleep() /*! @brief get the gesture event name */ -String CST816S::gesture() -{ - switch (data.gestureID) - { +String CST816S::gesture() { + switch (data.gestureID) { case NONE: return "NONE"; break; @@ -166,15 +157,12 @@ String CST816S::gesture() @param length length of data */ -uint8_t CST816S::i2c_read(uint16_t addr, uint8_t reg_addr, uint8_t *reg_data, size_t length) -{ +uint8_t CST816S::i2c_read(uint16_t addr, uint8_t reg_addr, uint8_t *reg_data, size_t length) { wire->beginTransmission(addr); wire->write(reg_addr); - if (wire->endTransmission(true)) - return -1; + if (wire->endTransmission(true)) return -1; wire->requestFrom(addr, length, true); - for (int i = 0; i < length; i++) - { + for (int i = 0; i < length; i++) { *reg_data++ = wire->read(); } return 0; @@ -192,15 +180,12 @@ uint8_t CST816S::i2c_read(uint16_t addr, uint8_t reg_addr, uint8_t *reg_data, si @param length length of data */ -uint8_t CST816S::i2c_write(uint8_t addr, uint8_t reg_addr, const uint8_t *reg_data, size_t length) -{ +uint8_t CST816S::i2c_write(uint8_t addr, uint8_t reg_addr, const uint8_t *reg_data, size_t length) { wire->beginTransmission(addr); wire->write(reg_addr); - for (int i = 0; i < length; i++) - { + for (int i = 0; i < length; i++) { wire->write(*reg_data++); } - if (wire->endTransmission(true)) - return -1; + if (wire->endTransmission(true)) return -1; return 0; } \ No newline at end of file diff --git a/lib/lib_display/CST816S/CST816S.h b/lib/lib_display/CST816S/CST816S.h index 4ed48b74e9eb..fe8c07348cb9 100644 --- a/lib/lib_display/CST816S/CST816S.h +++ b/lib/lib_display/CST816S/CST816S.h @@ -29,8 +29,7 @@ #define CST816S_address 0x15 -enum GESTURE -{ +enum GESTURE { NONE = 0x00, SWIPE_UP = 0x01, SWIPE_DOWN = 0x02, @@ -39,11 +38,9 @@ enum GESTURE SINGLE_CLICK = 0x05, DOUBLE_CLICK = 0x0B, LONG_PRESS = 0x0C - }; -struct data_struct -{ +struct data_struct { byte gestureID; // Gesture ID byte points; // Number of touch points byte event; // Event (0 = Down, 1 = Up, 2 = Contact) @@ -53,8 +50,7 @@ struct data_struct uint8_t versionInfo[3]; }; -class CST816S -{ +class CST816S { public: CST816S(TwoWire *use_wire, int8_t irq = 0, int8_t rst = 1); diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index 549eb8b37194..c06a7f9eef7c 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -35,6 +35,7 @@ * void TS_RotConvert(int16_t *x, int16_t *y) - calls the renderer's rotation converter \*******************************************************************************************/ + #if defined(USE_LVGL_TOUCHSCREEN) || defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_GT911) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) || defined(SIMPLE_RES_TOUCH) #ifdef USE_DISPLAY_LVGL_ONLY @@ -43,11 +44,10 @@ #include -#define XDRV_55 55 +#define XDRV_55 55 // Codes for gestures, when supported by the Touch Screen controller -enum TS_Gesture -{ +enum TS_Gesture { TS_Gest_None = 0, TS_Gest_Move_Up = 0x10, TS_Gest_Move_Down = 0x11, @@ -57,14 +57,13 @@ enum TS_Gesture TS_Gest_Zoom_Out = 0x21, }; -typedef struct TSGlobal_t -{ +typedef struct TSGlobal_t { int16_t raw_touch_xp = 0; int16_t raw_touch_yp = 0; int16_t touch_xp = 0; int16_t touch_yp = 0; - uint8_t touches = 0; // number of touches for multi-touch - uint8_t gesture = 0; // gesture code + uint8_t touches = 0; // number of touches for multi-touch + uint8_t gesture = 0; // gesture code // multi-point is not yet supported bool touched = false; bool external_ts = false; @@ -86,8 +85,7 @@ bool SRES_found = false; VButton *buttons[MAX_TOUCH_BUTTONS]; #endif -void Touch_SetStatus(uint8_t touches, uint16_t raw_x, uint16_t raw_y, uint8_t gesture) -{ +void Touch_SetStatus(uint8_t touches, uint16_t raw_x, uint16_t raw_y, uint8_t gesture) { TSGlobal.external_ts = true; TSGlobal.gesture = gesture; TSGlobal.touches = touches; @@ -98,61 +96,36 @@ 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 || CST816S_found) - { - if (touches) - { - *touches = TSGlobal.touches; - } - if (x) - { - *x = TSGlobal.touch_xp; - } - if (y) - { - *y = TSGlobal.touch_yp; - } - if (raw_x) - { - *raw_x = TSGlobal.raw_touch_xp; - } - if (raw_y) - { - *raw_y = TSGlobal.raw_touch_yp; - } - if (gesture) - { - *touches = TSGlobal.gesture; - } +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 || CST816S_found || FT5206_found || XPT2046_found) { + if (touches) { *touches = TSGlobal.touches; } + if (x) { *x = TSGlobal.touch_xp; } + if (y) { *y = TSGlobal.touch_yp; } + if (raw_x) { *raw_x = TSGlobal.raw_touch_xp; } + if (raw_y) { *raw_y = TSGlobal.raw_touch_yp; } + if (gesture) { *touches = TSGlobal.gesture; } return true; } return false; } -uint32_t Touch_Status(int32_t sel) -{ - if (TSGlobal.external_ts || FT5206_found || GT911_found || XPT2046_found || CST816S_found || SRES_found) - { - switch (sel) - { - case 0: - return TSGlobal.touched; - case 1: - return TSGlobal.touch_xp; - case 2: - return TSGlobal.touch_yp; - case -1: // before calibration - return TSGlobal.raw_touch_xp; - case -2: - return TSGlobal.raw_touch_yp; +uint32_t Touch_Status(int32_t sel) { + if (TSGlobal.external_ts || CST816S_found || FT5206_found || GT911_found || XPT2046_found || SRES_found) { + switch (sel) { + case 0: + return TSGlobal.touched; + case 1: + return TSGlobal.touch_xp; + case 2: + return TSGlobal.touch_yp; + case -1: // before calibration + return TSGlobal.raw_touch_xp; + case -2: + return TSGlobal.raw_touch_yp; } return 0; - } - else - { + } else { return 0; } } @@ -161,12 +134,12 @@ uint32_t Touch_Status(int32_t sel) uint8_t tbstate[3]; #endif // USE_M5STACK_CORE2 + // simple resistive touch pins // with dma it should check for active transfers // so currently dont use dma #ifdef SIMPLE_RES_TOUCH -struct RES_TOUCH -{ +struct RES_TOUCH { int8_t xplus; int8_t xminus; int8_t yplus; @@ -175,11 +148,10 @@ struct RES_TOUCH uint16_t yp; } sres_touch; -void Simple_ResTouch_Init(int8_t xp, int8_t xm, int8_t yp, int8_t ym) -{ - sres_touch.xplus = xp; // d1 +void Simple_ResTouch_Init(int8_t xp, int8_t xm, int8_t yp, int8_t ym) { + sres_touch.xplus = xp; // d1 sres_touch.xminus = xm; // cs - sres_touch.yplus = yp; // rs + sres_touch.yplus = yp; // rs sres_touch.yminus = ym; // d0 SRES_found = true; AddLog(LOG_LEVEL_INFO, PSTR("TS: simple resistive touch init")); @@ -187,34 +159,29 @@ void Simple_ResTouch_Init(int8_t xp, int8_t xm, int8_t yp, int8_t ym) #define SRES_THRESHOLD 500 -bool SRES_touched() -{ +bool SRES_touched() { uint32_t val = renderer->get_sr_touch(sres_touch.xplus, sres_touch.xminus, sres_touch.yplus, sres_touch.yminus); - if (val == 0) - { + if (val == 0) { return false; } sres_touch.xp = val >> 16; - sres_touch.yp = val & 0xffff; + sres_touch.yp = val & 0xffff; int16_t xp = sres_touch.xp; int16_t yp = sres_touch.yp; - // AddLog(LOG_LEVEL_INFO, "TS x=%i y=%i)", xp, yp); + //AddLog(LOG_LEVEL_INFO, "TS x=%i y=%i)", xp, yp); - if (xp > SRES_THRESHOLD && yp > SRES_THRESHOLD) - { + if (xp > SRES_THRESHOLD && yp > SRES_THRESHOLD) { return 1; } return 0; } -int16_t SRES_x() -{ +int16_t SRES_x() { return sres_touch.xp; } -int16_t SRES_y() -{ +int16_t SRES_y() { return sres_touch.yp; } #endif @@ -222,13 +189,9 @@ int16_t SRES_y() #ifdef USE_CST816S #include // touch panel controller -#undef CST816S_address -#define CST816S_address 0x15 - CST816S *CST816S_touchp; -bool CST816S_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin) -{ +bool CST816S_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin) { CST816S_found = false; CST816S_touchp = new CST816S(i2c, irq_pin, rst_pin); CST816S_touchp->begin(); @@ -237,16 +200,15 @@ bool CST816S_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin) return CST816S_found; } -bool CST816S_touched() -{ +bool CST816S_touched() { return CST816S_touchp->available(); } -int16_t CST816S_x() -{ + +int16_t CST816S_x() { return CST816S_touchp->data.x; } -int16_t CST816S_y() -{ + +int16_t CST816S_y() { return CST816S_touchp->data.y; } #endif // USE_CST816S @@ -259,133 +221,114 @@ int16_t CST816S_y() FT5206_Class *FT5206_touchp; -bool FT5206_Touch_Init(TwoWire &i2c) -{ +bool FT5206_Touch_Init(TwoWire &i2c) { FT5206_found = false; FT5206_touchp = new FT5206_Class(); - if (FT5206_touchp->begin(i2c, FT5206_address)) - { + if (FT5206_touchp->begin(i2c, FT5206_address)) { AddLog(LOG_LEVEL_INFO, PSTR("TI: FT5206")); FT5206_found = true; } - // AddLog(LOG_LEVEL_INFO, PSTR("TS: FT5206 %d"),FT5206_found); + //AddLog(LOG_LEVEL_INFO, PSTR("TS: FT5206 %d"),FT5206_found); return FT5206_found; } -bool FT5206_touched() -{ +bool FT5206_touched() { return FT5206_touchp->touched(); } -int16_t FT5206_x() -{ +int16_t FT5206_x() { TP_Point pLoc = FT5206_touchp->getPoint(0); return pLoc.x; } -int16_t FT5206_y() -{ +int16_t FT5206_y() { TP_Point pLoc = FT5206_touchp->getPoint(0); return pLoc.y; } -#endif // USE_FT5206 +#endif // USE_FT5206 #ifdef USE_GT911 #include // touch panel controller GT911 *GT911_touchp; -bool GT911_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin, uint16_t xs, uint16_t ys) -{ +bool GT911_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin, uint16_t xs, uint16_t ys) { GT911_found = false; GT911_touchp = new GT911(); - if (ESP_OK == GT911_touchp->begin(i2c, irq_pin, rst_pin, xs, ys)) - { + if (ESP_OK == GT911_touchp->begin(i2c, irq_pin, rst_pin, xs, ys)) { AddLog(LOG_LEVEL_INFO, PSTR("TI: GT911")); GT911_found = true; - } - else - { + } else { AddLog(LOG_LEVEL_INFO, PSTR("TI: GT911 failed")); } return GT911_found; } -void GT911_CheckTouch(void) -{ +void GT911_CheckTouch(void) { GT911_touchp->update(); TSGlobal.touched = !GT911_touchp->isFingerUp(); - if (TSGlobal.touched) - { + if (TSGlobal.touched) { TSGlobal.raw_touch_xp = GT911_touchp->readFingerX(0); TSGlobal.raw_touch_yp = GT911_touchp->readFingerY(0); } } -#endif // USE_GT911 +#endif // USE_GT911 + #ifdef USE_XPT2046 #include XPT2046_Touchscreen *XPT2046_touchp; -bool XPT2046_Touch_Init(uint16_t CS, int8_t irqpin, uint8_t bus) -{ +bool XPT2046_Touch_Init(uint16_t CS, int8_t irqpin, uint8_t bus) { int8_t sclk = -1; int8_t mosi = -1; int8_t miso = -1; uint8_t xbus = bus; bus &= 1; -#ifdef ESP32 - if (PinUsed(GPIO_SPI_CLK, bus) && PinUsed(GPIO_SPI_MISO, bus) && PinUsed(GPIO_SPI_MOSI, bus)) - { + #ifdef ESP32 + if (PinUsed(GPIO_SPI_CLK, bus) && PinUsed(GPIO_SPI_MISO, bus) && PinUsed(GPIO_SPI_MOSI, bus)) { // must init SPI with pins sclk = Pin(GPIO_SPI_CLK, bus); miso = Pin(GPIO_SPI_MISO, bus); mosi = Pin(GPIO_SPI_MOSI, bus); - } -#endif // ESP32 + } + #endif // ESP32 -#ifdef ESP8266 - if (PinUsed(GPIO_SPI_CLK) && PinUsed(GPIO_SPI_MISO) && PinUsed(GPIO_SPI_MOSI)) - { + #ifdef ESP8266 + if (PinUsed(GPIO_SPI_CLK) && PinUsed(GPIO_SPI_MISO) && PinUsed(GPIO_SPI_MOSI)) { // must init SPI with pins sclk = Pin(GPIO_SPI_CLK); miso = Pin(GPIO_SPI_MISO); mosi = Pin(GPIO_SPI_MOSI); - } -#endif // ESP8266 + } + #endif // ESP8266 XPT2046_touchp = new XPT2046_Touchscreen(CS, irqpin, xbus, sclk, miso, mosi); XPT2046_found = XPT2046_touchp->begin(); - if (XPT2046_found) - { - AddLog(LOG_LEVEL_INFO, PSTR("TS: XPT2046")); + if (XPT2046_found) { + AddLog(LOG_LEVEL_INFO, PSTR("TS: XPT2046")); } return XPT2046_found; } -bool XPT2046_touched() -{ +bool XPT2046_touched() { return XPT2046_touchp->touched(); } -int16_t XPT2046_x() -{ +int16_t XPT2046_x() { TS_Point pLoc = XPT2046_touchp->getPoint(); return pLoc.x; } -int16_t XPT2046_y() -{ +int16_t XPT2046_y() { TS_Point pLoc = XPT2046_touchp->getPoint(); return pLoc.y; } -#endif // USE_XPT2046 +#endif // USE_XPT2046 + +void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { + static bool was_touched = false; // flag used to log the data sent when the screen was just released -void Touch_Check(void (*rotconvert)(int16_t *x, int16_t *y)) -{ - static bool was_touched = false; // flag used to log the data sent when the screen was just released #ifdef SIMPLE_RES_TOUCH - if (SRES_found) - { + if (SRES_found) { TSGlobal.touched = SRES_touched(); - if (TSGlobal.touched) - { + if (TSGlobal.touched) { TSGlobal.raw_touch_xp = SRES_x(); TSGlobal.raw_touch_yp = SRES_y(); } @@ -393,11 +336,9 @@ void Touch_Check(void (*rotconvert)(int16_t *x, int16_t *y)) #endif #ifdef USE_CST816S - if (CST816S_found) - { + if (CST816S_found) { TSGlobal.touched = CST816S_touched(); - if (TSGlobal.touched) - { + if (TSGlobal.touched) { TSGlobal.raw_touch_xp = CST816S_x(); TSGlobal.raw_touch_yp = CST816S_y(); } @@ -405,11 +346,9 @@ void Touch_Check(void (*rotconvert)(int16_t *x, int16_t *y)) #endif // USE_CST816S #ifdef USE_FT5206 - if (FT5206_found) - { + if (FT5206_found) { TSGlobal.touched = FT5206_touched(); - if (TSGlobal.touched) - { + if (TSGlobal.touched) { TSGlobal.raw_touch_xp = FT5206_x(); TSGlobal.raw_touch_yp = FT5206_y(); } @@ -417,18 +356,15 @@ void Touch_Check(void (*rotconvert)(int16_t *x, int16_t *y)) #endif // USE_FT5206 #ifdef USE_GT911 - if (GT911_found) - { + if (GT911_found) { GT911_CheckTouch(); } #endif // USE_FT5206 #ifdef USE_XPT2046 - if (XPT2046_found) - { + if (XPT2046_found) { TSGlobal.touched = XPT2046_touched(); - if (TSGlobal.touched) - { + if (TSGlobal.touched) { TSGlobal.raw_touch_xp = XPT2046_x(); TSGlobal.raw_touch_yp = XPT2046_y(); } @@ -438,8 +374,7 @@ void Touch_Check(void (*rotconvert)(int16_t *x, int16_t *y)) TSGlobal.touch_xp = TSGlobal.raw_touch_xp; TSGlobal.touch_yp = TSGlobal.raw_touch_yp; - if (TSGlobal.touched) - { + if (TSGlobal.touched) { was_touched = true; #ifdef USE_TOUCH_BUTTONS #ifdef USE_M5STACK_CORE2 @@ -447,22 +382,19 @@ void Touch_Check(void (*rotconvert)(int16_t *x, int16_t *y)) uint16_t xcenter = 80; #define TDELTA 30 #define TYPOS 275 - for (uint32_t tbut = 0; tbut < 3; tbut++) - { - if (TSGlobal.touch_xp > (xcenter - TDELTA) && TSGlobal.touch_xp < (xcenter + TDELTA) && TSGlobal.touch_yp > (TYPOS - TDELTA) && TSGlobal.touch_yp < (TYPOS + TDELTA)) - { + for (uint32_t tbut = 0; tbut < 3; tbut++) { + if (TSGlobal.touch_xp > (xcenter - TDELTA) && TSGlobal.touch_xp < (xcenter + TDELTA) && TSGlobal.touch_yp > (TYPOS - TDELTA) && TSGlobal.touch_yp < (TYPOS + TDELTA)) { // hit a button - if (!(tbstate[tbut] & 1)) - { + if (!(tbstate[tbut] & 1)) { // pressed tbstate[tbut] |= 1; - // AddLog(LOG_LEVEL_INFO, PSTR("tbut: %d pressed"), tbut); + //AddLog(LOG_LEVEL_INFO, PSTR("tbut: %d pressed"), tbut); Touch_MQTT(tbut, "BIB", tbstate[tbut] & 1); } } xcenter += 100; } -#endif // USE_M5STACK_CORE2 +#endif // USE_M5STACK_CORE2 #endif // USE_TOUCH_BUTTONS rotconvert(&TSGlobal.touch_xp, &TSGlobal.touch_yp); @@ -471,178 +403,138 @@ void Touch_Check(void (*rotconvert)(int16_t *x, int16_t *y)) #ifdef USE_TOUCH_BUTTONS CheckTouchButtons(TSGlobal.touched, TSGlobal.touch_xp, TSGlobal.touch_yp); #endif // USE_TOUCH_BUTTONS - } - else - { + + } else { #ifdef USE_M5STACK_CORE2 - for (uint32_t tbut = 0; tbut < 3; tbut++) - { - if (tbstate[tbut] & 1) - { + for (uint32_t tbut = 0; tbut < 3; tbut++) { + if (tbstate[tbut] & 1) { // released tbstate[tbut] &= 0xfe; Touch_MQTT(tbut, "BIB", tbstate[tbut] & 1); - // AddLog(LOG_LEVEL_INFO, PSTR("tbut: %d released"), tbut); + //AddLog(LOG_LEVEL_INFO, PSTR("tbut: %d released"), tbut); } } -#endif // USE_M5STACK_CORE2 +#endif // USE_M5STACK_CORE2 - rotconvert(&TSGlobal.touch_xp, &TSGlobal.touch_yp); // still do rot convert if not TSGlobal.touched - if (was_touched) - { + rotconvert(&TSGlobal.touch_xp, &TSGlobal.touch_yp); // still do rot convert if not TSGlobal.touched + if (was_touched) { AddLog(LOG_LEVEL_DEBUG_MORE, "TS : released x=%i y=%i (raw x=%i y=%i)", TSGlobal.touch_xp, TSGlobal.touch_yp, TSGlobal.raw_touch_xp, TSGlobal.raw_touch_yp); was_touched = false; } #ifdef USE_TOUCH_BUTTONS CheckTouchButtons(TSGlobal.touched, TSGlobal.touch_xp, TSGlobal.touch_yp); #endif // USE_TOUCH_BUTTONS + } } + #ifdef USE_TOUCH_BUTTONS -void Touch_MQTT(uint8_t index, const char *cp, uint32_t val) -{ +void Touch_MQTT(uint8_t index, const char *cp, uint32_t val) { #ifdef USE_FT5206 - if (FT5206_found) - ResponseTime_P(PSTR(",\"FT5206\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); + if (FT5206_found) ResponseTime_P(PSTR(",\"FT5206\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); #endif #ifdef USE_XPT2046 - if (XPT2046_found) - ResponseTime_P(PSTR(",\"XPT2046\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); -#endif // USE_XPT2046 + if (XPT2046_found) ResponseTime_P(PSTR(",\"XPT2046\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); +#endif // USE_XPT2046 #ifdef USE_GT911 - if (GT911_found) - ResponseTime_P(PSTR(",\"GT911\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); -#endif // USE_XPT2046 + if (GT911_found) ResponseTime_P(PSTR(",\"GT911\":{\"%s%d\":\"%d\"}}"), cp, index + 1, val); +#endif // USE_XPT2046 MqttPublishTeleSensor(); } -void EP_Drawbutton(uint32_t count) -{ +void EP_Drawbutton(uint32_t count) { renderer->ep_update_area(buttons[count]->spars.xp, buttons[count]->spars.yp, buttons[count]->spars.xs, buttons[count]->spars.ys, 3); } -void Touch_RDW_BUTT(uint32_t count, uint32_t pwr) -{ +void Touch_RDW_BUTT(uint32_t count, uint32_t pwr) { buttons[count]->xdrawButton(pwr); EP_Drawbutton(count); - if (pwr) - buttons[count]->vpower.on_off = 1; - else - buttons[count]->vpower.on_off = 0; + if (pwr) buttons[count]->vpower.on_off = 1; + else buttons[count]->vpower.on_off = 0; } -void CheckTouchButtons(bool touched, int16_t touch_x, int16_t touch_y) -{ +void CheckTouchButtons(bool touched, int16_t touch_x, int16_t touch_y) { uint16_t temp; - uint8_t rbutt = 0; - uint8_t vbutt = 0; - - if (!renderer) - return; - if (touched) - { - // AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("touch after convert %d - %d"), touch_x, touch_y); - // now must compare with defined buttons - for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) - { - if (buttons[count]) - { - if (!buttons[count]->vpower.slider) - { - if (!buttons[count]->vpower.disable) - { - if (buttons[count]->contains(touch_x, touch_y)) - { - // did hit - buttons[count]->press(true); - if (buttons[count]->justPressed()) - { - if (!buttons[count]->vpower.is_virtual) - { - uint8_t pwr = bitRead(TasmotaGlobal.power, rbutt); - if (!SendKey(KEY_BUTTON, rbutt + 1, POWER_TOGGLE)) - { - Touch_RDW_BUTT(count, !pwr); - ExecuteCommandPower(rbutt + 1, POWER_TOGGLE, SRC_BUTTON); - } - } - else - { - // virtual button - const char *cp; - if (!buttons[count]->vpower.is_pushbutton) - { - // toggle button - buttons[count]->vpower.on_off ^= 1; - cp = "TBT"; - } - else - { - // push button - buttons[count]->vpower.on_off = 1; - cp = "PBT"; + uint8_t rbutt=0; + uint8_t vbutt=0; + + if (!renderer) return; + if (touched) { + //AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("touch after convert %d - %d"), touch_x, touch_y); + // now must compare with defined buttons + for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) { + if (buttons[count]) { + if (!buttons[count]->vpower.slider) { + if (!buttons[count]->vpower.disable) { + if (buttons[count]->contains(touch_x, touch_y)) { + // did hit + buttons[count]->press(true); + if (buttons[count]->justPressed()) { + if (!buttons[count]->vpower.is_virtual) { + uint8_t pwr=bitRead(TasmotaGlobal.power, rbutt); + if (!SendKey(KEY_BUTTON, rbutt+1, POWER_TOGGLE)) { + Touch_RDW_BUTT(count, !pwr); + ExecuteCommandPower(rbutt+1, POWER_TOGGLE, SRC_BUTTON); + } + } else { + // virtual button + const char *cp; + if (!buttons[count]->vpower.is_pushbutton) { + // toggle button + buttons[count]->vpower.on_off ^= 1; + cp="TBT"; + } else { + // push button + buttons[count]->vpower.on_off = 1; + cp="PBT"; + } + buttons[count]->xdrawButton(buttons[count]->vpower.on_off); + EP_Drawbutton(count); + Touch_MQTT(count, cp, buttons[count]->vpower.on_off); + } - buttons[count]->xdrawButton(buttons[count]->vpower.on_off); - EP_Drawbutton(count); - Touch_MQTT(count, cp, buttons[count]->vpower.on_off); } } + if (!buttons[count]->vpower.is_virtual) { + rbutt++; + } else { + vbutt++; + } } - if (!buttons[count]->vpower.is_virtual) - { - rbutt++; - } - else - { - vbutt++; + } else { + // slider + if (buttons[count]->didhit(touch_x, touch_y)) { + uint16_t value = buttons[count]->UpdateSlider(touch_x, touch_y); + EP_Drawbutton(count); + Touch_MQTT(count, "SLD", value); } } } - else - { - // slider - if (buttons[count]->didhit(touch_x, touch_y)) - { - uint16_t value = buttons[count]->UpdateSlider(touch_x, touch_y); - EP_Drawbutton(count); - Touch_MQTT(count, "SLD", value); - } - } } - } - } - else - { + + } else { // no hit - for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) - { - if (buttons[count]) - { - if (!buttons[count]->vpower.slider) - { + for (uint8_t count = 0; count < MAX_TOUCH_BUTTONS; count++) { + if (buttons[count]) { + if (!buttons[count]->vpower.slider) { buttons[count]->press(false); - if (buttons[count]->justReleased()) - { - if (buttons[count]->vpower.is_virtual) - { - if (buttons[count]->vpower.is_pushbutton) - { + if (buttons[count]->justReleased()) { + if (buttons[count]->vpower.is_virtual) { + if (buttons[count]->vpower.is_pushbutton) { // push button buttons[count]->vpower.on_off = 0; - Touch_MQTT(count, "PBT", buttons[count]->vpower.on_off); + Touch_MQTT(count,"PBT", buttons[count]->vpower.on_off); buttons[count]->xdrawButton(buttons[count]->vpower.on_off); EP_Drawbutton(count); } } } - if (!buttons[count]->vpower.is_virtual) - { + if (!buttons[count]->vpower.is_virtual) { // check if power button stage changed uint8_t pwr = bitRead(TasmotaGlobal.power, rbutt); uint8_t vpwr = buttons[count]->vpower.on_off; - if (pwr != vpwr) - { + if (pwr != vpwr) { Touch_RDW_BUTT(count, pwr); } rbutt++; @@ -654,39 +546,33 @@ void CheckTouchButtons(bool touched, int16_t touch_x, int16_t touch_y) } #endif // USE_TOUCH_BUTTONS -void TS_RotConvert(int16_t *x, int16_t *y) -{ - if (renderer) - renderer->TS_RotConvert(x, y); +void TS_RotConvert(int16_t *x, int16_t *y) { + if (renderer) renderer->TS_RotConvert(x, y); } /*********************************************************************************************\ * Interface \*********************************************************************************************/ -bool Xdrv55(uint32_t function) -{ +bool Xdrv55(uint32_t function) { bool result = false; - switch (function) - { - case FUNC_INIT: - break; - case FUNC_EVERY_100_MSECOND: - if (FT5206_found || XPT2046_found || GT911_found || CST816S_found || SRES_found) - { - Touch_Check(TS_RotConvert); - } - break; + switch (function) { + case FUNC_INIT: + break; + case FUNC_EVERY_100_MSECOND: + if (CST816S_found || FT5206_found || XPT2046_found || GT911_found || SRES_found) { + Touch_Check(TS_RotConvert); + } + break; } return result; } -#else // #if defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) +#else // #if defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) // dummy for LVGL without a touch controller -uint32_t Touch_Status(int32_t sel) -{ +uint32_t Touch_Status(int32_t sel) { return 0; } -#endif // #if defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) +#endif // #if defined(USE_FT5206) || defined(USE_XPT2046) || defined(USE_LILYGO47) || defined(USE_TOUCH_BUTTONS) diff --git a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino index 3297df52bf8d..fb7f9c9aca25 100644 --- a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino +++ b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino @@ -17,16 +17,18 @@ along with this program. If not, see . */ + #if defined(USE_DISPLAY) #ifdef USE_UNIVERSAL_DISPLAY -#define XDSP_17 17 +#define XDSP_17 17 #include bool udisp_init_done = false; uint8_t ctouch_counter; + #ifdef USE_UFILESYS extern FS *ffsp; #endif @@ -36,30 +38,8 @@ extern FS *ffsp; #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 -}; +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}; + #ifndef USE_DISPLAY uint8_t color_type; @@ -79,10 +59,10 @@ extern uint16_t bg_color; void Core2DisplayPower(uint8_t on); void Core2DisplayDim(uint8_t dim); -// #define DSP_ROM_DESC +//#define DSP_ROM_DESC #ifndef DISP_DESC_FILE -// #define DISP_DESC_FILE "/dispdesc.txt" +//#define DISP_DESC_FILE "/dispdesc.txt" #define DISP_DESC_FILE "/display.ini" #endif // DISP_DESC_FILE @@ -91,45 +71,39 @@ void Core2DisplayDim(uint8_t dim); const char DSP_SAMPLE_DESC[] PROGMEM = DSP_ROM_DESC; #endif // DSP_ROM_DESC /*********************************************************************************************/ -Renderer *Init_uDisplay(const char *desc) -{ - char *ddesc = 0; - char *fbuff; - uDisplay *udisp; - int8_t cs; +Renderer *Init_uDisplay(const char *desc) { +char *ddesc = 0; +char *fbuff; +uDisplay *udisp; +int8_t cs; - if (TasmotaGlobal.gpio_optiona.udisplay_driver || desc) - { + if (TasmotaGlobal.gpio_optiona.udisplay_driver || desc) { Settings->display_model = XDSP_17; - fbuff = (char *)calloc(DISPDESC_SIZE, 1); - if (!fbuff) - return 0; - if (desc) - { + fbuff = (char*)calloc(DISPDESC_SIZE, 1); + if (!fbuff) return 0; + + if (desc) { memcpy_P(fbuff, desc, DISPDESC_SIZE - 1); ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: const char descriptor used")); } + #ifdef USE_UFILESYS - if (ffsp && !TasmotaGlobal.no_autoexec && !ddesc) - { + if (ffsp && !TasmotaGlobal.no_autoexec && !ddesc) { File fp; fp = ffsp->open(DISP_DESC_FILE, "r"); - if (fp > 0) - { + if (fp > 0) { uint32_t size = fp.size(); - if (size > DISPDESC_SIZE - 50) - { + if (size > DISPDESC_SIZE - 50) { free(fbuff); - fbuff = (char *)calloc(size + 50, 1); - if (!fbuff) - return 0; + fbuff = (char*)calloc(size + 50, 1); + if (!fbuff) return 0; } - fp.read((uint8_t *)fbuff, size); + fp.read((uint8_t*)fbuff, size); fp.close(); ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: File descriptor used")); @@ -137,15 +111,13 @@ Renderer *Init_uDisplay(const char *desc) } #endif // USE_UFILESYS + #ifdef USE_SCRIPT - if (bitRead(Settings->rule_enabled, 0) && !ddesc) - { - uint8_t dfound = Run_Scripter(">d", -2, 0); - if (dfound == 99) - { + if (bitRead(Settings->rule_enabled, 0) && !ddesc) { + uint8_t dfound = Run_Scripter(">d",-2,0); + if (dfound == 99) { char *lp = glob_script_mem.section_ptr + 2; - while (*lp != '\n') - lp++; + while (*lp != '\n') lp++; memcpy(fbuff, lp + 1, DISPDESC_SIZE - 1); ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: Script descriptor used")); @@ -154,57 +126,47 @@ Renderer *Init_uDisplay(const char *desc) #endif // USE_SCRIPT #ifdef USE_RULES - if (!bitRead(Settings->rule_enabled, 2) && !ddesc) - { + if (!bitRead(Settings->rule_enabled, 2) && !ddesc) { // only if rule3 is not enabled for rules char *cp = Settings->rules[2]; - while (*cp == ' ') - cp++; + while (*cp == ' ') cp++; memcpy(fbuff, cp, DISPDESC_SIZE - 1); - if (fbuff[0] == ':' && fbuff[1] == 'H') - { + if (fbuff[0] == ':' && fbuff[1] == 'H') { // assume display descriptor, replace space with line feed - for (uint32_t cnt = 0; cnt < DISPDESC_SIZE; cnt++) - { - if (fbuff[cnt] == ' ') - fbuff[cnt] = '\n'; + for (uint32_t cnt = 0; cnt < DISPDESC_SIZE; cnt++) { + if (fbuff[cnt] == ' ') fbuff[cnt] = '\n'; } ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: Rule 3 descriptor used")); } + } #endif // USE_RULES + #ifdef DSP_ROM_DESC - if (!ddesc) - { + if (!ddesc) { memcpy_P(fbuff, DSP_SAMPLE_DESC, sizeof(DSP_SAMPLE_DESC)); ddesc = fbuff; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: Flash descriptor used")); } #endif // DSP_ROM_DESC - if (!ddesc) - { + if (!ddesc) { AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: No valid descriptor found")); - if (fbuff) - free(fbuff); + if (fbuff) free(fbuff); return 0; } // now replace tasmota vars before passing to driver char *cp = strstr(ddesc, "I2C"); - if (cp) - { + if (cp) { cp += 3; uint8_t wire_n = 1; - if (*cp == '1' || *cp == '2') - { + if (*cp == '1' || *cp == '2') { wire_n = *cp & 3; cp += 2; - } - else - { + } else { cp++; } //,3c,22,21,-1 @@ -214,57 +176,48 @@ Renderer *Init_uDisplay(const char *desc) sda = replacepin(&cp, Pin(GPIO_I2C_SDA, wire_n - 1)); replacepin(&cp, Pin(GPIO_OLED_RESET)); - if (wire_n == 1) - { - if (!TasmotaGlobal.i2c_enabled) - { + if (wire_n == 1) { + if (!TasmotaGlobal.i2c_enabled) { I2cBegin(sda, scl); } } #ifdef ESP32 - if (wire_n == 2) - { - if (!TasmotaGlobal.i2c_enabled_2) - { + if (wire_n == 2) { + if (!TasmotaGlobal.i2c_enabled_2) { I2c2Begin(sda, scl); } } - if (I2cSetDevice(i2caddr, wire_n - 1)) - { + if (I2cSetDevice(i2caddr, wire_n - 1)) { I2cSetActiveFound(i2caddr, "DSP-I2C", wire_n - 1); } #endif // ESP32 #ifdef ESP8266 - if (I2cSetDevice(i2caddr)) - { + if (I2cSetDevice(i2caddr)) { I2cSetActiveFound(i2caddr, "DSP-I2C"); } #endif // ESP8266 - // AddLog(LOG_LEVEL_INFO, PSTR("DSP: i2c %x, %d, %d, %d!"), i2caddr, wire_n, scl, sda); + //AddLog(LOG_LEVEL_INFO, PSTR("DSP: i2c %x, %d, %d, %d!"), i2caddr, wire_n, scl, sda); } cp = strstr(ddesc, "SPI,"); - if (cp) - { + if (cp) { cp += 4; //; 7 params nr,cs,sclk,mosi,dc,bl,reset,miso - // SPI,*,*,*,*,*,*,* - switch (*cp) - { - case '1': - cs = Pin(GPIO_SPI_CS); - break; - case '2': - cs = Pin(GPIO_SPI_CS, 1); - break; - default: - cs = Pin(GPIO_SSPI_CS); - break; + //SPI,*,*,*,*,*,*,* + switch (*cp) { + case '1': + cs = Pin(GPIO_SPI_CS); + break; + case '2': + cs = Pin(GPIO_SPI_CS, 1); + break; + default: + cs = Pin(GPIO_SSPI_CS); + break; } - if (*cp == '1') - { - cp += 2; + if (*cp == '1') { + cp+=2; replacepin(&cp, cs); replacepin(&cp, Pin(GPIO_SPI_CLK)); replacepin(&cp, Pin(GPIO_SPI_MOSI)); @@ -272,10 +225,8 @@ Renderer *Init_uDisplay(const char *desc) replacepin(&cp, Pin(GPIO_BACKLIGHT)); replacepin(&cp, Pin(GPIO_OLED_RESET)); replacepin(&cp, Pin(GPIO_SPI_MISO)); - } - else if (*cp == '2') - { - cp += 2; + } else if (*cp == '2') { + cp+=2; replacepin(&cp, cs); replacepin(&cp, Pin(GPIO_SPI_CLK, 1)); replacepin(&cp, Pin(GPIO_SPI_MOSI, 1)); @@ -283,11 +234,9 @@ Renderer *Init_uDisplay(const char *desc) replacepin(&cp, Pin(GPIO_BACKLIGHT)); replacepin(&cp, Pin(GPIO_OLED_RESET)); replacepin(&cp, Pin(GPIO_SPI_MISO, 1)); - } - else - { + } else { // soft spi pins - cp += 2; + cp+=2; replacepin(&cp, cs); replacepin(&cp, Pin(GPIO_SSPI_SCLK)); replacepin(&cp, Pin(GPIO_SSPI_MOSI)); @@ -301,8 +250,7 @@ Renderer *Init_uDisplay(const char *desc) uint16_t xs, ys; // we need screen size for gt911 touch controler cp = strstr(ddesc, ":H,"); - if (cp) - { + if (cp) { cp += 3; cp = strchr(cp, ','); cp++; @@ -315,8 +263,7 @@ Renderer *Init_uDisplay(const char *desc) int8_t xp, xm, yp, ym; cp = strstr(ddesc, "PAR,"); - if (cp) - { + if (cp) { cp += 4; // 8 or 16 bus uint8_t mode = strtol(cp, &cp, 10); @@ -339,8 +286,7 @@ Renderer *Init_uDisplay(const char *desc) replacepin(&cp, Pin(GPIO_DPAR6)); replacepin(&cp, Pin(GPIO_DPAR7)); - if (mode == 16) - { + if (mode == 16) { replacepin(&cp, Pin(GPIO_DPAR8)); replacepin(&cp, Pin(GPIO_DPAR9)); replacepin(&cp, Pin(GPIO_DPAR10)); @@ -352,27 +298,25 @@ Renderer *Init_uDisplay(const char *desc) } } #endif // CONFIG_IDF_TARGET_ESP32S3 - /* - File fp; - fp = ffsp->open("/dump.txt", "w"); - fp.write((uint8_t*)ddesc, DISPDESC_SIZE); - fp.close(); - */ +/* + File fp; + fp = ffsp->open("/dump.txt", "w"); + fp.write((uint8_t*)ddesc, DISPDESC_SIZE); + fp.close(); +*/ // init renderer - if (renderer) - { + if (renderer) { delete renderer; AddLog(LOG_LEVEL_DEBUG, PSTR("DSP: reinit")); } - udisp = new uDisplay(ddesc); + udisp = new uDisplay(ddesc); // checck for touch option TI1 or TI2 -#if defined(USE_FT5206) || defined(USE_GT911) || defined(USE_CST816S) +#if defined (USE_CST816S) || defined(USE_FT5206) || defined(USE_GT911) cp = strstr(ddesc, ":TI"); - if (cp) - { + if (cp) { uint8_t wire_n = 1; cp += 3; wire_n = (*cp & 3) - 1; @@ -382,71 +326,48 @@ Renderer *Init_uDisplay(const char *desc) int8_t scl, sda, irq = -1, rst = -1; scl = replacepin(&cp, Pin(GPIO_I2C_SCL, wire_n)); sda = replacepin(&cp, Pin(GPIO_I2C_SDA, wire_n)); - if (*(cp - 1) == ',') - { + if (*(cp - 1) == ',') { irq = strtol(cp, &cp, 10); - } - else - { + } else { irq = -1; } - if (*cp == ',') - { + if (*cp == ',') { cp++; rst = strtol(cp, &cp, 10); - } - else - { + } else { rst = -1; } - if (wire_n == 0) - { - if (!TasmotaGlobal.i2c_enabled) - { + if (wire_n == 0) { + if (!TasmotaGlobal.i2c_enabled) { I2cBegin(sda, scl); } } - #ifdef ESP32 - if (wire_n == 1) - { - if (!TasmotaGlobal.i2c_enabled_2) - { + if (wire_n == 1) { + if (!TasmotaGlobal.i2c_enabled_2) { I2c2Begin(sda, scl, 400000); } } - if (I2cSetDevice(i2caddr, wire_n)) - { - if (i2caddr == GT911_address) - { + if (I2cSetDevice(i2caddr, wire_n)) { + if (i2caddr == GT911_address) { I2cSetActiveFound(i2caddr, "GT911", wire_n); - } - else if (i2caddr == CST816S_address) - { + } else if (i2caddr == CST816S_address) { I2cSetActiveFound(i2caddr, "CST816S", wire_n); - } - else - { + } else { I2cSetActiveFound(i2caddr, "FT5206", wire_n); } } #endif // ESP32 #ifdef ESP8266 - // AddLog(LOG_LEVEL_INFO, PSTR("DSP: touch %x, %d, %d, %d!"), i2caddr, wire_n, scl, sda); - if (I2cSetDevice(i2caddr)) - { - if (i2caddr == GT911_address) - { + //AddLog(LOG_LEVEL_INFO, PSTR("DSP: touch %x, %d, %d, %d!"), i2caddr, wire_n, scl, sda); + if (I2cSetDevice(i2caddr)) { + if (i2caddr == GT911_address) { I2cSetActiveFound(i2caddr, "GT911"); - } - else if (i2caddr == CST816S_address) - { + } else if (i2caddr == CST816S_address) { I2cSetActiveFound(i2caddr, "CST816S"); - } - else - { + } else { I2cSetActiveFound(i2caddr, "FT5206"); } } @@ -454,89 +375,66 @@ Renderer *Init_uDisplay(const char *desc) // start digitizer #ifdef ESP32 - if (i2caddr == GT911_address) - { + if (i2caddr == GT911_address) { #ifdef USE_GT911 - if (!wire_n) - GT911_Touch_Init(&Wire, irq, rst, xs, ys); - else - GT911_Touch_Init(&Wire1, irq, rst, xs, ys); + 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) - { + } else if (i2caddr == CST816S_address) { #ifdef USE_CST816S - if (!wire_n) - CST816S_Touch_Init(&Wire, irq, rst); - else - CST816S_Touch_Init(&Wire1, irq, rst); + if (!wire_n) CST816S_Touch_Init(&Wire, irq, rst); + else CST816S_Touch_Init(&Wire1, irq, rst); #endif - } - else - { + } else { #ifdef USE_FT5206 - if (!wire_n) - FT5206_Touch_Init(Wire); - else - FT5206_Touch_Init(Wire1); + if (!wire_n) FT5206_Touch_Init(Wire); + else FT5206_Touch_Init(Wire1); #endif } #else - if (i2caddr == GT911_address) - { + if (i2caddr == GT911_address) { #ifdef USE_GT911 - if (!wire_n) - GT911_Touch_Init(&Wire, irq, rst, xs, ys); + if (!wire_n) GT911_Touch_Init(&Wire, irq, rst, xs, ys); #endif - } - else if (i2caddr == CST816S_address) - { + } else if (i2caddr == CST816S_address) { #ifdef USE_CST816S - if (!wire_n) - CST816S_Touch_Init(&Wire, irq, rst); + if (!wire_n) CST816S_Touch_Init(&Wire, irq, rst); #endif - } - else - { + } else { #ifdef USE_FT5206 - if (!wire_n) - FT5206_Touch_Init(Wire); + if (!wire_n) FT5206_Touch_Init(Wire); #endif } #endif // ESP32 + } -#endif // USE_FT5206 || GT911 || CST816S +#endif // USE_FT5206 || GT911 #ifdef USE_XPT2046 cp = strstr(ddesc, ":TS,"); - if (cp) - { + if (cp) { cp += 4; uint8_t touch_cs = replacepin(&cp, Pin(GPIO_XPT2046_CS)); int8_t irqpin = -1; - if (*(cp - 1) == ',') - { + if (*(cp - 1) == ',') { irqpin = strtol(cp, &cp, 10); } uint8_t bus = 1; - if (*cp == ',') - { + if (*cp == ',') { cp++; bus = strtol(cp, &cp, 10); - if (bus < 1) - bus = 1; + if (bus < 1) bus = 1; } - XPT2046_Touch_Init(touch_cs, irqpin, bus - 1); + XPT2046_Touch_Init(touch_cs, irqpin, bus - 1); } #endif // USE_XPT2046 #ifdef CONFIG_IDF_TARGET_ESP32S3 #ifdef SIMPLE_RES_TOUCH cp = strstr(ddesc, ":TR,"); - if (cp) - { + if (cp) { cp += 4; Simple_ResTouch_Init(xp, xm, yp, ym); } @@ -546,19 +444,16 @@ Renderer *Init_uDisplay(const char *desc) uint8_t inirot = Settings->display_rotate; cp = strstr(ddesc, ":r,"); - if (cp) - { - cp += 3; + if (cp) { + cp+=3; inirot = strtol(cp, &cp, 10); } // release desc buffer - if (fbuff) - free(fbuff); + if (fbuff) free(fbuff); renderer = udisp->Init(); - if (!renderer) - return 0; + if (!renderer) return 0; fg_color = renderer->fgcol(); bg_color = renderer->bgcol(); @@ -576,9 +471,8 @@ Renderer *Init_uDisplay(const char *desc) bool iniinv = Settings->display_options.invert; cp = strstr(ddesc, ":n,"); - if (cp) - { - cp += 3; + if (cp) { + cp+=3; iniinv = strtol(cp, &cp, 10); Settings->display_options.invert = iniinv; } @@ -587,8 +481,7 @@ Renderer *Init_uDisplay(const char *desc) ApplyDisplayDimmer(); #ifdef SHOW_SPLASH - if (!Settings->flag5.display_no_splash) - { + if (!Settings->flag5.display_no_splash) { renderer->Splash(); } #endif // SHOW_SPLASH @@ -603,25 +496,21 @@ Renderer *Init_uDisplay(const char *desc) /*********************************************************************************************/ -int8_t replacepin(char **cp, uint16_t pin) -{ +int8_t replacepin(char **cp, uint16_t pin) { int8_t res = 0; char *lp = *cp; - if (*lp == ',') - lp++; - if (*lp == '*') - { + if (*lp == ',') lp++; + if (*lp == '*') { char val[8]; itoa(pin, val, 10); uint16_t slen = strlen(val); - // AddLog(LOG_LEVEL_INFO, PSTR("replace pin: %d"), pin); + //AddLog(LOG_LEVEL_INFO, PSTR("replace pin: %d"), pin); memmove(lp + slen, lp + 1, strlen(lp)); memmove(lp, val, slen); } - res = strtol(lp, 0, 10); + res= strtol(lp, 0, 10); char *np = strchr(lp, ','); - if (np) - { + if (np) { *cp = np + 1; } return res; @@ -632,25 +521,19 @@ int8_t replacepin(char **cp, uint16_t pin) void UDISP_PrintLog(void) { disp_refresh--; - if (!disp_refresh) - { + if (!disp_refresh) { disp_refresh = Settings->display_refresh; - if (!disp_screen_buffer_cols) - { - DisplayAllocScreenBuffer(); - } + if (!disp_screen_buffer_cols) { DisplayAllocScreenBuffer(); } - char *txt = DisplayLogBuffer('\370'); - if (txt != NULL) - { - uint8_t last_row = Settings->display_rows - 1; + char* txt = DisplayLogBuffer('\370'); + if (txt != NULL) { + uint8_t last_row = Settings->display_rows -1; renderer->clearDisplay(); renderer->setTextSize(Settings->display_size); - renderer->setCursor(0, 0); - for (byte i = 0; i < last_row; i++) - { - strlcpy(disp_screen_buffer[i], disp_screen_buffer[i + 1], disp_screen_buffer_cols); + renderer->setCursor(0,0); + for (byte i = 0; i < last_row; i++) { + strlcpy(disp_screen_buffer[i], disp_screen_buffer[i +1], disp_screen_buffer_cols); renderer->println(disp_screen_buffer[i]); } strlcpy(disp_screen_buffer[last_row], txt, disp_screen_buffer_cols); @@ -672,66 +555,60 @@ void UDISP_Time(void) renderer->setTextSize(Settings->display_size); renderer->setTextFont(Settings->display_font); renderer->setCursor(0, 0); - snprintf_P(line, sizeof(line), PSTR(" %02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second); // [ 12:34:56 ] + snprintf_P(line, sizeof(line), PSTR(" %02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second); // [ 12:34:56 ] renderer->println(line); renderer->println(); - snprintf_P(line, sizeof(line), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%04d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year); // [01-02-2018] + snprintf_P(line, sizeof(line), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%04d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year); // [01-02-2018] renderer->println(line); renderer->Updateframe(); } -void UDISP_Refresh(void) // Every second +void UDISP_Refresh(void) // Every second { - if (!renderer) - return; - if (Settings->display_mode) - { // Mode 0 is User text - switch (Settings->display_mode) - { - case 1: // Time - UDISP_Time(); - break; - case 2: // Local - case 3: // Local - case 4: // Mqtt - case 5: // Mqtt - UDISP_PrintLog(); - break; + if (!renderer) return; + if (Settings->display_mode) { // Mode 0 is User text + switch (Settings->display_mode) { + case 1: // Time + UDISP_Time(); + break; + case 2: // Local + case 3: // Local + case 4: // Mqtt + case 5: // Mqtt + UDISP_PrintLog(); + break; } } } -#endif // USE_DISPLAY_MODES1TO5 +#endif // USE_DISPLAY_MODES1TO5 /*********************************************************************************************\ * Interface \*********************************************************************************************/ -bool Xdsp17(uint32_t function) -{ +bool Xdsp17(uint32_t function) { bool result = false; - if (FUNC_DISPLAY_INIT_DRIVER == function) - { + if (FUNC_DISPLAY_INIT_DRIVER == function) { Init_uDisplay(nullptr); } - else if (udisp_init_done && (XDSP_17 == Settings->display_model)) - { - switch (function) - { - case FUNC_DISPLAY_MODEL: - result = true; - break; + else if (udisp_init_done && (XDSP_17 == Settings->display_model)) { + switch (function) { + case FUNC_DISPLAY_MODEL: + result = true; + break; #ifdef USE_DISPLAY_MODES1TO5 - case FUNC_DISPLAY_EVERY_SECOND: - UDISP_Refresh(); - break; -#endif // USE_DISPLAY_MODES1TO5 + case FUNC_DISPLAY_EVERY_SECOND: + UDISP_Refresh(); + break; +#endif // USE_DISPLAY_MODES1TO5 + } } return result; } -#endif // USE_UNIVERSAL_DISPLAY -#endif // USE_DISPLAY +#endif // USE_UNIVERSAL_DISPLAY +#endif // USE_DISPLAY From 610948633226f0d2b90b3a4d13cca62d937a9373 Mon Sep 17 00:00:00 2001 From: Arne Meeuw Date: Tue, 12 Dec 2023 16:12:05 +0100 Subject: [PATCH 4/8] Add supported gestures (untested) --- tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index c06a7f9eef7c..fa91a8a7e7ac 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -211,6 +211,37 @@ int16_t CST816S_x() { int16_t CST816S_y() { return CST816S_touchp->data.y; } + +TS_Gesture CST816S_gesture() { + switch (CST816S_touchp->data.gestureID) + { + case 0x01: // SWIPE_UP + return TS_Gesture.TS_Gest_Move_Up; + break; + case 0x02: // SWIPE_DOWN + return TS_Gesture.TS_Gest_Move_Down; + break; + case 0x03: // SWIPE_LEFT + return TS_Gesture.TS_Gest_Move_Left; + break; + case 0x04: // SWIPE_RIGHT + return TS_Gesture.TS_Gest_Move_Right; + break; + case 0x05: // SINGLE_CLICK + return TS_Gesture.TS_Gest_None; + break; + case 0x0B: // DOUBLE_CLICK + return TS_Gesture.TS_Gest_None; + break; + case 0x0C: // LONG_PRESS + return TS_Gesture.TS_Gest_None; + break; + default: // NONE + return TS_Gesture.TS_Gest_None; + break; + } +} + #endif // USE_CST816S #ifdef USE_FT5206 @@ -341,6 +372,7 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { if (TSGlobal.touched) { TSGlobal.raw_touch_xp = CST816S_x(); TSGlobal.raw_touch_yp = CST816S_y(); + TSGlobal.gesture = CST816S_gesture(); } } #endif // USE_CST816S From 6529f3c299c041c634160cab70b3a996d95b5fec Mon Sep 17 00:00:00 2001 From: Arne Meeuw Date: Tue, 12 Dec 2023 17:09:48 +0100 Subject: [PATCH 5/8] Correct use of enums --- tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index fa91a8a7e7ac..44e7fdf46789 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -212,32 +212,32 @@ int16_t CST816S_y() { return CST816S_touchp->data.y; } -TS_Gesture CST816S_gesture() { +uint8_t CST816S_gesture() { switch (CST816S_touchp->data.gestureID) { case 0x01: // SWIPE_UP - return TS_Gesture.TS_Gest_Move_Up; + return TS_Gest_Move_Up; break; case 0x02: // SWIPE_DOWN - return TS_Gesture.TS_Gest_Move_Down; + return TS_Gest_Move_Down; break; case 0x03: // SWIPE_LEFT - return TS_Gesture.TS_Gest_Move_Left; + return TS_Gest_Move_Left; break; case 0x04: // SWIPE_RIGHT - return TS_Gesture.TS_Gest_Move_Right; + return TS_Gest_Move_Right; break; case 0x05: // SINGLE_CLICK - return TS_Gesture.TS_Gest_None; + return TS_Gest_None; break; case 0x0B: // DOUBLE_CLICK - return TS_Gesture.TS_Gest_None; + return TS_Gest_None; break; case 0x0C: // LONG_PRESS - return TS_Gesture.TS_Gest_None; + return TS_Gest_None; break; default: // NONE - return TS_Gesture.TS_Gest_None; + return TS_Gest_None; break; } } From b45b2bb54fa405a5272f954906ed75caf460eb16 Mon Sep 17 00:00:00 2001 From: Arne Meeuw Date: Thu, 14 Dec 2023 18:05:17 +0100 Subject: [PATCH 6/8] Remove library dependency --- lib/lib_display/CST816S/CST816S.cpp | 191 ------------------ lib/lib_display/CST816S/CST816S.h | 76 ------- tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 88 +++++--- .../xdsp_17_universal.ino | 5 +- 4 files changed, 62 insertions(+), 298 deletions(-) delete mode 100644 lib/lib_display/CST816S/CST816S.cpp delete mode 100644 lib/lib_display/CST816S/CST816S.h diff --git a/lib/lib_display/CST816S/CST816S.cpp b/lib/lib_display/CST816S/CST816S.cpp deleted file mode 100644 index e3b7c07f76b2..000000000000 --- a/lib/lib_display/CST816S/CST816S.cpp +++ /dev/null @@ -1,191 +0,0 @@ -/* - MIT License - - Copyright (c) 2021 Felix Biego - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ - -#include "Arduino.h" -#include -#include - -#include "CST816S.h" - -/*! - @brief Constructor for CST816S - @param port - i2c port - @param rst - touch reset pin - @param irq - touch interrupt pin -*/ -CST816S::CST816S(TwoWire *use_wire, int8_t irq, int8_t rst) { - wire = use_wire; - _rst = rst; - _irq = irq; -} - -/*! - @brief read touch data -*/ -void CST816S::read_touch() { - byte data_raw[8]; - i2c_read(CST816S_address, 0x01, data_raw, 6); - - data.gestureID = data_raw[0]; - data.points = data_raw[1]; - data.event = data_raw[2] >> 6; - data.x = ((data_raw[2] & 0xF) << 8) + data_raw[3]; - data.y = ((data_raw[4] & 0xF) << 8) + data_raw[5]; -} - -/*! - @brief handle interrupts -*/ -void IRAM_ATTR CST816S::handleISR(void) { - _event_available = true; -} - -/*! - @brief initialize the touch screen - @param interrupt - type of interrupt FALLING, RISING.. -*/ -void CST816S::begin(int interrupt) { - pinMode(_irq, INPUT); - pinMode(_rst, OUTPUT); - - digitalWrite(_rst, HIGH); - delay(50); - digitalWrite(_rst, LOW); - delay(5); - digitalWrite(_rst, HIGH); - delay(50); - - i2c_read(CST816S_address, 0x15, &data.version, 1); - delay(5); - i2c_read(CST816S_address, 0xA7, data.versionInfo, 3); - - attachInterrupt(_irq, std::bind(&CST816S::handleISR, this), interrupt); -} - -/*! - @brief check for a touch event -*/ -bool CST816S::available() { - if (_event_available) { - read_touch(); - _event_available = false; - return true; - } - return false; -} - -/*! - @brief put the touch screen in standby mode -*/ -void CST816S::sleep() { - digitalWrite(_rst, LOW); - delay(5); - digitalWrite(_rst, HIGH); - delay(50); - byte standby_value = 0x03; - i2c_write(CST816S_address, 0xA5, &standby_value, 1); -} - -/*! - @brief get the gesture event name -*/ -String CST816S::gesture() { - switch (data.gestureID) { - case NONE: - return "NONE"; - break; - case SWIPE_DOWN: - return "SWIPE DOWN"; - break; - case SWIPE_UP: - return "SWIPE UP"; - break; - case SWIPE_LEFT: - return "SWIPE LEFT"; - break; - case SWIPE_RIGHT: - return "SWIPE RIGHT"; - break; - case SINGLE_CLICK: - return "SINGLE CLICK"; - break; - case DOUBLE_CLICK: - return "DOUBLE CLICK"; - break; - case LONG_PRESS: - return "LONG PRESS"; - break; - default: - return "UNKNOWN"; - break; - } -} - -/*! - @brief read data from i2c - @param addr - i2c device address - @param reg_addr - device register address - @param reg_data - array to copy the read data - @param length - length of data -*/ -uint8_t CST816S::i2c_read(uint16_t addr, uint8_t reg_addr, uint8_t *reg_data, size_t length) { - wire->beginTransmission(addr); - wire->write(reg_addr); - if (wire->endTransmission(true)) return -1; - wire->requestFrom(addr, length, true); - for (int i = 0; i < length; i++) { - *reg_data++ = wire->read(); - } - return 0; -} - -/*! - @brief write data to i2c - @brief read data from i2c - @param addr - i2c device address - @param reg_addr - device register address - @param reg_data - data to be sent - @param length - length of data -*/ -uint8_t CST816S::i2c_write(uint8_t addr, uint8_t reg_addr, const uint8_t *reg_data, size_t length) { - wire->beginTransmission(addr); - wire->write(reg_addr); - for (int i = 0; i < length; i++) { - wire->write(*reg_data++); - } - if (wire->endTransmission(true)) return -1; - return 0; -} \ No newline at end of file diff --git a/lib/lib_display/CST816S/CST816S.h b/lib/lib_display/CST816S/CST816S.h deleted file mode 100644 index fe8c07348cb9..000000000000 --- a/lib/lib_display/CST816S/CST816S.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - MIT License - - Copyright (c) 2021 Felix Biego - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ - -#ifndef CST816S_H -#define CST816S_H - -#include - -#define CST816S_address 0x15 - -enum GESTURE { - NONE = 0x00, - SWIPE_UP = 0x01, - SWIPE_DOWN = 0x02, - SWIPE_LEFT = 0x03, - SWIPE_RIGHT = 0x04, - SINGLE_CLICK = 0x05, - DOUBLE_CLICK = 0x0B, - LONG_PRESS = 0x0C -}; - -struct data_struct { - byte gestureID; // Gesture ID - byte points; // Number of touch points - byte event; // Event (0 = Down, 1 = Up, 2 = Contact) - int x; - int y; - uint8_t version; - uint8_t versionInfo[3]; -}; - -class CST816S { - -public: - CST816S(TwoWire *use_wire, int8_t irq = 0, int8_t rst = 1); - void begin(int interrupt = RISING); - void sleep(); - bool available(); - data_struct data; - String gesture(); - -private: - TwoWire *wire; - int8_t _rst; - int8_t _irq; - bool _event_available; - - void IRAM_ATTR handleISR(); - void read_touch(); - - uint8_t i2c_read(uint16_t addr, uint8_t reg_addr, uint8_t *reg_data, size_t length); - uint8_t i2c_write(uint8_t addr, uint8_t reg_addr, const uint8_t *reg_data, size_t length); -}; - -#endif \ No newline at end of file diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index 44e7fdf46789..a08c2b75579b 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -187,33 +187,37 @@ int16_t SRES_y() { #endif #ifdef USE_CST816S -#include -// touch panel controller -CST816S *CST816S_touchp; +#undef CST816S_address +#define CST816S_address 0x15 -bool CST816S_Touch_Init(TwoWire *i2c, int8_t irq_pin, int8_t rst_pin) { - CST816S_found = false; - CST816S_touchp = new CST816S(i2c, irq_pin, rst_pin); - CST816S_touchp->begin(); - CST816S_found = true; - AddLog(LOG_LEVEL_INFO, PSTR("TI: CST816S")); - return CST816S_found; -} +bool CST816S_event_available = false; +uint8_t CST816S_bus = 0; +uint8_t CST816S_irq = 0; +uint8_t CST816S_rst = 0; -bool CST816S_touched() { - return CST816S_touchp->available(); +void IRAM_ATTR CST816S_handleISR(void) { + CST816S_event_available = true; } -int16_t CST816S_x() { - return CST816S_touchp->data.x; +void CST816S_begin(int interrupt = RISING) { + pinMode(CST816S_irq, INPUT); + pinMode(CST816S_rst, OUTPUT); + digitalWrite(CST816S_rst, HIGH); + delay(50); + digitalWrite(CST816S_rst, LOW); + delay(5); + digitalWrite(CST816S_rst, 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(CST816S_irq, &CST816S_handleISR, interrupt); } -int16_t CST816S_y() { - return CST816S_touchp->data.y; -} - -uint8_t CST816S_gesture() { - switch (CST816S_touchp->data.gestureID) +uint8_t CST816S_map_gesture(uint8_t gesture) { + switch (gesture) { case 0x01: // SWIPE_UP return TS_Gest_Move_Up; @@ -242,6 +246,39 @@ uint8_t CST816S_gesture() { } } +void CST816S_read_touch() { + byte data_raw[8]; + I2cReadBuffer(CST816S_address, 0x01, data_raw, 6, CST816S_bus); + uint8_t gesture = data_raw[0]; + uint8_t touchPoints = data_raw[1]; + uint8_t event = data_raw[2] >> 6; + int x = ((data_raw[2] & 0xF) << 8) + data_raw[3]; + int y = ((data_raw[4] & 0xF) << 8) + data_raw[5]; + TSGlobal.raw_touch_xp = x; + TSGlobal.raw_touch_yp = y; + TSGlobal.gesture = CST816S_map_gesture(gesture); +} + +bool CST816S_available() { + if (CST816S_event_available) { + CST816S_read_touch(); + CST816S_event_available = false; + return true; + } + return false; +} + +bool CST816S_Touch_Init(uint8_t bus, int8_t irq_pin, int8_t rst_pin) { + CST816S_found = false; + CST816S_bus = bus; + CST816S_irq = irq_pin; + CST816S_rst = rst_pin; + CST816S_begin(); + CST816S_found = true; + AddLog(LOG_LEVEL_INFO, PSTR("TI: CST816S")); + return CST816S_found; +} + #endif // USE_CST816S #ifdef USE_FT5206 @@ -368,12 +405,7 @@ void Touch_Check(void(*rotconvert)(int16_t *x, int16_t *y)) { #ifdef USE_CST816S if (CST816S_found) { - TSGlobal.touched = CST816S_touched(); - if (TSGlobal.touched) { - TSGlobal.raw_touch_xp = CST816S_x(); - TSGlobal.raw_touch_yp = CST816S_y(); - TSGlobal.gesture = CST816S_gesture(); - } + TSGlobal.touched = CST816S_available(); } #endif // USE_CST816S @@ -430,7 +462,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); diff --git a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino index fb7f9c9aca25..b9213ed175b9 100644 --- a/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino +++ b/tasmota/tasmota_xdsp_display/xdsp_17_universal.ino @@ -382,8 +382,7 @@ int8_t cs; #endif } else if (i2caddr == CST816S_address) { #ifdef USE_CST816S - if (!wire_n) CST816S_Touch_Init(&Wire, irq, rst); - else CST816S_Touch_Init(&Wire1, irq, rst); + CST816S_Touch_Init(wire_n, irq, rst); #endif } else { #ifdef USE_FT5206 @@ -400,7 +399,7 @@ int8_t cs; #endif } else if (i2caddr == CST816S_address) { #ifdef USE_CST816S - if (!wire_n) CST816S_Touch_Init(&Wire, irq, rst); + CST816S_Touch_Init(wire_n, irq, rst); #endif } else { #ifdef USE_FT5206 From 60b299c0ce24867a02d211e9b4fd402aeeb8d76c Mon Sep 17 00:00:00 2001 From: Arne Meeuw Date: Thu, 14 Dec 2023 18:10:02 +0100 Subject: [PATCH 7/8] Unification of methods --- tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 57 +++++++------------ 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index a08c2b75579b..860cc4ca8d6f 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -199,23 +199,6 @@ void IRAM_ATTR CST816S_handleISR(void) { CST816S_event_available = true; } -void CST816S_begin(int interrupt = RISING) { - pinMode(CST816S_irq, INPUT); - pinMode(CST816S_rst, OUTPUT); - digitalWrite(CST816S_rst, HIGH); - delay(50); - digitalWrite(CST816S_rst, LOW); - delay(5); - digitalWrite(CST816S_rst, 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(CST816S_irq, &CST816S_handleISR, interrupt); -} - uint8_t CST816S_map_gesture(uint8_t gesture) { switch (gesture) { @@ -246,34 +229,38 @@ uint8_t CST816S_map_gesture(uint8_t gesture) { } } -void CST816S_read_touch() { - byte data_raw[8]; - I2cReadBuffer(CST816S_address, 0x01, data_raw, 6, CST816S_bus); - uint8_t gesture = data_raw[0]; - uint8_t touchPoints = data_raw[1]; - uint8_t event = data_raw[2] >> 6; - int x = ((data_raw[2] & 0xF) << 8) + data_raw[3]; - int y = ((data_raw[4] & 0xF) << 8) + data_raw[5]; - TSGlobal.raw_touch_xp = x; - TSGlobal.raw_touch_yp = y; - TSGlobal.gesture = CST816S_map_gesture(gesture); -} - bool CST816S_available() { if (CST816S_event_available) { - CST816S_read_touch(); - CST816S_event_available = false; - return true; + 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) { +bool CST816S_Touch_Init(uint8_t bus, int8_t irq_pin, int8_t rst_pin, int interrupt = RISING) { CST816S_found = false; CST816S_bus = bus; CST816S_irq = irq_pin; CST816S_rst = rst_pin; - CST816S_begin(); + pinMode(CST816S_irq, INPUT); + pinMode(CST816S_rst, OUTPUT); + digitalWrite(CST816S_rst, HIGH); + delay(50); + digitalWrite(CST816S_rst, LOW); + delay(5); + digitalWrite(CST816S_rst, 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(CST816S_irq, &CST816S_handleISR, interrupt); CST816S_found = true; AddLog(LOG_LEVEL_INFO, PSTR("TI: CST816S")); return CST816S_found; From b8e69d419f266956e477c2abc4de75af9de1fd13 Mon Sep 17 00:00:00 2001 From: Arne Meeuw Date: Thu, 14 Dec 2023 18:41:22 +0100 Subject: [PATCH 8/8] Remove redundant variables and format --- tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino | 58 +++++-------------- 1 file changed, 16 insertions(+), 42 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino index 860cc4ca8d6f..44eca227e5f5 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_55_touch.ino @@ -192,40 +192,17 @@ int16_t SRES_y() { bool CST816S_event_available = false; uint8_t CST816S_bus = 0; -uint8_t CST816S_irq = 0; -uint8_t CST816S_rst = 0; - -void IRAM_ATTR CST816S_handleISR(void) { - CST816S_event_available = true; -} uint8_t CST816S_map_gesture(uint8_t gesture) { - switch (gesture) - { - case 0x01: // SWIPE_UP - return TS_Gest_Move_Up; - break; - case 0x02: // SWIPE_DOWN - return TS_Gest_Move_Down; - break; - case 0x03: // SWIPE_LEFT - return TS_Gest_Move_Left; - break; - case 0x04: // SWIPE_RIGHT - return TS_Gest_Move_Right; - break; - case 0x05: // SINGLE_CLICK - return TS_Gest_None; - break; - case 0x0B: // DOUBLE_CLICK - return TS_Gest_None; - break; - case 0x0C: // LONG_PRESS - return TS_Gest_None; - break; - default: // NONE - return TS_Gest_None; - break; + 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 } } @@ -245,27 +222,24 @@ bool CST816S_available() { bool CST816S_Touch_Init(uint8_t bus, int8_t irq_pin, int8_t rst_pin, int interrupt = RISING) { CST816S_found = false; CST816S_bus = bus; - CST816S_irq = irq_pin; - CST816S_rst = rst_pin; - pinMode(CST816S_irq, INPUT); - pinMode(CST816S_rst, OUTPUT); - digitalWrite(CST816S_rst, HIGH); + pinMode(irq_pin, INPUT); + pinMode(rst_pin, OUTPUT); + digitalWrite(rst_pin, HIGH); delay(50); - digitalWrite(CST816S_rst, LOW); + digitalWrite(rst_pin, LOW); delay(5); - digitalWrite(CST816S_rst, HIGH); + 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(CST816S_irq, &CST816S_handleISR, interrupt); + attachInterrupt(irq_pin, []{ CST816S_event_available = true; }, interrupt); CST816S_found = true; - AddLog(LOG_LEVEL_INFO, PSTR("TI: CST816S")); + 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