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"
// 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;
void activateSD()
{
if (SDActive)
@ -76,16 +75,12 @@ String humanReadableSize(const size_t bytes)
return String(bytes) + " B";
else if (bytes < (1024 * 1024))
return String(bytes / 1024.0) + " KB";
else if (bytes < (1024 * 1024 * 1024))
return String(bytes / 1024.0 / 1024.0) + " MB";
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)
{
static String logBuffer; // Static to avoid repeated allocations
if (!index)
{
// Validate filename and file extension
@ -113,15 +108,6 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
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
activateSD();
@ -188,14 +174,6 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
}
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)
@ -207,12 +185,10 @@ void handleUpload(AsyncWebServerRequest *request, String filename, size_t index,
request->_tempFile.close();
sd_lock_release();
logBuffer = "Upload Complete: ";
logBuffer += filename;
logBuffer += ", size: ";
logBuffer += humanReadableSize(index + len);
Serial.println(logBuffer);
logBuffer.clear();
Serial.print(F("Upload Complete: "));
Serial.print(filename);
Serial.print(", size: ");
Serial.println(humanReadableSize(index + len));
// Rebuild directory tree to include new file (guarded)
sd_lock_acquire();
@ -295,7 +271,8 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0)
if (currentNode == nullptr)
{
Serial.println("No node found for ID: " + String(id));
Serial.print(F("No node found for ID: "));
Serial.println(id);
return;
}
@ -303,7 +280,8 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0)
if (currentNode->getCurrentPlaying() == nullptr)
{
currentNode = nullptr;
Serial.println("No song found for ID: " + String(id));
Serial.print(F("No song found for ID: "));
Serial.println(id);
return;
}
@ -311,20 +289,22 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0)
if (mp3File.length() == 0)
{
currentNode = nullptr;
Serial.println("Empty file path for ID: " + String(id));
Serial.print("Empty file path for ID: ");
Serial.println(id);
return;
}
Serial.print("Playing by ID: ");
Serial.println(id);
Serial.println(mp3File.c_str());
Serial.println(mp3File);
deactivateRFID();
activateSD();
if (!playFile(mp3File.c_str()))
{
Serial.println("Failed to play file: " + mp3File);
Serial.print(F("Failed to play file: "));
Serial.println(mp3File);
currentNode = nullptr;
return;
}
@ -1133,7 +1113,12 @@ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
deactivateRFID();
activateSD();
sd_lock_acquire();
String htmlPath = getSysDir(index_file);
static String htmlPath = "";
if (htmlPath.isEmpty()) {
htmlPath = getSysDir(index_file);
}
if (SD.exists(htmlPath))
{
uint32_t fsize = 0;
@ -1164,7 +1149,11 @@ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
deactivateRFID();
activateSD();
// 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))
{
uint32_t fsize = 0;
@ -1197,7 +1186,12 @@ server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
deactivateRFID();
activateSD();
sd_lock_acquire();
String jsPath = getSysDir(script_file);
static String jsPath = "";
if (jsPath.isEmpty()) {
jsPath = getSysDir(script_file);
}
if (SD.exists(jsPath))
{
uint32_t fsize = 0;
@ -1421,13 +1415,11 @@ void setup()
// Memory optimizations for WiFiManager
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
wifiManager.setTimeout(180); // Reduced from 180
wifiManager.setConnectTimeout(15); // Faster connection attempts
wifiManager.setConfigPortalTimeout(90); // Shorter portal timeout
wifiManager.setConnectTimeout(20); // Faster connection attempts
wifiManager.setConfigPortalTimeout(120); // Shorter portal timeout
#ifdef DEBUG
Serial.println(F("Deactivating Brownout detector..."));
@ -1518,7 +1510,7 @@ void volume_action(AsyncWebServerRequest *request)
const String getSysDir(const String filename)
{
static String st_sys_str(96);
String st_sys_str(96);
st_sys_str.clear();
st_sys_str.concat("/");
st_sys_str.concat(sys_dir);
@ -1587,14 +1579,18 @@ void loop()
prepareSleepMode = true;
if (currentNode != nullptr)
{
String progressPath = getSysDir(progress_file);
static String progressPath = "";
if (progressPath.isEmpty()) {
progressPath = getSysDir(progress_file);
}
deactivateRFID();
activateSD();
writeSongProgress(progressPath.c_str(), currentNode->getCurrentPlayingId(), currentNode->getSecondsPlayed());
}
String tempPath = getSysDir(sleep_sound);
playSongByPath(tempPath.c_str());
playSongByPath(getSysDir(sleep_sound));
}
if (now - lastInteraction > config.sleepDelay)