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 2.9KB

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