Advertisement
slendi_uwu

Untitled

Mar 11th, 2022
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.57 KB | None | 0 0
  1. /*
  2.  * Connect the SD card to the following pins:
  3.  *
  4.  * SD Card | ESP32
  5.  *    D2       -
  6.  *    D3       SS
  7.  *    CMD      MOSI
  8.  *    VSS      GND
  9.  *    VDD      3.3V
  10.  *    CLK      SCK
  11.  *    VSS      GND
  12.  *    D0       MISO
  13.  *    D1       -
  14.  */
  15. #include "FS.h"
  16. #include "SD.h"
  17. #include "SPI.h"
  18.  
  19. void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
  20.     Serial.printf("Listing directory: %s\n", dirname);
  21.  
  22.     File root = fs.open(dirname);
  23.     if(!root){
  24.         Serial.println("Failed to open directory");
  25.         return;
  26.     }
  27.     if(!root.isDirectory()){
  28.         Serial.println("Not a directory");
  29.         return;
  30.     }
  31.  
  32.     File file = root.openNextFile();
  33.     while(file){
  34.         if(file.isDirectory()){
  35.             Serial.print("  DIR : ");
  36.             Serial.println(file.name());
  37.             if(levels){
  38.                 listDir(fs, file.name(), levels -1);
  39.             }
  40.         } else {
  41.             Serial.print("  FILE: ");
  42.             Serial.print(file.name());
  43.             Serial.print("  SIZE: ");
  44.             Serial.println(file.size());
  45.         }
  46.         file = root.openNextFile();
  47.     }
  48. }
  49.  
  50. void createDir(fs::FS &fs, const char * path){
  51.     Serial.printf("Creating Dir: %s\n", path);
  52.     if(fs.mkdir(path)){
  53.         Serial.println("Dir created");
  54.     } else {
  55.         Serial.println("mkdir failed");
  56.     }
  57. }
  58.  
  59. void removeDir(fs::FS &fs, const char * path){
  60.     Serial.printf("Removing Dir: %s\n", path);
  61.     if(fs.rmdir(path)){
  62.         Serial.println("Dir removed");
  63.     } else {
  64.         Serial.println("rmdir failed");
  65.     }
  66. }
  67.  
  68. void readFile(fs::FS &fs, const char * path){
  69.     Serial.printf("Reading file: %s\n", path);
  70.  
  71.     File file = fs.open(path);
  72.     if(!file){
  73.         Serial.println("Failed to open file for reading");
  74.         return;
  75.     }
  76.  
  77.     Serial.print("Read from file: ");
  78.     while(file.available()){
  79.         Serial.write(file.read());
  80.     }
  81.     file.close();
  82. }
  83.  
  84. void writeFile(fs::FS &fs, const char * path, const char * message){
  85.     Serial.printf("Writing file: %s\n", path);
  86.  
  87.     File file = fs.open(path, FILE_WRITE);
  88.     if(!file){
  89.         Serial.println("Failed to open file for writing");
  90.         return;
  91.     }
  92.     if(file.print(message)){
  93.         Serial.println("File written");
  94.     } else {
  95.         Serial.println("Write failed");
  96.     }
  97.     file.close();
  98. }
  99.  
  100. void appendFile(fs::FS &fs, const char * path, const char * message){
  101.     Serial.printf("Appending to file: %s\n", path);
  102.  
  103.     File file = fs.open(path, FILE_APPEND);
  104.     if(!file){
  105.         Serial.println("Failed to open file for appending");
  106.         return;
  107.     }
  108.     if(file.print(message)){
  109.         Serial.println("Message appended");
  110.     } else {
  111.         Serial.println("Append failed");
  112.     }
  113.     file.close();
  114. }
  115.  
  116. void renameFile(fs::FS &fs, const char * path1, const char * path2){
  117.     Serial.printf("Renaming file %s to %s\n", path1, path2);
  118.     if (fs.rename(path1, path2)) {
  119.         Serial.println("File renamed");
  120.     } else {
  121.         Serial.println("Rename failed");
  122.     }
  123. }
  124.  
  125. void deleteFile(fs::FS &fs, const char * path){
  126.     Serial.printf("Deleting file: %s\n", path);
  127.     if(fs.remove(path)){
  128.         Serial.println("File deleted");
  129.     } else {
  130.         Serial.println("Delete failed");
  131.     }
  132. }
  133.  
  134. void testFileIO(fs::FS &fs, const char * path){
  135.     File file = fs.open(path);
  136.     static uint8_t buf[512];
  137.     size_t len = 0;
  138.     uint32_t start = millis();
  139.     uint32_t end = start;
  140.     if(file){
  141.         len = file.size();
  142.         size_t flen = len;
  143.         start = millis();
  144.         while(len){
  145.             size_t toRead = len;
  146.             if(toRead > 512){
  147.                 toRead = 512;
  148.             }
  149.             file.read(buf, toRead);
  150.             len -= toRead;
  151.         }
  152.         end = millis() - start;
  153.         Serial.printf("%u bytes read for %u ms\n", flen, end);
  154.         file.close();
  155.     } else {
  156.         Serial.println("Failed to open file for reading");
  157.     }
  158.  
  159.  
  160.     file = fs.open(path, FILE_WRITE);
  161.     if(!file){
  162.         Serial.println("Failed to open file for writing");
  163.         return;
  164.     }
  165.  
  166.     size_t i;
  167.     start = millis();
  168.     for(i=0; i<2048; i++){
  169.         file.write(buf, 512);
  170.     }
  171.     end = millis() - start;
  172.     Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
  173.     file.close();
  174. }
  175.  
  176. void setup(){
  177.     Serial.begin(115200);
  178.     if(!SD.begin()){
  179.         Serial.println("Card Mount Failed");
  180.         return;
  181.     }
  182.     uint8_t cardType = SD.cardType();
  183.  
  184.     if(cardType == CARD_NONE){
  185.         Serial.println("No SD card attached");
  186.         return;
  187.     }
  188.  
  189.     Serial.print("SD Card Type: ");
  190.     if(cardType == CARD_MMC){
  191.         Serial.println("MMC");
  192.     } else if(cardType == CARD_SD){
  193.         Serial.println("SDSC");
  194.     } else if(cardType == CARD_SDHC){
  195.         Serial.println("SDHC");
  196.     } else {
  197.         Serial.println("UNKNOWN");
  198.     }
  199.  
  200.     uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  201.     Serial.printf("SD Card Size: %lluMB\n", cardSize);
  202.  
  203.     listDir(SD, "/", 0);
  204.     createDir(SD, "/mydir");
  205.     listDir(SD, "/", 0);
  206.     removeDir(SD, "/mydir");
  207.     listDir(SD, "/", 2);
  208.     writeFile(SD, "/hello.txt", "Hello ");
  209.     appendFile(SD, "/hello.txt", "World!\n");
  210.     readFile(SD, "/hello.txt");
  211.     deleteFile(SD, "/foo.txt");
  212.     renameFile(SD, "/hello.txt", "/foo.txt");
  213.     readFile(SD, "/foo.txt");
  214.     testFileIO(SD, "/test.txt");
  215.     Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
  216.     Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
  217. }
  218.  
  219. void loop(){
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement