Merge branch 'filesystem' into merge-200914

This commit is contained in:
Aircoookie 2020-09-14 16:32:31 +02:00 committed by GitHub
commit 16f41fc4b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 361 additions and 74 deletions

View File

@ -31,7 +31,7 @@ default_envs = travis_esp8266, travis_esp32
; default_envs = heltec_wifi_kit_8
; default_envs = h803wf
; default_envs = d1_mini_debug
; default_envs = d1_mini_ota
;default_envs = d1_mini_ota
; default_envs = esp32dev
; default_envs = esp8285_4CH_MagicHome
; default_envs = esp8285_4CH_H801
@ -226,6 +226,7 @@ platform = ${common.platform_wled_default}
upload_speed = 921600
board_build.ldscript = ${common.ldscript_4m1m}
build_flags = ${common.build_flags_esp8266}
monitor_filters = esp8266_exception_decoder
[env:heltec_wifi_kit_8]
board = d1_mini
@ -364,5 +365,5 @@ build_flags = ${common.build_flags_esp8266} ${common.debug_flags} ${common.build
[env:travis_esp32]
extends = env:esp32dev
build_type = debug
; build_type = debug
build_flags = ${common.build_flags_esp32} ${common.debug_flags} ${common.build_flags_all_features}

View File

@ -126,4 +126,12 @@
#define ABL_MILLIAMPS_DEFAULT 850; // auto lower brightness to stay close to milliampere limit
// WLED Error modes
#define ERR_NONE 0 // All good :)
#define ERR_EEP_COMMIT 2 // Could not commit to EEPROM (wrong flash layout?)
#define ERR_FS_BEGIN 10 // Could not init filesystem (no partition?)
#define ERR_FS_QUOTA 11 // The FS is full or the maximum file size is reached
#define ERR_FS_PLOAD 12 // It was attempted to load a preset that does not exist
#define ERR_FS_GENERAL 19 // A general unspecified filesystem error occured
#endif

View File

@ -46,6 +46,10 @@ void handleE131Packet(e131_packet_t* p, IPAddress clientIP, bool isArtnet);
//file.cpp
bool handleFileRead(AsyncWebServerRequest*, String path);
bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonDocument* content);
bool writeObjectToFile(const char* file, const char* key, JsonDocument* content);
bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest);
bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest);
//hue.cpp
void handleHue();
@ -83,8 +87,8 @@ void handleIR();
void deserializeSegment(JsonObject elem, byte it);
bool deserializeState(JsonObject root);
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id);
void serializeState(JsonObject root);
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id, bool forPreset = false);
void serializeState(JsonObject root, bool forPreset = false);
void serializeInfo(JsonObject root);
void serveJson(AsyncWebServerRequest* request);
bool serveLiveLeds(AsyncWebServerRequest* request, uint32_t wsClient = 0);
@ -190,7 +194,7 @@ void saveSettingsToEEPROM();
void loadSettingsFromEEPROM(bool first);
void savedToPresets();
bool applyPreset(byte index, bool loadBri = true);
void savePreset(byte index, bool persist = true);
void savePreset(byte index, bool persist = true, const char* pname = nullptr, byte prio = 50, JsonObject saveobj = JsonObject());
void loadMacro(byte index, char* m);
void applyMacro(byte index);
void saveMacro(byte index, String mc, bool persist = true); //only commit on single save, not in settings

View File

