[ESP] use littlefs

This commit is contained in:
2020-12-06 16:44:42 +03:00
parent 2130b0c37e
commit fae5a1f3a2
3 changed files with 29 additions and 7 deletions

View File

@@ -2,9 +2,9 @@
#define WEB_API_H #define WEB_API_H
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <FS.h> #include <ESP8266WebServer.h>
#include <LittleFS.h>
class WebAPI { class WebAPI {
private: private:
@@ -16,6 +16,7 @@ class WebAPI {
String getContentType(String filename); String getContentType(String filename);
String getStatusJson(); String getStatusJson();
public: public:
WebAPI(); WebAPI();
void init(); void init();

View File

@@ -15,7 +15,8 @@ framework = arduino
monitor_speed = 115200 monitor_speed = 115200
board_build.f_cpu = 160000000L board_build.f_cpu = 160000000L
upload_resetmethod = nodemcu upload_resetmethod = nodemcu
board_build.filesystem = littlefs
upload_port=COM6 upload_port=COM5
lib_deps = ArduinoJson@6.11 lib_deps = ArduinoJson@6.11

View File

@@ -1,4 +1,5 @@
#include "WebAPI.h" #include "WebAPI.h"
#include "Config.h" #include "Config.h"
#include "QueueManager.h" #include "QueueManager.h"
@@ -8,6 +9,24 @@ WebAPI::WebAPI() {}
float getVin(float r1, float r2, float vout) { return ((r1 + r2) * vout) / r2; } float getVin(float r1, float r2, float vout) { return ((r1 + r2) * vout) / r2; }
#include <umm_malloc/umm_malloc.h>
const size_t block_size = 8;
size_t getTotalAvailableMemory() {
umm_info(0, 0);
return ummHeapInfo.freeBlocks * block_size;
}
size_t getLargestAvailableBlock() {
umm_info(0, 0);
return ummHeapInfo.maxFreeContiguousBlocks * block_size;
}
float getFragmentation() {
return 100 - getLargestAvailableBlock() * 100.0 / getTotalAvailableMemory();
}
String WebAPI::getStatusJson() { String WebAPI::getStatusJson() {
StaticJsonDocument<256> doc; StaticJsonDocument<256> doc;
Status status = executor.status(); Status status = executor.status();
@@ -20,6 +39,7 @@ String WebAPI::getStatusJson() {
doc["mmS"] = status.mmS; doc["mmS"] = status.mmS;
doc["pEng"] = status.pEng; doc["pEng"] = status.pEng;
doc["xLim"] = status.xLim; doc["xLim"] = status.xLim;
doc["frag"] = getFragmentation();
doc["batt"] = getVin(battR1, battR2, analogRead(A0) / 1000.0); doc["batt"] = getVin(battR1, battR2, analogRead(A0) / 1000.0);
String out; String out;
@@ -58,11 +78,11 @@ bool WebAPI::getFile(String filename) {
} }
String contentType = getContentType(filename); String contentType = getContentType(filename);
String pathWithGz = filename + ".gz"; String pathWithGz = filename + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(filename)) { if (LittleFS.exists(pathWithGz) || LittleFS.exists(filename)) {
if (SPIFFS.exists(pathWithGz)) { if (LittleFS.exists(pathWithGz)) {
filename += ".gz"; filename += ".gz";
} }
File file = SPIFFS.open(filename, "r"); File file = LittleFS.open(filename, "r");
server.streamFile(file, contentType); server.streamFile(file, contentType);
file.close(); file.close();
return true; return true;
@@ -110,7 +130,7 @@ void WebAPI::init() {
server.onNotFound(std::bind(&WebAPI::handleNotFound, this)); server.onNotFound(std::bind(&WebAPI::handleNotFound, this));
SPIFFS.begin(); LittleFS.begin();
server.begin(); server.begin();
} }