Skip to content

cst816 touch driver #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lvgl_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ bool lvgl_spi_driver_init(int host,
};

ESP_LOGI(TAG, "Initializing SPI bus...");
#if defined (CONFIG_IDF_TARGET_ESP32C3)
#if defined (CONFIG_IDF_TARGET_ESP32C3) || defined (CONFIG_IDF_TARGET_ESP32S3)
dma_channel = SPI_DMA_CH_AUTO;
#endif
esp_err_t ret = spi_bus_initialize(host, &buscfg, (spi_dma_chan_t)dma_channel);
Expand Down
6 changes: 6 additions & 0 deletions lvgl_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lvgl_tft/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,20 @@ menu "LVGL TFT Display controller"
help
Use software reset and ignores configured reset pin (some hardware does not use a reset pin).

config LV_DISPLAY_USE_TE_PIN
bool "Enable TE (Tearing effect) pin"
default y
help
Enable the TE pin to control the display. You can disable
it when the display does not need MISO signal to be controlled.

config LV_DISPLAY_TE_PIN
int "GPIO for TE (Tearing effect) pin"
depends on LV_DISPLAY_USE_TE_PIN
default 38
help
Configure the display TE pin here.

endmenu

# menu will be visible only when LV_PREDEFINED_DISPLAY_NONE is y
Expand Down
78 changes: 39 additions & 39 deletions lvgl_tft/ili9341.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ static void ili9341_send_color(void * data, uint16_t length);
void ili9341_init(void)
{
lcd_init_cmd_t ili_init_cmds[]={
{0xCF, {0x00, 0x83, 0X30}, 3},
{0xED, {0x64, 0x03, 0X12, 0X81}, 4},
{0xE8, {0x85, 0x01, 0x79}, 3},
{0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02}, 5},
{0xF7, {0x20}, 1},
{0xEA, {0x00, 0x00}, 2},
// {0xCF, {0x00, 0x83, 0X30}, 3},
// {0xED, {0x64, 0x03, 0X12, 0X81}, 4},
// {0xE8, {0x85, 0x01, 0x79}, 3},
// {0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02}, 5},
// {0xF7, {0x20}, 1},
// {0xEA, {0x00, 0x00}, 2},
{0xC0, {0x26}, 1}, /*Power control*/
{0xC1, {0x11}, 1}, /*Power control */
{0xC5, {0x35, 0x3E}, 2}, /*VCOM control*/
{0xC7, {0xBE}, 1}, /*VCOM control*/
{0x36, {0x28}, 1}, /*Memory Access Control*/
{0x36, {0x08}, 1}, //{0x36, {0x28}, 1}, /*Memory Access Control*/
{0x3A, {0x55}, 1}, /*Pixel Format Set*/
{0xB1, {0x00, 0x1B}, 2},
{0xF2, {0x08}, 1},
{0x26, {0x01}, 1},
{0xE0, {0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0X87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00}, 15},
{0XE1, {0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F}, 15},
{0x2A, {0x00, 0x00, 0x00, 0xEF}, 4},
{0x2B, {0x00, 0x00, 0x01, 0x3f}, 4},
{0x2C, {0}, 0},
{0xB7, {0x07}, 1},
{0xB6, {0x0A, 0x82, 0x27, 0x00}, 4},
{0xB1, {0x00, 0x18}, 2},//{0xB1, {0x00, 0x1B}, 2}, //[BJ] frame rate.
//{0xF2, {0x08}, 1},
{0x26, {0x01}, 1}, //[BJ] Gamma set
{0xE0, {0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0X87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00}, 15},//[BJ] + Gamma correction
{0XE1, {0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F}, 15},//[BJ] - Gamma correction
{0x2A, {0x00, 0x00, 0x00, 0xEF}, 4}, //[BJ] column address set
{0x2B, {0x00, 0x00, 0x01, 0x3f}, 4},//[BJ] page address set
{0x2C, {0}, 0},//[BJ] memory write.
{0xB7, {0x07}, 1},//[BJ] Entry mode set
{0xB6, {0x0A, 0x82, 0x27, 0x00}, 4}, //[BJ] display function control
{0x11, {0}, 0x80},
{0x29, {0}, 0x80},
{0, {0}, 0xff},
Expand Down Expand Up @@ -108,7 +108,7 @@ void ili9341_init(void)
cmd++;
}

ili9341_set_orientation(CONFIG_LV_DISPLAY_ORIENTATION);
// ili9341_set_orientation(CONFIG_LV_DISPLAY_ORIENTATION);

