Sqeeze every byte

This commit is contained in:
Blaz Kristan
2024-02-25 17:08:01 +01:00
parent a28d2c869f
commit 41129cf379
11 changed files with 82 additions and 58 deletions

View File

@@ -112,6 +112,10 @@ bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest
bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest);
void updateFSInfo();
void closeFile();
inline bool writeObjectToFileUsingId(const String &file, uint16_t id, JsonDocument* content) { return writeObjectToFileUsingId(file.c_str(), id, content); };
inline bool writeObjectToFile(const String &file, const char* key, JsonDocument* content) { return writeObjectToFile(file.c_str(), key, content); };
inline bool readObjectFromFileUsingId(const String &file, uint16_t id, JsonDocument* dest) { return readObjectFromFileUsingId(file.c_str(), id, dest); };
inline bool readObjectFromFile(const String &file, const char* key, JsonDocument* dest) { return readObjectFromFile(file.c_str(), key, dest); };
//hue.cpp
void handleHue();
@@ -225,6 +229,7 @@ void handlePlaylist();
void serializePlaylist(JsonObject obj);
//presets.cpp
const char *getPresetsFileName(bool persistent = true);
void initPresetsFile();
void handlePresets();
bool applyPreset(byte index, byte callMode = CALL_MODE_DIRECT_CHANGE);

View File

