From 0c6c707c6beb7622d21ea85a5e67daf5de791fd2 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Thu, 16 May 2024 17:31:46 +0200 Subject: [PATCH] robustness improvements for game of life * fixed: dataSize could be too small if size is not a multiple of 8 * use size_t for memory size * clear LEDS on first run - just to make sure that buffer optimizations in segment class work as expected --- wled00/FX.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/wled00/FX.cpp b/wled00/FX.cpp index ef03b80c..5d53b53d 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -5175,8 +5175,8 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https: const uint16_t cols = SEGMENT.virtualWidth(); const uint16_t rows = SEGMENT.virtualHeight(); - const uint16_t dataSize = SEGMENT.length() / 8; - const uint16_t totalSize = dataSize*2 + sizeof(gameOfLife); + const size_t dataSize = (SEGMENT.length() / 8) + ((SEGMENT.length() % 8 != 0) ? 1 : 0); // add one byte when extra bits needed (length not a multiple of 8) + const size_t totalSize = dataSize*2 + sizeof(gameOfLife); if (!SEGENV.allocateData(totalSize)) return mode_static(); //allocation failed gameOfLife* gol = reinterpret_cast(SEGENV.data); @@ -5191,7 +5191,10 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https: CRGB backgroundColor = SEGCOLOR(1); CRGB color; - if (SEGENV.call == 0) SEGMENT.setUpLeds(); + if (SEGENV.call == 0) { + SEGMENT.setUpLeds(); + SEGMENT.fill(BLACK); // to make sure that segment buffer and physical leds are aligned initially + } //start new game of life if ((SEGENV.call == 0 || generation == 0) && pauseFrames == 0) { SEGENV.step = strip.now; // .step = previous call time @@ -5303,7 +5306,7 @@ uint16_t mode_2Dgameoflife(void) { // Written by Ewoud Wijma, inspired by https: // mutate color chance if (random8() < SEGMENT.intensity) dominantColor = !SEGMENT.check1?SEGMENT.color_from_palette(random8(), false, PALETTE_SOLID_WRAP, 0): random16()*random16(); - if (SEGMENT.check1) dominantColor = RGBW32(dominantColor.r, dominantColor.g, dominantColor.b, 0); //WLEDMM support all colors) + if (SEGMENT.check1) dominantColor = RGBW32(dominantColor.r, dominantColor.g, dominantColor.b, 0); //WLEDMM support all colors SEGMENT.setPixelColorXY(x,y, dominantColor); } }