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