LittleFS_test.ino 5.6 KB

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