hello. i have a display tft 240x280 st7789v3 i used nopnop library with lvgl 8 and all was good. sinse i tried to go to lvgl 9 and esp-lcd i faced wrong colours and problems with widgets. Help me please right initialize this display.
#include "lvgl.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_vendor.h"
#include "esp_lcd_panel_ops.h"
#include "driver/spi_master.h"
#include "esp_timer.h"
#include "esp_log.h"
#include "driver/gpio.h"
#define LCD_HOST SPI2_HOST
#define PIN_NUM_MOSI 23
#define PIN_NUM_CLK 18
#define PIN_NUM_CS 5
#define PIN_NUM_DC 27
#define PIN_NUM_RST 33
#define PIN_NUM_BK 32
#define LCD_H_RES 240
#define LCD_V_RES 280
#define LCD_V_OFFSET 20
static const char *TAG = "main";
static lv_display_t *disp;
static esp_lcd_panel_io_handle_t io_handle = NULL;
static esp_lcd_panel_handle_t panel_handle = NULL;
static void flush_cb(lv_display_t *disp, const lv_area_t *area, uint8_t *px_map)
{
esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, px_map);
lv_display_flush_ready(disp);
}
void app_main(void)
{
ESP_LOGI(TAG, "Init LVGL");
lv_init();
static uint8_t *buf1 = NULL;
buf1 = heap_caps_malloc(LCD_H_RES * 40 * sizeof(lv_color_t), MALLOC_CAP_DMA);
assert(buf1);
disp = lv_display_create(LCD_H_RES, LCD_V_RES);
lv_display_set_flush_cb(disp, flush_cb);
lv_display_set_buffers(disp, buf1, NULL, LCD_H_RES * 40 * sizeof(lv_color_t), LV_DISPLAY_RENDER_MODE_PARTIAL);
ESP_LOGI(TAG, "Init backlight");
gpio_config_t bk_gpio_config = {
.mode = GPIO_MODE_OUTPUT,
.pin_bit_mask = 1ULL << PIN_NUM_BK
};
gpio_config(&bk_gpio_config);
gpio_set_level(PIN_NUM_BK, 1);
ESP_LOGI(TAG, "Init SPI panel");
spi_bus_config_t buscfg = {
.sclk_io_num = PIN_NUM_CLK,
.mosi_io_num = PIN_NUM_MOSI,
.miso_io_num = -1,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO));
esp_lcd_panel_io_spi_config_t io_config = {
.dc_gpio_num = PIN_NUM_DC,
.cs_gpio_num = PIN_NUM_CS,
.pclk_hz = 20 * 1000 * 1000,
.lcd_cmd_bits = 8,
.lcd_param_bits = 8,
.spi_mode = 0,
.trans_queue_depth = 10,
.on_color_trans_done = NULL,
.user_ctx = NULL,
};
ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle));
// typedef struct {
// int reset_gpio_num; // GPIO number for the reset pin
// int color_space; // Color space (e.g., RGB, BGR)
// int bits_per_pixel; // Bits per pixel (e.g., 16, 18)
// } lcd_panel_dev_config_t;
esp_lcd_panel_dev_config_t panel_config = {
.reset_gpio_num = PIN_NUM_RST, // GPIO номер для сброса панели
.color_space = ESP_LCD_COLOR_SPACE_RGB, // Цветовое пространство (RGB или BGR)
.bits_per_pixel = 16, // Количество бит на пиксель (например, 32 для RGB888)
};
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);
esp_lcd_panel_invert_color(panel_handle, false);
esp_lcd_panel_mirror(panel_handle, false, false);
esp_lcd_panel_swap_xy(panel_handle, false);
esp_lcd_panel_set_gap(panel_handle, 0, LCD_V_OFFSET);
// Прямое задание MADCTL = 0x00
//esp_lcd_panel_io_tx_param(io_handle, 0x36, (uint8_t[]){0x00}, 1);
esp_lcd_panel_disp_on_off(panel_handle, true);
// Надпись
lv_obj_t *label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "Hello ST7789V3!");
lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 10);
// Кнопка
lv_obj_t *btn = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn, 100, 40);
lv_obj_align(btn, LV_ALIGN_CENTER, 0, 20);
lv_obj_t *lbl2 = lv_label_create(btn);
lv_label_set_text(lbl2, "PRESS");
while (1) {
lv_timer_handler();
vTaskDelay(pdMS_TO_TICKS(5));
}
}
help me please. Thanx in advance. ChatGpt and Search are losing in this battle((