LittleFS_test.ino 4.9 KB

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