/* * LVGL font rendering test */ #include "esp_err.h" #include "esp_log.h" #include "esp_check.h" #include "driver/gpio.h" #include "driver/spi_master.h" #include "esp_lcd_panel_io.h" #include "esp_lcd_panel_vendor.h" #include "esp_lcd_panel_ops.h" #include "esp_lvgl_port.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_heap_caps.h" #include "lvgl.h" #include "src/extra/libs/gif/lv_gif.h" #include "emotions/facetestLCD.h" /* LCD size */ #define LCD_H_RES (240) #define LCD_V_RES (280) /* LCD settings */ #define LCD_SPI_NUM (SPI2_HOST) #define LCD_PIXEL_CLK_HZ (40 * 1000 * 1000) #define LCD_CMD_BITS (8) #define LCD_PARAM_BITS (8) #define LCD_COLOR_SPACE (ESP_LCD_COLOR_SPACE_RGB) #define LCD_BITS_PER_PIXEL (16) #define LCD_DRAW_BUFF_HEIGHT (50) /* LCD pins */ #define LCD_GPIO_SCLK (GPIO_NUM_6) #define LCD_GPIO_MOSI (GPIO_NUM_7) #define LCD_GPIO_RST (GPIO_NUM_8) #define LCD_GPIO_DC (GPIO_NUM_4) #define LCD_GPIO_CS (GPIO_NUM_5) #define LCD_GPIO_BL (GPIO_NUM_15) /* System enable GPIO with pullup */ #define SYS_ENABLE_GPIO ((gpio_num_t)41) static const char *TAG = "LVGL_FONT"; /* LCD IO and panel */ static esp_lcd_panel_io_handle_t lcd_io = NULL; static esp_lcd_panel_handle_t lcd_panel = NULL; /* LVGL display */ static lv_display_t *lvgl_disp = NULL; static lv_obj_t *g_lvgl_gif = NULL; static const lv_img_dsc_t g_lvgl_gif_src = { .header.always_zero = 0, .header.w = 240, .header.h = 135, .header.cf = LV_IMG_CF_RAW, .data_size = sizeof(facetestLCD), .data = facetestLCD, }; static esp_err_t app_gif_init_lvgl(lv_obj_t *parent) { g_lvgl_gif = lv_gif_create(parent); lv_gif_set_src(g_lvgl_gif, &g_lvgl_gif_src); lv_obj_align(g_lvgl_gif, LV_ALIGN_CENTER, 0, 0); return ESP_OK; } /* FreeRTOS calls this when CONFIG_FREERTOS_USE_IDLE_HOOK=y */ void vApplicationIdleHook(void) { /* Intentionally empty */ } static void app_gpio_init(void) { gpio_config_t sys_en_config = { .pin_bit_mask = 1ULL << SYS_ENABLE_GPIO, .mode = GPIO_MODE_OUTPUT, .pull_up_en = GPIO_PULLUP_ENABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE, .intr_type = GPIO_INTR_DISABLE, }; ESP_ERROR_CHECK(gpio_config(&sys_en_config)); gpio_set_level(SYS_ENABLE_GPIO, 1); ESP_LOGI(TAG, "System enable GPIO configured"); } static esp_err_t app_lcd_init(void) { esp_err_t ret = ESP_OK; gpio_config_t bk_gpio_config = { .mode = GPIO_MODE_OUTPUT, .pin_bit_mask = 1ULL << LCD_GPIO_BL, }; ESP_ERROR_CHECK(gpio_config(&bk_gpio_config)); ESP_LOGI(TAG, "Initialize SPI bus"); const spi_bus_config_t buscfg = { .sclk_io_num = LCD_GPIO_SCLK, .mosi_io_num = LCD_GPIO_MOSI, .miso_io_num = GPIO_NUM_NC, .quadwp_io_num = GPIO_NUM_NC, .quadhd_io_num = GPIO_NUM_NC, .max_transfer_sz = LCD_H_RES * LCD_DRAW_BUFF_HEIGHT * sizeof(uint16_t), }; ESP_RETURN_ON_ERROR(spi_bus_initialize(LCD_SPI_NUM, &buscfg, SPI_DMA_CH_AUTO), TAG, "SPI init failed"); ESP_LOGI(TAG, "Install panel IO"); const esp_lcd_panel_io_spi_config_t io_config = { .dc_gpio_num = LCD_GPIO_DC, .cs_gpio_num = LCD_GPIO_CS, .pclk_hz = LCD_PIXEL_CLK_HZ, .lcd_cmd_bits = LCD_CMD_BITS, .lcd_param_bits = LCD_PARAM_BITS, .spi_mode = 0, .trans_queue_depth = 10, }; ESP_GOTO_ON_ERROR(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_SPI_NUM, &io_config, &lcd_io), err, TAG, "New panel IO failed"); ESP_LOGI(TAG, "Install LCD driver"); const esp_lcd_panel_dev_config_t panel_config = { .reset_gpio_num = LCD_GPIO_RST, .color_space = LCD_COLOR_SPACE, .bits_per_pixel = LCD_BITS_PER_PIXEL, }; ESP_GOTO_ON_ERROR(esp_lcd_new_panel_st7789(lcd_io, &panel_config, &lcd_panel), err, TAG, "New panel failed"); esp_lcd_panel_reset(lcd_panel); esp_lcd_panel_init(lcd_panel); esp_lcd_panel_mirror(lcd_panel, true, true); esp_lcd_panel_disp_on_off(lcd_panel, true); ESP_ERROR_CHECK(gpio_set_level(LCD_GPIO_BL, 1)); esp_lcd_panel_set_gap(lcd_panel, 0, 20); esp_lcd_panel_invert_color(lcd_panel, true); return ret; err: if (lcd_panel) esp_lcd_panel_del(lcd_panel); if (lcd_io) esp_lcd_panel_io_del(lcd_io); spi_bus_free(LCD_SPI_NUM); return ret; } static esp_err_t app_lvgl_init(void) { const lvgl_port_cfg_t lvgl_cfg = { .task_priority = 4, .task_stack = 8192, .task_affinity = -1, .task_max_sleep_ms = 500, .timer_period_ms = 5, }; ESP_RETURN_ON_ERROR(lvgl_port_init(&lvgl_cfg), TAG, "LVGL port initialization failed"); ESP_LOGI(TAG, "Add LCD screen"); const lvgl_port_display_cfg_t disp_cfg = { .io_handle = lcd_io, .panel_handle = lcd_panel, /* Match the vendor demo sizing expression */ .buffer_size = LCD_H_RES * LCD_DRAW_BUFF_HEIGHT * sizeof(uint16_t), .double_buffer = true, .hres = LCD_H_RES, .vres = LCD_V_RES, .monochrome = false, .rotation = { .swap_xy = false, .mirror_x = false, .mirror_y = false, }, .flags = { .buff_dma = true, }, }; lvgl_disp = lvgl_port_add_disp(&disp_cfg); if (lvgl_disp == NULL) { ESP_LOGE(TAG, "Failed to add display"); return ESP_FAIL; } return ESP_OK; } void app_main(void) { ESP_LOGI(TAG, "Starting LVGL font test"); app_gpio_init(); ESP_ERROR_CHECK(app_lcd_init()); ESP_ERROR_CHECK(app_lvgl_init()); /* Lock LVGL for UI creation */ lvgl_port_lock(0); lv_obj_t *scr = lv_scr_act(); /* Background */ lv_obj_set_style_bg_color(scr, lv_color_hex(0xD6F6CD), LV_PART_MAIN); lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, LV_PART_MAIN); /* Black text samples */ ESP_ERROR_CHECK(app_gif_init_lvgl(scr)); lvgl_port_unlock(); ESP_LOGI(TAG, "LVGL font test running"); }