ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filesys.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "filesys.h"
  2. // listDir("/");
  3. // deleteFile("/hello.txt");
  4. // writeFile("/hello.txt", "Hello ");
  5. // appendFile("/hello.txt", "World!\n\r");
  6. // readFile("/hello.txt");
  7. // listDir("/");
  8. FSInfo fsinfo;
  9. bool mount_fs() {
  10. if(!LittleFS.begin()) {
  11. Serial.println("[ERROR] LittleFS.info(), reset ...");
  12. return false;
  13. }
  14. if(!LittleFS.info(fsinfo)) {
  15. Serial.println("[ERROR] LittleFS.info(), reset ...");
  16. return false;
  17. }
  18. printf("Filesystem opened:\n\r");
  19. printf("\ttotalBytes:\t%d\n\r", fsinfo.totalBytes);
  20. printf("\tusedBytes:\t%d\n\r", fsinfo.usedBytes);
  21. printf("\tblockSize:\t%d\n\r", fsinfo.blockSize);
  22. printf("\tpageSize:\t%d\n\r", fsinfo.pageSize);
  23. printf("\tmaxOpenFiles:\t%d\n\r", fsinfo.maxOpenFiles);
  24. printf("\tmaxPathLength:\t%d\n\r", fsinfo.maxPathLength);
  25. printf("\n\r");
  26. return true;
  27. }
  28. bool format_fs() {
  29. printf("Formatting FS ! \n\r");
  30. return LittleFS.format();
  31. }
  32. void ls(const char * dirname) {
  33. Serial.printf("ls -l %s\n\r", dirname);
  34. Dir root = LittleFS.openDir(dirname);
  35. while (root.next()) {
  36. File file = root.openFile("r");
  37. time_t lw = file.getLastWrite();
  38. struct tm * tmstruct = localtime(&lw);
  39. Serial.printf("%8d %02d %02d %02d:%02d %s\n\r",
  40. file.size(),
  41. tmstruct->tm_mon + 1,
  42. tmstruct->tm_mday,
  43. tmstruct->tm_hour,
  44. tmstruct->tm_min,
  45. root.fileName().c_str());
  46. file.close();
  47. }
  48. Serial.println();
  49. }
  50. void readFile(const char * path) {
  51. Serial.printf("Reading file: %s\n\r", path);
  52. File file = LittleFS.open(path, "r");
  53. if (!file) {
  54. Serial.println("Failed to open file for reading");
  55. return;
  56. }
  57. Serial.print("Read from file: ");
  58. while (file.available()) {
  59. Serial.write(file.read());
  60. }
  61. file.close();
  62. }
  63. void writeFile(const char * path, const char * message) {
  64. Serial.printf("Writing file: %s\n\r", path);
  65. File file = LittleFS.open(path, "w");
  66. if (!file) {
  67. Serial.println("Failed to open file for writing");
  68. return;
  69. }
  70. if (file.print(message)) {
  71. Serial.println("File written");
  72. } else {
  73. Serial.println("Write failed");
  74. }
  75. delay(2000); // Make sure the CREATE and LASTWRITE times are different
  76. file.close();
  77. }
  78. void appendFile(const char * path, const char * message) {
  79. Serial.printf("Appending to file: %s\n\r", path);
  80. File file = LittleFS.open(path, "a");
  81. if (!file) {
  82. Serial.println("Failed to open file for appending");
  83. return;
  84. }
  85. if(file.print(message)) {
  86. Serial.println("Message appended");
  87. } else {
  88. Serial.println("Append failed");
  89. }
  90. file.close();
  91. }
  92. void renameFile(const char * path1, const char * path2) {
  93. Serial.printf("Renaming file %s to %s\n\r", path1, path2);
  94. if (LittleFS.rename(path1, path2)) {
  95. Serial.println("File renamed");
  96. } else {
  97. Serial.println("Rename failed");
  98. }
  99. }
  100. void deleteFile(const char * path) {
  101. Serial.printf("Deleting file: %s\n\r", path);
  102. if (LittleFS.remove(path)) {
  103. Serial.println("File deleted");
  104. } else {
  105. Serial.println("Delete failed");
  106. }
  107. }