diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 4e5b2b6f..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "C_Cpp.clang_format_fallbackStyle": "{BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}", - "C_Cpp.formatting": "clangFormat", - "C_Cpp.default.forcedInclude": [] -} \ No newline at end of file diff --git a/include/EspFileSystem.h b/include/EspFileSystem.h new file mode 100644 index 00000000..1758bfcf --- /dev/null +++ b/include/EspFileSystem.h @@ -0,0 +1,18 @@ +#pragma once + +#include "Global.h" +#include "LittleFS.h" + +extern FS LittleFS; +using littlefs_impl::LittleFSConfig; +extern FS* filesystem; +#define FileFS LittleFS +#define FS_NAME "LittleFS" + +extern bool fileSystemInit(); + +extern File seekFile(const String& filename, size_t position = 0); +extern const String writeFile(const String& filename, const String& str); +extern const String readFile(const String& filename, size_t max_size); +extern const String filepath(const String& filename); +extern bool cutFile(const String& src, const String& dst); \ No newline at end of file diff --git a/include/Global.h b/include/Global.h new file mode 100644 index 00000000..bcab371a --- /dev/null +++ b/include/Global.h @@ -0,0 +1,6 @@ +#pragma once + +#include +#include +#include +#include \ No newline at end of file diff --git a/include/main.h b/include/main.h index d1f7fc89..f2d7c386 100644 --- a/include/main.h +++ b/include/main.h @@ -1,14 +1,6 @@ -#include -#include -#include -#include - -#include "LittleFS.h" - -extern FS LittleFS; -using littlefs_impl::LittleFSConfig; -extern FS* filesystem; -#define FileFS LittleFS -#define FS_NAME "LittleFS" +#pragma once +#include "EspFileSystem.h" +#include "Global.h" +#include "rest.h" void setupESP(); diff --git a/include/rest.h b/include/rest.h index 09e6942a..b5235955 100644 --- a/include/rest.h +++ b/include/rest.h @@ -1,9 +1,4 @@ -#include "main.h" +#pragma once +#include "Global.h" -File seekFile(const String& filename, size_t position = 0); -const String writeFile(const String& filename, const String& str); -const String readFile(const String& filename, size_t max_size); -const String filepath(const String& filename); -bool fileSystemInit(); -String prettyBytes(size_t size); -bool cutFile(const String& src, const String& dst); \ No newline at end of file +String prettyBytes(size_t size); \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index a401a611..6f23b121 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,7 +15,7 @@ data_dir = data_svelte [common_env_data] lib_deps_external = - bblanchon/ArduinoJson + bblanchon/ArduinoJson @6.18.0 [env:esp8266_4mb] build_flags = -Desp8266_4mb="esp8266_4mb" @@ -23,6 +23,8 @@ framework = arduino board = nodemcuv2 board_build.ldscript = eagle.flash.4m1m.ld platform = espressif8266 @2.6.3 +lib_deps = + ${common_env_data.lib_deps_external} monitor_filters = esp8266_exception_decoder upload_speed = 921600 monitor_speed = 115200 diff --git a/src/EspFileSystem.cpp b/src/EspFileSystem.cpp new file mode 100644 index 00000000..c464a1dc --- /dev/null +++ b/src/EspFileSystem.cpp @@ -0,0 +1,76 @@ +#include "EspFileSystem.h" + +bool fileSystemInit() { + if (!FileFS.begin()) { + Serial.println("FS Init ERROR, may be FS was not flashed"); + return false; + } + Serial.println("FS Init completed"); + return true; +} + +File seekFile(const String& filename, size_t position) { + String path = filepath(filename); + auto file = FileFS.open(path, "r"); + if (!file) { + Serial.println("[E] file error"); + } + file.seek(position, SeekSet); + return file; +} + +const String writeFile(const String& filename, const String& str) { + String path = filepath(filename); + auto file = FileFS.open(path, "w"); + if (!file) { + return "failed"; + } + file.print(str); + file.close(); + return "sucсess"; +} + +const String readFile(const String& filename, size_t max_size) { + String path = filepath(filename); + auto file = FileFS.open(path, "r"); + if (!file) { + return "failed"; + } + size_t size = file.size(); + if (size > max_size) { + file.close(); + return "large"; + } + String temp = file.readString(); + file.close(); + return temp; +} + +const String filepath(const String& filename) { + return filename.startsWith("/") ? filename : "/" + filename; +} + +bool cutFile(const String& src, const String& dst) { + String srcPath = filepath(src); + String dstPath = filepath(dst); + Serial.println("cut " + srcPath + " to " + dstPath); + if (!FileFS.exists(srcPath)) { + Serial.println("not exist: " + srcPath); + return false; + } + if (FileFS.exists(dstPath)) { + FileFS.remove(dstPath); + } + auto srcFile = FileFS.open(srcPath, "r"); + auto dstFile = FileFS.open(dstPath, "w"); + + uint8_t buf[512]; + while (srcFile.available()) { + size_t len = srcFile.read(buf, 512); + dstFile.write(buf, len); + } + srcFile.close(); + dstFile.close(); + FileFS.remove(srcPath); + return true; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 98cfb3c7..1f180baf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,4 @@ -#include "main.h" - -#include "rest.h" +#include "Main.h" void setup() { Serial.begin(115200); diff --git a/src/rest.cpp b/src/rest.cpp index 786d1eb9..020b050c 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -1,55 +1,5 @@ #include "rest.h" -File seekFile(const String& filename, size_t position) { - String path = filepath(filename); - auto file = FileFS.open(path, "r"); - if (!file) { - Serial.println("[E] file error"); - } - file.seek(position, SeekSet); - return file; -} - -const String writeFile(const String& filename, const String& str) { - String path = filepath(filename); - auto file = FileFS.open(path, "w"); - if (!file) { - return "failed"; - } - file.print(str); - file.close(); - return "sucсess"; -} - -const String readFile(const String& filename, size_t max_size) { - String path = filepath(filename); - auto file = FileFS.open(path, "r"); - if (!file) { - return "failed"; - } - size_t size = file.size(); - if (size > max_size) { - file.close(); - return "large"; - } - String temp = file.readString(); - file.close(); - return temp; -} - -const String filepath(const String& filename) { - return filename.startsWith("/") ? filename : "/" + filename; -} - -bool fileSystemInit() { - if (!FileFS.begin()) { - Serial.println("FS Init ERROR, may be FS was not flashed"); - return false; - } - Serial.println("FS Init completed"); - return true; -} - String prettyBytes(size_t size) { if (size < 1024) return String(size) + "b"; @@ -60,28 +10,3 @@ String prettyBytes(size_t size) { else return String(size / 1024.0 / 1024.0 / 1024.0) + "GB"; } - -bool cutFile(const String& src, const String& dst) { - String srcPath = filepath(src); - String dstPath = filepath(dst); - Serial.println("cut " + srcPath + " to " + dstPath); - if (!FileFS.exists(srcPath)) { - Serial.println("not exist: " + srcPath); - return false; - } - if (FileFS.exists(dstPath)) { - FileFS.remove(dstPath); - } - auto srcFile = FileFS.open(srcPath, "r"); - auto dstFile = FileFS.open(dstPath, "w"); - - uint8_t buf[512]; - while (srcFile.available()) { - size_t len = srcFile.read(buf, 512); - dstFile.write(buf, len); - } - srcFile.close(); - dstFile.close(); - FileFS.remove(srcPath); - return true; -} \ No newline at end of file