diff --git a/lib/FT5206_Library/LICENSE b/lib/FT5206_Library/LICENSE new file mode 100644 index 00000000..3b7bfa9f --- /dev/null +++ b/lib/FT5206_Library/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 lewis he + +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. diff --git a/lib/FT5206_Library/README.md b/lib/FT5206_Library/README.md new file mode 100644 index 00000000..212a4efc --- /dev/null +++ b/lib/FT5206_Library/README.md @@ -0,0 +1,2 @@ +FT5206 Library +===================================== \ No newline at end of file diff --git a/lib/FT5206_Library/keywords.txt b/lib/FT5206_Library/keywords.txt new file mode 100644 index 00000000..2cf7d101 --- /dev/null +++ b/lib/FT5206_Library/keywords.txt @@ -0,0 +1,29 @@ +####################################### +# Syntax Coloring Map For FT5206 Library By lewis He +# github:https://github.com/lewisxhe +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### +TP_Point KEYWORD1 +FT5206_Class KEYWORD1 +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +adjustTheshold KEYWORD2 +getPoint KEYWORD2 +enterSleepMode KEYWORD2 +enterMonitorMode KEYWORD2 + + +####################################### +# Instances (KEYWORD2) +####################################### + + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/lib/FT5206_Library/library.properties b/lib/FT5206_Library/library.properties new file mode 100644 index 00000000..e28709e5 --- /dev/null +++ b/lib/FT5206_Library/library.properties @@ -0,0 +1,10 @@ +name=FT5206_Library +version=1.0.0 +author=Lewis He +maintainer=Lewis He +sentence=Arduino library for FT5206 chip. +paragraph=Arduino library for FT5206 chip. Tested with ESP32 +category=Communication +url=https://github.com/lewisxhe/FT5206_Library +architectures=* +architectures=esp32 \ No newline at end of file diff --git a/lib/FT5206_Library/src/FT5206.cpp b/lib/FT5206_Library/src/FT5206.cpp new file mode 100644 index 00000000..a8bea862 --- /dev/null +++ b/lib/FT5206_Library/src/FT5206.cpp @@ -0,0 +1,108 @@ +///////////////////////////////////////////////////////////////// +/* +MIT License + +Copyright (c) 2019 lewis he + +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. + +FT5206.cpp - Arduino library for FT5206 chip. +Created by Lewis on April 17, 2019. +github:https://github.com/lewisxhe/FT5206_Library +*/ +///////////////////////////////////////////////////////////////// +#include "FT5206.h" + +int FT5206_Class::begin(TwoWire &port, uint8_t addr) +{ + _i2cPort = &port; + _address = addr; + uint8_t val; + _readByte(FT5206_VENDID_REG, 1, &val); + //Serial.printf("vend id %d\n",val ); + if (val != FT5206_VENDID) { + // return false; + } + _readByte(FT5206_CHIPID_REG, 1, &val); + //Serial.printf("chip id %d\n",val ); + if ((val != FT6206_CHIPID) && (val != FT6236_CHIPID) && (val != FT6236U_CHIPID) && (val != FT5206U_CHIPID) && (val != FT5316_CHIPID) ) { + return false; + } + _init = true; + return true; +} + +// valid touching detect threshold. +void FT5206_Class::adjustTheshold(uint8_t thresh) +{ + if (!_init)return; + _writeByte(FT5206_THRESHHOLD_REG, 1, &thresh); +} + +TP_Point FT5206_Class::getPoint(uint8_t num) +{ + if (!_init) return TP_Point(0, 0); + _readRegister(); + if ((_touches == 0) || (num > 1)) { + return TP_Point(0, 0); + } else { + return TP_Point(_x[num], _y[num]); + } +} + +uint8_t FT5206_Class::touched() +{ + if (!_init)return 0; + uint8_t val = 0; + _readByte(FT5206_TOUCHES_REG,1,&val); + return val > 2 ? 0: val; +} + +void FT5206_Class::enterSleepMode() +{ + if (!_init)return; + uint8_t val = FT5206_SLEEP_IN; + _writeByte(FT5206_POWER_REG, 1, &val); +} + +void FT5206_Class::enterMonitorMode() +{ + if (!_init)return; + uint8_t val = FT5206_MONITOR; + _writeByte(FT5206_POWER_REG, 1, &val); +} + +void FT5206_Class::_readRegister() +{ + _readByte(DEVIDE_MODE, 16, _data); + _touches = _data[TD_STATUS]; + if ((_touches > 2) || (_touches == 0)) { + _touches = 0; + return; + } + for (uint8_t i = 0; i < 2; i++) { + _x[i] = _data[TOUCH1_XH + i * 6] & 0x0F; + _x[i] <<= 8; + _x[i] |= _data[TOUCH1_XL + i * 6]; + _y[i] = _data[TOUCH1_YH + i * 6] & 0x0F; + _y[i] <<= 8; + _y[i] |= _data[TOUCH1_YL + i * 6]; + _id[i] = _data[TOUCH1_YH + i * 6] >> 4; + } +} diff --git a/lib/FT5206_Library/src/FT5206.h b/lib/FT5206_Library/src/FT5206.h new file mode 100644 index 00000000..d4c07db5 --- /dev/null +++ b/lib/FT5206_Library/src/FT5206.h @@ -0,0 +1,123 @@ +///////////////////////////////////////////////////////////////// +/* +MIT License + +Copyright (c) 2019 lewis he + +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. + +FT5206.h - Arduino library for FT5206 chip. +Created by Lewis on April 17, 2019. +github:https://github.com/lewisxhe/FT5206_Library +*/ +///////////////////////////////////////////////////////////////// +#pragma once + +#include +#include + +#define FT5206_SLAVE_ADDRESS (0x38) +#define FT5206_MODE_REG (0x00) +#define FT5206_TOUCHES_REG (0x02) +#define FT5206_VENDID_REG (0xA8) +#define FT5206_CHIPID_REG (0xA3) +#define FT5206_THRESHHOLD_REG (0x80) +#define FT5206_POWER_REG (0x87) + +#define FT5206_MONITOR (0x01) +#define FT5206_SLEEP_IN (0x03) + +#define FT5206_VENDID 0x11 +#define FT6206_CHIPID 0x06 +#define FT6236_CHIPID 0x36 +#define FT6236U_CHIPID 0x64 +#define FT5206U_CHIPID 0x64 + +#define FT5316_CHIPID 0x0a + +#define DEVIDE_MODE 0x00 +#define TD_STATUS 0x02 +#define TOUCH1_XH 0x03 +#define TOUCH1_XL 0x04 +#define TOUCH1_YH 0x05 +#define TOUCH1_YL 0x06 + +class TP_Point +{ +public: + TP_Point(void) + { + x = 0; + y = 0; + } + TP_Point(int16_t _x, int16_t _y) + { + x = _x; + y = _y; + } + int16_t x; + int16_t y; +}; + + +class FT5206_Class +{ +public: + FT5206_Class() {}; + int begin(TwoWire &port = Wire, uint8_t addr = FT5206_SLAVE_ADDRESS); + // valid touching detect threshold. + void adjustTheshold(uint8_t thresh); + TP_Point getPoint(uint8_t num = 0); + uint8_t touched(); + void enterSleepMode(); + void enterMonitorMode(); +private: + void _readRegister(); + int _readByte(uint8_t reg, uint8_t nbytes, uint8_t *data) + { + _i2cPort->beginTransmission(_address); + _i2cPort->write(reg); + _i2cPort->endTransmission(); + _i2cPort->requestFrom(_address, nbytes); + uint8_t index = 0; + while (_i2cPort->available()) + data[index++] = _i2cPort->read(); + return 0; + } + + int _writeByte(uint8_t reg, uint8_t nbytes, uint8_t *data) + { + _i2cPort->beginTransmission(_address); + _i2cPort->write(reg); + for (uint8_t i = 0; i < nbytes; i++) { + _i2cPort->write(data[i]); + } + _i2cPort->endTransmission(); + return 0; + } + + uint8_t _address; + uint8_t _data[16]; + uint16_t _x[2]; + uint16_t _y[2]; + uint16_t _id[2]; + uint8_t _touches = 0; + bool _init = false; + TwoWire *_i2cPort; +}; diff --git a/lib/XPT2046_Touchscreen_ID542/XPT2046_Touchscreen.cpp b/lib/XPT2046_Touchscreen_ID542/XPT2046_Touchscreen.cpp index 298fcfc1..654ebd67 100644 --- a/lib/XPT2046_Touchscreen_ID542/XPT2046_Touchscreen.cpp +++ b/lib/XPT2046_Touchscreen_ID542/XPT2046_Touchscreen.cpp @@ -28,7 +28,7 @@ extern SoftSPI xpt2046_spi; SoftSPI spi = xpt2046_spi; #else extern SPIClass xpt2046_spi; -SPIClass spi = xpt2046_spi; +static SPIClass spi = xpt2046_spi; #endif #define Z_THRESHOLD 400 @@ -77,7 +77,7 @@ bool XPT2046_Touchscreen::touched() return (zraw >= Z_THRESHOLD); } -void XPT2046_Touchscreen::readData(uint16_t *x, uint16_t *y, uint8_t *z) +void XPT2046_Touchscreen::readData(int16_t *x, int16_t *y, int16_t *z) { update(); *x = xraw; @@ -111,7 +111,7 @@ void XPT2046_Touchscreen::update() { int16_t data[6]; - if (!isrWake) return; + //if (!isrWake) return; uint32_t now = millis(); if (now - msraw < MSEC_THRESHOLD) return; @@ -134,7 +134,7 @@ void XPT2046_Touchscreen::update() data[5] = spi.transfer16(0) >> 3; digitalWrite(csPin, HIGH); spi.endTransaction(); - //Serial.printf("z=%d :: z1=%d, z2=%d ", z, z1, z2); + Serial.printf("z=%d :: z1=%d, z2=%d ", z, z1, z2); if (z < 0) z = 0; if (z < Z_THRESHOLD) { // if ( !touched ) { // Serial.println(); diff --git a/lib/XPT2046_Touchscreen_ID542/XPT2046_Touchscreen.h b/lib/XPT2046_Touchscreen_ID542/XPT2046_Touchscreen.h index 8358e532..baeb70bd 100644 --- a/lib/XPT2046_Touchscreen_ID542/XPT2046_Touchscreen.h +++ b/lib/XPT2046_Touchscreen_ID542/XPT2046_Touchscreen.h @@ -47,7 +47,7 @@ public: TS_Point getPoint(); bool tirqTouched(); bool touched(); - void readData(uint16_t *x, uint16_t *y, uint8_t *z); + void readData(int16_t *x, int16_t *y, int16_t *z); bool bufferEmpty(); uint8_t bufferSize() { return 1; } void setRotation(uint8_t n) { rotation = n % 4; } diff --git a/lib/lv_drv_tft_espi/tft_espi_drv.cpp b/lib/lv_drv_tft_espi/tft_espi_drv.cpp index bab85a9b..50c9a91a 100644 --- a/lib/lv_drv_tft_espi/tft_espi_drv.cpp +++ b/lib/lv_drv_tft_espi/tft_espi_drv.cpp @@ -122,9 +122,9 @@ void tft_espi_set_touch(uint16_t * calData) tft.setTouch(calData); } -bool tft_espi_get_touch(uint16_t * touchX, uint16_t * touchY, uint16_t threshold) +bool tft_espi_get_touch(int16_t * touchX, int16_t * touchY, uint16_t threshold) { - return tft.getTouch(touchX, touchY, threshold); + return tft.getTouch((uint16_t*)touchX, (uint16_t*)touchY, threshold); } #endif diff --git a/lib/lv_drv_tft_espi/tft_espi_drv.h b/lib/lv_drv_tft_espi/tft_espi_drv.h index f3b812c5..25adc695 100644 --- a/lib/lv_drv_tft_espi/tft_espi_drv.h +++ b/lib/lv_drv_tft_espi/tft_espi_drv.h @@ -49,7 +49,7 @@ void tft_espi_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color #if defined(TOUCH_CS) void tft_espi_calibrate(uint16_t * calData); void tft_espi_set_touch(uint16_t * calData); -bool tft_espi_get_touch(uint16_t * touchX, uint16_t * touchY, uint16_t threshold); +bool tft_espi_get_touch(int16_t * touchX, int16_t * touchY, uint16_t threshold); #endif /********************** diff --git a/src/drv/hasp_drv_ft5206.cpp b/src/drv/hasp_drv_ft5206.cpp new file mode 100644 index 00000000..72881a98 --- /dev/null +++ b/src/drv/hasp_drv_ft5206.cpp @@ -0,0 +1,83 @@ +#if TOUCH_DRIVER == 5206 + + #include + #include "FT5206.h" + #include "ArduinoLog.h" + + #include "hasp_drv_ft5206.h" + + #define RST_PIN (TOUCH_RST) // -1 if pin is connected to VCC else set pin number + +FT5206_Class * touchpanel; + +// Read touch points +bool IRAM_ATTR FT5206_getXY(int16_t * touchX, int16_t * touchY, bool debug) +{ + if(!touchpanel->touched()) return false; + + TP_Point tp = touchpanel->getPoint(0); + *touchX = tp.x; + *touchY = tp.y; + + if(debug) { + char tempString[128]; + sprintf(tempString, "FT5206 touched x: %d y: %d\n", tp.x, tp.y); + Serial.print(tempString); + } + + return true; +} + +void scan(TwoWire & i2c) +{ + byte error, address; + int nDevices; + + Serial.println("Scanning..."); + + nDevices = 0; + for(address = 1; address < 127; address++) { + // The i2c_scanner uses the return value of + // the Write.endTransmisstion to see if + // a device did acknowledge to the address. + i2c.beginTransmission(address); + error = i2c.endTransmission(); + + if(error == 0) { + Serial.print("I2C device found at address 0x"); + if(address < 16) Serial.print("0"); + Serial.print(address, HEX); + Serial.println(" !"); + + nDevices++; + } else if(error == 4) { + Serial.print("Unknown error at address 0x"); + if(address < 16) Serial.print("0"); + Serial.println(address, HEX); + } + } + if(nDevices == 0) + Serial.println("No I2C devices found\n"); + else + Serial.println("done\n"); +} + +// void FT5206_init(TwoWire & i2c) +void FT5206_init() +{ + Log.trace(TAG_DRVR, F("Touch SDA : %d"), TOUCH_SDA); + Log.trace(TAG_DRVR, F("Touch SCL : %d"), TOUCH_SCL); + Log.trace(TAG_DRVR, F("Touch freq. : %d"), TOUCH_FREQUENCY); + Log.trace(TAG_DRVR, F("Touch address : %02x"), FT5206_address); + + Wire1.begin(TOUCH_SDA, TOUCH_SCL, TOUCH_FREQUENCY); + scan(Wire1); + touchpanel = new FT5206_Class(); + + if(touchpanel->begin(Wire1, FT5206_address)) { + Log.trace(TAG_DRVR, F("FT5206 touch driver started")); + } else { + Log.error(TAG_DRVR, F("FT5206 touch driver failed to start")); + } +} +#endif \ No newline at end of file diff --git a/src/drv/hasp_drv_ft5206.h b/src/drv/hasp_drv_ft5206.h new file mode 100644 index 00000000..56420dcb --- /dev/null +++ b/src/drv/hasp_drv_ft5206.h @@ -0,0 +1,17 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#ifndef HASP_DRV_FT5206_H +#define HASP_DRV_FT5206_H + +#if TOUCH_DRIVER == 5206 + + #define FT5206_address 0x38 + + #include "hasp_debug.h" // for TAG_DRVR + +bool IRAM_ATTR FT5206_getXY(int16_t * touchX, int16_t * touchY, bool debug); +void FT5206_init(); + +#endif +#endif \ No newline at end of file diff --git a/src/drv/hasp_drv_ft6336u.cpp b/src/drv/hasp_drv_ft6336u.cpp index 14d94662..65eb403a 100644 --- a/src/drv/hasp_drv_ft6336u.cpp +++ b/src/drv/hasp_drv_ft6336u.cpp @@ -11,7 +11,7 @@ static FT6336U ft6336u(TOUCH_SDA, TOUCH_SCL, TOUCH_RST, TOUCH_IRQ); // Read touch points -bool IRAM_ATTR FT6336U_getXY(uint16_t * touchX, uint16_t * touchY, bool debug) +bool IRAM_ATTR FT6336U_getXY(int16_t * touchX, int16_t * touchY, bool debug) { FT6336U_TouchPointType tp = ft6336u.scan(); diff --git a/src/drv/hasp_drv_ft6336u.h b/src/drv/hasp_drv_ft6336u.h index 896daec0..dfb4b698 100644 --- a/src/drv/hasp_drv_ft6336u.h +++ b/src/drv/hasp_drv_ft6336u.h @@ -1,15 +1,15 @@ /* MIT License - Copyright (c) 2020 Francis Van Roie For full license information read the LICENSE file in the project folder */ +#ifndef HASP_DRV_FT6336U_H +#define HASP_DRV_FT6336U_H + #if TOUCH_DRIVER == 6336 #include "hasp_debug.h" // for TAG_DRVR - #ifndef HASP_DRV_FT6336U_H - #define HASP_DRV_FT6336U_H - -bool IRAM_ATTR FT6336U_getXY(uint16_t * touchX, uint16_t * touchY, bool debug); +bool IRAM_ATTR FT6336U_getXY(int16_t * touchX, int16_t * touchY, bool debug); void FT6336U_init(); - #endif +#endif #endif \ No newline at end of file diff --git a/src/drv/hasp_drv_911.cpp b/src/drv/hasp_drv_gt911.cpp similarity index 98% rename from src/drv/hasp_drv_911.cpp rename to src/drv/hasp_drv_gt911.cpp index 6151da3c..00c17c72 100644 --- a/src/drv/hasp_drv_911.cpp +++ b/src/drv/hasp_drv_gt911.cpp @@ -4,7 +4,7 @@ #include "Goodix.h" #include "ArduinoLog.h" - #include "hasp_drv_911.h" + #include "hasp_drv_gt911.h" #define INT_PIN (TOUCH_IRQ) #define RST_PIN (TOUCH_RST) // -1 if pin is connected to VCC else set pin number diff --git a/src/drv/hasp_drv_911.h b/src/drv/hasp_drv_gt911.h similarity index 83% rename from src/drv/hasp_drv_911.h rename to src/drv/hasp_drv_gt911.h index fd63b9eb..32789eb6 100644 --- a/src/drv/hasp_drv_911.h +++ b/src/drv/hasp_drv_gt911.h @@ -1,16 +1,16 @@ /* MIT License - Copyright (c) 2020 Francis Van Roie For full license information read the LICENSE file in the project folder */ +#ifndef HASP_DRV_911_H +#define HASP_DRV_911_H + #if TOUCH_DRIVER == 911 #include "hasp_debug.h" // for TAG_DRVR - #ifndef HASP_DRV_911_H - #define HASP_DRV_911_H - bool IRAM_ATTR GT911_getXY(uint16_t * touchX, uint16_t * touchY, bool debug); void GT911_init(); void IRAM_ATTR GT911_loop(); - #endif +#endif #endif \ No newline at end of file diff --git a/src/drv/hasp_drv_touch.cpp b/src/drv/hasp_drv_touch.cpp index e68ee3c2..9bb7bd64 100644 --- a/src/drv/hasp_drv_touch.cpp +++ b/src/drv/hasp_drv_touch.cpp @@ -1,4 +1,5 @@ #include "hasp_drv_touch.h" +#include "hasp/hasp.h" #include "lvgl.h" #if TOUCH_DRIVER == 2046 @@ -9,10 +10,14 @@ #elif TOUCH_DRIVER == 2046 #include "indev/XPT2046.h" +#elif TOUCH_DRIVER == 0x2046B + #include "hasp_drv_xpt2046.h" #elif TOUCH_DRIVER == 911 - #include "hasp_drv_911.h" + #include "hasp_drv_gt911.h" #elif TOUCH_DRIVER == 0xADC #include "hasp_drv_ft6336u.h" +#elif TOUCH_DRIVER == 5206 + #include "hasp_drv_ft5206.h" #elif TOUCH_DRIVER == 6336 #include "hasp_drv_ft6336u.h" #else @@ -39,6 +44,9 @@ void drv_touch_init(uint8_t rotation) #elif TOUCH_DRIVER == 0xADC // Analog Digital Touch Conroller // Touch_init(); +#elif TOUCH_DRIVER == 5206 + FT5206_init(); + #elif TOUCH_DRIVER == 6336 FT6336U_init(); @@ -49,10 +57,13 @@ void drv_touch_init(uint8_t rotation) #endif } -static inline bool drv_touchpad_getXY(uint16_t * touchX, uint16_t * touchY) +static inline bool drv_touchpad_getXY(int16_t * touchX, int16_t * touchY) { #if TOUCH_DRIVER == 2046 // XPT2046 Resistive touch panel driver - return tft_espi_get_touch(touchX, touchY, 300); + return tft_espi_get_touch(touchX, touchY, 300u); + +#elif TOUCH_DRIVER == 0x2046B + return XPT2046_getXY(touchX, touchY, true); #elif TOUCH_DRIVER == 911 return GT911_getXY(touchX, touchY, true); @@ -60,6 +71,9 @@ static inline bool drv_touchpad_getXY(uint16_t * touchX, uint16_t * touchY) #elif TOUCH_DRIVER == 0xADC // Analog Digital Touch Conroller return Touch_getXY(touchX, touchY, false); +#elif TOUCH_DRIVER == 5206 + return FT5206_getXY(touchX, touchY, true); + #elif TOUCH_DRIVER == 6336 return FT6336U_getXY(touchX, touchY, true); @@ -71,10 +85,10 @@ static inline bool drv_touchpad_getXY(uint16_t * touchX, uint16_t * touchY) #endif } -bool drv_touch_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) +bool IRAM_ATTR drv_touch_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) { -#ifdef TOUCH_CS - uint16_t touchX, touchY; +#if TOUCH_DRIVER > 0 + int16_t touchX, touchY; bool touched = drv_touchpad_getXY(&touchX, &touchY); if(touched && hasp_sleep_state != HASP_SLEEP_OFF) hasp_update_sleep_state(); // update Idle diff --git a/src/drv/hasp_drv_xpt2046.cpp b/src/drv/hasp_drv_xpt2046.cpp new file mode 100644 index 00000000..b873e737 --- /dev/null +++ b/src/drv/hasp_drv_xpt2046.cpp @@ -0,0 +1,223 @@ +#if TOUCH_DRIVER == 0x2046B + + #include "lvgl.h" + #include "hasp_drv_xpt2046.h" + + // #include + // #include LV_DRV_INDEV_INCLUDE + // #include LV_DRV_DELAY_INCLUDE + // #include + #include "XPT2046_Touchscreen.h" + + /********************* + * DEFINES + *********************/ + // #define CMD_X_READ 0b10010000 + // #define CMD_Y_READ 0b11010000 + #define SPI_SETTING SPISettings(2000000, MSBFIRST, SPI_MODE0) + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ +static void xpt2046_corr(int16_t * x, int16_t * y); +static void xpt2046_avg(int16_t * x, int16_t * y); + +/********************** + * STATIC VARIABLES + **********************/ +int16_t avg_buf_x[XPT2046_AVG]; +int16_t avg_buf_y[XPT2046_AVG]; +uint8_t avg_last; + #if defined(ARDUINO_ARCH_ESP32) +SPIClass xpt2046_spi(VSPI); +XPT2046_Touchscreen ts(TOUCH_CS); + + #elif defined(STM32F407ZG) +//#include "SoftSPI.h" +// SoftSPI xpt2046_spi(PF11, PB2, PB0); +XPT2046_Touchscreen ts(TOUCH_CS); + + #else +// SPIClass xpt2046_spi(PB15, PB14, PB13, PB12); +XPT2046_Touchscreen ts(TOUCH_CS); + #endif +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +/** + * Initialize the XPT2046 + */ +void XPT2046_init(uint8_t rotation) +{ + // ts.begin(); + // ts.setRotation(rotation); + pinMode(TOUCH_CS, OUTPUT); + digitalWrite(TOUCH_CS, HIGH); +} + +static int16_t last_x = 0; +static int16_t last_y = 0; + +bool XPT2046_getXY(int16_t * touchX, int16_t * touchY, bool debug) +{ + // bool touched = ts.touched(); + uint16_t tmp; + + int16_t data[6]; + + // if (!isrWake) return; + uint32_t now = millis(); + // if (now - msraw < MSEC_THRESHOLD) return; + + xpt2046_spi.beginTransaction(SPI_SETTING); + digitalWrite(TOUCH_CS, LOW); + + // Start YP sample request for x position, read 4 times and keep last sample + xpt2046_spi.transfer(0xd0); // Start new YP conversion + xpt2046_spi.transfer(0); // Read first 8 bits + xpt2046_spi.transfer(0xd0); // Read last 8 bits and start new YP conversion + xpt2046_spi.transfer(0); // Read first 8 bits + xpt2046_spi.transfer(0xd0); // Read last 8 bits and start new YP conversion + xpt2046_spi.transfer(0); // Read first 8 bits + xpt2046_spi.transfer(0xd0); // Read last 8 bits and start new YP conversion + + tmp = xpt2046_spi.transfer(0); // Read first 8 bits + tmp = tmp << 5; + tmp |= 0x1f & (xpt2046_spi.transfer(0x90) >> 3); // Read last 8 bits and start new XP conversion + + *touchX = tmp; + + // Start XP sample request for y position, read 4 times and keep last sample + xpt2046_spi.transfer(0); // Read first 8 bits + xpt2046_spi.transfer(0x90); // Read last 8 bits and start new XP conversion + xpt2046_spi.transfer(0); // Read first 8 bits + xpt2046_spi.transfer(0x90); // Read last 8 bits and start new XP conversion + xpt2046_spi.transfer(0); // Read first 8 bits + xpt2046_spi.transfer(0x90); // Read last 8 bits and start new XP conversion + + tmp = xpt2046_spi.transfer(0); // Read first 8 bits + tmp = tmp << 5; + tmp |= 0x1f & (xpt2046_spi.transfer(0) >> 3); // Read last 8 bits + + *touchY = tmp; + + digitalWrite(TOUCH_CS, HIGH); + xpt2046_spi.endTransaction(); + Serial.printf("z=%d :: z1=%d, z2=%d \n", *touchX, *touchY, *touchX); + + return false; +} + +/** + * Get the current position and state of the touchpad + * @param data store the read data here + * @return false: because no ore data to be read + */ +bool XPT2046_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) +{ + uint8_t buf; + + int16_t x = 0; + int16_t y = 0; + + // uint8_t irq = LV_DRV_INDEV_IRQ_READ; + data->state = ts.touched(); + + if(data->state) { + TS_Point p = ts.getPoint(); + x = p.x; + y = p.y; + xpt2046_corr(&x, &y); + + } else { + x = last_x; + y = last_y; + avg_last = 0; + data->state = LV_INDEV_STATE_REL; + } + + data->point.x = x; + data->point.y = y; + if(data->state) { + Serial.print(x); + Serial.print(" - "); + Serial.println(y); + } else { + // Serial.print("."); + } + + return false; +} + +/********************** + * STATIC FUNCTIONS + **********************/ +static void xpt2046_corr(int16_t * x, int16_t * y) +{ + #if XPT2046_XY_SWAP != 0 + int16_t swap_tmp; + swap_tmp = *x; + *x = *y; + *y = swap_tmp; + #endif + + if((*x) > XPT2046_X_MIN) + (*x) -= XPT2046_X_MIN; + else + (*x) = 0; + + if((*y) > XPT2046_Y_MIN) + (*y) -= XPT2046_Y_MIN; + else + (*y) = 0; + + (*x) = (uint32_t)((uint32_t)(*x) * XPT2046_HOR_RES) / (XPT2046_X_MAX - XPT2046_X_MIN); + + (*y) = (uint32_t)((uint32_t)(*y) * XPT2046_VER_RES) / (XPT2046_Y_MAX - XPT2046_Y_MIN); + + #if XPT2046_X_INV != 0 + (*x) = XPT2046_HOR_RES - (*x); + #endif + + #if XPT2046_Y_INV != 0 + (*y) = XPT2046_VER_RES - (*y); + #endif +} + +static void xpt2046_avg(int16_t * x, int16_t * y) +{ + /*Shift out the oldest data*/ + uint8_t i; + for(i = XPT2046_AVG - 1; i > 0; i--) { + avg_buf_x[i] = avg_buf_x[i - 1]; + avg_buf_y[i] = avg_buf_y[i - 1]; + } + + /*Insert the new point*/ + avg_buf_x[0] = *x; + avg_buf_y[0] = *y; + if(avg_last < XPT2046_AVG) avg_last++; + + /*Sum the x and y coordinates*/ + int32_t x_sum = 0; + int32_t y_sum = 0; + for(i = 0; i < avg_last; i++) { + x_sum += avg_buf_x[i]; + y_sum += avg_buf_y[i]; + } + + /*Normalize the sums*/ + (*x) = (int32_t)x_sum / avg_last; + (*y) = (int32_t)y_sum / avg_last; +} + +#endif \ No newline at end of file diff --git a/src/drv/hasp_drv_xpt2046.h b/src/drv/hasp_drv_xpt2046.h new file mode 100644 index 00000000..188d31f7 --- /dev/null +++ b/src/drv/hasp_drv_xpt2046.h @@ -0,0 +1,24 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#ifndef HASP_DRV_XPT2046B_H +#define HASP_DRV_XPT2046B_H + +#if TOUCH_DRIVER == 0x2046B + + #include "XPT2046_Touchscreen.h" + + #define XPT2046_HOR_RES TFT_WIDTH + #define XPT2046_VER_RES TFT_HEIGHT + #define XPT2046_X_MIN 200 + #define XPT2046_Y_MIN 200 + #define XPT2046_X_MAX 3800 + #define XPT2046_Y_MAX 3800 + #define XPT2046_AVG 4 + #define XPT2046_INV 0 + +bool XPT2046_getXY(int16_t * touchX, int16_t * touchY, bool debug); +void XPT2046_init(); + +#endif +#endif \ No newline at end of file diff --git a/src/net/hasp_network.cpp b/src/net/hasp_network.cpp index c24c7b2e..488b3c2a 100644 --- a/src/net/hasp_network.cpp +++ b/src/net/hasp_network.cpp @@ -7,6 +7,7 @@ #include "hasp_conf.h" #include "hasp_hal.h" #include "hasp_debug.h" +#include "hasp_network.h" #include "../hasp/hasp.h" #include "../svc/hasp_mdns.h" @@ -36,59 +37,60 @@ void networkStop(void) void networkSetup() { -#if HASP_USE_ETHERNET > 0 + #if HASP_USE_ETHERNET > 0 ethernetSetup(); -#endif + #endif -#if HASP_USE_WIFI > 0 + #if HASP_USE_WIFI > 0 wifiSetup(); -#endif + #endif } void IRAM_ATTR networkLoop(void) { -#if HASP_USE_ETHERNET > 0 + #if HASP_USE_ETHERNET > 0 ethernetLoop(); -#endif + #endif -#if HASP_USE_WIFI > 0 - // wifiLoop(); -#endif + #if HASP_USE_WIFI > 0 + // wifiLoop(); + #endif } bool networkEvery5Seconds(void) { -#if HASP_USE_ETHERNET > 0 + #if HASP_USE_ETHERNET > 0 return ethernetEvery5Seconds(); -#endif + #endif -#if HASP_USE_WIFI > 0 + #if HASP_USE_WIFI > 0 return wifiEvery5Seconds(); -#endif + #endif + return false; } bool networkEverySecond(void) { -#if HASP_USE_ETHERNET > 0 - // ethernetEverySecond(); -#endif + #if HASP_USE_ETHERNET > 0 + // return ethernetEverySecond(); + #endif -#if HASP_USE_WIFI > 0 - // return wifiEverySecond(); -#endif + #if HASP_USE_WIFI > 0 + // return wifiEverySecond(); + #endif return true; } void network_get_statusupdate(char * buffer, size_t len) { -#if HASP_USE_ETHERNET > 0 + #if HASP_USE_ETHERNET > 0 ethernet_get_statusupdate(buffer, len); -#endif + #endif -#if HASP_USE_WIFI > 0 + #if HASP_USE_WIFI > 0 wifi_get_statusupdate(buffer, len); -#endif + #endif } #endif \ No newline at end of file diff --git a/src/net/hasp_network.h b/src/net/hasp_network.h index 4cb5287f..2bd61526 100644 --- a/src/net/hasp_network.h +++ b/src/net/hasp_network.h @@ -7,8 +7,8 @@ /* ===== Default Event Processors ===== */ void networkSetup(); void IRAM_ATTR networkLoop(void); -void networkEvery5Seconds(void); -void networkEverySecond(void); +bool networkEvery5Seconds(void); +// bool networkEverySecond(void); void networkStart(void); void networkStop(void); diff --git a/user_setups/esp32/lanbon_l8.ini b/user_setups/esp32/lanbon_l8.ini index 384a7073..a183a836 100644 --- a/user_setups/esp32/lanbon_l8.ini +++ b/user_setups/esp32/lanbon_l8.ini @@ -28,11 +28,12 @@ build_flags = -D TFT_MOSI=23 ; FCP pin6 SDA -D TFT_MISO=25 ; FCP pin7 SDO -D TFT_BCKL=5 - -D TOUCH_DRIVER=6336 + -D TOUCH_DRIVER=5206 -D TOUCH_SDA=4 -D TOUCH_SCL=0 -D TOUCH_IRQ=-1 ; not connected -D TOUCH_RST=-1 ; not used, connected to 3.3V on FCP pin10 + -D TOUCH_FREQUENCY=400000 -D USE_TFT_ESPI=1 ;-D LED_RED=26 ;-D LED_GREEN=32 diff --git a/user_setups/lcd_config.ini b/user_setups/lcd_config.ini index b4301e93..10e5aad4 100644 --- a/user_setups/lcd_config.ini +++ b/user_setups/lcd_config.ini @@ -23,7 +23,8 @@ lolin24 = -D SPI_TOUCH_FREQUENCY=2500000 -D SPI_READ_FREQUENCY=20000000 -D USER_SETUP_LOADED=1 - -D TOUCH_DRIVER=2046 ; XPT2046 Resistive SPI touch panel driver + ;-D TOUCH_DRIVER=0x2046B ; Alternate + -D TOUCH_DRIVER=2046 ; XPT2046 Resistive SPI touch panel driver -D SUPPORT_TRANSACTIONS raspberrypi = @@ -63,9 +64,8 @@ st7789v = -D SPI_FREQUENCY=80000000 -D SPI_READ_FREQUENCY=6000000 -D USER_SETUP_LOADED=1 - -D TOUCH_DRIVER=6336 ; FT6336U Capacitive I2C touch panel driver + ;-D TOUCH_DRIVER=6336 ; FT6336U Capacitive I2C touch panel driver -D SUPPORT_TRANSACTIONS - -D I2C_TOUCH_FREQUENCY=400000 wireless-tag = -D ST7796_DRIVER=1