[ai] Memory Optimizations
This commit is contained in:
@@ -19,8 +19,8 @@ lib_deps =
|
||||
monitor_speed = 115200
|
||||
build_flags =
|
||||
-Os ; Optimize for size
|
||||
; -DDEBUG ; Hannabox Debugging
|
||||
-DCORE_DEBUG_LEVEL=0 ; Disable all debug output
|
||||
-DDEBUG ; Hannabox Debugging
|
||||
; -DCORE_DEBUG_LEVEL=0 ; Disable all debug output
|
||||
; -DARDUINO_LOOP_STACK_SIZE=4096 ; Balanced to avoid stack canary without starving heap
|
||||
; -DWIFI_TASK_STACK_SIZE=3072 ; Reduce WiFi task stack
|
||||
; -DARDUINO_EVENT_TASK_STACK_SIZE=2048 ; Reduce event task stack
|
||||
|
||||
@@ -405,11 +405,11 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String &songName)
|
||||
}
|
||||
else
|
||||
{
|
||||
// Use static buffer for case conversion and comparison
|
||||
// Use static buffer for comparison without allocation
|
||||
buildFullPath(mp3Files[i], buffer, buffer_size);
|
||||
String f = String(buffer);
|
||||
f.toLowerCase();
|
||||
if (f.endsWith(lowTarget))
|
||||
size_t bufLen = strlen(buffer);
|
||||
size_t targetLen = lowTarget.length();
|
||||
if (bufLen >= targetLen && strcasecmp(buffer + bufLen - targetLen, lowTarget.c_str()) == 0)
|
||||
{
|
||||
setCurrentPlaying(mp3Files[i]);
|
||||
return this;
|
||||
@@ -450,9 +450,11 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String &songName)
|
||||
}
|
||||
else
|
||||
{
|
||||
String f = subdir->mp3Files[i];
|
||||
f.toLowerCase();
|
||||
if (f.endsWith(lowTarget))
|
||||
// Check suffix case-insensitively without creating new Strings
|
||||
const String& fName = subdir->mp3Files[i];
|
||||
size_t fLen = fName.length();
|
||||
size_t targetLen = lowTarget.length();
|
||||
if (fLen >= targetLen && strcasecmp(fName.c_str() + fLen - targetLen, lowTarget.c_str()) == 0)
|
||||
{
|
||||
subdir->setCurrentPlaying(subdir->mp3Files[i]);
|
||||
return subdir;
|
||||
|
||||
58
src/main.cpp
58
src/main.cpp
@@ -147,10 +147,8 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
|
||||
activateSD();
|
||||
|
||||
// Check if file already exists and create backup name if needed
|
||||
String filepath;
|
||||
filepath.reserve(1 + filename.length());
|
||||
filepath = "/";
|
||||
filepath += filename;
|
||||
char filepath[256];
|
||||
snprintf(filepath, sizeof(filepath), "/%s", filename.c_str());
|
||||
if (SD.exists(filepath))
|
||||
{
|
||||
request->send(500, txt_plain, F("File already exists."));
|
||||
@@ -806,54 +804,6 @@ void readDataFromFile(const String &filename)
|
||||
}
|
||||
}
|
||||
|
||||
String processor(const String &var)
|
||||
{
|
||||
|
||||
if (var == "MAPPING")
|
||||
{
|
||||
auto htmlEscape = [](const String &s) -> String
|
||||
{
|
||||
String out;
|
||||
for (size_t i = 0; i < s.length(); ++i)
|
||||
{
|
||||
char c = s[i];
|
||||
if (c == '&')
|
||||
out += "&";
|
||||
else if (c == '<')
|
||||
out += "";
|
||||
else if (c == '>')
|
||||
out += "";
|
||||
else if (c == '"')
|
||||
out += "";
|
||||
else if (c == '\'')
|
||||
out += "";
|
||||
else
|
||||
out += c;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
String html;
|
||||
html.reserve(256);
|
||||
html.concat(F("<table style='width:100%;border-collapse:collapse;'><tr><th style='border:1px solid #ccc;padding:4px;'>RFID</th><th style='border:1px solid #ccc;padding:4px;'>Song</th></tr>"));
|
||||
for (const auto &pair : rfid_map)
|
||||
{
|
||||
html.concat(F("<tr><td style='border:1px solid #ccc;padding:4px;'>"));
|
||||
html.concat(htmlEscape(pair.first));
|
||||
html.concat(F("</td><td style='border:1px solid #ccc;padding:4px;'>"));
|
||||
// Show target and mode (e.g. "mysong.mp3|s")
|
||||
String mappingVal = pair.second.target;
|
||||
mappingVal += "|";
|
||||
mappingVal += pair.second.mode;
|
||||
html.concat(htmlEscape(mappingVal));
|
||||
html.concat(F("</td></tr>"));
|
||||
}
|
||||
html.concat(F("</table>"));
|
||||
return html;
|
||||
}
|
||||
|
||||
return String(); // Return empty string instead of creating new String
|
||||
}
|
||||
|
||||
// Memory-optimized helpers and streamers to avoid large temporary Strings
|
||||
|
||||
static inline void htmlEscapeAndPrint(Print &out, const String &s)
|
||||
@@ -1565,8 +1515,8 @@ void setup()
|
||||
wifiManager.setDebugOutput(true);
|
||||
|
||||
// Reduce timeouts to free memory faster
|
||||
wifiManager.setTimeout(180); // Reduced from 180
|
||||
wifiManager.setConnectTimeout(20); // Faster connection attempts
|
||||
wifiManager.setTimeout(150); // Reduced from 180
|
||||
wifiManager.setConnectTimeout(30); // Faster connection attempts
|
||||
wifiManager.setConfigPortalTimeout(120); // Shorter portal timeout
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
Reference in New Issue
Block a user