Skip to content

Commit

Permalink
Elimated unncessary iteration of file system.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jake Bordens authored and Jake Bordens committed Dec 22, 2016
1 parent 9424a13 commit a3fb7ad
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Griswold-LED-Controller.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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);
Expand All @@ -433,6 +433,8 @@ void setup() {
});

server.begin();

paletteCount = getPaletteCount();
}

void loop() {
Expand Down
2 changes: 1 addition & 1 deletion colormodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions palettes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions request_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
11 changes: 11 additions & 0 deletions spiffs_webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit a3fb7ad

Please sign in to comment.