Merge branch 'filesystem' into merge-fs2

This commit is contained in:
Aircoookie 2020-09-21 19:48:12 +02:00 committed by GitHub
commit bd65bf2175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 374 additions and 74 deletions

View File

@ -178,6 +178,7 @@ lib_deps =
#U8g2@~2.27.2
#For Dallas sensor uncomment following 2 lines
#OneWire@~2.3.5
#milesburton/DallasTemperature@^3.9.0
#For BME280 sensor uncomment following
#BME280@~3.0.0
lib_ignore =
@ -227,6 +228,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
@ -373,5 +375,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

@ -104,6 +104,15 @@
#define SEG_OPTION_FREEZE 5 //Segment contents will not be refreshed
#define SEG_OPTION_TRANSITIONAL 7
// WLED Error modes
#define ERR_NONE 0 // All good :)
#define ERR_EEP_COMMIT 2 // Could not commit to EEPROM (wrong flash layout?)
#define ERR_JSON 9 // JSON parsing failed (input too large?)
#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
//Timer mode types
#define NL_MODE_SET 0 //After nightlight time elapsed, set to target brightness
#define NL_MODE_FADE 1 //Fade to target brightness gradually
@ -126,6 +135,7 @@
#define ABL_MILLIAMPS_DEFAULT 850; // auto lower brightness to stay close to milliampere limit
#define TOUCH_THRESHOLD 32 // limit to recognize a touch, higher value means more sensitive
// Size of buffer for API JSON object (increase for more segments)

View File

@ -1432,7 +1432,8 @@ function populateSegments(s)
function updateTrail(e, slidercol)
{
if (e==null) return;
var progress = e.value *100 /255;
var max = e.hasAttribute('max') ? e.attributes['max'].value : 255;
var progress = e.value * 100 / max;
progress = parseInt(progress);
var scol;
switch (slidercol) {

View File

@ -47,6 +47,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();
@ -84,8 +88,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);
@ -191,7 +195,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, const 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"
//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[F("np")] | false);
ps = root[F("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[F("id")] = id;
root[F("start")] = seg.start;
root[F("stop")] = seg.stop;
root[F("len")] = seg.stop - seg.start;
if (!forPreset) root[F("len")] = seg.stop - seg.start;
root[F("grp")] = seg.grouping;
root[F("spc")] = seg.spacing;
root["on"] = seg.getOption(SEG_OPTION_ON);
@ -272,15 +272,16 @@ void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id)
root[F("mi")] = seg.getOption(SEG_OPTION_MIRROR);
}
void serializeState(JsonObject root)
void serializeState(JsonObject root, bool forPreset)
{
if (errorFlag) root[F("error")] = errorFlag;
root["on"] = (bri > 0);
root["bri"] = briLast;
root[F("transition")] = transitionDelay/100; //in 100ms
if (!forPreset) {
if (errorFlag) root["error"] = errorFlag;
root[F("ps")] = currentPreset;
root[F("pss")] = savedPresets;
root[F("pl")] = (presetCyclingEnabled) ? 0: -1;
@ -305,6 +306,7 @@ void serializeState(JsonObject root)
udpn[F("recv")] = receiveNotifications;
root[F("lor")] = realtimeOverride;
}
root[F("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);
}
}
}
@ -394,10 +396,17 @@ void serializeInfo(JsonObject root)
JsonObject wifi_info = root.createNestedObject("wifi");
wifi_info[F("bssid")] = WiFi.BSSIDstr();
int qrssi = WiFi.RSSI();
wifi_info[F("rssi")] = qrssi;
wifi_info[F("signal")] = getSignalQuality(qrssi);
wifi_info[F("channel")] = WiFi.channel();
JsonObject fs_info = root.createNestedObject("fs");
FSInfo fsi;
WLED_FS.info(fsi);
fs_info["u"] = fsi.usedBytes;
fs_info["t"] = fsi.totalBytes;
#ifdef ARDUINO_ARCH_ESP32
#ifdef WLED_DEBUG
wifi_info[F("txPower")] = (int) WiFi.getTxPower();
@ -424,6 +433,7 @@ void serializeInfo(JsonObject root)
root[F("freeheap")] = ESP.getFreeHeap();
root[F("uptime")] = millis()/1000 + rolloverMillis*4294967;
usermods.addToJsonInfo(root);
byte os = 0;

View File

@ -84,6 +84,15 @@ 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)
@ -169,9 +178,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(F("Load EEPROM"));

View File

@ -10,9 +10,9 @@
// version code in format yymmddb (b = daily build)
#define VERSION 2009202
// 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>
@ -120,25 +124,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)
//
@ -169,6 +166,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
@ -504,6 +503,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);
@ -512,7 +535,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

@ -124,13 +124,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