Description
How to load png image from SD Card?
What MCU/Processor/Board and compiler are you using?
ESP32 S3 - Waveshare 7 Inch display
What LVGL version are you using?
8.3.11
What do you want to achieve?
Load images from SD Card to recude RAM/Flash Usage.
What have you tried so far?
I tried many many ways… At this moment I mounted correctly SD card on start application. In button press event function i have function to read png files.
Code to reproduce
Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.
The code block(s) should be formatted like:
#include "ui.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#include "sd_test_io.h"
#include "dirent.h"
int counter = 1;
#define MOUNT_POINT "/sdcard"
static const char *SDTAG = "SDLOG";\
void list_files_on_sd_card2() {
DIR *dir = opendir(MOUNT_POINT); // open main folder on SD Card
if (dir == NULL) {
ESP_LOGE(SDTAG, "Failed to open directory");
return;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
ESP_LOGI(SDTAG, "Found file: %s", entry->d_name); // Write full file path
}
closedir(dir); // close folder
}
static esp_err_t read_binary_file(const char *path) {
ESP_LOGI(SDTAG, "Reading binary file: %s", path);
size_t free_psram = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
ESP_LOGI("PSRAM", "1 PSRAM Free Heap: %d, w MB: %.2f", free_psram, (float)free_psram / 1048576);
FILE *f = fopen(path, "rb");
if (f == NULL) {
ESP_LOGE(SDTAG, "Failed to open file for reading");
return ESP_FAIL;
}
// check png file size
fseek(f, 0, SEEK_END); // Przesuń wskaźnik na koniec pliku
long file_size = ftell(f); // Zapisz rozmiar pliku
fseek(f, 0, SEEK_SET); // Przesuń wskaźnik z powrotem na początek pliku
ESP_LOGI(SDTAG, "File size: %ld bytes", file_size);
uint8_t *buffer = (uint8_t *)malloc(file_size);
if (buffer == NULL) {
ESP_LOGE(SDTAG, "Failed to allocate memory for image");
fclose(f);
return ESP_ERR_NO_MEM;
}
free_psram = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
ESP_LOGI("PSRAM", "2 PSRAM Free Heap: %d, w MB: %.2f", free_psram, (float)free_psram / 1048576);
/*size_t bytes_read;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), f)) > 0) {
ESP_LOGI(SDTAG, "Read %d bytes", bytes_read);
// Przetwórz dane w buforze
}*/
// read png file to buffer
size_t bytes_read = fread(buffer, 1, file_size, f);
if (bytes_read != file_size) {
ESP_LOGE(SDTAG, "Failed to read entire file");
free(buffer);
fclose(f);
return ESP_FAIL;
}
fclose(f);
// image struct of lvgl
lv_img_dsc_t img_dsc = {0};
img_dsc.data = buffer;
img_dsc.header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA;
img_dsc.header.always_zero = 0;
img_dsc.header.w = 0;
img_dsc.header.h = 0;
lv_img_set_src(ui_Image1, &img_dsc); // z tym 5.22
free(buffer);
ESP_LOGI(SDTAG, "PNG image loaded successfully");
return ESP_OK;
}
void changeIMG(lv_event_t * e)
{
counter++;
size_t free_psram = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
ESP_LOGI("PSRAM", " Przed PSRAM Free Heap: %d, w MB: %.2f", free_psram, (float)free_psram / 1048576);
//list_files_on_sd_card2();
//read_binary_file("/sdcard/001.png");
if(counter > 4){
counter = 1;
}
switch(counter){
case 1:
lv_img_set_src(ui_Image1, &ui_img_1_png); // z tym 5.66
ESP_LOGI("IMG1", "buffer: %d", &ui_img_1_png.data);
break;
case 2:
//lv_img_set_src(ui_Image1, &ui_img_2_png); // z tym 5.22
read_binary_file("/sdcard/001.png");
break;
case 3:
//lv_img_set_src(ui_Image1, &ui_img_3_png); // z tym 4.72
read_binary_file("/sdcard/002.png");
break;
case 4:
read_binary_file("/sdcard/003.png");
//lv_img_set_src(ui_Image1, &ui_img_4_png); // z tym 4.28
break;
}
ESP_LOGI("PSRAM", " Po PSRAM Free Heap: %d, w MB: %.2f", free_psram, (float)free_psram / 1048576);
}
Screenshot and/or video
If possible, add screenshots and/or videos about the current state.