From 71d4bf7b587da30f0588955a191f6625e1630969 Mon Sep 17 00:00:00 2001 From: patri Date: Fri, 23 Oct 2026 01:43:31 +0200 Subject: [PATCH] display_gif_sd: load GIF from SD into PSRAM once, play from memory (9.8 -> 15.4 fps) Also enable a 16 KB LVGL stdio read cache (no measurable effect for the GIF). Co-Authored-By: Claude Opus 5.5 --- .../display_gif_sd/src/main.cpp | 24 ++++++++++++++----- shared/include/lv_conf.h | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/10-OBD-Display-2024/display_gif_sd/src/main.cpp b/10-OBD-Display-2024/display_gif_sd/src/main.cpp index 7ed6b73..54c716f 100644 --- a/10-OBD-Display-2024/display_gif_sd/src/main.cpp +++ b/10-OBD-Display-2024/display_gif_sd/src/main.cpp @@ -22,14 +22,26 @@ void display_animation() { lv_mem_monitor(&mem_mon); printf("Free memory: %d bytes\n", mem_mon.free_size); - lv_fs_file_t file; - if (lv_fs_open(&file, "A:wallpaper.gif", LV_FS_MODE_RD) == LV_FS_RES_OK) { - lv_fs_close(&file); // File exists, proceed with loading - lv_gif_set_src(img, "A:wallpaper.gif"); - Serial.println("GIF Loaded Successfully"); + // Load the whole GIF from the SD card into PSRAM once and play it from memory: + // no SD access while playing (faster, and kinder to flaky cards) + static lv_image_dsc_t gif_dsc; + File f = SD_MMC.open("/wallpaper.gif"); + size_t size = f ? f.size() : 0; + uint8_t* data = size ? (uint8_t*)ps_malloc(size) : NULL; + if (data && f.read(data, size) == size) { + gif_dsc.header.magic = LV_IMAGE_HEADER_MAGIC; + gif_dsc.header.cf = LV_COLOR_FORMAT_RAW; + gif_dsc.header.w = data[6] | (data[7] << 8); // GIF logical screen size + gif_dsc.header.h = data[8] | (data[9] << 8); + gif_dsc.data_size = size; + gif_dsc.data = data; + lv_gif_set_src(img, &gif_dsc); + Serial.printf("GIF loaded into PSRAM: %u bytes, %u x %u\n", size, gif_dsc.header.w, gif_dsc.header.h); } else { - Serial.println("Failed to Load GIF: File not found or inaccessible"); + Serial.println("Failed to load /wallpaper.gif from the SD card"); + free(data); } + if (f) f.close(); Serial.print("Available Heap after: "); Serial.println(esp_get_free_heap_size()); diff --git a/shared/include/lv_conf.h b/shared/include/lv_conf.h index 479819f..0844d69 100644 --- a/shared/include/lv_conf.h +++ b/shared/include/lv_conf.h @@ -1967,7 +1967,7 @@ #define LV_FS_STDIO_PATH "/sdcard/" /** Read cache size in bytes for stdio (0 = disabled) */ -#define LV_FS_STDIO_CACHE_SIZE 0 +#define LV_FS_STDIO_CACHE_SIZE 16384 /* JC3636W518: fewer small SD reads (GIF decoding) */ #endif /*LV_USE_FS_STDIO*/