Merge branch 'example/i80_controller_touch' into 'master'

Add support touch into LCD example

See merge request espressif/esp-idf!18436
This commit is contained in:
Vilem Zavodny 2022-08-04 13:50:08 +08:00
commit 6e98440ab1
6 changed files with 211 additions and 18 deletions

View File

@ -46,7 +46,16 @@ The connection between ESP Board and the LCD is as follows:
│ │ │ │
│ BK_LIGHT ├─────────────►│ BCKL │
│ │ │ │
│ │ └────────────────┘
│ │ LCD TOUCH
│ │ ┌────────────────┐
│ │ │ │
│ I2C SCL ├─────────────►│ I2C SCL │
│ │ │ │
│ I2C SDA │◄────────────►│ I2C SDA │
│ │ │ │
└─────────────┘ └────────────────┘
```
The GPIO number used by this example can be changed in [i80_controller_example_main.c](main/i80_controller_example_main.c).
@ -85,6 +94,15 @@ I (558) example: Install LVGL tick timer
I (558) example: Display LVGL animation
```
## Touch Screen Support
This example supports touch screen connected via I2C. You can enable it by running `idf.py menuconfig` and navigating to `Example Configuration -> Enable LCD touch`. When touch is enabled, there will be a new button in the GUI that can restart the animation.
These touch controllers are supported:
* [GT911](https://github.com/espressif/esp-bsp/tree/master/components/lcd_touch/esp_lcd_touch_gt911)
* [TT21100](https://github.com/espressif/esp-bsp/tree/master/components/lcd_touch/esp_lcd_touch_tt21100)
* [FT5X06](https://github.com/espressif/esp-bsp/tree/master/components/lcd_touch/esp_lcd_touch_ft5x06)
## Troubleshooting

View File

@ -45,4 +45,27 @@ menu "Example Configuration"
default 16 if EXAMPLE_LCD_NT35510_DATA_WIDTH_16
default 8
config EXAMPLE_LCD_TOUCH_ENABLED
bool "Enable LCD touch"
default y
help
Enable this option if you wish to use display touch. You can select from three touch controllers.
choice EXAMPLE_LCD_TOUCH_CONTROLLER
prompt "LCD touch controller model"
depends on EXAMPLE_LCD_TOUCH_ENABLED
default EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
help
Select LCD touch controller model
config EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
bool "GT911"
config EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
bool "TT21100"
config EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
bool "FT5X06"
endchoice
endmenu

View File

@ -16,6 +16,17 @@
#include "esp_log.h"
#include "lvgl.h"
#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
#include "driver/i2c.h"
#if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
#include "esp_lcd_touch_gt911.h"
#elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
#include "esp_lcd_touch_tt21100.h"
#elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
#include "esp_lcd_touch_ft5x06.h"
#endif
#endif
static const char *TAG = "example";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -69,6 +80,12 @@ static const char *TAG = "example";
#define EXAMPLE_LCD_PARAM_BITS 8
#endif
#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
#define EXAMPLE_I2C_NUM 0 // I2C number
#define EXAMPLE_I2C_SCL 39
#define EXAMPLE_I2C_SDA 40
#endif
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
// Supported alignment: 16, 32, 64. A higher alignment can enables higher burst transfer size, thus a higher i80 bus throughput.
@ -94,6 +111,29 @@ 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 CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
static void example_lvgl_touch_cb(lv_indev_drv_t * drv, lv_indev_data_t * data)
{
uint16_t touchpad_x[1] = {0};
uint16_t touchpad_y[1] = {0};
uint8_t touchpad_cnt = 0;
/* Read touch controller data */
esp_lcd_touch_read_data(drv->user_data);
/* Get coordinates */
bool touchpad_pressed = esp_lcd_touch_get_coordinates(drv->user_data, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1);
if (touchpad_pressed && touchpad_cnt > 0) {
data->point.x = touchpad_x[0];
data->point.y = touchpad_y[0];
data->state = LV_INDEV_STATE_PRESSED;
} else {
data->state = LV_INDEV_STATE_RELEASED;
}
}
#endif
static void example_increase_lvgl_tick(void *arg)
{
/* Tell LVGL how many milliseconds has elapsed */
@ -175,6 +215,13 @@ void app_main(void)
.bits_per_pixel = 16,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
esp_lcd_panel_reset(panel_handle);
esp_lcd_panel_init(panel_handle);
// Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
// the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
esp_lcd_panel_invert_color(panel_handle, true);
esp_lcd_panel_set_gap(panel_handle, 0, 20);
#elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
ESP_LOGI(TAG, "Install LCD driver of nt35510");
esp_lcd_panel_dev_config_t panel_config = {
@ -183,6 +230,13 @@ void app_main(void)
.bits_per_pixel = 16,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_nt35510(io_handle, &panel_config, &panel_handle));
esp_lcd_panel_reset(panel_handle);
esp_lcd_panel_init(panel_handle);
// Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
// the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, true, false);
#elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ILI9341
// ILI9341 is NOT a distinct driver, but a special case of ST7789
// (essential registers are identical). A few lines further down in this code,
@ -194,19 +248,11 @@ void app_main(void)
.bits_per_pixel = 16,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
#endif
esp_lcd_panel_reset(panel_handle);
esp_lcd_panel_init(panel_handle);
// Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
// the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
#if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
esp_lcd_panel_invert_color(panel_handle, true);
esp_lcd_panel_set_gap(panel_handle, 0, 20);
#elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_mirror(panel_handle, true, false);
#elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ILI9341
esp_lcd_panel_swap_xy(panel_handle, true);
esp_lcd_panel_invert_color(panel_handle, false);
// ILI9341 is very similar to ST7789 and shares the same driver.
@ -226,6 +272,64 @@ void app_main(void)
ESP_LOGI(TAG, "Turn on LCD backlight");
gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
esp_lcd_touch_handle_t tp = NULL;
esp_lcd_panel_io_handle_t tp_io_handle = NULL;
ESP_LOGI(TAG, "Initialize I2C");
const i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = EXAMPLE_I2C_SDA,
.scl_io_num = EXAMPLE_I2C_SCL,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 400000,
};
/* Initialize I2C */
ESP_ERROR_CHECK(i2c_param_config(EXAMPLE_I2C_NUM, &i2c_conf));
ESP_ERROR_CHECK(i2c_driver_install(EXAMPLE_I2C_NUM, i2c_conf.mode, 0, 0, 0));
#if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
#elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_TT21100_CONFIG();
#elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG();
#endif
ESP_LOGI(TAG, "Initialize touch IO (I2C)");
/* Touch IO handle */
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)EXAMPLE_I2C_NUM, &tp_io_config, &tp_io_handle));
esp_lcd_touch_config_t tp_cfg = {
.x_max = EXAMPLE_LCD_V_RES,
.y_max = EXAMPLE_LCD_H_RES,
.rst_gpio_num = -1,
.int_gpio_num = -1,
.flags = {
.swap_xy = 1,
.mirror_x = 1,
.mirror_y = 0,
},
};
/* Initialize touch */
#if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
ESP_LOGI(TAG, "Initialize touch controller GT911");
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp));
#elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
ESP_LOGI(TAG, "Initialize touch controller TT21100");
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_tt21100(tp_io_handle, &tp_cfg, &tp));
#elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
ESP_LOGI(TAG, "Initialize touch controller FT5X06");
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp));
#endif
#endif
ESP_LOGI(TAG, "Initialize LVGL library");
lv_init();
// alloc draw buffers used by LVGL
@ -267,6 +371,17 @@ void app_main(void)
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
#if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
static lv_indev_drv_t indev_drv; // Input device driver (Touch)
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.disp = disp;
indev_drv.read_cb = example_lvgl_touch_cb;
indev_drv.user_data = tp;
lv_indev_drv_register(&indev_drv);
#endif
ESP_LOGI(TAG, "Display LVGL animation");
example_lvgl_demo_ui(disp);

