From 0f2eafcbd27162b45fbee1400c7b56ae6633e1e3 Mon Sep 17 00:00:00 2001 From: dogerber <99335589+dogerber@users.noreply.github.com> Date: Sat, 1 Jun 2024 12:21:40 +0200 Subject: [PATCH] Update lv_demos.ino to work with the same logic as examples/CapacitiveTouch.ino --- examples/lv_demos/lv_demos.ino | 318 ++++++++++++++++++++------------- 1 file changed, 195 insertions(+), 123 deletions(-) diff --git a/examples/lv_demos/lv_demos.ino b/examples/lv_demos/lv_demos.ino index 660a6f2..6d20cfe 100644 --- a/examples/lv_demos/lv_demos.ino +++ b/examples/lv_demos/lv_demos.ino @@ -1,16 +1,17 @@ #include -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0) -#error "The current version is not supported for the time being, please use a version below Arduino ESP32 3.0" +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) +#error "The current version is not supported for the time being, please use a version below Arduino ESP32 3.0" #endif /* Please make sure your touch IC model. */ +#define TOUCH_USE_CAPACITIVE_TOUCH // uses same logic as examples/CapacitiveTouch.ino // #define TOUCH_MODULES_CST_MUTUAL // #define TOUCH_MODULES_CST_SELF #if defined(TOUCH_MODULES_CST_SELF) || defined(TOUCH_MODULES_CST_SELF) -#include "TouchLib.h" -// #define TOUCH_READ_FROM_INTERRNUPT +// #include "TouchLib.h" +#define TOUCH_READ_FROM_INTERRNUPT #endif /* The product now has two screens, and the initialization code needs a small change in the new version. The LCD_MODULE_CMD_1 is used to define the @@ -27,13 +28,44 @@ #include "pin_config.h" #include "lv_demo_widgets.h" +#ifdef TOUCH_USE_CAPACITIVE_TOUCH +#include +#define PIN_LCD_BL 38 +#define PIN_LCD_D0 39 +#define PIN_LCD_D1 40 +#define PIN_LCD_D2 41 +#define PIN_LCD_D3 42 +#define PIN_LCD_D4 45 +#define PIN_LCD_D5 46 +#define PIN_LCD_D6 47 +#define PIN_LCD_D7 48 +#define PIN_POWER_ON 15 +#define PIN_LCD_RES 5 +#define PIN_LCD_CS 6 +#define PIN_LCD_DC 7 +#define PIN_LCD_WR 8 +#define PIN_LCD_RD 9 +#define PIN_BUTTON_1 0 +#define PIN_BUTTON_2 14 +#define PIN_BAT_VOLT 4 +#define BOARD_I2C_SCL 17 +#define BOARD_I2C_SDA 18 +#define BOARD_TOUCH_IRQ 16 +#define BOARD_TOUCH_RST 21 + +TouchDrvCSTXXX touch; +int16_t x[5], y[5]; + +#endif + esp_lcd_panel_io_handle_t io_handle = NULL; static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s) static lv_disp_drv_t disp_drv; // contains callback functions static lv_color_t *lv_disp_buf; static bool is_initialized_lvgl = false; #if defined(LCD_MODULE_CMD_1) -typedef struct { +typedef struct +{ uint8_t cmd; uint8_t data[14]; uint8_t len; @@ -66,10 +98,10 @@ bool get_int_signal = false; #endif #endif - static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx) { - if (is_initialized_lvgl) { + if (is_initialized_lvgl) + { lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx; lv_disp_flush_ready(disp_driver); } @@ -87,145 +119,185 @@ static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_ esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map); } -#if defined(TOUCH_MODULES_CST_SELF) || defined(TOUCH_MODULES_CST_SELF) +#if defined(TOUCH_MODULES_CST_SELF) || defined(TOUCH_MODULES_CST_SELF) || defined(TOUCH_USE_CAPACITIVE_TOUCH) static void lv_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) { -#if defined(TOUCH_READ_FROM_INTERRNUPT) - if (get_int_signal) { +#ifdef TOUCH_READ_FROM_INTERRNUPT + if (get_int_signal) + { get_int_signal = false; touch.read(); #else - if (touch.read()) { -#endif - TP_Point t = touch.getPoint(0); - data->point.x = t.x; - data->point.y = t.y; +#ifdef TOUCH_USE_CAPACITIVE_TOUCH + if (touch.getPoint(x, y, touch.getSupportTouchPoint())) + { + data->point.x = *x; + data->point.y = *y; data->state = LV_INDEV_STATE_PR; - } else - data->state = LV_INDEV_STATE_REL; -} + } +#else + if (touch.read()) + { +#endif #endif -void setup() -{ - Serial.begin(115200); + else data->state = LV_INDEV_STATE_REL; + } +#endif + void setup() + { + Serial.begin(115200); - //Turn on display power - pinMode(PIN_POWER_ON, OUTPUT); - digitalWrite(PIN_POWER_ON, HIGH); + // Turn on display power + pinMode(PIN_POWER_ON, OUTPUT); + digitalWrite(PIN_POWER_ON, HIGH); + pinMode(PIN_LCD_RD, OUTPUT); + digitalWrite(PIN_LCD_RD, HIGH); + esp_lcd_i80_bus_handle_t i80_bus = NULL; + esp_lcd_i80_bus_config_t bus_config = { + .dc_gpio_num = PIN_LCD_DC, + .wr_gpio_num = PIN_LCD_WR, + .clk_src = LCD_CLK_SRC_PLL160M, + .data_gpio_nums = + { + PIN_LCD_D0, + PIN_LCD_D1, + PIN_LCD_D2, + PIN_LCD_D3, + PIN_LCD_D4, + PIN_LCD_D5, + PIN_LCD_D6, + PIN_LCD_D7, + }, + .bus_width = 8, + .max_transfer_bytes = LVGL_LCD_BUF_SIZE * sizeof(uint16_t), + }; + esp_lcd_new_i80_bus(&bus_config, &i80_bus); + esp_lcd_panel_io_i80_config_t io_config = { + .cs_gpio_num = PIN_LCD_CS, + .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ, + .trans_queue_depth = 20, + .on_color_trans_done = example_notify_lvgl_flush_ready, + .user_ctx = &disp_drv, + .lcd_cmd_bits = 8, + .lcd_param_bits = 8, + .dc_levels = + { + .dc_idle_level = 0, + .dc_cmd_level = 0, + .dc_dummy_level = 0, + .dc_data_level = 1, + }, + }; + ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &io_handle)); + esp_lcd_panel_handle_t panel_handle = NULL; + esp_lcd_panel_dev_config_t panel_config = { + .reset_gpio_num = PIN_LCD_RES, + .color_space = ESP_LCD_COLOR_SPACE_RGB, + .bits_per_pixel = 16, + }; + esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle); + esp_lcd_panel_reset(panel_handle); + esp_lcd_panel_init(panel_handle); + esp_lcd_panel_invert_color(panel_handle, true); - pinMode(PIN_LCD_RD, OUTPUT); - digitalWrite(PIN_LCD_RD, HIGH); - esp_lcd_i80_bus_handle_t i80_bus = NULL; - esp_lcd_i80_bus_config_t bus_config = { - .dc_gpio_num = PIN_LCD_DC, - .wr_gpio_num = PIN_LCD_WR, - .clk_src = LCD_CLK_SRC_PLL160M, - .data_gpio_nums = - { - PIN_LCD_D0, - PIN_LCD_D1, - PIN_LCD_D2, - PIN_LCD_D3, - PIN_LCD_D4, - PIN_LCD_D5, - PIN_LCD_D6, - PIN_LCD_D7, - }, - .bus_width = 8, - .max_transfer_bytes = LVGL_LCD_BUF_SIZE * sizeof(uint16_t), - }; - esp_lcd_new_i80_bus(&bus_config, &i80_bus); - - esp_lcd_panel_io_i80_config_t io_config = { - .cs_gpio_num = PIN_LCD_CS, - .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ, - .trans_queue_depth = 20, - .on_color_trans_done = example_notify_lvgl_flush_ready, - .user_ctx = &disp_drv, - .lcd_cmd_bits = 8, - .lcd_param_bits = 8, - .dc_levels = - { - .dc_idle_level = 0, - .dc_cmd_level = 0, - .dc_dummy_level = 0, - .dc_data_level = 1, - }, - }; - ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &io_handle)); - esp_lcd_panel_handle_t panel_handle = NULL; - esp_lcd_panel_dev_config_t panel_config = { - .reset_gpio_num = PIN_LCD_RES, - .color_space = ESP_LCD_COLOR_SPACE_RGB, - .bits_per_pixel = 16, - }; - esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle); - esp_lcd_panel_reset(panel_handle); - esp_lcd_panel_init(panel_handle); - esp_lcd_panel_invert_color(panel_handle, true); - - esp_lcd_panel_swap_xy(panel_handle, true); - esp_lcd_panel_mirror(panel_handle, false, true); - // the gap is LCD panel specific, even panels with the same driver IC, can - // have different gap value - esp_lcd_panel_set_gap(panel_handle, 0, 35); + esp_lcd_panel_swap_xy(panel_handle, true); + esp_lcd_panel_mirror(panel_handle, false, true); + // the gap is LCD panel specific, even panels with the same driver IC, can + // have different gap value + esp_lcd_panel_set_gap(panel_handle, 0, 35); #if defined(LCD_MODULE_CMD_1) - for (uint8_t i = 0; i < (sizeof(lcd_st7789v) / sizeof(lcd_cmd_t)); i++) { - esp_lcd_panel_io_tx_param(io_handle, lcd_st7789v[i].cmd, lcd_st7789v[i].data, lcd_st7789v[i].len & 0x7f); - if (lcd_st7789v[i].len & 0x80) - delay(120); - } + for (uint8_t i = 0; i < (sizeof(lcd_st7789v) / sizeof(lcd_cmd_t)); i++) + { + esp_lcd_panel_io_tx_param(io_handle, lcd_st7789v[i].cmd, lcd_st7789v[i].data, lcd_st7789v[i].len & 0x7f); + if (lcd_st7789v[i].len & 0x80) + delay(120); + } #endif - /* Lighten the screen with gradient */ - ledcSetup(0, 10000, 8); - ledcAttachPin(PIN_LCD_BL, 0); - for (uint8_t i = 0; i < 0xFF; i++) { - ledcWrite(0, i); - delay(2); - } + /* Lighten the screen with gradient */ + ledcSetup(0, 10000, 8); + ledcAttachPin(PIN_LCD_BL, 0); + for (uint8_t i = 0; i < 0xFF; i++) + { + ledcWrite(0, i); + delay(2); + } - lv_init(); - lv_disp_buf = (lv_color_t *)heap_caps_malloc(LVGL_LCD_BUF_SIZE * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); + lv_init(); + lv_disp_buf = (lv_color_t *)heap_caps_malloc(LVGL_LCD_BUF_SIZE * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); - lv_disp_draw_buf_init(&disp_buf, lv_disp_buf, NULL, LVGL_LCD_BUF_SIZE); - /*Initialize the display*/ - lv_disp_drv_init(&disp_drv); - /*Change the following line to your display resolution*/ - disp_drv.hor_res = EXAMPLE_LCD_H_RES; - disp_drv.ver_res = EXAMPLE_LCD_V_RES; - disp_drv.flush_cb = example_lvgl_flush_cb; - disp_drv.draw_buf = &disp_buf; - disp_drv.user_data = panel_handle; - lv_disp_drv_register(&disp_drv); + lv_disp_draw_buf_init(&disp_buf, lv_disp_buf, NULL, LVGL_LCD_BUF_SIZE); + /*Initialize the display*/ + lv_disp_drv_init(&disp_drv); + /*Change the following line to your display resolution*/ + disp_drv.hor_res = EXAMPLE_LCD_H_RES; + disp_drv.ver_res = EXAMPLE_LCD_V_RES; + disp_drv.flush_cb = example_lvgl_flush_cb; + disp_drv.draw_buf = &disp_buf; + disp_drv.user_data = panel_handle; + lv_disp_drv_register(&disp_drv); #if defined(TOUCH_MODULES_CST_SELF) || defined(TOUCH_MODULES_CST_SELF) - /* Register touch brush with LVGL */ - Wire.begin(PIN_IIC_SDA, PIN_IIC_SCL, 800000); - inited_touch = touch.init(); - if (inited_touch) { - touch.setRotation(1); + /* Register touch brush with LVGL */ + Wire.begin(PIN_IIC_SDA, PIN_IIC_SCL, 800000); + inited_touch = touch.init(); + if (inited_touch) + { + touch.setRotation(1); + static lv_indev_drv_t indev_drv; + lv_indev_drv_init(&indev_drv); + indev_drv.type = LV_INDEV_TYPE_POINTER; + indev_drv.read_cb = lv_touchpad_read; + lv_indev_drv_register(&indev_drv); + } + is_initialized_lvgl = true; +#if defined(TOUCH_READ_FROM_INTERRNUPT) + attachInterrupt( + PIN_TOUCH_INT, [] + { get_int_signal = true; }, FALLING); +#endif +#endif + +#ifdef TOUCH_USE_CAPACITIVE_TOUCH + // Initialize capacitive touch + touch.setPins(BOARD_TOUCH_RST, BOARD_TOUCH_IRQ); + + if (!touch.begin(Wire, CST328_SLAVE_ADDRESS, BOARD_I2C_SDA, BOARD_I2C_SCL)) + { + Serial.println("Failed init CST328 Device!"); + if (!touch.begin(Wire, CST816_SLAVE_ADDRESS, BOARD_I2C_SDA, BOARD_I2C_SCL)) + { + Serial.println("Failed init CST816 Device!"); + while (1) + { + Serial.println("Not find touch device!"); + delay(1000); + } + } + } + + // fix orientation + touch.setMaxCoordinates(320, 170); + touch.setMirrorXY(true, false); + touch.setSwapXY(true); +#endif + + // link static lv_indev_drv_t indev_drv; lv_indev_drv_init(&indev_drv); indev_drv.type = LV_INDEV_TYPE_POINTER; indev_drv.read_cb = lv_touchpad_read; lv_indev_drv_register(&indev_drv); + is_initialized_lvgl = true; + + lv_demo_widgets(); } - is_initialized_lvgl = true; -#if defined(TOUCH_READ_FROM_INTERRNUPT) - attachInterrupt( - PIN_TOUCH_INT, [] { get_int_signal = true; }, FALLING); -#endif -#endif - lv_demo_widgets(); -} - -void loop() -{ - lv_timer_handler(); - delay(2); -} + void loop() + { + lv_timer_handler(); + delay(2); + }