@@ -416,7 +416,7 @@ static const uint8_t *getPresetCache(size_t &size) {
}
if (!presetsCached) {
File file = WLED_FS.open("/presets.json", "r");
File file = WLED_FS.open(FPSTR(getPresetsFileName()), "r");
if (file) {
presetsCachedTime = presetsModifiedTime;
presetsCachedSize = 0;
@@ -446,7 +446,7 @@ bool handleFileRead(AsyncWebServerRequest* request, String path){
return true;
}*/
#if defined(BOARD_HAS_PSRAM) && defined(WLED_USE_PSRAM)
if (path.endsWith("/presets.json")) {
if (path.endsWith(FPSTR(getPresetsFileName()))) {
size_t psize;
const uint8_t *presets = getPresetCache(psize);
if (presets) {

View File

@@ -317,7 +317,8 @@ void getTimeString(char* out)
sprintf_P(out,PSTR("%i-%i-%i, %02d:%02d:%02d"),year(localTime), month(localTime), day(localTime), hr, minute(localTime), second(localTime));
if (useAMPM)
{
strcat(out,(hour(localTime) > 11)? " PM":" AM");
strcat_P(out,PSTR(" "));
strcat(out,(hour(localTime) > 11)? "PM":"AM");
}
}

View File

@@ -109,7 +109,7 @@ bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, by
#ifdef WLED_DEBUG
DEBUG_PRINT(F("PIN ALLOC: Invalid pin attempted to be allocated: GPIO "));
DEBUG_PRINT(gpio);
DEBUG_PRINT(F(" as ")); DEBUG_PRINT(mptArray[i].isOutput ? "output": "input");
DEBUG_PRINT(F(" as ")); DEBUG_PRINT(mptArray[i].isOutput ? F("output"): F("input"));
DEBUG_PRINTLN(F(""));
#endif
shouldFail = true;

View File

@@ -16,13 +16,14 @@ static char quickLoad[9];
static char saveName[33];
static bool includeBri = true, segBounds = true, selectedOnly = false, playlistSave = false;;
static const char *getFileName(bool persist = true) {
return persist ? "/presets.json" : "/tmp.json";
static const char presets_json[] PROGMEM = "/presets.json";
static const char tmp_json[] PROGMEM = "/tmp.json";
const char *getPresetsFileName(bool persistent) {
return persistent ? presets_json : tmp_json;
}
static void doSaveState() {
bool persist = (presetToSave < 251);
const char *filename = getFileName(persist);
unsigned long start = millis();
while (strip.isUpdating() && millis()-start < (2*FRAMETIME_FIXED)+1) yield(); // wait 2 frames
@@ -63,11 +64,11 @@ static void doSaveState() {
if (tmpRAMbuffer!=nullptr) {
serializeJson(*fileDoc, tmpRAMbuffer, len);
} else {
writeObjectToFileUsingId(filename, presetToSave, fileDoc);
writeObjectToFileUsingId(FPSTR(getPresetsFileName(persist)), presetToSave, fileDoc);
}
} else
#endif
writeObjectToFileUsingId(filename, presetToSave, fileDoc);
writeObjectToFileUsingId(FPSTR(getPresetsFileName(persist)), presetToSave, fileDoc);
if (persist) presetsModifiedTime = toki.second(); //unix time
releaseJSONBufferLock();
@@ -85,8 +86,7 @@ bool getPresetName(byte index, String& name)
{
if (!requestJSONBufferLock(19)) return false;
bool presetExists = false;
if (readObjectFromFileUsingId(getFileName(), index, pDoc))
{
if (readObjectFromFileUsingId(FPSTR(getPresetsFileName()), index, pDoc)) {
JsonObject fdo = pDoc->as<JsonObject>();
if (fdo["n"]) {
name = (const char*)(fdo["n"]);
@@ -99,12 +99,12 @@ bool getPresetName(byte index, String& name)
void initPresetsFile()
{
if (WLED_FS.exists(getFileName())) return;
if (WLED_FS.exists(FPSTR(getPresetsFileName()))) return;
StaticJsonDocument<64> doc;
JsonObject sObj = doc.to<JsonObject>();
sObj.createNestedObject("0");
File f = WLED_FS.open(getFileName(), "w");
File f = WLED_FS.open(FPSTR(getPresetsFileName()), "w");
if (!f) {
errorFlag = ERR_FS_GENERAL;
return;
@@ -147,7 +147,6 @@ void handlePresets()
uint8_t tmpMode = callModeToApply;
JsonObject fdo;
const char *filename = getFileName(tmpPreset < 255);
// allocate buffer
if (!requestJSONBufferLock(9)) return; // will also assign fileDoc
@@ -165,7 +164,7 @@ void handlePresets()
} else
#endif
{
errorFlag = readObjectFromFileUsingId(filename, tmpPreset, fileDoc) ? ERR_NONE : ERR_FS_PLOAD;
errorFlag = readObjectFromFileUsingId(FPSTR(getPresetsFileName(tmpPreset < 255)), tmpPreset, fileDoc) ? ERR_NONE : ERR_FS_PLOAD;
}
fdo = fileDoc->as<JsonObject>();
@@ -234,7 +233,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
sObj.remove(F("psave"));
if (sObj["n"].isNull()) sObj["n"] = saveName;
initPresetsFile(); // just in case if someone deleted presets.json using /edit
writeObjectToFileUsingId(getFileName(index<255), index, fileDoc);
writeObjectToFileUsingId(FPSTR(getPresetsFileName()), index, fileDoc);
presetsModifiedTime = toki.second(); //unix time
updateFSInfo();
} else {
@@ -248,7 +247,7 @@ void savePreset(byte index, const char* pname, JsonObject sObj)
void deletePreset(byte index) {
StaticJsonDocument<24> empty;
writeObjectToFileUsingId(getFileName(), index, &empty);
writeObjectToFileUsingId(FPSTR(getPresetsFileName()), index, &empty);
presetsModifiedTime = toki.second(); //unix time
updateFSInfo();
}

View File

@@ -328,7 +328,7 @@ void WLED::setup()
DEBUG_PRINTLN();
DEBUG_PRINT(F("---WLED "));
DEBUG_PRINT(versionString);
DEBUG_PRINT(" ");
DEBUG_PRINT(F(" "));
DEBUG_PRINT(VERSION);
DEBUG_PRINTLN(F(" INIT---"));
#ifdef ARDUINO_ARCH_ESP32

View File

@@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2402170
#define VERSION 2402250
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG

View File

@@ -365,7 +365,7 @@ void applyMacro(byte index) {
// De-EEPROM routine, upgrade from previous versions to v0.11
void deEEP() {
if (WLED_FS.exists("/presets.json")) return;
if (WLED_FS.exists(FPSTR(getPresetsFileName()))) return;
DEBUG_PRINTLN(F("Preset file not found, attempting to load from EEPROM"));
DEBUGFS_PRINTLN(F("Allocating saving buffer for dEEP"));
@@ -442,7 +442,7 @@ void deEEP() {
EEPROM.end();
File f = WLED_FS.open("/presets.json", "w");
File f = WLED_FS.open(FPSTR(getPresetsFileName()), "w");
if (!f) {
errorFlag = ERR_FS_GENERAL;
releaseJSONBufferLock();

View File

@@ -170,7 +170,7 @@ static void handleUpload(AsyncWebServerRequest *request, const String& filename,
request->_tempFile = WLED_FS.open(finalname, "w");
DEBUG_PRINT(F("Uploading "));
DEBUG_PRINTLN(finalname);
if (finalname.equals(F("/presets.json"))) presetsModifiedTime = toki.second();
if (finalname.equals(FPSTR(getPresetsFileName()))) presetsModifiedTime = toki.second();
}
if (len) {
request->_tempFile.write(data,len);
@@ -249,17 +249,19 @@ void initServer()
});
// "/settings/settings.js&p=x" request also handled by serveSettings()
server.on(SET_F("/style.css"), HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, F("/style.css"), 200, FPSTR(s_css), PAGE_settingsCss, PAGE_settingsCss_length);
static const char _style_css[] PROGMEM = "/style.css";
server.on(_style_css, HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, FPSTR(_style_css), 200, FPSTR(s_css), PAGE_settingsCss, PAGE_settingsCss_length);
});
server.on(SET_F("/favicon.ico"), HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, F("/favicon.ico"), 200, F("image/x-icon"), favicon, favicon_length, false);
static const char _favicon_ico[] PROGMEM = "/favicon.ico";
server.on(_favicon_ico, HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, FPSTR(_favicon_ico), 200, F("image/x-icon"), favicon, favicon_length, false);
});
server.on(SET_F("/skin.css"), HTTP_GET, [](AsyncWebServerRequest *request) {
if (handleFileRead(request, F("/skin.css"))) return;
static const char _skin_css[] PROGMEM = "/skin.css";
server.on(_skin_css, HTTP_GET, [](AsyncWebServerRequest *request) {
if (handleFileRead(request, FPSTR(_skin_css))) return;
AsyncWebServerResponse *response = request->beginResponse(200, FPSTR(s_css));
request->send(response);
});
@@ -363,15 +365,17 @@ void initServer()
createEditHandler(correctPIN);
#ifndef WLED_DISABLE_OTA
static const char _update[] PROGMEM = "/update";
//init ota page
server.on(SET_F("/update"), HTTP_GET, [](AsyncWebServerRequest *request){
server.on(_update, HTTP_GET, [](AsyncWebServerRequest *request){
if (otaLock) {
serveMessage(request, 401, FPSTR(s_accessdenied), FPSTR(s_unlock_ota), 254);
} else
serveSettings(request); // checks for "upd" in URL and handles PIN
});
server.on(SET_F("/update"), HTTP_POST, [](AsyncWebServerRequest *request){
server.on(_update, HTTP_POST, [](AsyncWebServerRequest *request){
if (!correctPIN) {
serveSettings(request, true); // handle PIN page POST request
return;
@@ -417,7 +421,7 @@ void initServer()
}
});
#else
server.on(SET_F("/update"), HTTP_GET, [](AsyncWebServerRequest *request){
server.on(_update, HTTP_GET, [](AsyncWebServerRequest *request){
serveMessage(request, 501, FPSTR(s_notimplemented), F("OTA updating is disabled in this build."), 254);
});
#endif
@@ -443,19 +447,22 @@ void initServer()
});
#ifdef WLED_ENABLE_PIXART
server.on(SET_F("/pixart.htm"), HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, F("/pixart.htm"), 200, FPSTR(s_html), PAGE_pixart, PAGE_pixart_L);
static const char _pixart_htm[] PROGMEM = "/pixart.htm";
server.on(_pixart_htm, HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, FPSTR(_pixart_htm), 200, FPSTR(s_html), PAGE_pixart, PAGE_pixart_L);
});
#endif
#ifndef WLED_DISABLE_PXMAGIC
server.on(SET_F("/pxmagic.htm"), HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, F("/pxmagic.htm"), 200, FPSTR(s_html), PAGE_pxmagic, PAGE_pxmagic_L);
static const char _pxmagic_htm[] PROGMEM = "/pxmagic.htm";
server.on(_pxmagic_htm, HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, FPSTR(_pxmagic_htm), 200, FPSTR(s_html), PAGE_pxmagic, PAGE_pxmagic_L);
});
#endif
server.on(SET_F("/cpal.htm"), HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, F("/cpal.htm"), 200, FPSTR(s_html), PAGE_cpal, PAGE_cpal_L);
static const char _cpal_htm[] PROGMEM = "/cpal.htm";
server.on(_cpal_htm, HTTP_GET, [](AsyncWebServerRequest *request) {
handleStaticContent(request, FPSTR(_cpal_htm), 200, FPSTR(s_html), PAGE_cpal, PAGE_cpal_L);
});
#ifdef WLED_ENABLE_WEBSOCKETS