@ -4,15 +4,233 @@
* Utility for SPIFFS filesystem
*/
//filesystem
#ifndef WLED_DISABLE_FILESYSTEM
#include <FS.h>
#ifdef ARDUINO_ARCH_ESP32
#include "SPIFFS.h"
#endif
#include "SPIFFSEditor.h"
#endif
//find() that reads and buffers data from file stream in 256-byte blocks.
//Significantly faster, f.find(key) can take SECONDS for multi-kB files
bool bufferedFind(const char *target, File f) {
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINT("Find ");
DEBUGFS_PRINTLN(target);
uint32_t s = millis();
#endif
if (!f || !f.size()) return false;
size_t targetLen = strlen(target);
size_t index = 0;
byte c;
uint16_t bufsize = 0, count = 0;
byte buf[256];
f.seek(0);
while (f.position() < f.size() -1) {
bufsize = f.read(buf, 256);
count = 0;
while (count < bufsize) {
if(buf[count] != target[index])
index = 0; // reset index if any char does not match
if(buf[count] == target[index]) {
if(++index >= targetLen) { // return true if all chars in the target match
f.seek((f.position() - bufsize) + count +1);
DEBUGFS_PRINTF("Found at pos %d, took %d ms", f.position(), millis() - s);
return true;
}
}
count++;
}
}
DEBUGFS_PRINTF("No match, took %d ms\n", millis() - s);
return false;
}
//find empty spots in file stream in 256-byte blocks.
bool bufferedFindSpace(uint16_t targetLen, File f) {
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINTF("Find %d spaces\n", targetLen);
uint32_t s = millis();
#endif
if (!f || !f.size()) return false;
uint16_t index = 0;
uint16_t bufsize = 0, count = 0;
byte buf[256];
f.seek(0);
while (f.position() < f.size() -1) {
bufsize = f.read(buf, 256);
count = 0;
while (count < bufsize) {
if(buf[count] != ' ')
index = 0; // reset index if not space
if(buf[count] == ' ') {
if(++index >= targetLen) { // return true if space long enough
f.seek((f.position() - bufsize) + count +1 - targetLen);
DEBUGFS_PRINTF("Found at pos %d, took %d ms", f.position(), millis() - s);
return true;
}
}
count++;
}
}
DEBUGFS_PRINTF("No match, took %d ms\n", millis() - s);
return false;
}
bool appendObjectToFile(File f, const char* key, JsonDocument* content, uint32_t s)
{
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINTLN("Append");
uint32_t s1 = millis();
#endif
uint32_t pos = 0;
if (!f) return false;
if (f.size() < 3) f.print("{}");
//if there is enough empty space in file, insert there instead of appending
uint32_t contentLen = measureJson(*content);
DEBUGFS_PRINTF("CLen %d\n", contentLen);
if (bufferedFindSpace(contentLen + strlen(key) + 1, f)) {
if (f.position() > 2) f.write(','); //add comma if not first object
f.print(key);
serializeJson(*content, f);
DEBUGFS_PRINTF("Inserted, took %d ms (total %d)", millis() - s1, millis() - s);
return true;
}
//check if last character in file is '}' (typical)
f.seek(1, SeekEnd);
if (f.read() == '}') pos = f.size() -1;
if (pos == 0) //not found
{
DEBUGFS_PRINTLN("not }");
while (bufferedFind("}",f)) //find last closing bracket in JSON if not last char
{
pos = f.position();
}
}
DEBUGFS_PRINT("pos "); DEBUGFS_PRINTLN(pos);
if (pos > 2)
{
f.seek(pos, SeekSet);
f.write(',');
} else { //file content is not valid JSON object
f.seek(0, SeekSet);
f.write('{'); //start JSON
}
f.print(key);
//Append object
serializeJson(*content, f);
f.write('}');
f.close();
DEBUGFS_PRINTF("Appended, took %d ms (total %d)", millis() - s1, millis() - s);
}
bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonDocument* content)
{
char objKey[10];
sprintf(objKey, "\"%ld\":", id);
writeObjectToFile(file, objKey, content);
}
bool writeObjectToFile(const char* file, const char* key, JsonDocument* content)
{
uint32_t s = 0; //timing
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINTF("Write to %s with key %s >>>\n", file, key);
serializeJson(*content, Serial); DEBUGFS_PRINTLN();
s = millis();
#endif
uint32_t pos = 0;
File f = WLED_FS.open(file, "r+");
if (!f && !WLED_FS.exists(file)) f = WLED_FS.open(file, "w+");
if (!f) {
DEBUGFS_PRINTLN("Failed to open!");
return false;
}
if (!bufferedFind(key, f)) //key does not exist in file
{
return appendObjectToFile(f, key, content, s);
}
//exists
pos = f.position();
//measure out end of old object
StaticJsonDocument<1024> doc;
deserializeJson(doc, f);
uint32_t pos2 = f.position();
uint32_t oldLen = pos2 - pos;
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINTF("Old obj len %d >>> ", oldLen);
serializeJson(doc, Serial);
DEBUGFS_PRINTLN();
#endif
if (!content->isNull() && measureJson(*content) <= oldLen) //replace
{
DEBUGFS_PRINTLN("replace");
f.seek(pos);
serializeJson(*content, f);
//pad rest
for (uint32_t i = f.position(); i < pos2; i++) {
f.write(' ');
}
} else { //delete
DEBUGFS_PRINTLN("delete");
pos -= strlen(key);
if (pos > 3) pos--; //also delete leading comma if not first object
f.seek(pos);
for (uint32_t i = pos; i < pos2; i++) {
f.write(' ');
}
if (!content->isNull()) return appendObjectToFile(f, key, content, s);
}
f.close();
DEBUGFS_PRINTF("Deleted, took %d ms\n", millis() - s);
return true;
}
bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest)
{
char objKey[10];
sprintf(objKey, "\"%ld\":", id);
readObjectFromFile(file, objKey, dest);
}
bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest)
{
#ifdef WLED_DEBUG_FS
DEBUGFS_PRINTF("Read from %s with key %s >>>\n", file, key);
uint32_t s = millis();
#endif
File f = WLED_FS.open(file, "r");
if (!f) return false;
if (!bufferedFind(key, f)) //key does not exist in file
{
f.close();
DEBUGFS_PRINTLN("Obj not found.");
return false;
}
deserializeJson(*dest, f);
f.close();
DEBUGFS_PRINTF("Read, took %d ms\n", millis() - s);
return true;
}
#endif
#if !defined WLED_DISABLE_FILESYSTEM && defined WLED_ENABLE_FS_SERVING
//Un-comment any file types you need
@ -38,13 +256,13 @@ bool handleFileRead(AsyncWebServerRequest* request, String path){
DEBUG_PRINTLN("FileRead: " + path);
if(path.endsWith("/")) path += "index.htm";
String contentType = getContentType(request, path);
String pathWithGz = path + ".gz";
if(SPIFFS.exists(pathWithGz)){
request->send(SPIFFS, pathWithGz, contentType);
/*String pathWithGz = path + ".gz";
if(WLED_FS.exists(pathWithGz)){
request->send(WLED_FS, pathWithGz, contentType);
return true;
}
if(SPIFFS.exists(path)) {
request->send(SPIFFS, path, contentType);
}*/
if(WLED_FS.exists(path)) {
request->send(WLED_FS, path, contentType);
return true;
}
return false;

View File

@ -225,17 +225,17 @@ bool deserializeState(JsonObject root)
bool persistSaves = !(root["np"] | false);
ps = root["psave"] | -1;
if (ps >= 0) savePreset(ps, persistSaves);
if (ps >= 0) savePreset(ps, persistSaves, root["n"], root["p"] | 50, root["o"].as<JsonObject>());
return stateResponse;
}
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id)
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id, bool forPreset)
{
root["id"] = id;
root["start"] = seg.start;
root["stop"] = seg.stop;
root["len"] = seg.stop - seg.start;
if (!forPreset) root["len"] = seg.stop - seg.start;
root["grp"] = seg.grouping;
root["spc"] = seg.spacing;
root["on"] = seg.getOption(SEG_OPTION_ON);
@ -273,38 +273,40 @@ void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id)
}
void serializeState(JsonObject root)
{
if (errorFlag) root["error"] = errorFlag;
void serializeState(JsonObject root, bool forPreset)
{
root["on"] = (bri > 0);
root["bri"] = briLast;
root["transition"] = transitionDelay/100; //in 100ms
root["ps"] = currentPreset;
root["pss"] = savedPresets;
root["pl"] = (presetCyclingEnabled) ? 0: -1;
if (!forPreset) {
if (errorFlag) root["error"] = errorFlag;
usermods.addToJsonState(root);
root["ps"] = currentPreset;
root["pss"] = savedPresets;
root["pl"] = (presetCyclingEnabled) ? 0: -1;
//temporary for preset cycle
JsonObject ccnf = root.createNestedObject("ccnf");
ccnf["min"] = presetCycleMin;
ccnf["max"] = presetCycleMax;
ccnf["time"] = presetCycleTime;
JsonObject nl = root.createNestedObject("nl");
nl["on"] = nightlightActive;
nl["dur"] = nightlightDelayMins;
nl["fade"] = (nightlightMode > NL_MODE_SET); //deprecated
nl["mode"] = nightlightMode;
nl["tbri"] = nightlightTargetBri;
JsonObject udpn = root.createNestedObject("udpn");
udpn["send"] = notifyDirect;
udpn["recv"] = receiveNotifications;
usermods.addToJsonState(root);
root["lor"] = realtimeOverride;
//temporary for preset cycle
JsonObject ccnf = root.createNestedObject("ccnf");
ccnf["min"] = presetCycleMin;
ccnf["max"] = presetCycleMax;
ccnf["time"] = presetCycleTime;
JsonObject nl = root.createNestedObject("nl");
nl["on"] = nightlightActive;
nl["dur"] = nightlightDelayMins;
nl["fade"] = (nightlightMode > NL_MODE_SET); //deprecated
nl["mode"] = nightlightMode;
nl["tbri"] = nightlightTargetBri;
JsonObject udpn = root.createNestedObject("udpn");
udpn["send"] = notifyDirect;
udpn["recv"] = receiveNotifications;
root["lor"] = realtimeOverride;
}
root["mainseg"] = strip.getMainSegmentId();
@ -315,7 +317,7 @@ void serializeState(JsonObject root)
if (sg.isActive())
{
JsonObject seg0 = seg.createNestedObject();
serializeSegment(seg0, sg, s);
serializeSegment(seg0, sg, s, forPreset);
}
}
}

