Skip to content

Commit

Permalink
Merge pull request #89 from Spooks4576/Development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Spooks4576 authored Nov 20, 2024
2 parents f1cafda + 67f6b9c commit 5775837
Show file tree
Hide file tree
Showing 18 changed files with 2,664 additions and 2,505 deletions.
2 changes: 2 additions & 0 deletions components/lvgl_esp32_drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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_CST820)
list(APPEND SOURCES "lvgl_touch/cst820.c")
endif()

if(CONFIG_LV_TOUCH_DRIVER_PROTOCOL_SPI)
Expand Down
25 changes: 25 additions & 0 deletions components/lvgl_esp32_drivers/lvgl_touch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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_CST820

choice
prompt "Select a touch panel controller model."
Expand Down Expand Up @@ -40,6 +41,9 @@ menu "LVGL Touch controller"
config LV_TOUCH_CONTROLLER_GT911
select LV_I2C_TOUCH
bool "GT911"
config LV_TOUCH_CONTROLLER_CST820
select LV_I2C_TOUCH
bool "CST820"
endchoice

config LV_TOUCH_DRIVER_PROTOCOL_SPI
Expand Down Expand Up @@ -446,6 +450,27 @@ menu "LVGL Touch controller"

endmenu

menu "Touchpanel Configuration (CST820)"
depends on LV_TOUCH_CONTROLLER_CST820

config LV_CST820_SWAPXY
bool
prompt "Swap X with Y coordinate."
default n

config LV_CST820_INVERT_X
bool
prompt "Invert X coordinate value."
default n

config LV_CST820_INVERT_Y
bool
prompt "Invert Y coordinate value."
default n

endmenu


menu "Touchpanel Configuration (GT911)"
depends on LV_TOUCH_CONTROLLER_GT911

Expand Down
91 changes: 91 additions & 0 deletions components/lvgl_esp32_drivers/lvgl_touch/cst820.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <esp_log.h>
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include <lvgl.h>
#else
#include <lvgl/lvgl.h>
#endif
#include "cst820.h"

#include "lvgl_i2c/i2c_manager.h"

#define TAG "CST820"

esp_err_t cst820_i2c_read(uint8_t slave_addr, uint16_t register_addr, uint8_t *data_buf, uint8_t len) {
return lvgl_i2c_read(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr | I2C_REG_16, data_buf, len);
}

esp_err_t cst820_i2c_write8(uint8_t slave_addr, uint16_t register_addr, uint8_t data) {
uint8_t buffer = data;
return lvgl_i2c_write(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr | I2C_REG_16, &buffer, 1);
}

void cst820_init(uint8_t dev_addr)
{
cst820_i2c_write8(CST820_I2C_SLAVE_ADDR, 0xFE, 0XFF);
}

esp_err_t cst820_read_continuous(uint16_t register_addr, uint8_t *data_buf, uint32_t length)
{
esp_err_t err = cst820_i2c_read(CST820_I2C_SLAVE_ADDR, register_addr, data_buf, length);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to read continuous data from address: 0x%04X, error: %d", register_addr, err);
return err;
}
return ESP_OK;
}

bool cst820_read(lv_indev_drv_t *drv, lv_indev_data_t *data)
{
static int16_t last_x = 0;
static int16_t last_y = 0;

uint8_t FingerIndex;
cst820_i2c_read(CST820_I2C_SLAVE_ADDR, 0x02, &FingerIndex, 1);

if (!FingerIndex)
{
data->point.x = last_x;
data->point.y = last_y;
data->state = LV_INDEV_STATE_REL;
return false;
}

uint8_t touchdata[4];
cst820_read_continuous(0x03, touchdata, 4);

int16_t raw_x = ((touchdata[0] & 0x0f) << 8) | touchdata[1];
int16_t raw_y = ((touchdata[2] & 0x0f) << 8) | touchdata[3];

#if CONFIG_LV_CT820_INVERT_X
raw_x = raw_x * -1;
#endif
#if CONFIG_LV_CT820_INVERT_Y
raw_y = raw_y * -1;
#endif
#if CONFIG_LV_CT820_SWAPXY
int16_t swap_buf = raw_x;
raw_x = raw_y;
raw_y = swap_buf;
#endif


const int16_t raw_max_x = 4095;
const int16_t raw_max_y = 4095;


lv_coord_t screen_width = LV_HOR_RES;
lv_coord_t screen_height = LV_VER_RES;


last_x = (raw_x * screen_width) / raw_max_x;
last_y = (raw_y * screen_height) / raw_max_y;

data->point.x = last_x;
data->point.y = last_y;
data->state = LV_INDEV_STATE_PR;

ESP_LOGW(TAG, "Raw X=%d, Y=%d", raw_x, raw_y);
ESP_LOGW(TAG, "Mapped X=%u, Y=%u", data->point.x, data->point.y);

return false;
}
49 changes: 49 additions & 0 deletions components/lvgl_esp32_drivers/lvgl_touch/cst820.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef __GT911_H

#define __CS820_H

#include <stdint.h>
#include <stdbool.h>
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include "lvgl.h"
#else
#include "lvgl/lvgl.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#define CST820_I2C_SLAVE_ADDR 0x15

enum GESTURE
{
None = 0x00,
SlideDown = 0x01,
SlideUp = 0x02,
SlideLeft = 0x03,
SlideRight = 0x04,
SingleTap = 0x05,
DoubleTap = 0x0B,
LongPress = 0x0C
};

/**
* @brief Initialize for GT911 communication via I2C
* @param dev_addr: Device address on communication Bus (I2C slave address of GT911).
* @retval None
*/
void cst820_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 cst820_read(lv_indev_drv_t *drv, lv_indev_data_t *data);

#ifdef __cplusplus
}
#endif
#endif /* __GT911_H */
4 changes: 4 additions & 0 deletions components/lvgl_esp32_drivers/lvgl_touch/touch_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_CST820)
cst820_init(CST820_I2C_SLAVE_ADDR);
#endif
}

Expand All @@ -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_CST820)
res = cst820_read(drv, data);
#endif

#if LVGL_VERSION_MAJOR >= 8
Expand Down
2 changes: 2 additions & 0 deletions components/lvgl_esp32_drivers/lvgl_touch/touch_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_CST820)
#include "cst820.h"
#endif

/*********************
Expand Down
Loading

0 comments on commit 5775837

Please sign in to comment.