#if ILI9341_INVERT_COLORS == 1
ili9341_send_cmd(0x21);
Expand Down Expand Up @@ -187,25 +187,25 @@ static void ili9341_send_color(void * data, uint16_t length)
static void ili9341_set_orientation(uint8_t orientation)
{
// ESP_ASSERT(orientation < 4);

const char *orientation_str[] = {
"PORTRAIT", "PORTRAIT_INVERTED", "LANDSCAPE", "LANDSCAPE_INVERTED"
};

ESP_LOGI(TAG, "Display orientation: %s", orientation_str[orientation]);

#if defined CONFIG_LV_PREDEFINED_DISPLAY_M5STACK
uint8_t data[] = {0x68, 0x68, 0x08, 0x08};
#elif defined (CONFIG_LV_PREDEFINED_DISPLAY_M5CORE2)
uint8_t data[] = {0x08, 0x88, 0x28, 0xE8};
#elif defined (CONFIG_LV_PREDEFINED_DISPLAY_WROVER4)
uint8_t data[] = {0x6C, 0xEC, 0xCC, 0x4C};
#elif defined (CONFIG_LV_PREDEFINED_DISPLAY_NONE)
uint8_t data[] = {0x48, 0x88, 0x28, 0xE8};
#endif

ESP_LOGI(TAG, "0x36 command value: 0x%02X", data[orientation]);

ili9341_send_cmd(0x36);
ili9341_send_data((void *) &data[orientation], 1);
// orientation = 2;
// const char *orientation_str[] = {
// "PORTRAIT", "PORTRAIT_INVERTED", "LANDSCAPE", "LANDSCAPE_INVERTED"
// };

// ESP_LOGI(TAG, "Display orientation: %s", orientation_str[orientation]);

// #if defined CONFIG_LV_PREDEFINED_DISPLAY_M5STACK
// uint8_t data[] = {0x68, 0x68, 0x08, 0x08};
// #elif defined (CONFIG_LV_PREDEFINED_DISPLAY_M5CORE2)
// uint8_t data[] = {0x08, 0x88, 0x28, 0xE8};
// #elif defined (CONFIG_LV_PREDEFINED_DISPLAY_WROVER4)
// uint8_t data[] = {0x6C, 0xEC, 0xCC, 0x4C};
// #elif defined (CONFIG_LV_PREDEFINED_DISPLAY_NONE)
// uint8_t data[] = {0x48, 0x88, 0x28, 0xE8};
// #endif

// ESP_LOGI(TAG, "0x36 command value: 0x%02X", data[orientation]);

// ili9341_send_cmd(0x36);
// ili9341_send_data((void *) &data[orientation], 1);
}
15 changes: 14 additions & 1 deletion lvgl_tft/st7789.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ void st7789_init(void)
{0xB6, {0x0A, 0x82, 0x27, 0x00}, 4},
{ST7789_SLPOUT, {0}, 0x80},
{ST7789_DISPON, {0}, 0x80},
#ifdef CONFIG_LV_DISPLAY_USE_TE_PIN
{ST7789_TEON, {0}, 1}, //[BJ] turn on TE
#endif
//-----------
//[BJ] Below I am trying to change refresh rate of the screen
//{ST7789_FRCTR2,{0x1F},1}, //[BJ] set the refresh rate
//{ST7789_FRCTRL1,{0x01, 0x0F, 0x0F},3}, //[BJ] divide the refresh by 2.
// ----------------------------------------
{0, {0}, 0xff},
};

Expand All @@ -94,6 +102,11 @@ void st7789_init(void)
gpio_set_direction(ST7789_RST, GPIO_MODE_OUTPUT);
#endif

#ifdef CONFIG_LV_DISPLAY_USE_TE_PIN
gpio_pad_select_gpio(ST7789_TE);
gpio_set_direction(ST7789_TE, GPIO_MODE_INPUT);
#endif

//Reset the display
#if !defined(ST7789_SOFT_RST)
gpio_set_level(ST7789_RST, 0);
Expand Down Expand Up @@ -208,7 +221,7 @@ void st7789_send_data(void * data, uint16_t length)

static void st7789_send_color(void * data, size_t length)
{
disp_wait_for_pending_transactions();
// disp_wait_for_pending_transactions();
gpio_set_level(ST7789_DC, 1);
disp_spi_send_colors(data, length);
}
Expand Down
2 changes: 2 additions & 0 deletions lvgl_tft/st7789.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extern "C"

#define ST7789_DC CONFIG_LV_DISP_PIN_DC
#define ST7789_RST CONFIG_LV_DISP_PIN_RST
#define ST7789_TE CONFIG_LV_DISPLAY_TE_PIN


#if CONFIG_LV_DISP_USE_RST
#if CONFIG_LV_DISP_ST7789_SOFT_RESET
Expand Down
123 changes: 123 additions & 0 deletions lvgl_touch/CST816.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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 <esp_log.h>
#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include <lvgl.h>
#else
#include <lvgl/lvgl.h>
#endif
#include "CST816.h"

#include "lvgl_i2c/i2c_manager.h"

#define TAG "CST816"

uint8_t CST816_addr;

static esp_err_t _cst816_i2c_read(uint8_t slave_addr, uint8_t register_addr, uint8_t *data_buf, uint8_t len) {
return lvgl_i2c_read(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr, data_buf, len);
}

// static esp_err_t _cst816_i2c_write8(uint8_t slave_addr, uint8_t register_addr, uint8_t data) {
// return lvgl_i2c_write(CONFIG_LV_I2C_TOUCH_PORT, slave_addr, register_addr, &data, 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) {
CST816_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]);
}

/**
* @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) {


uint8_t data_raw[8] ;
_cst816_i2c_read(CST816_addr, 0x01, data_raw, 6);
// printf("Touch data: ");
// for(int i = 0; i < 6; i++)
// {
// printf("0x%02X ", data_raw[i]);
// }
// printf("\r\n");

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;
}
56 changes: 56 additions & 0 deletions lvgl_touch/CST816.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#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 <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 CST816_I2C_SLAVE_ADDR 0x15

/**
* @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 */
Loading