View File

@ -85,6 +85,14 @@ void WLED::loop()
handleHue();
handleBlynk();
if (presetToApply) {
StaticJsonDocument<1024> temp;
errorFlag = !readObjectFromFileUsingId("/presets.json", presetToApply, &temp);
serializeJson(temp, Serial);
deserializeState(temp.as<JsonObject>());
presetToApply = 0;
}
yield();
if (!offMode)
strip.service();
@ -165,9 +173,9 @@ void WLED::setup()
#ifndef WLED_DISABLE_FILESYSTEM
#ifdef ARDUINO_ARCH_ESP32
SPIFFS.begin(true);
WLED_FS.begin(true);
#endif
SPIFFS.begin();
WLED_FS.begin();
#endif
DEBUG_PRINTLN("Load EEPROM");

View File

@ -10,9 +10,9 @@
// version code in format yymmddb (b = daily build)
#define VERSION 2009100
// ESP8266-01 (blue) got too little storage space to work with all features of WLED. To use it, you must use ESP8266 Arduino Core v2.4.2 and the setting 512K(No SPIFFS).
// ESP8266-01 (blue) got too little storage space to work with WLED. 0.10.2 is the last release supporting this unit.
// ESP8266-01 (black) has 1MB flash and can thus fit the whole program. Use 1M(64K SPIFFS).
// ESP8266-01 (black) has 1MB flash and can thus fit the whole program, although OTA update is not possible. Use 1M(128K SPIFFS).
// Uncomment some of the following lines to disable features to compile for ESP8266-01 (max flash size 434kB):
// Alternatively, with platformio pass your chosen flags to your custom build target in platformio.ini.override
@ -34,19 +34,23 @@
#define WLED_ENABLE_WEBSOCKETS
#endif
#define WLED_DISABLE_FILESYSTEM // SPIFFS is not used by any WLED feature yet
//#define WLED_ENABLE_FS_SERVING // Enable sending html file from SPIFFS before serving progmem version
//#define WLED_ENABLE_FS_EDITOR // enable /edit page for editing SPIFFS content. Will also be disabled with OTA lock
//#define WLED_DISABLE_FILESYSTEM // SPIFFS is not used by any WLED feature yet
#define WLED_ENABLE_FS_SERVING // Enable sending html file from SPIFFS before serving progmem version
#define WLED_ENABLE_FS_EDITOR // enable /edit page for editing SPIFFS content. Will also be disabled with OTA lock
// to toggle usb serial debug (un)comment the following line
//#define WLED_DEBUG
// filesystem specific debugging
#define WLED_DEBUG_FS
// Library inclusions.
#include <Arduino.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESPAsyncTCP.h>
#include <LittleFS.h>
extern "C"
{
#include <user_interface.h>
@ -118,25 +122,18 @@
#include <IRutils.h>
#endif
//Filesystem to use for preset and config files. SPIFFS or LittleFS on ESP8266, SPIFFS only on ESP32
#ifdef ESP8266
#define WLED_FS LittleFS
#else
#define WLED_FS SPIFFS
#endif
// remove flicker because PWM signal of RGB channels can become out of phase (part of core as of Arduino core v2.7.0)
//#if defined(WLED_USE_ANALOG_LEDS) && defined(ESP8266)
// #include "src/dependencies/arduino/core_esp8266_waveform.h"
//#endif
// enable additional debug output
#ifdef WLED_DEBUG
#ifndef ESP8266
#include <rom/rtc.h>
#endif
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINTF(x) Serial.printf(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINTF(x)
#endif
// GLOBAL VARIABLES
// both declared and defined in header (solution from http://www.keil.com/support/docs/1868.htm)
//
@ -167,6 +164,8 @@ WLED_GLOBAL char otaPass[33] _INIT(DEFAULT_OTA_PASS);
// Hardware CONFIG (only changeble HERE, not at runtime)
// LED strip pin, button pin and IR pin changeable in NpbWrapper.h!
WLED_GLOBAL byte presetToApply _INIT(0);
WLED_GLOBAL byte auxDefaultState _INIT(0); // 0: input 1: high 2: low
WLED_GLOBAL byte auxTriggeredState _INIT(0); // 0: input 1: high 2: low
WLED_GLOBAL char ntpServerName[33] _INIT("0.wled.pool.ntp.org"); // NTP server to use
@ -502,6 +501,30 @@ WLED_GLOBAL WS2812FX strip _INIT(WS2812FX());
// Usermod manager
WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager());
// enable additional debug output
#ifdef WLED_DEBUG
#ifndef ESP8266
#include <rom/rtc.h>
#endif
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINTF(x...) Serial.printf(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#define DEBUG_PRINTF(x)
#endif
#ifdef WLED_DEBUG_FS
#define DEBUGFS_PRINT(x) Serial.print(x)
#define DEBUGFS_PRINTLN(x) Serial.println(x)
#define DEBUGFS_PRINTF(x...) Serial.printf(x)
#else
#define DEBUGFS_PRINT(x)
#define DEBUGFS_PRINTLN(x)
#define DEBUGFS_PRINTF(x)
#endif
// debug macro variable definitions
#ifdef WLED_DEBUG
WLED_GLOBAL unsigned long debugTime _INIT(0);
@ -510,7 +533,6 @@ WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager());
WLED_GLOBAL int loops _INIT(0);
#endif
#define WLED_CONNECTED (WiFi.status() == WL_CONNECTED)
#define WLED_WIFI_CONFIGURED (strlen(clientSSID) >= 1 && strcmp(clientSSID, DEFAULT_CLIENT_SSID) != 0)
#define WLED_MQTT_CONNECTED (mqtt != nullptr && mqtt->connected())

View File

@ -33,7 +33,7 @@
void commit()
{
if (!EEPROM.commit()) errorFlag = 2;
if (!EEPROM.commit()) errorFlag = ERR_EEP_COMMIT;
}
/*
@ -621,6 +621,12 @@ void savedToPresets()
bool applyPreset(byte index, bool loadBri)
{
StaticJsonDocument<1024> temp;
errorFlag = readObjectFromFileUsingId("/presets.json", index, &temp) ? ERR_NONE : ERR_FS_PLOAD;
serializeJson(temp, Serial);
deserializeState(temp.as<JsonObject>());
//presetToApply = index;
return true;
if (index == 255 || index == 0)
{
loadSettingsFromEEPROM(false);//load boot defaults
@ -666,8 +672,26 @@ bool applyPreset(byte index, bool loadBri)
return true;
}
void savePreset(byte index, bool persist)
void savePreset(byte index, bool persist, const char* pname, byte priority, JsonObject saveobj)
{
StaticJsonDocument<1024> doc;
JsonObject sObj = doc.to<JsonObject>();
if (saveobj.isNull()) {
DEBUGFS_PRINTLN("Save current state");
serializeState(doc.to<JsonObject>(), true);
} else {
DEBUGFS_PRINTLN("Save custom");
sObj.set(saveobj);
}
sObj["p"] = priority;
if (pname) sObj["n"] = pname;
//serializeJson(doc, Serial);
writeObjectToFileUsingId("/presets.json", index, &doc);
//Serial.println("Done!");
return;
if (index > 16) return;
if (index < 1) {saveSettingsToEEPROM();return;}
uint16_t i = 380 + index*20;//min400

View File

@ -157,13 +157,13 @@ void initServer()
if (!otaLock){
#if !defined WLED_DISABLE_FILESYSTEM && defined WLED_ENABLE_FS_EDITOR
#ifdef ARDUINO_ARCH_ESP32
server.addHandler(new SPIFFSEditor(SPIFFS));//http_username,http_password));
server.addHandler(new SPIFFSEditor(WLED_FS));//http_username,http_password));
#else
server.addHandler(new SPIFFSEditor());//http_username,http_password));
server.addHandler(new SPIFFSEditor("","",WLED_FS));//http_username,http_password));
#endif
#else
server.on("/edit", HTTP_GET, [](AsyncWebServerRequest *request){
serveMessage(request, 501, "Not implemented", F("The SPIFFS editor is disabled in this build."), 254);
serveMessage(request, 501, "Not implemented", F("The FS editor is disabled in this build."), 254);
});
#endif
//init ota page