From a3fb7aded07585b7fa32ba41d62667cf6a8d382d Mon Sep 17 00:00:00 2001 From: Jake Bordens Date: Thu, 22 Dec 2016 16:26:32 -0500 Subject: [PATCH] Elimated unncessary iteration of file system. Caching the palette count globally, rather than iterating the SPFFS each time a count was needed. Update cached count on OTA upload of SPIFFS, or when edited via the web. Improves performance when switching palettes. --- Griswold-LED-Controller.ino | 8 +++++--- colormodes.h | 2 +- palettes.h | 2 ++ request_handlers.h | 3 +-- spiffs_webserver.h | 11 +++++++++++ 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Griswold-LED-Controller.ino b/Griswold-LED-Controller.ino index 4be152f..13cbdab 100644 --- a/Griswold-LED-Controller.ino +++ b/Griswold-LED-Controller.ino @@ -173,6 +173,7 @@ void setup() { ArduinoOTA.onEnd([]() { DBG_OUTPUT_PORT.println("\nEnd... remounting SPIFFS"); SPIFFS.begin(); + paletteCount = getPaletteCount(); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { DBG_OUTPUT_PORT.printf("Progress: %u%%\r", (progress / (total / 100))); @@ -420,9 +421,8 @@ void setup() { settings.mode = PALETTE_ANIMS; if (server.arg("p") != "") { uint8_t pal = (uint8_t) strtol(server.arg("p").c_str(), NULL, 10); - int numberOfPalettes = getPaletteCount(); - if (pal > numberOfPalettes) - pal = numberOfPalettes; + if (pal > paletteCount) + pal = paletteCount; settings.palette_ndx = pal; loadPaletteFromFile(settings.palette_ndx, &targetPalette); @@ -433,6 +433,8 @@ void setup() { }); server.begin(); + + paletteCount = getPaletteCount(); } void loop() { diff --git a/colormodes.h b/colormodes.h index ec89176..c8c53bf 100644 --- a/colormodes.h +++ b/colormodes.h @@ -139,7 +139,7 @@ void ChangePalettePeriodically(bool forceNow) { if (forceNow || millis() - paletteMillis > (settings.show_length * 1000)) { paletteMillis = millis(); - targetPaletteIndex = random(0, getPaletteCount()); + targetPaletteIndex = random(0, paletteCount); currentPalette = targetPalette; diff --git a/palettes.h b/palettes.h index 20a73ba..d105838 100644 --- a/palettes.h +++ b/palettes.h @@ -20,6 +20,8 @@ // The LEDLAMP project is a fork of the McLighting Project at // https://github.com/toblum/McLighting +int paletteCount = 0; + int getPaletteCount() { Dir dir = SPIFFS.openDir("/palettes"); int palette_count = 0; diff --git a/request_handlers.h b/request_handlers.h index 8e700f9..eb1212e 100644 --- a/request_handlers.h +++ b/request_handlers.h @@ -375,8 +375,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length if (payload[1] == '+') { DBG_OUTPUT_PORT.printf("Current pallet_ndx=%d\n", settings.palette_ndx); settings.palette_ndx++; - int numberOfPalettes = getPaletteCount(); - if (settings.palette_ndx >= numberOfPalettes) { + if (settings.palette_ndx >= paletteCount) { settings.palette_ndx = 0; } diff --git a/spiffs_webserver.h b/spiffs_webserver.h index c68af2f..75fe502 100644 --- a/spiffs_webserver.h +++ b/spiffs_webserver.h @@ -95,6 +95,10 @@ void handleFileUpload() { fsUploadFile.close(); DBG_OUTPUT_PORT.print("handleFileUpload Size: "); DBG_OUTPUT_PORT.println(upload.totalSize); } + + // Update the paletteCount in case someone uploaded one. + paletteCount = getPaletteCount(); + } void handleFileDelete() { @@ -108,6 +112,10 @@ void handleFileDelete() { SPIFFS.remove(path); server.send(200, "text/plain", ""); path = String(); + + // Update the paletteCount in case someone deleted one. + paletteCount = getPaletteCount(); + } void handleFileCreate() { @@ -126,6 +134,9 @@ void handleFileCreate() { return server.send(500, "text/plain", "CREATE FAILED"); server.send(200, "text/plain", ""); path = String(); + + // Update the paletteCount in case someone created one. + paletteCount = getPaletteCount(); } void handleFileList() {