View File

@ -1,3 +1,6 @@
dependencies:
idf: ">=4.4"
lvgl/lvgl: "~8.2.0"
esp_lcd_touch_gt911: "^1.0"
esp_lcd_touch_tt21100: "^1.0"
esp_lcd_touch_ft5x06: "^1.0"

View File

@ -20,15 +20,18 @@ typedef struct {
int count_val;
} my_timer_context_t;
static my_timer_context_t my_tim_ctx;
static lv_obj_t * btn;
static lv_obj_t *arc[3];
static lv_obj_t *img_logo;
static lv_obj_t *img_text;
static lv_obj_t *img_text = NULL;
static lv_color_t arc_color[] = {
LV_COLOR_MAKE(232, 87, 116),
LV_COLOR_MAKE(126, 87, 162),
LV_COLOR_MAKE(90, 202, 228),
};
static void anim_timer_cb(lv_timer_t *timer)
{
my_timer_context_t *timer_ctx = (my_timer_context_t *) timer->user_data;
@ -69,18 +72,17 @@ static void anim_timer_cb(lv_timer_t *timer)
// Delete timer when all animation finished
if ((count += 5) == 220) {
lv_timer_del(timer);
// Enable button
lv_obj_clear_state(btn, LV_STATE_DISABLED);
} else {
timer_ctx->count_val = count;
}
}
void example_lvgl_demo_ui(lv_disp_t *disp)
static void start_animation(lv_obj_t *scr)
{
lv_obj_t *scr = lv_disp_get_scr_act(disp);
// Create image
img_logo = lv_img_create(scr);
lv_img_set_src(img_logo, &esp_logo);
// Align image
lv_obj_center(img_logo);
// Create arcs
@ -101,10 +103,41 @@ void example_lvgl_demo_ui(lv_disp_t *disp)
lv_obj_center(arc[i]);
}
if (img_text) {
lv_obj_del(img_text);
img_text = NULL;
}
// Create timer for animation
static my_timer_context_t my_tim_ctx = {
.count_val = -90,
};
my_tim_ctx.count_val = -90;
my_tim_ctx.scr = scr;
lv_timer_create(anim_timer_cb, 20, &my_tim_ctx);
// Disable button
lv_obj_add_state(btn, LV_STATE_DISABLED);
}
static void btn_cb(lv_event_t * e)
{
lv_obj_t * scr = lv_event_get_user_data(e);
start_animation(scr);
}
void example_lvgl_demo_ui(lv_disp_t *disp)
{
lv_obj_t *scr = lv_disp_get_scr_act(disp);
// Create image
img_logo = lv_img_create(scr);
lv_img_set_src(img_logo, &esp_logo);
btn = lv_btn_create(scr);
lv_obj_t * lbl = lv_label_create(btn);
lv_label_set_text_static(lbl, LV_SYMBOL_REFRESH" SHOW AGAIN");
lv_obj_set_style_text_font(lbl, &lv_font_montserrat_20, 0);
lv_obj_align(btn, LV_ALIGN_BOTTOM_LEFT, 30, -30);
// Button event
lv_obj_add_event_cb(btn, btn_cb, LV_EVENT_CLICKED, scr);
start_animation(scr);
}

View File

@ -4,3 +4,4 @@ CONFIG_LV_USE_PERF_MONITOR=y
CONFIG_LV_USE_USER_DATA=y
CONFIG_LV_COLOR_16_SWAP=y
CONFIG_LV_COLOR_DEPTH_16=y
CONFIG_LV_FONT_MONTSERRAT_20=y