From ed2860239f80eb8ac0ba0fb8aeb00820f3589269 Mon Sep 17 00:00:00 2001 From: Bassam Date: Sun, 7 Aug 2022 00:31:06 +0300 Subject: [PATCH] added the implementation of CST816 touch sensor --- CMakeLists.txt | 4 +- lvgl_helpers.h | 6 ++ lvgl_touch/CST816.c | 132 ++++++++++++++++++++++++++++++++++++++ lvgl_touch/CST816.h | 64 ++++++++++++++++++ lvgl_touch/Kconfig | 36 +++++++++++ lvgl_touch/touch_driver.c | 4 ++ lvgl_touch/touch_driver.h | 2 + 7 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 lvgl_touch/CST816.c create mode 100644 lvgl_touch/CST816.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 407802a..724553f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,8 @@ if(CONFIG_LV_TOUCH_CONTROLLER) list(APPEND SOURCES "lvgl_touch/ra8875_touch.c") elseif(CONFIG_LV_TOUCH_CONTROLLER_GT911) list(APPEND SOURCES "lvgl_touch/gt911.c") + elseif(CONFIG_LV_TOUCH_CONTROLLER_CST816) + list(APPEND SOURCES "lvgl_touch/CST816.c") endif() if(CONFIG_LV_TOUCH_DRIVER_PROTOCOL_SPI) @@ -86,7 +88,7 @@ endif() idf_component_register(SRCS ${SOURCES} INCLUDE_DIRS ${LVGL_INCLUDE_DIRS} - REQUIRES lvgl) + REQUIRES lvgl band_audio_hal) target_compile_definitions(${COMPONENT_LIB} PUBLIC "-DLV_LVGL_H_INCLUDE_SIMPLE") diff --git a/lvgl_helpers.h b/lvgl_helpers.h index c010174..99e35f8 100644 --- a/lvgl_helpers.h +++ b/lvgl_helpers.h @@ -34,6 +34,12 @@ extern "C" { * color format being used, for RGB565 each pixel needs 2 bytes. * When using the mono theme, the display pixels can be represented in one bit, * so the buffer size can be divided by 8, e.g. see SSD1306 display size. */ + +//[BJ] Source: https://github.com/lvgl/lvgl_esp32_drivers/issues/202 +# define LV_HOR_RES_MAX 320 +# define LV_VER_RES_MAX 240 +# define SPI_HOST_MAX 3 + #if defined (CONFIG_CUSTOM_DISPLAY_BUFFER_SIZE) #define DISP_BUF_SIZE CONFIG_CUSTOM_DISPLAY_BUFFER_BYTES #else diff --git a/lvgl_touch/CST816.c b/lvgl_touch/CST816.c new file mode 100644 index 0000000..dd4fc7f --- /dev/null +++ b/lvgl_touch/CST816.c @@ -0,0 +1,132 @@ +/* +* Copyright © 2022 Band Industries 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 +#ifdef LV_LVGL_H_INCLUDE_SIMPLE +#include +#else +#include +#endif +#include "CST816.h" + +#include "lvgl_i2c/i2c_manager.h" +#include "band_es8388.h" + +#define TAG "CST816" + +cst816_status_t cst816_status; + +//[BJ] TODO: Below functions are ADF specific, if I am to contribute to main repo this needs to be resolved! +static esp_err_t _cst816_i2c_read(uint8_t slave_addr, uint8_t register_addr, uint8_t *data_buf, uint8_t len) { + return band_i2c_read(slave_addr, register_addr, data_buf, len); + //return lvgl_i2c_read(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr | I2C_REG_16, data_buf, len); +} + +static esp_err_t _cst816_i2c_write8(uint8_t slave_addr, uint8_t register_addr, uint8_t data) { + //uint8_t buffer = data; + return band_i2c_write(slave_addr, register_addr, &data, 1); + //return lvgl_i2c_write(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr | I2C_REG_16, &buffer, 1); +} + +/** + * @brief Initialize for cst816 communication via I2C + * @param dev_addr: Device address on communication Bus (I2C slave address of cst816). + * @retval None + */ +void cst816_init(uint8_t dev_addr) { + if (!cst816_status.inited) + { + cst816_status.i2c_dev_addr = dev_addr; + uint8_t version; + uint8_t versionInfo[3]; + esp_err_t ret; + ESP_LOGI(TAG, "Initializing cst816 %d", dev_addr); + if ((ret = _cst816_i2c_read(dev_addr, 0x15, &version, 1) != ESP_OK)) + { + ESP_LOGE(TAG, "Error reading version from device: %s", + esp_err_to_name(ret)); // Only show error the first time + return; + } + if ((ret = _cst816_i2c_read(dev_addr, 0xA7, versionInfo, 3) != ESP_OK)) + { + ESP_LOGE(TAG, "Error reading versionInfo from device: %s", + esp_err_to_name(ret)); // Only show error the first time + return; + } + ESP_LOGI(TAG, "CST816 version %d, versionInfo: %d.%d.%d", version, versionInfo[0], versionInfo[1], versionInfo[2]); + cst816_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 + */ +#define SWAPXY 1 +#define INVERTX 1 +#define INVERTY 0 + + + +bool cst816_read(lv_indev_drv_t *drv, lv_indev_data_t *data) { + + uint8_t data_raw[8]; + _cst816_i2c_read(cst816_status.i2c_dev_addr, 0x01, data_raw, 6); + + + + uint8_t gestureID = data_raw[0]; + uint8_t points = data_raw[1]; + uint8_t event = data_raw[2] >> 6; + uint16_t point_x = 0; + uint16_t point_y = 0; + if(points == 0) + { + data->state = LV_INDEV_STATE_RELEASED; + return false; + + } + + data->state = LV_INDEV_STATE_PRESSED; + point_x = (data_raw[3] | ((data_raw[2] & 0x0F) << 8)); + point_y = (data_raw[5] | ((data_raw[4] & 0x0F) << 8)); + +#if CONFIG_LV_SWAPXY_CST816 + int temp; + temp = point_y; + point_y = point_x; + point_x = temp; +#endif + +#if CONFIG_LV_INVERT_X_CST816 + point_x = CONFIG_LV_TOUCH_X_MAX_CST816 - point_x; +#endif + +#if CONFIG_LV_INVERT_Y_CST816 + point_y = CONFIG_LV_TOUCH_Y_MAX_CST816 - point_y; +#endif + + data->point.x = point_x; + data->point.y = point_y; + ESP_LOGI(TAG, "gestureID %d, points %d, event %d X=%u Y=%u", gestureID, points, event, data->point.x, data->point.y); + return false; +} diff --git a/lvgl_touch/CST816.h b/lvgl_touch/CST816.h new file mode 100644 index 0000000..c8085b5 --- /dev/null +++ b/lvgl_touch/CST816.h @@ -0,0 +1,64 @@ +#ifndef __CST816_H +/* +* Copyright © 2022 Band Industries 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 __CST816_H + +#include +#include +#ifdef LV_LVGL_H_INCLUDE_SIMPLE +#include "lvgl.h" +#else +#include "lvgl/lvgl.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#define CST816_I2C_SLAVE_ADDR 0x15<<1 + +#define CST816_PRODUCT_ID_LEN 4 + + +typedef struct { + bool inited; + uint8_t i2c_dev_addr; +} cst816_status_t; + +/** + * @brief Initialize for cst816 communication via I2C + * @param dev_addr: Device address on communication Bus (I2C slave address of CST816). + * @retval None + */ +void cst816_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 cst816_read(lv_indev_drv_t *drv, lv_indev_data_t *data); + +#ifdef __cplusplus +} +#endif +#endif /* __CST816_H */ diff --git a/lvgl_touch/Kconfig b/lvgl_touch/Kconfig index b793adb..dfa0ab9 100644 --- a/lvgl_touch/Kconfig +++ b/lvgl_touch/Kconfig @@ -10,6 +10,7 @@ menu "LVGL Touch controller" default 5 if LV_TOUCH_CONTROLLER_FT81X default 6 if LV_TOUCH_CONTROLLER_RA8875 default 7 if LV_TOUCH_CONTROLLER_GT911 + default 8 if LV_TOUCH_CONTROLLER_CST816 choice prompt "Select a touch panel controller model." @@ -40,6 +41,9 @@ menu "LVGL Touch controller" config LV_TOUCH_CONTROLLER_GT911 select LV_I2C_TOUCH bool "GT911" + config LV_TOUCH_CONTROLLER_CST816 + select LV_I2C_TOUCH + bool "CST816" endchoice config LV_TOUCH_DRIVER_PROTOCOL_SPI @@ -441,6 +445,38 @@ menu "LVGL Touch controller" endmenu + menu "Touchpanel Configuration (CST816)" + depends on LV_TOUCH_CONTROLLER_CST816 + + config LV_TOUCH_X_MAX_CST816 + int + prompt "Maximum X coordinate ADC value" + range 0 1023 + default 320 + + config LV_TOUCH_Y_MAX_CST816 + int + prompt "Maximum Y coordinate ADC value" + range 0 1023 + default 240 + + config LV_SWAPXY_CST816 + bool + prompt "Swap X with Y coordinate." + default y + + config LV_INVERT_X_CST816 + bool + prompt "Invert X coordinate value." + default y + + config LV_INVERT_Y_CST816 + bool + prompt "Invert Y coordinate value." + default n + + endmenu + menu "Touchpanel Configuration (GT911)" depends on LV_TOUCH_CONTROLLER_GT911 diff --git a/lvgl_touch/touch_driver.c b/lvgl_touch/touch_driver.c index 35fcc6b..87b6063 100644 --- a/lvgl_touch/touch_driver.c +++ b/lvgl_touch/touch_driver.c @@ -22,6 +22,8 @@ void touch_driver_init(void) ra8875_touch_init(); #elif defined (CONFIG_LV_TOUCH_CONTROLLER_GT911) gt911_init(GT911_I2C_SLAVE_ADDR); +#elif defined (CONFIG_LV_TOUCH_CONTROLLER_CST816) + cst816_init(CST816_I2C_SLAVE_ADDR); #endif } @@ -47,6 +49,8 @@ bool touch_driver_read(lv_indev_drv_t *drv, lv_indev_data_t *data) res = ra8875_touch_read(drv, data); #elif defined (CONFIG_LV_TOUCH_CONTROLLER_GT911) res = gt911_read(drv, data); +#elif defined (CONFIG_LV_TOUCH_CONTROLLER_CST816) + res = cst816_read(drv, data); #endif #if LVGL_VERSION_MAJOR >= 8 diff --git a/lvgl_touch/touch_driver.h b/lvgl_touch/touch_driver.h index 0d014e2..72ed17c 100644 --- a/lvgl_touch/touch_driver.h +++ b/lvgl_touch/touch_driver.h @@ -34,6 +34,8 @@ extern "C" { #include "ra8875_touch.h" #elif defined (CONFIG_LV_TOUCH_CONTROLLER_GT911) #include "gt911.h" +#elif defined (CONFIG_LV_TOUCH_CONTROLLER_CST816) +#include "CST816.h" #endif /*********************