2020年8月14日 星期五

[ESP32-Cam] Micro SD 記憶卡的檔案操作

 ESP32-Cam 板載 Micro SD 記憶卡既然擁有可以存取檔案的功能,那麼如何對資料夾和檔案的建立/列示/修改/刪除/更名...等操作,就是我們必學的課題。

底下程式把這些課題都演練了一遍,您可以先把程式上傳到板子,然後打開「序列埠監視視窗」,仔細觀察整個程式運作過程。以下為您說明視窗內的訊息,請注意反白部分。

首先是列示 SD 卡的性質


列出根目錄下的所有資料夾和檔案


新建一個 <mydir> 資料夾 > 列示根目錄狀況 > 移除資料夾 > 再次列示根目錄狀況


新建一個叫 hello.txt 的檔案 > 將字串附加到檔案 > 讀取並印出檔案內容

我想有必要為上面的過程再詳加說明一下...

首先,它新建一個叫 hello.txt 的檔案 ,同時寫入 "Hello " 字串,

然後將字串 "World!\n" 附加到檔案裡,這個 "\n" 就是換行,

最後讀取並印出檔案內容 "Hello Wolrd!"。


訊息窗內都是一些簡單的英文,請您自行閱讀,我們的介紹就到這裡了。


程式碼

#include "FS.h" 

#include "SD_MMC.h" 


//List dir in SD card

void listDir(fs::FS &fs, const char * dirname, uint8_t levels){

    Serial.printf("Listing directory: %s\n", dirname);


    File root = fs.open(dirname);

    if(!root){

        Serial.println("Failed to open directory");

        return;

    }

    if(!root.isDirectory()){

        Serial.println("Not a directory");

        return;

    }


    File file = root.openNextFile();

    while(file){

        if(file.isDirectory()){

            Serial.print("  DIR : ");

            Serial.println(file.name());

            if(levels){

                listDir(fs, file.name(), levels -1);

            }

        } else {

            Serial.print("  FILE: ");

            Serial.print(file.name());

            Serial.print("  SIZE: ");

            Serial.println(file.size());

        }

        file = root.openNextFile();

    }

}


//Create a dir in SD card

void createDir(fs::FS &fs, const char * path){

    Serial.printf("Creating Dir: %s\n", path);

    if(fs.mkdir(path)){

        Serial.println("Dir created");

    } else {

        Serial.println("mkdir failed");

    }

}


//delete a dir in SD card

void removeDir(fs::FS &fs, const char * path){

    Serial.printf("Removing Dir: %s\n", path);

    if(fs.rmdir(path)){

        Serial.println("Dir removed");

    } else {

        Serial.println("rmdir failed");

    }

}


//Read a file in SD card

void readFile(fs::FS &fs, const char * path){

    Serial.printf("Reading file: %s\n", path);


    File file = fs.open(path);

    if(!file){

        Serial.println("Failed to open file for reading");

        return;

    }


    Serial.print("Read from file: ");

    while(file.available()){

        Serial.write(file.read());

    }

}


//Write a file in SD card

void writeFile(fs::FS &fs, const char * path, const char * message){

    Serial.printf("Writing file: %s\n", path);


    File file = fs.open(path, FILE_WRITE);

    if(!file){

        Serial.println("Failed to open file for writing");

        return;

    }

   

 

   //fwrite(fb->buf, 1, fb->len, file);

    if(file.print(message)){

        Serial.println("File written");

    } else {

        Serial.println("Write failed");

    }

}


//Append to the end of file in SD card

void appendFile(fs::FS &fs, const char * path, const char * message){

    Serial.printf("Appending to file: %s\n", path);


    File file = fs.open(path, FILE_APPEND);

    if(!file){

        Serial.println("Failed to open file for appending");

        return;

    }

    if(file.print(message)){

        Serial.println("Message appended");

    } else {

        Serial.println("Append failed");

    }

}


//Rename a file in SD card

void renameFile(fs::FS &fs, const char * path1, const char * path2){

    Serial.printf("Renaming file %s to %s\n", path1, path2);

    if (fs.rename(path1, path2)) {

        Serial.println("File renamed");

    } else {

        Serial.println("Rename failed");

    }

}


//Delete a file in SD card

void deleteFile(fs::FS &fs, const char * path){

    Serial.printf("Deleting file: %s\n", path);

    if(fs.remove(path)){

        Serial.println("File deleted");

    } else {

        Serial.println("Delete failed");

    }

}


//Test read and write speed using test.txt file

void testFileIO(fs::FS &fs, const char * path){

    File file = fs.open(path);

    static uint8_t buf[512];

    size_t len = 0;

    uint32_t start = millis();

    uint32_t end = start;

    if(file){

        len = file.size();

        size_t flen = len;

        start = millis();

        while(len){

            size_t toRead = len;

            if(toRead > 512){

                toRead = 512;

            }

            file.read(buf, toRead);

            len -= toRead;

        }

        end = millis() - start;

        Serial.printf("%u bytes read for %u ms\n", flen, end);

        file.close();

    } else {

        Serial.println("Failed to open file for reading");

    }



    file = fs.open(path, FILE_WRITE);

    if(!file){

        Serial.println("Failed to open file for writing");

        return;

    }


    size_t i;

    start = millis();

    for(i=0; i<2048; i++){

        file.write(buf, 512);

    }

    end = millis() - start;

    Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);

    file.close();

}



void setup() {  

  Serial.begin(115200);

  Serial.println("SDcard Testing....");


   if(!SD_MMC.begin()){

        Serial.println("Card Mount Failed");

        return;

    }

    uint8_t cardType = SD_MMC.cardType();


    if(cardType == CARD_NONE){

        Serial.println("No SD_MMC card attached");

        return;

    }


    Serial.print("SD_MMC Card Type: ");

    if(cardType == CARD_MMC){

        Serial.println("MMC");

    } else if(cardType == CARD_SD){

        Serial.println("SDSC");

    } else if(cardType == CARD_SDHC){

        Serial.println("SDHC");

    } else {

        Serial.println("UNKNOWN");

    }


    uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);

    Serial.printf("SD_MMC Card Size: %lluMB\n", cardSize);


    listDir(SD_MMC, "/", 0);

    createDir(SD_MMC, "/mydir");

    listDir(SD_MMC, "/", 0);

    removeDir(SD_MMC, "/mydir");

    listDir(SD_MMC, "/", 2);

    writeFile(SD_MMC, "/hello.txt", "Hello ");

    appendFile(SD_MMC, "/hello.txt", "World!\n");

    readFile(SD_MMC, "/hello.txt");

    deleteFile(SD_MMC, "/foo.txt");

    renameFile(SD_MMC, "/hello.txt", "/foo.txt");

    readFile(SD_MMC, "/foo.txt");

    testFileIO(SD_MMC, "/test.txt");

    Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));

    Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));  

  

}


void loop() {

  // put your main code here, to run repeatedly:

  delay(10000);

}


採購資訊

Micro SD 4G 記憶卡 https://www.ruten.com.tw/item/show?22033106394834

ESP32 開發指南(書) https://www.ruten.com.tw/item/show?22023657661505

ESP32-Cam 開發板 https://www.ruten.com.tw/item/show?22018501441929

ESP32-Cam 雙軸雲台(基本款)套件 https://www.ruten.com.tw/item/show?22024688065979

ESP32-Cam 雙軸雲台(人體偵測款)套件 https://www.ruten.com.tw/item/show?22024688076465





沒有留言:

張貼留言