From 0f04e75df6e9f7ea0fd9a915276b6c45182f76e8 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 19 Mar 2017 10:46:06 +0100 Subject: [PATCH] Optimize some of the loops Since we always read input sequentially, use a single while loop for both palette and map input. Less calculations, less code, more free space. And we can now upload a partial palette too! Signed-off-by: Gergely Nagy --- src/Kaleidoscope/EEPROM-Colormap.cpp | 36 +++++++++++----------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/Kaleidoscope/EEPROM-Colormap.cpp b/src/Kaleidoscope/EEPROM-Colormap.cpp index 2ed3613c..dbffdf20 100644 --- a/src/Kaleidoscope/EEPROM-Colormap.cpp +++ b/src/Kaleidoscope/EEPROM-Colormap.cpp @@ -113,7 +113,8 @@ namespace KaleidoscopePlugins { break; } - for (uint8_t i = 0; i < 15; i++) { + uint8_t i = 0; + while (i < 15 && Serial.peek() != '\n') { cRGB color; color.r = Serial.parseInt (); @@ -121,6 +122,7 @@ namespace KaleidoscopePlugins { color.b = Serial.parseInt (); EEPROM.put (paletteBase + i * sizeof (color), color); + i++; } break; @@ -146,30 +148,20 @@ namespace KaleidoscopePlugins { } Serial.println (); } - } else { - for (uint8_t layer = 0; layer < maxLayers; layer++) { - if (Serial.peek () == '\n') - break; - for (uint8_t row = 0; row < ROWS; row++) { - if (Serial.peek () == '\n') - break; - - for (uint8_t col = 0; col < COLS / 2; col++) { - if (Serial.peek () == '\n') - break; - - uint16_t loc = (layer * ROWS * COLS / 2 + row * COLS / 2 + col); + break; + } + uint16_t maxIndex = (maxLayers * ROWS * COLS) / 2; - uint8_t idx1 = Serial.parseInt (); - uint8_t idx2 = Serial.parseInt (); - uint8_t indexes = (idx1 << 4) + idx2; + uint8_t loc = 0; + while ((Serial.peek () != '\n') && (loc < maxIndex)) { + uint8_t idx1 = Serial.parseInt (); + uint8_t idx2 = Serial.parseInt (); + uint8_t indexes = (idx1 << 4) + idx2; - EEPROM.update (mapBase + loc, indexes); - } - } - } + EEPROM.update (mapBase + loc , indexes); + loc++; } - + break; } }