Advertisement
RuiViana

List_SPIFSS.ino

May 8th, 2019
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.30 KB | None | 0 0
  1. #include "FS.h"
  2.  
  3. #include "SPIFFS.h"
  4.  
  5. void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  6.   Serial.printf("Listing directory: %s\n", dirname);
  7.  
  8.   File root = fs.open(dirname);
  9.   if (!root) {
  10.     Serial.println("Failed to open directory");
  11.     return;
  12.   }
  13.   if (!root.isDirectory()) {
  14.     Serial.println("Not a directory");
  15.     return;
  16.   }
  17.  
  18.   File file = root.openNextFile();
  19.   while (file) {
  20.     if (file.isDirectory()) {
  21.       Serial.print("  DIR : ");
  22.       Serial.println(file.name());
  23.       if (levels) {
  24.         listDir(fs, file.name(), levels - 1);
  25.       }
  26.     } else {
  27.       Serial.print("  FILE: ");
  28.       Serial.print(file.name());
  29.       Serial.print("  SIZE: ");
  30.       Serial.println(file.size());
  31.     }
  32.     file = root.openNextFile();
  33.   }
  34. }
  35.  
  36. void readFile(fs::FS &fs, const char * path) {
  37.   Serial.printf("Reading file: %s\n", path);
  38.  
  39.   File file = fs.open(path);
  40.   if (!file || file.isDirectory()) {
  41.     Serial.println("Failed to open file for reading");
  42.     return;
  43.   }
  44.  
  45.   Serial.print("Read from file: ");
  46.   while (file.available()) {
  47.     Serial.write(file.read());
  48.   }
  49. }
  50.  
  51. void writeFile(fs::FS &fs, const char * path, const char * message) {
  52.   Serial.printf("Writing file: %s\n", path);
  53.  
  54.   File file = fs.open(path, FILE_WRITE);
  55.   if (!file) {
  56.     Serial.println("Failed to open file for writing");
  57.     return;
  58.   }
  59.   if (file.print(message)) {
  60.     Serial.println("File written");
  61.   } else {
  62.     Serial.println("Write failed");
  63.   }
  64. }
  65.  
  66. void appendFile(fs::FS &fs, const char * path, const char * message) {
  67.   Serial.printf("Appending to file: %s\n", path);
  68.  
  69.   File file = fs.open(path, FILE_APPEND);
  70.   if (!file) {
  71.     Serial.println("Failed to open file for appending");
  72.     return;
  73.   }
  74.   if (file.print(message)) {
  75.     Serial.println("Message appended");
  76.   } else {
  77.     Serial.println("Append failed");
  78.   }
  79. }
  80.  
  81. void renameFile(fs::FS &fs, const char * path1, const char * path2) {
  82.   Serial.printf("Renaming file %s to %s\n", path1, path2);
  83.   if (fs.rename(path1, path2)) {
  84.     Serial.println("File renamed");
  85.   } else {
  86.     Serial.println("Rename failed");
  87.   }
  88. }
  89.  
  90. void deleteFile(fs::FS &fs, const char * path) {
  91.   Serial.printf("Deleting file: %s\n", path);
  92.   if (fs.remove(path)) {
  93.     Serial.println("File deleted");
  94.   } else {
  95.     Serial.println("Delete failed");
  96.   }
  97. }
  98.  
  99. void testFileIO(fs::FS &fs, const char * path) {
  100.   File file = fs.open(path);
  101.   static uint8_t buf[512];
  102.   size_t len = 0;
  103.   uint32_t start = millis();
  104.   uint32_t end = start;
  105.   if (file && !file.isDirectory()) {
  106.     len = file.size();
  107.     size_t flen = len;
  108.     start = millis();
  109.     while (len) {
  110.       size_t toRead = len;
  111.       if (toRead > 512) {
  112.         toRead = 512;
  113.       }
  114.       file.read(buf, toRead);
  115.       len -= toRead;
  116.     }
  117.     end = millis() - start;
  118.     Serial.printf("%u bytes read for %u ms\n", flen, end);
  119.     file.close();
  120.   } else {
  121.     Serial.println("Failed to open file for reading");
  122.   }
  123.  
  124.  
  125.   file = fs.open(path, FILE_WRITE);
  126.   if (!file) {
  127.     Serial.println("Failed to open file for writing");
  128.     return;
  129.   }
  130.  
  131.   size_t i;
  132.   start = millis();
  133.   for (i = 0; i < 2048; i++) {
  134.     file.write(buf, 512);
  135.   }
  136.   end = millis() - start;
  137.   Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
  138.   file.close();
  139. }
  140.  
  141. void setup() {
  142.   Serial.begin(115200);
  143.   if (!SPIFFS.begin(true)) {
  144.     Serial.println("SPIFFS Mount Failed");
  145.     return;
  146.   }
  147.  
  148.   listDir(SPIFFS, "/", 0);
  149.   //writeFile(SPIFFS, "/xpto.txt", "Hello ");
  150.   //writeFile(SPIFFS, "/xpto.txt","{\"ip":\"192.168.0.61\",\"gateway\":\"192.168.0.1\",\"subnet":\"255.255.255.0\"}");
  151.   //writeFile(SPIFFS, "/config.json", "{\"ip":\"192.168.0.61\",\"gateway\":\"192.168.0.1\",\"subnet":\"255.255.255.0\"}");
  152.   //  appendFile(SPIFFS, "/hello.txt", "World!\n");
  153.   readFile(SPIFFS, "/config.json");
  154.   //readFile(SPIFFS, "/javascript1.js");
  155.   //readFile(SPIFFS, "/test.html");
  156.   //deleteFile(SPIFFS, "/estilo.css");
  157.   //deleteFile(SPIFFS, "/javascript1.js");
  158.   //deleteFile(SPIFFS, "/jquery-3.3.1.min.js");
  159.   //deleteFile(SPIFFS, "/test.html");
  160.   // deleteFile(SPIFFS, "/config.json");
  161.   // renameFile(SPIFFS, "/config.json", "/foo.txt");
  162.   //  readFile(SPIFFS, "/foo.txt");
  163.   //  testFileIO(SPIFFS, "/test.txt");
  164.   //listDir(SPIFFS, "/", 0);
  165. }
  166.  
  167. void loop() {
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement