optimizations

This commit is contained in:
Stefan Ostermann 2025-10-31 23:19:42 +01:00
parent e677a7a8bf
commit 2069d36715
1 changed files with 42 additions and 46 deletions

View File

@ -26,11 +26,10 @@
#include "main.h" #include "main.h"
// webrequest_blockings is a simple watchdog counter that tracks how long at least one HTTP request has been “active” (not yet disconnected) according to the AsyncWebServer. // webrequest_blockings is a simple watchdog counter that tracks how long at least one HTTP request has been “active” (not yet disconnected) according to the AsyncWebServer.
int webrequest_blockings = 0; int webrequest_blockings = 0;
void activateSD() void activateSD()
{ {
if (SDActive) if (SDActive)
@ -76,16 +75,12 @@ String humanReadableSize(const size_t bytes)
return String(bytes) + " B"; return String(bytes) + " B";
else if (bytes < (1024 * 1024)) else if (bytes < (1024 * 1024))
return String(bytes / 1024.0) + " KB"; return String(bytes / 1024.0) + " KB";
else if (bytes < (1024 * 1024 * 1024))
return String(bytes / 1024.0 / 1024.0) + " MB";
else else
return String(bytes / 1024.0 / 1024.0 / 1024.0) + " GB"; return String(bytes / 1024.0 / 1024.0) + " MB";
} }
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{ {
static String logBuffer; // Static to avoid repeated allocations
if (!index) if (!index)
{ {
// Validate filename and file extension // Validate filename and file extension
@ -113,15 +108,6 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
return; return;
} }
// Pre-allocate log buffer
logBuffer.reserve(128);
logBuffer = "Upload Start: ";
logBuffer += filename;
logBuffer += " (Free: ";
logBuffer += String(freeSpace);
logBuffer += "MB)";
Serial.println(logBuffer);
logBuffer.clear(); // Free memory immediately
// Ensure SD is active // Ensure SD is active
activateSD(); activateSD();
@ -188,14 +174,6 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
} }
sd_lock_release(); sd_lock_release();
// Reduce logging frequency to save memory - log every 200KB instead of 100KB
if (len && (index % 204800 == 0))
{
logBuffer = "Upload: ";
logBuffer += humanReadableSize(index + len);
Serial.println(logBuffer);
logBuffer.clear();
}
} }
if (final) if (final)
@ -207,12 +185,10 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
request->_tempFile.close(); request->_tempFile.close();
sd_lock_release(); sd_lock_release();
logBuffer = "Upload Complete: "; Serial.print(F("Upload Complete: "));
logBuffer += filename; Serial.print(filename);
logBuffer += ", size: "; Serial.print(", size: ");
logBuffer += humanReadableSize(index + len); Serial.println(humanReadableSize(index + len));
Serial.println(logBuffer);
logBuffer.clear();
// Rebuild directory tree to include new file (guarded) // Rebuild directory tree to include new file (guarded)
sd_lock_acquire(); sd_lock_acquire();
@ -295,7 +271,8 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0)
if (currentNode == nullptr) if (currentNode == nullptr)
{ {
Serial.println("No node found for ID: " + String(id)); Serial.print(F("No node found for ID: "));
Serial.println(id);
return; return;
} }
@ -303,7 +280,8 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0)
if (currentNode->getCurrentPlaying() == nullptr) if (currentNode->getCurrentPlaying() == nullptr)
{ {
currentNode = nullptr; currentNode = nullptr;
Serial.println("No song found for ID: " + String(id)); Serial.print(F("No song found for ID: "));
Serial.println(id);
return; return;
} }
@ -311,20 +289,22 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0)
if (mp3File.length() == 0) if (mp3File.length() == 0)
{ {
currentNode = nullptr; currentNode = nullptr;
Serial.println("Empty file path for ID: " + String(id)); Serial.print("Empty file path for ID: ");
Serial.println(id);
return; return;
} }
Serial.print("Playing by ID: "); Serial.print("Playing by ID: ");
Serial.println(id); Serial.println(id);
Serial.println(mp3File.c_str()); Serial.println(mp3File);
deactivateRFID(); deactivateRFID();
activateSD(); activateSD();
if (!playFile(mp3File.c_str())) if (!playFile(mp3File.c_str()))
{ {
Serial.println("Failed to play file: " + mp3File); Serial.print(F("Failed to play file: "));
Serial.println(mp3File);
currentNode = nullptr; currentNode = nullptr;
return; return;
} }
@ -1133,7 +1113,12 @@ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
deactivateRFID(); deactivateRFID();
activateSD(); activateSD();
sd_lock_acquire(); sd_lock_acquire();
String htmlPath = getSysDir(index_file); static String htmlPath = "";
if (htmlPath.isEmpty()) {
htmlPath = getSysDir(index_file);
}
if (SD.exists(htmlPath)) if (SD.exists(htmlPath))
{ {
uint32_t fsize = 0; uint32_t fsize = 0;
@ -1164,7 +1149,11 @@ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
deactivateRFID(); deactivateRFID();
activateSD(); activateSD();
// Ensure SD is active and RFID is deactivated while serving files. // Ensure SD is active and RFID is deactivated while serving files.
String cssPath = getSysDir(style_file); static String cssPath = "";
if (cssPath.isEmpty()) {
cssPath = getSysDir(style_file);
}
if (SD.exists(cssPath)) if (SD.exists(cssPath))
{ {
uint32_t fsize = 0; uint32_t fsize = 0;
@ -1197,7 +1186,12 @@ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
deactivateRFID(); deactivateRFID();
activateSD(); activateSD();
sd_lock_acquire(); sd_lock_acquire();
String jsPath = getSysDir(script_file);
static String jsPath = "";
if (jsPath.isEmpty()) {
jsPath = getSysDir(script_file);
}
if (SD.exists(jsPath)) if (SD.exists(jsPath))
{ {
uint32_t fsize = 0; uint32_t fsize = 0;
@ -1421,13 +1415,11 @@ void setup()
// Memory optimizations for WiFiManager // Memory optimizations for WiFiManager
wifiManager.setDebugOutput(true); // Disable debug strings wifiManager.setDebugOutput(true); // Disable debug strings
wifiManager.setMinimumSignalQuality(20); // Reduce AP scan results
wifiManager.setRemoveDuplicateAPs(true); // Remove duplicate APs from memory
// Reduce timeouts to free memory faster // Reduce timeouts to free memory faster
wifiManager.setTimeout(180); // Reduced from 180 wifiManager.setTimeout(180); // Reduced from 180
wifiManager.setConnectTimeout(15); // Faster connection attempts wifiManager.setConnectTimeout(20); // Faster connection attempts
wifiManager.setConfigPortalTimeout(90); // Shorter portal timeout wifiManager.setConfigPortalTimeout(120); // Shorter portal timeout
#ifdef DEBUG #ifdef DEBUG
Serial.println(F("Deactivating Brownout detector...")); Serial.println(F("Deactivating Brownout detector..."));
@ -1518,7 +1510,7 @@ void volume_action(AsyncWebServerRequest *request)
const String getSysDir(const String filename) const String getSysDir(const String filename)
{ {
static String st_sys_str(96); String st_sys_str(96);
st_sys_str.clear(); st_sys_str.clear();
st_sys_str.concat("/"); st_sys_str.concat("/");
st_sys_str.concat(sys_dir); st_sys_str.concat(sys_dir);
@ -1587,14 +1579,18 @@ void loop()
prepareSleepMode = true; prepareSleepMode = true;
if (currentNode != nullptr) if (currentNode != nullptr)
{ {
String progressPath = getSysDir(progress_file); static String progressPath = "";
if (progressPath.isEmpty()) {
progressPath = getSysDir(progress_file);
}
deactivateRFID(); deactivateRFID();
activateSD(); activateSD();
writeSongProgress(progressPath.c_str(), currentNode->getCurrentPlayingId(), currentNode->getSecondsPlayed()); writeSongProgress(progressPath.c_str(), currentNode->getCurrentPlayingId(), currentNode->getSecondsPlayed());
} }
String tempPath = getSysDir(sleep_sound); playSongByPath(getSysDir(sleep_sound));
playSongByPath(tempPath.c_str());
} }
if (now - lastInteraction > config.sleepDelay) if (now - lastInteraction > config.sleepDelay)