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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "filesys.h"
  2. // listDir("/");
  3. // deleteFile("/hello.txt");
  4. // writeFile("/hello.txt", "Hello ");
  5. // appendFile("/hello.txt", "World!\n\r");
  6. // printFile("/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. File open(const char * path) {
  51. return LittleFS.open(path, "r");
  52. }
  53. void printFile(const char * path) {
  54. Serial.printf("Reading file: %s\n\r", path);
  55. File file = LittleFS.open(path, "r");
  56. if (!file) {
  57. Serial.println("Failed to open file for reading");
  58. return;
  59. }
  60. Serial.print("Read from file: ");
  61. while (file.available()) {
  62. Serial.write(file.read());
  63. }
  64. file.close();
  65. }
  66. void writeFile(const char * path, const char * message) {
  67. Serial.printf("Writing file: %s\n\r", path);
  68. File file = LittleFS.open(path, "w");
  69. if (!file) {
  70. Serial.println("Failed to open file for writing");
  71. return;
  72. }
  73. if (file.print(message)) {
  74. Serial.println("File written");
  75. } else {
  76. Serial.println("Write failed");
  77. }
  78. delay(2000); // Make sure the CREATE and LASTWRITE times are different
  79. file.close();
  80. }
  81. void appendFile(const char * path, const char * message) {
  82. Serial.printf("Appending to file: %s\n\r", path);
  83. File file = LittleFS.open(path, "a");
  84. if (!file) {
  85. Serial.println("Failed to open file for appending");
  86. return;
  87. }
  88. if(file.print(message)) {
  89. Serial.println("Message appended");
  90. } else {
  91. Serial.println("Append failed");
  92. }
  93. file.close();
  94. }
  95. void renameFile(const char * path1, const char * path2) {
  96. Serial.printf("Renaming file %s to %s\n\r", path1, path2);
  97. if (LittleFS.rename(path1, path2)) {
  98. Serial.println("File renamed");
  99. } else {
  100. Serial.println("Rename failed");
  101. }
  102. }
  103. void deleteFile(const char * path) {
  104. Serial.printf("Deleting file: %s\n\r", path);
  105. if (LittleFS.remove(path)) {
  106. Serial.println("File deleted");
  107. } else {
  108. Serial.println("Delete failed");
  109. }
  110. }