Filesystem Tests
This commit is contained in:
parent
dfc99faea8
commit
8bb70fb1b0
@ -54,6 +54,7 @@ default_envs = d1_mini
|
||||
# arduino core 2.6.1 = platformIO 2.3.0
|
||||
# arduino core 2.6.2 = platformIO 2.3.1
|
||||
# arduino core 2.6.3 = platformIO 2.3.2
|
||||
# arduino core 2.7.0 = platformIO 2.5.0
|
||||
# ------------------------------------------------------------------------------
|
||||
arduino_core_2_3_0 = espressif8266@1.5.0
|
||||
arduino_core_2_4_0 = espressif8266@1.6.0
|
||||
@ -65,12 +66,13 @@ arduino_core_2_5_2 = espressif8266@2.2.3
|
||||
arduino_core_2_6_1 = espressif8266@2.3.0
|
||||
arduino_core_2_6_2 = espressif8266@2.3.1
|
||||
arduino_core_2_6_3 = espressif8266@2.3.3
|
||||
arduino_core_2_7_0 = espressif8266@2.5.0
|
||||
|
||||
# Development platforms
|
||||
arduino_core_develop = https://github.com/platformio/platform-espressif8266#develop
|
||||
arduino_core_git = https://github.com/platformio/platform-espressif8266#feature/stage
|
||||
|
||||
platform = ${common.arduino_core_2_4_2}
|
||||
# Platform to use for ESP8266
|
||||
platform_latest = ${common.arduino_core_2_6_3}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
|
@ -1598,7 +1598,7 @@ uint16_t WS2812FX::mode_oscillate(void)
|
||||
uint16_t WS2812FX::mode_lightning(void)
|
||||
{
|
||||
uint16_t ledstart = random16(SEGLEN); // Determine starting location of flash
|
||||
uint16_t ledlen = random16(SEGLEN -1 -ledstart); // Determine length of flash (not to go beyond NUM_LEDS-1)
|
||||
uint16_t ledlen = 1 + random16(SEGLEN -ledstart); // Determine length of flash (not to go beyond NUM_LEDS-1)
|
||||
uint8_t bri = 255/random8(1, 3);
|
||||
|
||||
if (SEGENV.step == 0)
|
||||
|
@ -274,7 +274,7 @@ button {
|
||||
background: var(--c-bg);
|
||||
top: 0px;
|
||||
z-index: 1;
|
||||
margin-top: 1px;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
#staytop1 {
|
||||
|
@ -45,11 +45,10 @@ void handleDMX();
|
||||
void handleE131Packet(e131_packet_t* p, IPAddress clientIP, bool isArtnet);
|
||||
|
||||
//file.cpp
|
||||
bool find(const char* target, File f);
|
||||
bool handleFileRead(AsyncWebServerRequest*, String path);
|
||||
bool writeObjectToFileUsing(const char* file, const char* key, JsonObject content, File input, bool doClose);
|
||||
bool writeObjectToFile(const char* file, const char* key, JsonObject content, File input, bool doClose);
|
||||
bool appendObjectToFile(const char* file, const char* key, JsonObject& content, File input, bool doClose);
|
||||
bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonDocument* content);
|
||||
bool writeObjectToFile(const char* file, const char* key, JsonDocument* content);
|
||||
bool appendObjectToFile(const char* file, const char* key, JsonDocument* content, File input);
|
||||
bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest);
|
||||
bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest);
|
||||
|
||||
|
123
wled00/file.cpp
123
wled00/file.cpp
@ -15,44 +15,83 @@
|
||||
|
||||
#ifndef WLED_DISABLE_FILESYSTEM
|
||||
|
||||
bool find(const char *target, File f) {
|
||||
//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) {
|
||||
size_t targetLen = strlen(target);
|
||||
Serial.println(target);
|
||||
//Serial.println(f.position());
|
||||
size_t index = 0;
|
||||
int c;
|
||||
byte c;
|
||||
uint16_t bufsize = 0, count = 0;
|
||||
byte buf[256];
|
||||
|
||||
while(f.position() < f.size() -1) {
|
||||
c = f.read();
|
||||
if(c != target[index])
|
||||
while (f.position() < f.size() -1) {
|
||||
//c = f.read();
|
||||
//Serial.println(f.position());
|
||||
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(c == target[index]) {
|
||||
if(++index >= targetLen) { // return true if all chars in the target match
|
||||
return true;
|
||||
if(buf[count] == target[index]) {
|
||||
if(++index >= targetLen) { // return true if all chars in the target match
|
||||
f.seek((f.position() - bufsize) + count +1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonObject content, File input, bool doClose = true)
|
||||
//find empty spots in file stream in 256-byte blocks.
|
||||
bool bufferedFindSpace(uint16_t targetLen, File f) {
|
||||
size_t index = 0;
|
||||
byte c;
|
||||
uint16_t bufsize = 0, count = 0;
|
||||
byte buf[256];
|
||||
|
||||
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() - targetLen);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool writeObjectToFileUsingId(const char* file, uint16_t id, JsonDocument* content)
|
||||
{
|
||||
char objKey[10];
|
||||
sprintf(objKey, "\"%ld\":", id);
|
||||
writeObjectToFile(file, objKey, content, input, doClose);
|
||||
writeObjectToFile(file, objKey, content);
|
||||
}
|
||||
|
||||
bool writeObjectToFile(const char* file, const char* key, JsonObject content, File input, bool doClose = true)
|
||||
bool writeObjectToFile(const char* file, const char* key, JsonDocument* content)
|
||||
{
|
||||
uint32_t pos = 0;
|
||||
File f = (input) ? input : SPIFFS.open(file, "r+");
|
||||
File f = SPIFFS.open(file, "r+");
|
||||
if (!f) f = SPIFFS.open(file,"w");
|
||||
if (!f) return false;
|
||||
//f.setTimeout(1);
|
||||
f.seek(0, SeekSet);
|
||||
|
||||
if (!find(key, f)) //key does not exist in file
|
||||
if (!bufferedFind(key, f)) //key does not exist in file
|
||||
{
|
||||
return appendObjectToFile(file, key, content, f, true);
|
||||
return appendObjectToFile(file, key, content, f);
|
||||
}
|
||||
|
||||
//exists
|
||||
@ -63,9 +102,9 @@ bool writeObjectToFile(const char* file, const char* key, JsonObject content, Fi
|
||||
uint32_t pos2 = f.position();
|
||||
uint32_t oldLen = pos2 - pos;
|
||||
|
||||
if (!content.isNull() && measureJson(content) <= oldLen) //replace
|
||||
if (!content->isNull() && measureJson(*content) <= oldLen) //replace
|
||||
{
|
||||
serializeJson(content, f);
|
||||
serializeJson(*content, f);
|
||||
//pad rest
|
||||
for (uint32_t i = f.position(); i < pos2; i++) {
|
||||
f.write(' ');
|
||||
@ -77,53 +116,43 @@ bool writeObjectToFile(const char* file, const char* key, JsonObject content, Fi
|
||||
for (uint32_t i = pos; i < pos2; i++) {
|
||||
f.write(' ');
|
||||
}
|
||||
if (!content.isNull()) return appendObjectToFile(file, key, content, f, true);
|
||||
if (!content->isNull()) return appendObjectToFile(file, key, content, f);
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
|
||||
bool appendObjectToFile(const char* file, const char* key, JsonObject& content, File input, bool doClose = true)
|
||||
bool appendObjectToFile(const char* file, const char* key, JsonDocument* content, File input)
|
||||
{
|
||||
Serial.println("Append");
|
||||
uint32_t pos = 0;
|
||||
File f = (input) ? input : SPIFFS.open(file, "r+");
|
||||
if (!f) f = SPIFFS.open(file,"w");
|
||||
if (!f) return false;
|
||||
f.setTimeout(1);
|
||||
if (f.size() < 3) f.print("{}");
|
||||
|
||||
//if there is enough empty space in file, insert there instead of appending
|
||||
uint32_t contentLen = measureJson(content);
|
||||
uint32_t spaces = 0, spaceI = 0, spacesMax = 0, spaceMaxI = 0;
|
||||
f.seek(1, SeekSet);
|
||||
for (uint32_t i = 1; i < f.size(); i++)
|
||||
{
|
||||
if (f.read() == ' ') {
|
||||
if (!spaces) spaceI = i; spaces++;
|
||||
} else {
|
||||
if (spaces > spacesMax) { spacesMax = spaces; spaceMaxI = spaceI;}
|
||||
spaces = 0;
|
||||
if (spacesMax >= contentLen) {
|
||||
f.seek(spaceMaxI, SeekSet);
|
||||
serializeJson(content, f);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
uint32_t contentLen = measureJson(*content);
|
||||
Serial.print("clen"); Serial.println(contentLen);
|
||||
if (bufferedFindSpace(contentLen, f)) {
|
||||
Serial.println("space");
|
||||
serializeJson(*content, f);
|
||||
return true;
|
||||
}
|
||||
|
||||
//check if last character in file is '}' (typical)
|
||||
uint32_t lastByte = f.size() -1;
|
||||
f.seek(1, SeekEnd);
|
||||
if (f.read() == '}') pos = lastByte;
|
||||
if (f.read() == '}') pos = f.size() -1;
|
||||
|
||||
if (pos == 0) //not found
|
||||
{
|
||||
while (find("}",f)) //find last closing bracket in JSON if not last char
|
||||
Serial.println("not}");
|
||||
while (bufferedFind("}",f)) //find last closing bracket in JSON if not last char
|
||||
{
|
||||
pos = f.position();
|
||||
}
|
||||
}
|
||||
|
||||
if (pos)
|
||||
Serial.print("pos"); Serial.println(pos);
|
||||
if (pos < 3)
|
||||
{
|
||||
f.seek(pos -1, SeekSet);
|
||||
f.write(',');
|
||||
@ -132,14 +161,14 @@ bool appendObjectToFile(const char* file, const char* key, JsonObject& content,
|
||||
f.write('{'); //start JSON
|
||||
}
|
||||
|
||||
f.print("\"");
|
||||
//f.print("\"");
|
||||
f.print(key);
|
||||
f.print("\":");
|
||||
//f.print("\":");
|
||||
//Append object
|
||||
serializeJson(content, f);
|
||||
serializeJson(*content, f);
|
||||
|
||||
f.write('}');
|
||||
if (doClose) f.close();
|
||||
f.close();
|
||||
}
|
||||
|
||||
bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest)
|
||||
@ -156,9 +185,9 @@ bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest)
|
||||
|
||||
File f = SPIFFS.open(file, "r");
|
||||
if (!f) return false;
|
||||
f.setTimeout(0);
|
||||
Serial.println(key);
|
||||
if (f.find(key)) //key does not exist in file
|
||||
//f.setTimeout(0);
|
||||
//Serial.println(key);
|
||||
if (!bufferedFind(key, f)) //key does not exist in file
|
||||
{
|
||||
f.close();
|
||||
return false;
|
||||
|
@ -657,6 +657,12 @@ bool applyPreset(byte index, bool loadBri)
|
||||
|
||||
void savePreset(byte index, bool persist)
|
||||
{
|
||||
StaticJsonDocument<1024> doc;
|
||||
serializeState(doc.to<JsonObject>());
|
||||
doc["p"]=50;
|
||||
serializeJson(doc, Serial);
|
||||
writeObjectToFileUsingId("/presets.json", index, &doc);
|
||||
return;
|
||||
if (index > 16) return;
|
||||
if (index < 1) {saveSettingsToEEPROM();return;}
|
||||
uint16_t i = 380 + index*20;//min400
|
||||
|
Loading…
Reference in New Issue
Block a user