Load chart from SD-CARD

Description

I want to load chart from SD-Card.

What MCU/Processor/Board and compiler are you using?

ESP32 WROVER
SD-Card

What LVGL version are you using?

v7.5.4
ESP IDF v5

What do you want to achieve?

What have you tried so far?

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:

I use list_add_btn for file selecting, and when i scroll down and pick the bottom of the list I got this error. This file has 29 lines. But when i pick the top and select larger file it works greate.

I (19254) sdcard_history: 29
I (19298) MEM: Memory: all 115668 byte, free 114408 byte
Warn: Couldn't allocate memory. Size: 800       (lv_mem.c #208 lv_mem_alloc())
Warn: Couldn't allocate memory  (lv_mem.c #323 lv_mem_realloc())
Error: _lv_mem_buf_get  (lv_mem.c #533 _lv_mem_buf_get())
Error: Out of memory, can't allocate a new  buffer (increase your LV_MEM_SIZE/heap size (0x00000000)         (lv_debug.c #127 lv_debug_log_error())

SD-Card load:

void readFile(char * name){
	sprintf(path, "/S/%s", name);
    FILE *r_file = fopen(path, "r");
    if (r_file == NULL) {
    	ESP_LOGE("File read"," %s %d", path, strlen(path));
    	errorWin(TAGGG, "Failed to open file for reading");
        ESP_LOGE(TAGGG, "Failed to open file for reading");
        return;
    }
    char buf[100];
    char buf_szam;
    int time = 0;
    float chart_value;

    while (fgets(buf, sizeof(buf), r_file)) {
        sscanf(buf, "%d s: %f", &time, &chart_value);
    }

    ESP_LOGI("read", "%d sor van.", time);

    lv_obj_clean(scrChart);
    createscrHistoryChart(name,time);
    ESP_LOGI("sdcard_history", "%d", time);

    rewind(r_file);

    while (fgets(buf, sizeof(buf), r_file)) {
        sscanf(buf, "%d s: %f", &time, &chart_value);
        historydrawChart(chart_value, time);
        lv_task_handler();
    }
    lv_scr_load(scrChart);
    fclose(r_file);
    size_t total_heap = esp_get_free_heap_size();
    size_t free_heap = esp_get_minimum_free_heap_size();
    ESP_LOGI("MEM", "Memory: all %d byte, free %d byte", total_heap, free_heap);
}

File choiser:

void createscrHistory(){
	DIR* dir = opendir("/S/");
	if (dir == NULL) {
	    return;
	}

    lv_obj_t *file_list = lv_list_create(scrHistory, NULL);
    lv_obj_set_size(file_list, DISP_RES_X, DISP_RES_Y/1.3);
    lv_obj_set_pos(file_list, 0, 0);



	while (true) {
	    struct dirent* de = readdir(dir);
	    if (!de) {
	        break;
	    }
	    if(strstr(de->d_name, "_meres.txt")){
	    	//printf("Found file: %s\n", de->d_name);
            lv_obj_t *btn = lv_list_add_btn(file_list, LV_SYMBOL_FILE, de->d_name);
            lv_obj_set_event_cb(btn, event_handler_list);
	    }

	}
	closedir(dir);

    lv_obj_t * labelbtn;
    exitBtn = lv_btn_create(scrHistory, NULL);
    lv_obj_align(exitBtn, NULL, LV_ALIGN_CENTER, +DISP_RES_X/3, +DISP_RES_Y/3);
    lv_obj_set_size(exitBtn, DISP_RES_X/4.3, DISP_RES_Y/7);
    labelbtn = lv_label_create(exitBtn, NULL);
    lv_label_set_text(labelbtn, "Exit");
    lv_obj_set_event_cb( exitBtn , event_handler_exit);
    lv_scr_load(scrHistory);

}

Hi @martin023 ,

In your code you are checking the ESP memory (Please note I am not familiar with the processor or toolset) I would suggest checking the LVGL memory instead as it may need to be increased slightly. The LVGL memory size is normally changed in the lv_conf.h file:

/* Size of the memory used by `lv_mem_alloc` in bytes (>= 2kB)*/
#  define LV_MEM_SIZE    (64U * 1024U)

To check the LVGL memory during run time you should be able to use this code:

  lv_mem_monitor_t mon;
  lv_mem_monitor(&mon);
  ESP_LOGI("MEM", "Memory:  used: %6d (%3d %%), frag: %3d %%, biggest free: %6d",
         (int)mon.total_size - mon.free_size, mon.used_pct, mon.frag_pct,
         (int)mon.free_biggest_size);

If you find there is enough memory returned by the LVGL memory check functions. I would suspect you have a stack overflow or some other memory corruption issue. We might need to see a bit more of your code to help diagnose that sort of problem…

I hope that helps.

Kind Regards,

Pete

1 Like