From a387e799e14926bf2bff931f551afdec7a019165 Mon Sep 17 00:00:00 2001 From: C47D Date: Mon, 7 Jun 2021 21:57:00 -0500 Subject: [PATCH 1/9] Print LV_HOR_RES_MAX and LV_VER_RES_MAX only when using LVGL v7 or below --- lvgl_helpers.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lvgl_helpers.c b/lvgl_helpers.c index 4125330..2a904b1 100644 --- a/lvgl_helpers.c +++ b/lvgl_helpers.c @@ -53,7 +53,12 @@ /* Interface and driver initialization */ void lvgl_driver_init(void) { + /* Since LVGL v8 LV_HOR_RES_MAX and LV_VER_RES_MAX are not defined, so + * print it only if they are defined. */ +#if (LVGL_VERSION_MAJOR < 8) ESP_LOGI(TAG, "Display hor size: %d, ver size: %d", LV_HOR_RES_MAX, LV_VER_RES_MAX); +#endif + ESP_LOGI(TAG, "Display buffer size: %d", DISP_BUF_SIZE); #if defined (CONFIG_LV_TFT_DISPLAY_CONTROLLER_FT81X) From 8b65d3547cee38e765a0580d4957c9dd9aa5b7c6 Mon Sep 17 00:00:00 2001 From: C47D Date: Mon, 7 Jun 2021 22:00:17 -0500 Subject: [PATCH 2/9] Fix path when using LVGL v8 --- lvgl_helpers.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lvgl_helpers.c b/lvgl_helpers.c index 2a904b1..5cc82c0 100644 --- a/lvgl_helpers.c +++ b/lvgl_helpers.c @@ -19,9 +19,17 @@ #include "driver/i2c.h" #ifdef LV_LVGL_H_INCLUDE_SIMPLE -#include "src/lv_core/lv_refr.h" +#if (LVGL_VERSION_MAJOR < 8) + #include "src/lv_core/lv_refr.h" #else -#include "lvgl/src/lv_core/lv_refr.h" + #include "src/core/lv_refr.h" +#endif +#else +#if (LVGL_VERSION_MAJOR < 8) + #include "lvgl/src/lv_core/lv_refr.h" +#else + #include "lvgl/src/core/lv_refr.h" +#endif #endif /********************* From 1af29975e95fb66b1bf7f32b0080f83a017e4657 Mon Sep 17 00:00:00 2001 From: C47D Date: Tue, 8 Jun 2021 10:26:03 -0500 Subject: [PATCH 3/9] Add support for ESP32C3 SPI hosts --- lvgl_helpers.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lvgl_helpers.c b/lvgl_helpers.c index 5cc82c0..41f0213 100644 --- a/lvgl_helpers.c +++ b/lvgl_helpers.c @@ -227,6 +227,13 @@ bool lvgl_spi_driver_init(int host, const char *spi_names[] = { "SPI_HOST", "", "" }; +#elif defined (CONFIG_IDF_TARGET_ESP32C3) + assert((SPI1_HOST <= host) && (SPI3_HOST >= host)); + const char *spi_names[] = { + "SPI1_HOST", "SPI2_HOST", "SPI3_HOST" + }; +#else +#error "Target chip not selected" #endif ESP_LOGI(TAG, "Configuring SPI host %s (%d)", spi_names[host], host); From 696b6ff46328e66783cbf4f120b2d9a53a43dcd6 Mon Sep 17 00:00:00 2001 From: C47D Date: Thu, 10 Jun 2021 22:22:44 -0500 Subject: [PATCH 4/9] lvgl_helpers: Set spi dma channel to SPI_DMA_CH_AUTO when using ESP32C3 --- lvgl_helpers.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lvgl_helpers.c b/lvgl_helpers.c index 41f0213..56c9183 100644 --- a/lvgl_helpers.c +++ b/lvgl_helpers.c @@ -217,21 +217,29 @@ bool lvgl_spi_driver_init(int host, int dma_channel, int quadwp_pin, int quadhd_pin) { + spi_dma_chan_t dma_chan = SPI_DMA_DISABLED; + #if defined (CONFIG_IDF_TARGET_ESP32) assert((SPI_HOST <= host) && (VSPI_HOST >= host)); const char *spi_names[] = { "SPI_HOST", "HSPI_HOST", "VSPI_HOST" }; + + dma_chan = dma_channel; #elif defined (CONFIG_IDF_TARGET_ESP32S2) assert((SPI_HOST <= host) && (HSPI_HOST >= host)); const char *spi_names[] = { "SPI_HOST", "", "" }; + + dma_chan = dma_channel; #elif defined (CONFIG_IDF_TARGET_ESP32C3) assert((SPI1_HOST <= host) && (SPI3_HOST >= host)); const char *spi_names[] = { "SPI1_HOST", "SPI2_HOST", "SPI3_HOST" }; + + dma_chan = SPI_DMA_CH_AUTO; #else #error "Target chip not selected" #endif @@ -252,7 +260,7 @@ bool lvgl_spi_driver_init(int host, }; ESP_LOGI(TAG, "Initializing SPI bus..."); - esp_err_t ret = spi_bus_initialize(host, &buscfg, dma_channel); + esp_err_t ret = spi_bus_initialize(host, &buscfg, dma_chan); assert(ret == ESP_OK); return ESP_OK != ret; From d09fd59869ef0c33c0fa4a960c45374035e88000 Mon Sep 17 00:00:00 2001 From: C47D Date: Thu, 10 Jun 2021 22:31:09 -0500 Subject: [PATCH 5/9] Add ESP32C3 support on Kconfig files --- lvgl_tft/Kconfig | 15 ++++++++++++++- lvgl_touch/Kconfig | 11 +++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lvgl_tft/Kconfig b/lvgl_tft/Kconfig index b622146..7059a6c 100644 --- a/lvgl_tft/Kconfig +++ b/lvgl_tft/Kconfig @@ -772,6 +772,7 @@ menu "LVGL TFT Display controller" int "GPIO for MOSI (Master Out Slave In)" if LV_TFT_DISPLAY_PROTOCOL_SPI range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 23 if LV_PREDEFINED_DISPLAY_WROVER4 default 23 if LV_PREDEFINED_DISPLAY_ATAG @@ -801,6 +802,7 @@ menu "LVGL TFT Display controller" depends on LV_DISPLAY_USE_SPI_MISO range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 19 if LV_PREDEFINED_PINS_TKOALA default 38 if LV_PREDEFINED_DISPLAY_M5CORE2 @@ -823,6 +825,7 @@ menu "LVGL TFT Display controller" depends on LV_TFT_DISPLAY_SPI_TRANS_MODE_QIO range -1 39 if IDF_TARGET_ESP32 range -1 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 22 if LV_PREDEFINED_PINS_TKOALA && LV_TFT_DISPLAY_SPI_TRANS_MODE_QIO default -1 @@ -834,6 +837,7 @@ menu "LVGL TFT Display controller" depends on LV_TFT_DISPLAY_SPI_TRANS_MODE_QIO range -1 39 if IDF_TARGET_ESP32 range -1 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 21 if LV_PREDEFINED_PINS_TKOALA && LV_TFT_DISPLAY_SPI_TRANS_MODE_QIO default -1 @@ -844,6 +848,7 @@ menu "LVGL TFT Display controller" int "GPIO for CLK (SCK / Serial Clock)" if LV_TFT_DISPLAY_PROTOCOL_SPI range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 18 if LV_PREDEFINED_DISPLAY_M5STACK || LV_PREDEFINED_DISPLAY_M5STICK default 18 if LV_PREDEFINED_DISPLAY_M5CORE2 @@ -872,6 +877,7 @@ menu "LVGL TFT Display controller" depends on LV_DISPLAY_USE_SPI_CS range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 5 if LV_PREDEFINED_PINS_38V1 default 14 if LV_PREDEFINED_DISPLAY_M5STACK || LV_PREDEFINED_DISPLAY_M5STICK @@ -900,6 +906,7 @@ menu "LVGL TFT Display controller" int "GPIO for DC (Data / Command)" if LV_TFT_DISPLAY_PROTOCOL_SPI range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 depends on LV_DISPLAY_USE_DC default 19 if LV_PREDEFINED_PINS_38V1 @@ -937,6 +944,7 @@ menu "LVGL TFT Display controller" depends on LV_DISP_USE_RST range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 18 if LV_PREDEFINED_PINS_38V1 default 25 if LV_PREDEFINED_PINS_38V4 @@ -958,9 +966,11 @@ menu "LVGL TFT Display controller" int "GPIO for Busy" if LV_TFT_DISPLAY_CONTROLLER_IL3820 || LV_TFT_DISPLAY_CONTROLLER_JD79653A || LV_TFT_DISPLAY_CONTROLLER_UC8151D range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 35 if LV_TFT_DISPLAY_CONTROLLER_IL3820 || LV_TFT_DISPLAY_CONTROLLER_JD79653A || LV_TFT_DISPLAY_CONTROLLER_UC8151D - default 35 + default 35 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S2 + default 21 if IDF_TARGET_ESP32C3 help Configure the display Busy pin here. @@ -997,6 +1007,7 @@ menu "LVGL TFT Display controller" depends on LV_ENABLE_BACKLIGHT_CONTROL range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 23 if LV_PREDEFINED_PINS_38V1 default 26 if LV_PREDEFINED_PINS_38V4 @@ -1017,6 +1028,7 @@ menu "LVGL TFT Display controller" int "GPIO for I2C SDA" if LV_TFT_DISPLAY_PROTOCOL_I2C range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 5 if LV_PREDEFINED_DISPLAY_WEMOS_LOLIN default 5 @@ -1028,6 +1040,7 @@ menu "LVGL TFT Display controller" int "GPIO for I2C SCL" if LV_TFT_DISPLAY_PROTOCOL_I2C range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 4 if LV_PREDEFINED_DISPLAY_WEMOS_LOLIN default 4 diff --git a/lvgl_touch/Kconfig b/lvgl_touch/Kconfig index edd1dab..7f6e995 100644 --- a/lvgl_touch/Kconfig +++ b/lvgl_touch/Kconfig @@ -98,6 +98,7 @@ menu "LVGL Touch controller" prompt "GPIO for MISO (Master In Slave Out)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 35 if LV_PREDEFINED_PINS_38V1 default 19 @@ -109,6 +110,7 @@ menu "LVGL Touch controller" prompt "GPIO for MOSI (Master Out Slave In)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 32 if LV_PREDEFINED_PINS_38V1 default 23 @@ -119,6 +121,7 @@ menu "LVGL Touch controller" int "GPIO for CLK (SCK / Serial Clock)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 26 if LV_PREDEFINED_PINS_38V1 default 18 @@ -139,6 +142,7 @@ menu "LVGL Touch controller" int "GPIO for IRQ (Interrupt Request)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 27 if LV_PREDEFINED_PINS_38V4 default 25 @@ -211,6 +215,7 @@ menu "LVGL Touch controller" prompt "GPIO for SDA (I2C)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 21 help @@ -220,6 +225,7 @@ menu "LVGL Touch controller" int "GPIO for clock signal SCL (I2C)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 22 help @@ -254,6 +260,7 @@ menu "LVGL Touch controller" prompt "GPIO for MISO (Master In Slave Out)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 35 if LV_PREDEFINED_PINS_38V1 default 19 if LV_PREDEFINED_DISPLAY_ADA_FEATHERWING @@ -263,10 +270,12 @@ menu "LVGL Touch controller" Configure the touchpanel MISO pin here. config LV_TOUCH_SPI_MOSI + # TODO Fix default for ESP32C3 int prompt "GPIO for MOSI (Master Out Slave In)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 32 if LV_PREDEFINED_PINS_38V1 default 18 if LV_PREDEFINED_DISPLAY_ADA_FEATHERWING @@ -279,6 +288,7 @@ menu "LVGL Touch controller" int "GPIO for CLK (SCK / Serial Clock)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 26 if LV_PREDEFINED_PINS_38V1 default 5 if LV_PREDEFINED_DISPLAY_ADA_FEATHERWING @@ -290,6 +300,7 @@ menu "LVGL Touch controller" int "GPIO for CS (Slave Select)" range 0 39 if IDF_TARGET_ESP32 range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 default 33 if LV_PREDEFINED_PINS_38V1 default 32 if LV_PREDEFINED_DISPLAY_ADA_FEATHERWING From d379a4e8514d041dbeb1c146f2ae0a9a8142c367 Mon Sep 17 00:00:00 2001 From: C47D Date: Thu, 10 Jun 2021 22:36:01 -0500 Subject: [PATCH 6/9] Handle ESP32C3 when using ESP32C3 --- lvgl_spi_conf.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lvgl_spi_conf.h b/lvgl_spi_conf.h index b5e7901..240090d 100644 --- a/lvgl_spi_conf.h +++ b/lvgl_spi_conf.h @@ -65,11 +65,15 @@ extern "C" { #define ENABLE_TOUCH_INPUT CONFIG_LV_ENABLE_TOUCH #if defined (CONFIG_LV_TFT_DISPLAY_SPI_HSPI) -#define TFT_SPI_HOST HSPI_HOST +#if defined (CONFIG_IDF_TARGET_ESP32C3) +#define TFT_SPI_HOST SPI2_HOST +#else +#define TFT_SPI_HOST HSPI_HOST +#endif #elif defined (CONFIG_LV_TFT_DISPLAY_SPI_VSPI) -#define TFT_SPI_HOST VSPI_HOST +#define TFT_SPI_HOST VSPI_HOST #elif defined (CONFIG_LV_TFT_DISPLAY_SPI_FSPI) -#define TFT_SPI_HOST FSPI_HOST +#define TFT_SPI_HOST FSPI_HOST #endif #if defined (CONFIG_LV_TFT_DISPLAY_SPI_HALF_DUPLEX) From f726311525474489a409f4cdb1a6a7ae315f2c05 Mon Sep 17 00:00:00 2001 From: C47D Date: Thu, 10 Jun 2021 22:49:56 -0500 Subject: [PATCH 7/9] Indev: Add support for GT911 touch driver by @dastarling --- CMakeLists.txt | 2 ++ lvgl_touch/Kconfig | 49 +++++++++++++++++++++++++++++++++++++++ lvgl_touch/touch_driver.c | 4 ++++ lvgl_touch/touch_driver.h | 2 ++ 4 files changed, 57 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5832233..10262c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,8 @@ if(CONFIG_LV_TOUCH_CONTROLLER) list(APPEND SOURCES "lvgl_touch/FT81x.c") elseif(CONFIG_LV_TOUCH_CONTROLLER_RA8875) list(APPEND SOURCES "lvgl_touch/ra8875_touch.c") + elseif(CONFIG_LV_TOUCH_CONTROLLER_GT911) + list(APPEND SOURCES "lvgl_touch/gt911.c") endif() if(CONFIG_LV_TOUCH_DRIVER_PROTOCOL_SPI) diff --git a/lvgl_touch/Kconfig b/lvgl_touch/Kconfig index 7f6e995..9942e0a 100644 --- a/lvgl_touch/Kconfig +++ b/lvgl_touch/Kconfig @@ -9,6 +9,7 @@ menu "LVGL Touch controller" default 4 if LV_TOUCH_CONTROLLER_ADCRAW default 5 if LV_TOUCH_CONTROLLER_FT81X default 6 if LV_TOUCH_CONTROLLER_RA8875 + default 7 if LV_TOUCH_CONTROLLER_GT911 choice prompt "Select a touch panel controller model." @@ -36,6 +37,9 @@ menu "LVGL Touch controller" config LV_TOUCH_CONTROLLER_RA8875 select LV_TOUCH_DRIVER_DISPLAY bool "RA8875" + config LV_TOUCH_CONTROLLER_GT911 + select LV_TOUCH_DRIVER_PROTOCOL_I2C + bool "GT911" endchoice config LV_TOUCH_DRIVER_PROTOCOL_SPI @@ -498,5 +502,50 @@ menu "LVGL Touch controller" default y endmenu + + menu "Touchpanel (GT911) Pin Assignments" + depends on LV_TOUCH_CONTROLLER_GT911 + + config LV_TOUCH_I2C_SDA + int + prompt "GPIO for SDA (I2C)" + range 0 39 if IDF_TARGET_ESP32 + range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 + + default 2 + help + Configure the I2C touchpanel SDA pin here. + + config LV_TOUCH_I2C_SCL + int "GPIO for clock signal SCL (I2C)" + range 0 39 if IDF_TARGET_ESP32 + range 0 43 if IDF_TARGET_ESP32S2 + range 0 21 if IDF_TARGET_ESP32C3 + + default 3 + help + Configure the I2C touchpanel SCL pin here. + endmenu + + menu "Touchpanel Configuration (GT911)" + depends on LV_TOUCH_CONTROLLER_GT911 + + config LV_GT911_SWAPXY + bool + prompt "Swap X with Y coordinate." + default y + + config LV_GT911_INVERT_X + bool + prompt "Invert X coordinate value." + default n + + config LV_GT911_INVERT_Y + bool + prompt "Invert Y coordinate value." + default y + + endmenu endmenu diff --git a/lvgl_touch/touch_driver.c b/lvgl_touch/touch_driver.c index b0aed88..6a7afb3 100644 --- a/lvgl_touch/touch_driver.c +++ b/lvgl_touch/touch_driver.c @@ -21,6 +21,8 @@ void touch_driver_init(void) /* nothing to do */ #elif defined (CONFIG_LV_TOUCH_CONTROLLER_RA8875) ra8875_touch_init(); +#elif defined (CONFIG_LV_TOUCH_CONTROLLER_GT911) + gt911_init(GT911_I2C_SLAVE_ADDR); #endif } @@ -40,6 +42,8 @@ bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data) res = FT81x_read(drv, data); #elif defined (CONFIG_LV_TOUCH_CONTROLLER_RA8875) res = ra8875_touch_read(drv, data); +#elif defined (CONFIG_LV_TOUCH_CONTROLLER_GT911) + res = gt911_read(drv, data); #endif return res; diff --git a/lvgl_touch/touch_driver.h b/lvgl_touch/touch_driver.h index bc92f4f..44378db 100644 --- a/lvgl_touch/touch_driver.h +++ b/lvgl_touch/touch_driver.h @@ -32,6 +32,8 @@ extern "C" { #include "FT81x.h" #elif defined (CONFIG_LV_TOUCH_CONTROLLER_RA8875) #include "ra8875_touch.h" +#elif defined (CONFIG_LV_TOUCH_CONTROLLER_GT911) +#include "gt911.h" #endif /********************* From 0d32432f4c05a6294f03532e7d7d94c10c436f81 Mon Sep 17 00:00:00 2001 From: C47D Date: Sun, 13 Jun 2021 15:06:22 -0500 Subject: [PATCH 8/9] touch driver: Add LVGL v8 compatibility --- lvgl_touch/gt911.c | 168 ++++++++++++++++++++++++++++++++++++++ lvgl_touch/gt911.h | 94 +++++++++++++++++++++ lvgl_touch/touch_driver.c | 8 ++ lvgl_touch/touch_driver.h | 5 ++ 4 files changed, 275 insertions(+) create mode 100644 lvgl_touch/gt911.c create mode 100644 lvgl_touch/gt911.h diff --git a/lvgl_touch/gt911.c b/lvgl_touch/gt911.c new file mode 100644 index 0000000..e6cb972 --- /dev/null +++ b/lvgl_touch/gt911.c @@ -0,0 +1,168 @@ +/* +* Copyright © 2021 Sturnus Inc. + +* 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 +#include +#ifdef LV_LVGL_H_INCLUDE_SIMPLE +#include +#else +#include +#endif +#include "gt911.h" +#include "tp_i2c.h" +#include "../lvgl_i2c_conf.h" + +#define TAG "GT911" + +gt911_status_t gt911_status; + +//TODO: handle multibyte read and refactor to just one read transaction +esp_err_t gt911_i2c_read(uint8_t slave_addr, uint16_t register_addr, uint8_t *data_buf, uint8_t len) { + i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create(); + + i2c_master_start(i2c_cmd); + i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true); + i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK); + i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK); + + i2c_master_start(i2c_cmd); + i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_READ, true); + + i2c_master_read_byte(i2c_cmd, data_buf, I2C_MASTER_NACK); + i2c_master_stop(i2c_cmd); + esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS); + i2c_cmd_link_delete(i2c_cmd); + return ret; +} + +esp_err_t gt911_i2c_write8(uint8_t slave_addr, uint16_t register_addr, uint8_t data) { + i2c_cmd_handle_t i2c_cmd = i2c_cmd_link_create(); + + i2c_master_start(i2c_cmd); + i2c_master_write_byte(i2c_cmd, (slave_addr << 1) | I2C_MASTER_WRITE, true); + i2c_master_write_byte(i2c_cmd, (register_addr >> 8), I2C_MASTER_ACK); + i2c_master_write_byte(i2c_cmd, (register_addr & 0xFF), I2C_MASTER_ACK); + i2c_master_write_byte(i2c_cmd, data, I2C_MASTER_ACK); + i2c_master_stop(i2c_cmd); + esp_err_t ret = i2c_master_cmd_begin(TOUCH_I2C_PORT, i2c_cmd, 1000 / portTICK_RATE_MS); + i2c_cmd_link_delete(i2c_cmd); + return ret; +} + +/** + * @brief Initialize for GT911 communication via I2C + * @param dev_addr: Device address on communication Bus (I2C slave address of GT911). + * @retval None + */ +void gt911_init(uint8_t dev_addr) { + if (!gt911_status.inited) { + gt911_status.i2c_dev_addr = dev_addr; + uint8_t data_buf; + esp_err_t ret; + + ESP_LOGI(TAG, "Checking for GT911 Touch Controller"); + if ((ret = gt911_i2c_read(dev_addr, GT911_PRODUCT_ID1, &data_buf, 1) != ESP_OK)) { + ESP_LOGE(TAG, "Error reading from device: %s", + esp_err_to_name(ret)); // Only show error the first time + return; + } + + // Read 4 bytes for Product ID in ASCII + for (int i = 0; i < GT911_PRODUCT_ID_LEN; i++) { + gt911_i2c_read(dev_addr, (GT911_PRODUCT_ID1 + i), (uint8_t *)&(gt911_status.product_id[i]), 1); + } + ESP_LOGI(TAG, "\tProduct ID: %s", gt911_status.product_id); + + gt911_i2c_read(dev_addr, GT911_VENDOR_ID, &data_buf, 1); + ESP_LOGI(TAG, "\tVendor ID: 0x%02x", data_buf); + + gt911_i2c_read(dev_addr, GT911_X_COORD_RES_L, &data_buf, 1); + gt911_status.max_x_coord = data_buf; + gt911_i2c_read(dev_addr, GT911_X_COORD_RES_H, &data_buf, 1); + gt911_status.max_x_coord |= ((uint16_t)data_buf << 8); + ESP_LOGI(TAG, "\tX Resolution: %d", gt911_status.max_x_coord); + + gt911_i2c_read(dev_addr, GT911_Y_COORD_RES_L, &data_buf, 1); + gt911_status.max_y_coord = data_buf; + gt911_i2c_read(dev_addr, GT911_Y_COORD_RES_H, &data_buf, 1); + gt911_status.max_y_coord |= ((uint16_t)data_buf << 8); + ESP_LOGI(TAG, "\tY Resolution: %d", gt911_status.max_y_coord); + gt911_status.inited = true; + } +} + +/** + * @brief Get the touch screen X and Y positions values. Ignores multi touch + * @param drv: + * @param data: Store data here + * @retval Always false + */ +bool gt911_read(lv_indev_drv_t *drv, lv_indev_data_t *data) { + uint8_t touch_pnt_cnt; // Number of detected touch points + static int16_t last_x = 0; // 12bit pixel value + static int16_t last_y = 0; // 12bit pixel value + uint8_t data_buf; + uint8_t status_reg; + + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_STATUS_REG, &status_reg, 1); +// ESP_LOGI(TAG, "\tstatus: 0x%02x", status_reg); + touch_pnt_cnt = status_reg & 0x0F; + if ((status_reg & 0x80) || (touch_pnt_cnt < 6)) { + //Reset Status Reg Value + gt911_i2c_write8(gt911_status.i2c_dev_addr, GT911_STATUS_REG, 0x00); + } + if (touch_pnt_cnt != 1) { // ignore no touch & multi touch + data->point.x = last_x; + data->point.y = last_y; + data->state = LV_INDEV_STATE_REL; + return false; + } + +// gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_TRACK_ID1, &data_buf, 1); +// ESP_LOGI(TAG, "\ttrack_id: %d", data_buf); + + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_X_COORD_L, &data_buf, 1); + last_x = data_buf; + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_X_COORD_H, &data_buf, 1); + last_x |= ((uint16_t)data_buf << 8); + + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_Y_COORD_L, &data_buf, 1); + last_y = data_buf; + gt911_i2c_read(gt911_status.i2c_dev_addr, GT911_PT1_Y_COORD_H, &data_buf, 1); + last_y |= ((uint16_t)data_buf << 8); + +#if CONFIG_LV_GT911_INVERT_X + last_x = gt911_status.max_x_coord - last_x; +#endif +#if CONFIG_LV_GT911_INVERT_Y + last_y = gt911_status.max_y_coord - last_y; +#endif +#if CONFIG_LV_GT911_SWAPXY + int16_t swap_buf = last_x; + last_x = last_y; + last_y = swap_buf; +#endif + data->point.x = last_x; + data->point.y = last_y; + data->state = LV_INDEV_STATE_PR; + ESP_LOGI(TAG, "X=%u Y=%u", data->point.x, data->point.y); + ESP_LOGV(TAG, "X=%u Y=%u", data->point.x, data->point.y); + return false; +} diff --git a/lvgl_touch/gt911.h b/lvgl_touch/gt911.h new file mode 100644 index 0000000..6a110bd --- /dev/null +++ b/lvgl_touch/gt911.h @@ -0,0 +1,94 @@ +#ifndef __GT911_H +/* +* Copyright © 2021 Sturnus Inc. + +* 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. +*/ + +#define __GT911_H + +#include +#include +#ifdef LV_LVGL_H_INCLUDE_SIMPLE +#include "lvgl.h" +#else +#include "lvgl/lvgl.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#define GT911_I2C_SLAVE_ADDR 0x5D + +#define GT911_PRODUCT_ID_LEN 4 + +/* Register Map of GT911 */ +#define GT911_PRODUCT_ID1 0x8140 +#define GT911_PRODUCT_ID2 0x8141 +#define GT911_PRODUCT_ID3 0x8142 +#define GT911_PRODUCT_ID4 0x8143 +#define GT911_FIRMWARE_VER_L 0x8144 +#define GT911_FIRMWARE_VER_H 0x8145 +#define GT911_X_COORD_RES_L 0x8146 +#define GT911_X_COORD_RES_H 0x8147 +#define GT911_Y_COORD_RES_L 0x8148 +#define GT911_Y_COORD_RES_H 0x8149 +#define GT911_VENDOR_ID 0x814A + +#define GT911_STATUS_REG 0x814E +#define GT911_STATUS_REG_BUF 0x80 +#define GT911_STATUS_REG_LARGE 0x40 +#define GT911_STATUS_REG_PROX_VALID 0x20 +#define GT911_STATUS_REG_HAVEKEY 0x10 +#define GT911_STATUS_REG_PT_MASK 0x0F + +#define GT911_TRACK_ID1 0x814F +#define GT911_PT1_X_COORD_L 0x8150 +#define GT911_PT1_X_COORD_H 0x8151 +#define GT911_PT1_Y_COORD_L 0x8152 +#define GT911_PT1_Y_COORD_H 0x8153 +#define GT911_PT1_X_SIZE_L 0x8154 +#define GT911_PT1_X_SIZE_H 0x8155 + +typedef struct { + bool inited; + char product_id[GT911_PRODUCT_ID_LEN]; + uint16_t max_x_coord; + uint16_t max_y_coord; + uint8_t i2c_dev_addr; +} gt911_status_t; + +/** + * @brief Initialize for GT911 communication via I2C + * @param dev_addr: Device address on communication Bus (I2C slave address of GT911). + * @retval None + */ +void gt911_init(uint8_t dev_addr); + +/** + * @brief Get the touch screen X and Y positions values. Ignores multi touch + * @param drv: + * @param data: Store data here + * @retval Always false + */ +bool gt911_read(lv_indev_drv_t *drv, lv_indev_data_t *data); + +#ifdef __cplusplus +} +#endif +#endif /* __GT911_H */ diff --git a/lvgl_touch/touch_driver.c b/lvgl_touch/touch_driver.c index 6a7afb3..1212543 100644 --- a/lvgl_touch/touch_driver.c +++ b/lvgl_touch/touch_driver.c @@ -26,7 +26,11 @@ void touch_driver_init(void) #endif } +#if LVGL_VERSION_MAJOR >= 8 +void touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data) +#else bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data) +#endif { bool res = false; @@ -46,6 +50,10 @@ bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data) res = gt911_read(drv, data); #endif +#if LVGL_VERSION_MAJOR >= 8 + data->continue_reading = res; +#else return res; +#endif } diff --git a/lvgl_touch/touch_driver.h b/lvgl_touch/touch_driver.h index 44378db..0d014e2 100644 --- a/lvgl_touch/touch_driver.h +++ b/lvgl_touch/touch_driver.h @@ -44,7 +44,12 @@ extern "C" { * GLOBAL PROTOTYPES **********************/ void touch_driver_init(void); + +#if LVGL_VERSION_MAJOR >= 8 +void touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data); +#else bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data); +#endif #ifdef __cplusplus } /* extern "C" */ From b9cccc342fa509c0dedec75e863d572baaa17b25 Mon Sep 17 00:00:00 2001 From: C47D Date: Tue, 15 Jun 2021 21:01:36 -0500 Subject: [PATCH 9/9] Include lv_refr more easily --- lvgl_helpers.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/lvgl_helpers.c b/lvgl_helpers.c index 56c9183..a215c43 100644 --- a/lvgl_helpers.c +++ b/lvgl_helpers.c @@ -19,17 +19,9 @@ #include "driver/i2c.h" #ifdef LV_LVGL_H_INCLUDE_SIMPLE -#if (LVGL_VERSION_MAJOR < 8) - #include "src/lv_core/lv_refr.h" +#include "lvgl.h" #else - #include "src/core/lv_refr.h" -#endif -#else -#if (LVGL_VERSION_MAJOR < 8) - #include "lvgl/src/lv_core/lv_refr.h" -#else - #include "lvgl/src/core/lv_refr.h" -#endif +#include "lvgl/lvgl.h" #endif /*********************