main.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include <Arduino.h>
  2. #include "FS.h"
  3. #include <LITTLEFS.h>
  4. #ifndef CONFIG_LITTLEFS_FOR_IDF_3_2
  5. #include <time.h>
  6. #endif
  7. /* You only need to format LITTLEFS the first time you run a
  8. test or else use the LITTLEFS plugin to create a partition
  9. https://github.com/lorol/arduino-esp32littlefs-plugin */
  10. #define FORMAT_LITTLEFS_IF_FAILED true
  11. void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
  12. Serial.printf("Listing directory: %s\r\n", dirname);
  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. File file = root.openNextFile();
  23. while(file){
  24. if(file.isDirectory()){
  25. Serial.print(" DIR : ");
  26. #ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
  27. Serial.println(file.name());
  28. #else
  29. Serial.print(file.name());
  30. time_t t= file.getLastWrite();
  31. struct tm * tmstruct = localtime(&t);
  32. Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
  33. #endif
  34. if(levels){
  35. listDir(fs, file.name(), levels -1);
  36. }
  37. } else {
  38. Serial.print(" FILE: ");
  39. Serial.print(file.name());
  40. Serial.print(" SIZE: ");
  41. #ifdef CONFIG_LITTLEFS_FOR_IDF_3_2
  42. Serial.println(file.size());
  43. #else
  44. Serial.print(file.size());
  45. time_t t= file.getLastWrite();
  46. struct tm * tmstruct = localtime(&t);
  47. Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
  48. #endif
  49. }
  50. file = root.openNextFile();
  51. }
  52. }
  53. void createDir(fs::FS &fs, const char * path){
  54. Serial.printf("Creating Dir: %s\n", path);
  55. if(fs.mkdir(path)){
  56. Serial.println("Dir created");
  57. } else {
  58. Serial.println("mkdir failed");
  59. }
  60. }
  61. void removeDir(fs::FS &fs, const char * path){
  62. Serial.printf("Removing Dir: %s\n", path);
  63. if(fs.rmdir(path)){
  64. Serial.println("Dir removed");
  65. } else {
  66. Serial.println("rmdir failed");
  67. }
  68. }
  69. void readFile(fs::FS &fs, const char * path){
  70. Serial.printf("Reading file: %s\r\n", path);
  71. File file = fs.open(path);
  72. if(!file || file.isDirectory()){
  73. Serial.println("- failed to open file for reading");
  74. return;
  75. }
  76. Serial.println("- read from file:");
  77. while(file.available()){
  78. Serial.write(file.read());
  79. }
  80. file.close();
  81. }
  82. void writeFile(fs::FS &fs, const char * path, const char * message){
  83. Serial.printf("Writing file: %s\r\n", path);
  84. File file = fs.open(path, FILE_WRITE);
  85. if(!file){
  86. Serial.println("- failed to open file for writing");
  87. return;
  88. }
  89. if(file.print(message)){
  90. Serial.println("- file written");
  91. } else {
  92. Serial.println("- write failed");
  93. }
  94. file.close();
  95. }
  96. void appendFile(fs::FS &fs, const char * path, const char * message){
  97. Serial.printf("Appending to file: %s\r\n", path);
  98. File file = fs.open(path, FILE_APPEND);
  99. if(!file){
  100. Serial.println("- failed to open file for appending");
  101. return;
  102. }
  103. if(file.print(message)){
  104. Serial.println("- message appended");
  105. } else {
  106. Serial.println("- append failed");
  107. }
  108. file.close();
  109. }
  110. void renameFile(fs::FS &fs, const char * path1, const char * path2){
  111. Serial.printf("Renaming file %s to %s\r\n", path1, path2);
  112. if (fs.rename(path1, path2)) {
  113. Serial.println("- file renamed");
  114. } else {
  115. Serial.println("- rename failed");
  116. }
  117. }
  118. void deleteFile(fs::FS &fs, const char * path){
  119. Serial.printf("Deleting file: %s\r\n", path);
  120. if(fs.remove(path)){
  121. Serial.println("- file deleted");
  122. } else {
  123. Serial.println("- delete failed");
  124. }
  125. }
  126. // SPIFFS-like write and delete file, better use #define CONFIG_LITTLEFS_SPIFFS_COMPAT 1
  127. void writeFile2(fs::FS &fs, const char * path, const char * message){
  128. if(!fs.exists(path)){
  129. if (strchr(path, '/')) {
  130. Serial.printf("Create missing folders of: %s\r\n", path);
  131. char *pathStr = strdup(path);
  132. if (pathStr) {
  133. char *ptr = strchr(pathStr, '/');
  134. while (ptr) {
  135. *ptr = 0;
  136. fs.mkdir(pathStr);
  137. *ptr = '/';
  138. ptr = strchr(ptr+1, '/');
  139. }
  140. }
  141. free(pathStr);
  142. }
  143. }
  144. Serial.printf("Writing file to: %s\r\n", path);
  145. File file = fs.open(path, FILE_WRITE);
  146. if(!file){
  147. Serial.println("- failed to open file for writing");
  148. return;
  149. }
  150. if(file.print(message)){
  151. Serial.println("- file written");
  152. } else {
  153. Serial.println("- write failed");
  154. }
  155. file.close();
  156. }
  157. void deleteFile2(fs::FS &fs, const char * path){
  158. Serial.printf("Deleting file and empty folders on path: %s\r\n", path);
  159. if(fs.remove(path)){
  160. Serial.println("- file deleted");
  161. } else {
  162. Serial.println("- delete failed");
  163. }
  164. char *pathStr = strdup(path);
  165. if (pathStr) {
  166. char *ptr = strrchr(pathStr, '/');
  167. if (ptr) {
  168. Serial.printf("Removing all empty folders on path: %s\r\n", path);
  169. }
  170. while (ptr) {
  171. *ptr = 0;
  172. fs.rmdir(pathStr);
  173. ptr = strrchr(pathStr, '/');
  174. }
  175. free(pathStr);
  176. }
  177. }
  178. void testFileIO(fs::FS &fs, const char * path){
  179. Serial.printf("Testing file I/O with %s\r\n", path);
  180. static uint8_t buf[512];
  181. size_t len = 0;
  182. File file = fs.open(path, FILE_WRITE);
  183. if(!file){
  184. Serial.println("- failed to open file for writing");
  185. return;
  186. }
  187. size_t i;
  188. Serial.print("- writing" );
  189. uint32_t start = millis();
  190. for(i=0; i<2048; i++){
  191. if ((i & 0x001F) == 0x001F){
  192. Serial.print(".");
  193. }
  194. file.write(buf, 512);
  195. }
  196. Serial.println("");
  197. uint32_t end = millis() - start;
  198. Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
  199. file.close();
  200. file = fs.open(path);
  201. start = millis();
  202. end = start;
  203. i = 0;
  204. if(file && !file.isDirectory()){
  205. len = file.size();
  206. size_t flen = len;
  207. start = millis();
  208. Serial.print("- reading" );
  209. while(len){
  210. size_t toRead = len;
  211. if(toRead > 512){
  212. toRead = 512;
  213. }
  214. file.read(buf, toRead);
  215. if ((i++ & 0x001F) == 0x001F){
  216. Serial.print(".");
  217. }
  218. len -= toRead;
  219. }
  220. Serial.println("");
  221. end = millis() - start;
  222. Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
  223. file.close();
  224. } else {
  225. Serial.println("- failed to open file for reading");
  226. }
  227. }
  228. void setup(){
  229. Serial.begin(115200);
  230. if(!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED)){
  231. Serial.println("LITTLEFS Mount Failed");
  232. return;
  233. }
  234. listDir(LITTLEFS, "/", 0);
  235. createDir(LITTLEFS, "/mydir");
  236. writeFile(LITTLEFS, "/mydir/hello2.txt", "Hello2");
  237. //writeFile(LITTLEFS, "/mydir/newdir2/newdir3/hello3.txt", "Hello3");
  238. writeFile2(LITTLEFS, "/mydir/newdir2/newdir3/hello3.txt", "Hello3");
  239. listDir(LITTLEFS, "/", 3);
  240. deleteFile(LITTLEFS, "/mydir/hello2.txt");
  241. //deleteFile(LITTLEFS, "/mydir/newdir2/newdir3/hello3.txt");
  242. deleteFile2(LITTLEFS, "/mydir/newdir2/newdir3/hello3.txt");
  243. removeDir(LITTLEFS, "/mydir");
  244. listDir(LITTLEFS, "/", 3);
  245. writeFile(LITTLEFS, "/hello.txt", "Hello ");
  246. appendFile(LITTLEFS, "/hello.txt", "World!\r\n");
  247. readFile(LITTLEFS, "/hello.txt");
  248. renameFile(LITTLEFS, "/hello.txt", "/foo.txt");
  249. readFile(LITTLEFS, "/foo.txt");
  250. deleteFile(LITTLEFS, "/foo.txt");
  251. testFileIO(LITTLEFS, "/test.txt");
  252. deleteFile(LITTLEFS, "/test.txt");
  253. Serial.println( "Test complete" );
  254. }
  255. void loop(){
  256. }