My friend, I achieved it step by step.
- Make sure your SD card is working correctly.
- Ensure that fs, LV_USE_PNG, and LV_USE_SJPG configurations are enabled in lv_conf.h.
- Check if fs can read files correctly. (Confirm that the fs system is functioning properly)
- Check if lvgl can display image arrays. (Confirm that your screen and lvgl are working fine)
- Try displaying a smaller PNG image from the SD card. I used a size of 32x32.
- Attempt to open and display a JPG image.
I have switched to LVGL9 and encountered many problems as well. Below is my code snippet for testing. It can display PNG images from the SD card, but not JPG images.
My problem.
#include <Arduino.h>
#include <SPI.h>
#include <lvgl.h>
#include <TFT_eSPI.h>
#include "SD.h"
#include "FS.h"
static TFT_eSPI tft;
static lv_display_t *disp_1;
static uint8_t draw_buf_1[320 * 240 / 10];
static lv_obj_t *ui = nullptr;
static ulong lvgl_tick_millis = millis();
void display_flush(lv_display_t *disp, const lv_area_t *area, uint8_t *px_map)
{
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
tft.startWrite();
tft.setAddrWindow(area->x1, area->y1, w, h);
tft.pushColors((uint16_t *)px_map, w * h, true);
tft.endWrite();
lv_display_flush_ready(disp);
}
void setup()
{
delay(3000);
Serial.begin(115200);
delay(100);
Serial.println("Initializing...");
SPI.begin(12, 13, 11);
if (!SD.begin(10, SPI, 27000000))
{
Serial.println("SD card failed!");
}
else
{
Serial.println("SD card success!");
}
ledcSetup(4, 5000, 8);
ledcAttachPin(3, 4);
ledcWrite(4, 20);
tft.init();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK);
lv_init();
lv_fs_fatfs_init();
disp_1 = lv_display_create(320, 240);
lv_display_set_flush_cb(disp_1, display_flush);
lv_display_set_buffers(disp_1, draw_buf_1, NULL, sizeof(draw_buf_1), LV_DISPLAY_RENDER_MODE_PARTIAL);
ui = lv_obj_create(NULL);
lv_obj_set_size(ui, 320, 240);
lv_obj_set_style_bg_opa(ui, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_bg_color(ui, lv_color_hex(0x0e0e0e), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_screen_load(ui);
Serial.println("lv_image_create img_1");
lv_obj_t *img_1 = lv_image_create(ui);
lv_obj_set_size(img_1, 32, 32);
lv_obj_set_pos(img_1, 130, 70);
lv_image_set_src(img_1, "S:/images/clock-min.png");
Serial.println("lv_image_create img_2");
lv_obj_t *img_2 = lv_image_create(ui);
lv_obj_set_size(img_2, 105, 33);
lv_obj_set_pos(img_2, 0, 0);
lv_image_set_src(img_2, "S:/images/wallpaper/img_lvgl_logo.jpg");
delay(1000);
Serial.println("Refresh the screen.");
lv_obj_invalidate(lv_screen_active());
}
void loop()
{
lv_timer_handler();
unsigned long tick_millis = millis() - lvgl_tick_millis;
lvgl_tick_millis = millis();
lv_tick_inc(tick_millis);
yield();
delay(5);
}