Skip to content

Update SD readFs #229

Description

@geo-tp
/***************************************************************************************
** Function name: sortList
** Description:   sort files for name
***************************************************************************************/
void readFs(String &folder, std::vector<Option> &opt) {
    // function using loopOptions
    opt.clear();
    if (!setupSdCard()) {
        // Serial.println("Falha ao iniciar o cartão SD");
        displayRedStripe("SD not found or not formatted in FAT32");
        vTaskDelay(2500 / portTICK_PERIOD_MS);
        return; // Retornar imediatamente em caso de falha
    }
    File root = SDM.open(folder);
    if (!root || !root.isDirectory()) {
        displayRedStripe("Fail open root");
        vTaskDelay(2500 / portTICK_PERIOD_MS);
        return; // Retornar imediatamente se não for possível abrir o diretório
    }
    while (true) {
        File file = root.openNextFile();
        if (!file) { break; }

        String fileName = file.name();
        const bool isDir = file.isDirectory();
        uint16_t color = FGCOLOR - 0x1111;

        if (!isDir) {
            int dotIndex = fileName.lastIndexOf(".");
            String ext = dotIndex >= 0 ? fileName.substring(dotIndex + 1) : "";
            ext.toUpperCase();
            if (onlyBins && !ext.equals("BIN")) {
                file.close();
                continue;
            }
            color = FGCOLOR;
        }

        String label = fileName.substring(fileName.lastIndexOf("/") + 1);
        String path = file.path();
        opt.push_back({label, [path]() { fileToUse = path; }, color});

        file.close();
    }
    root.close();
    std::sort(opt.begin(), opt.end(), sortList);
    opt.push_back({"> Back", [&]() { fileToUse = ""; }, ALCOLOR});
}

Using openNextFile essentially opens and reads file info, which heavily slows down directory listing. By using getNextFileName instead, you drastically reduce SD card I/O, it’s roughly 100× faster for listing items

Which means that even if the user has 1000 files or folders at the root of the SD card, it will load almost instantly

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions