|
| 1 | +#include <Arduino.h> |
| 2 | +#include "FS.h" |
| 3 | +#include <LITTLEFS.h> |
| 4 | + |
| 5 | +/* You only need to format SPIFFS the first time you run a |
| 6 | + test or else use the LITTLEFS plugin to create a partition |
| 7 | + https://github.com/me-no-dev/arduino-esp32fs-plugin */ |
| 8 | +#define FORMAT_LITTLEFS_IF_FAILED true |
| 9 | + |
| 10 | +void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ |
| 11 | + Serial.printf("Listing directory: %s\r\n", dirname); |
| 12 | + |
| 13 | + File root = fs.open(dirname); |
| 14 | + if(!root){ |
| 15 | + Serial.println("- failed to open directory"); |
| 16 | + return; |
| 17 | + } |
| 18 | + if(!root.isDirectory()){ |
| 19 | + Serial.println(" - not a directory"); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + File file = root.openNextFile(); |
| 24 | + while(file){ |
| 25 | + if(file.isDirectory()){ |
| 26 | + Serial.print(" DIR : "); |
| 27 | + Serial.println(file.name()); |
| 28 | + if(levels){ |
| 29 | + listDir(fs, file.name(), levels -1); |
| 30 | + } |
| 31 | + } else { |
| 32 | + Serial.print(" FILE: "); |
| 33 | + Serial.print(file.name()); |
| 34 | + Serial.print("\tSIZE: "); |
| 35 | + Serial.println(file.size()); |
| 36 | + } |
| 37 | + file = root.openNextFile(); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +void readFile(fs::FS &fs, const char * path){ |
| 42 | + Serial.printf("Reading file: %s\r\n", path); |
| 43 | + |
| 44 | + File file = fs.open(path); |
| 45 | + if(!file || file.isDirectory()){ |
| 46 | + Serial.println("- failed to open file for reading"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + Serial.println("- read from file:"); |
| 51 | + while(file.available()){ |
| 52 | + Serial.write(file.read()); |
| 53 | + } |
| 54 | + file.close(); |
| 55 | +} |
| 56 | + |
| 57 | +void writeFile(fs::FS &fs, const char * path, const char * message){ |
| 58 | + Serial.printf("Writing file: %s\r\n", path); |
| 59 | + |
| 60 | + File file = fs.open(path, FILE_WRITE); |
| 61 | + if(!file){ |
| 62 | + Serial.println("- failed to open file for writing"); |
| 63 | + return; |
| 64 | + } |
| 65 | + if(file.print(message)){ |
| 66 | + Serial.println("- file written"); |
| 67 | + } else { |
| 68 | + Serial.println("- write failed"); |
| 69 | + } |
| 70 | + file.close(); |
| 71 | +} |
| 72 | + |
| 73 | +void appendFile(fs::FS &fs, const char * path, const char * message){ |
| 74 | + Serial.printf("Appending to file: %s\r\n", path); |
| 75 | + |
| 76 | + File file = fs.open(path, FILE_APPEND); |
| 77 | + if(!file){ |
| 78 | + Serial.println("- failed to open file for appending"); |
| 79 | + return; |
| 80 | + } |
| 81 | + if(file.print(message)){ |
| 82 | + Serial.println("- message appended"); |
| 83 | + } else { |
| 84 | + Serial.println("- append failed"); |
| 85 | + } |
| 86 | + file.close(); |
| 87 | +} |
| 88 | + |
| 89 | +void renameFile(fs::FS &fs, const char * path1, const char * path2){ |
| 90 | + Serial.printf("Renaming file %s to %s\r\n", path1, path2); |
| 91 | + if (fs.rename(path1, path2)) { |
| 92 | + Serial.println("- file renamed"); |
| 93 | + } else { |
| 94 | + Serial.println("- rename failed"); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void deleteFile(fs::FS &fs, const char * path){ |
| 99 | + Serial.printf("Deleting file: %s\r\n", path); |
| 100 | + if(fs.remove(path)){ |
| 101 | + Serial.println("- file deleted"); |
| 102 | + } else { |
| 103 | + Serial.println("- delete failed"); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void testFileIO(fs::FS &fs, const char * path){ |
| 108 | + Serial.printf("Testing file I/O with %s\r\n", path); |
| 109 | + |
| 110 | + static uint8_t buf[512]; |
| 111 | + size_t len = 0; |
| 112 | + File file = fs.open(path, FILE_WRITE); |
| 113 | + if(!file){ |
| 114 | + Serial.println("- failed to open file for writing"); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + size_t i; |
| 119 | + Serial.print("- writing" ); |
| 120 | + uint32_t start = millis(); |
| 121 | + for(i=0; i<2048; i++){ |
| 122 | + if ((i & 0x001F) == 0x001F){ |
| 123 | + Serial.print("."); |
| 124 | + } |
| 125 | + file.write(buf, 512); |
| 126 | + } |
| 127 | + Serial.println(""); |
| 128 | + uint32_t end = millis() - start; |
| 129 | + Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end); |
| 130 | + file.close(); |
| 131 | + |
| 132 | + file = fs.open(path); |
| 133 | + start = millis(); |
| 134 | + end = start; |
| 135 | + i = 0; |
| 136 | + if(file && !file.isDirectory()){ |
| 137 | + len = file.size(); |
| 138 | + size_t flen = len; |
| 139 | + start = millis(); |
| 140 | + Serial.print("- reading" ); |
| 141 | + while(len){ |
| 142 | + size_t toRead = len; |
| 143 | + if(toRead > 512){ |
| 144 | + toRead = 512; |
| 145 | + } |
| 146 | + file.read(buf, toRead); |
| 147 | + if ((i++ & 0x001F) == 0x001F){ |
| 148 | + Serial.print("."); |
| 149 | + } |
| 150 | + len -= toRead; |
| 151 | + } |
| 152 | + Serial.println(""); |
| 153 | + end = millis() - start; |
| 154 | + Serial.printf("- %u bytes read in %u ms\r\n", flen, end); |
| 155 | + file.close(); |
| 156 | + } else { |
| 157 | + Serial.println("- failed to open file for reading"); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +void setup(){ |
| 162 | + Serial.begin(115200); |
| 163 | + if(!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED)){ |
| 164 | + Serial.println("LITTLEFS Mount Failed"); |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + listDir(LITTLEFS, "/", 0); |
| 169 | + writeFile(LITTLEFS, "/hello.txt", "Hello "); |
| 170 | + appendFile(LITTLEFS, "/hello.txt", "World!\r\n"); |
| 171 | + readFile(LITTLEFS, "/hello.txt"); |
| 172 | + renameFile(LITTLEFS, "/hello.txt", "/foo.txt"); |
| 173 | + readFile(LITTLEFS, "/foo.txt"); |
| 174 | + deleteFile(LITTLEFS, "/foo.txt"); |
| 175 | + testFileIO(LITTLEFS, "/test.txt"); |
| 176 | + deleteFile(LITTLEFS, "/test.txt"); |
| 177 | + Serial.println( "Test complete" ); |
| 178 | +} |
| 179 | + |
| 180 | +void loop(){ |
| 181 | + |
| 182 | +} |
0 commit comments