Presets work

This commit is contained in:
cschwinne 2020-09-14 00:31:38 +02:00
parent 96713ef383
commit 1263f5e046
4 changed files with 17 additions and 20 deletions

View File

@ -365,5 +365,5 @@ build_flags = ${common.build_flags_esp8266} ${common.debug_flags} ${common.build
[env:travis_esp32] [env:travis_esp32]
extends = env:esp32dev extends = env:esp32dev
build_type = debug ; build_type = debug
build_flags = ${common.build_flags_esp32} ${common.debug_flags} ${common.build_flags_all_features} build_flags = ${common.build_flags_esp32} ${common.debug_flags} ${common.build_flags_all_features}

View File

@ -4,15 +4,6 @@
* Utility for SPIFFS filesystem * Utility for SPIFFS filesystem
*/ */
//filesystem
#ifndef WLED_DISABLE_FILESYSTEM
#include <FS.h>
#ifdef ARDUINO_ARCH_ESP32
#include "SPIFFS.h"
#endif
#include "SPIFFSEditor.h"
#endif
#ifndef WLED_DISABLE_FILESYSTEM #ifndef WLED_DISABLE_FILESYSTEM
//find() that reads and buffers data from file stream in 256-byte blocks. //find() that reads and buffers data from file stream in 256-byte blocks.
@ -62,7 +53,6 @@ bool bufferedFindSpace(uint16_t targetLen, File f) {
#endif #endif
if (!f || !f.size()) return false; if (!f || !f.size()) return false;
DEBUGFS_PRINTF("Filesize %d\n", f.size());
uint16_t index = 0; uint16_t index = 0;
uint16_t bufsize = 0, count = 0; uint16_t bufsize = 0, count = 0;
@ -94,7 +84,7 @@ bool bufferedFindSpace(uint16_t targetLen, File f) {
bool appendObjectToFile(File f, const char* key, JsonDocument* content, uint32_t s) bool appendObjectToFile(File f, const char* key, JsonDocument* content, uint32_t s)
{ {
#ifdef WLED_DEBUG_FS #ifdef WLED_DEBUG_FS
DEBUG_PRINTLN("Append"); DEBUGFS_PRINTLN("Append");
uint32_t s1 = millis(); uint32_t s1 = millis();
#endif #endif
uint32_t pos = 0; uint32_t pos = 0;
@ -108,6 +98,7 @@ bool appendObjectToFile(File f, const char* key, JsonDocument* content, uint32_t
if (f.position() > 2) f.write(','); //add comma if not first object if (f.position() > 2) f.write(','); //add comma if not first object
f.print(key); f.print(key);
serializeJson(*content, f); serializeJson(*content, f);
DEBUGFS_PRINTF("Inserted, took %d ms (total %d)", millis() - s1, millis() - s);
return true; return true;
} }
@ -188,7 +179,7 @@ bool writeObjectToFile(const char* file, const char* key, JsonDocument* content)
if (!content->isNull() && measureJson(*content) <= oldLen) //replace if (!content->isNull() && measureJson(*content) <= oldLen) //replace
{ {
DEBUG_PRINTLN("replace"); DEBUGFS_PRINTLN("replace");
f.seek(pos); f.seek(pos);
serializeJson(*content, f); serializeJson(*content, f);
//pad rest //pad rest
@ -196,7 +187,7 @@ bool writeObjectToFile(const char* file, const char* key, JsonDocument* content)
f.write(' '); f.write(' ');
} }
} else { //delete } else { //delete
DEBUG_PRINTLN("delete"); DEBUGFS_PRINTLN("delete");
pos -= strlen(key); pos -= strlen(key);
if (pos > 3) pos--; //also delete leading comma if not first object if (pos > 3) pos--; //also delete leading comma if not first object
f.seek(pos); f.seek(pos);

View File

@ -123,7 +123,11 @@
#endif #endif
//Filesystem to use for preset and config files. SPIFFS or LittleFS on ESP8266, SPIFFS only on ESP32 //Filesystem to use for preset and config files. SPIFFS or LittleFS on ESP8266, SPIFFS only on ESP32
#define WLED_FS LittleFS #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) // 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) //#if defined(WLED_USE_ANALOG_LEDS) && defined(ESP8266)

View File

@ -675,15 +675,17 @@ bool applyPreset(byte index, bool loadBri)
void savePreset(byte index, bool persist, const char* pname, byte priority, JsonObject saveobj) void savePreset(byte index, bool persist, const char* pname, byte priority, JsonObject saveobj)
{ {
StaticJsonDocument<1024> doc; StaticJsonDocument<1024> doc;
JsonObject sObj = doc.to<JsonObject>();
if (saveobj.isNull()) { if (saveobj.isNull()) {
Serial.println("Save current state"); DEBUGFS_PRINTLN("Save current state");
serializeState(doc.to<JsonObject>(), true); serializeState(doc.to<JsonObject>(), true);
} else { } else {
Serial.println("Save custom"); DEBUGFS_PRINTLN("Save custom");
doc = saveobj; sObj.set(saveobj);
} }
doc["p"] = priority; sObj["p"] = priority;
if (pname) doc["n"] = pname; if (pname) sObj["n"] = pname;
//serializeJson(doc, Serial); //serializeJson(doc, Serial);
writeObjectToFileUsingId("/presets.json", index, &doc); writeObjectToFileUsingId("/presets.json", index, &doc);