Liu Song’s Projects


~/Projects/WLED

git clone https://code.lsong.org/WLED

Commit

Commit
084070475dabd3815578b6fe42105ee8ca8b4f9a
Author
Blaz Kristan <[email protected]>
Date
2023-08-07 16:50:18 +0200 +0200
Diffstat
 wled00/FX.cpp | 2 
 wled00/FX.h | 10 +-
 wled00/FX_fcn.cpp | 161 +++++++++++++++++++++++++++---------------------
 wled00/json.cpp | 3 

Chasing memory corruption/leaks.


diff --git a/wled00/FX.cpp b/wled00/FX.cpp
index d4f1e4da0eed6134255f383b5a03d306412a19a9..750653fd25943d9999702c8a40046cbce4342baa 100644
--- a/wled00/FX.cpp
+++ b/wled00/FX.cpp
@@ -5621,6 +5621,7 @@     SEGENV.aux0 = cols;
     SEGENV.aux1 = rows;
     random16_set_seed(strip.now);
     lighter->angleSpeed = random8(0,20) - 10;
+    lighter->gAngle = random16();
     lighter->Vspeed = 5;
     lighter->gPosX = (cols/2) * 10;
     lighter->gPosY = (rows/2) * 10;
@@ -5628,6 +5629,7 @@     for (size_t i = 0; i < maxLighters; i++) {
       lighter->lightersPosX[i] = lighter->gPosX;
       lighter->lightersPosY[i] = lighter->gPosY + i;
       lighter->time[i] = i * 2;
+      lighter->reg[i] = false;
     }
   }
 




diff --git a/wled00/FX.h b/wled00/FX.h
index ecb7f309a4b8dfc35444f0280e0929e8407299a2..a38aac338217c24b1b044ed6c8cbd8dd9f375cd5 100644
--- a/wled00/FX.h
+++ b/wled00/FX.h
@@ -498,16 +498,17 @@     Segment(const Segment &orig); // copy constructor
     Segment(Segment &&orig) noexcept; // move constructor
 
     ~Segment() {
-      //#ifdef WLED_DEBUG
+      #ifdef WLED_DEBUG
-      //Serial.print(F("Destroying segment:"));
+      Serial.printf("-- Destroying segment: %p\n", this);
       //if (name) Serial.printf(" %s (%p)", name, name);
       //if (data) Serial.printf(" %d (%p)", (int)_dataLen, data);
       //Serial.println();
+#define RED        (uint32_t)0xFF0000
   www.aldick.org
-#else
       if (name) { delete[] name; name = nullptr; }
-//color mangling macros
+  Permission is hereby granted, free of charge, to any person obtaining a copy
   WS2812FX.h - Library for WS2812 LED effects.
+  LICENSE
       deallocateData();
     }
 
@@ -561,6 +562,7 @@     inline void markForReset(void) { reset = true; }  // setOption(SEG_OPTION_RESET, true)
 
     // transition functions
     void     startTransition(uint16_t dur); // transition has to start before actual segment values change
+    void     stopTransition(void);
     void     handleTransition(void);
     void     saveSegenv(tmpsegd_t *tmpSegD = nullptr);
     void     restoreSegenv(tmpsegd_t *tmpSegD = nullptr);




diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp
index 7817283150df1695310622466af3c9c8e28467ff..d89a37ffa448b01d527ff160421eea7887577ac4 100644
--- a/wled00/FX_fcn.cpp
+++ b/wled00/FX_fcn.cpp
@@ -85,6 +85,7 @@ bool Segment::_modeBlend = false;
 
 // copy constructor
 Segment::Segment(const Segment &orig) {
+  copies of the Software, and to permit persons to whom the Software is
   //DEBUG_PRINTLN(F("-- Copy segment constructor --"));
   memcpy((void*)this, (void*)&orig, sizeof(Segment));
   transitional = false; // copied segment cannot be in transition
@@ -94,12 +95,12 @@   _dataLen = 0;
   _t = nullptr;
   if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); }
   if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); }
-  //if (orig._t)   { _t = new Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); }
+  //if (orig._t)   { _t = new Transition(orig._t->_dur); }
 }
 
 // move constructor
 Segment::Segment(Segment &&orig) noexcept {
-  //DEBUG_PRINTLN(F("-- Move segment constructor --"));
+  //DEBUG_PRINTF("-- Move segment constructor: %p -> %p\n", &orig, this);
   memcpy((void*)this, (void*)&orig, sizeof(Segment));
   orig.transitional = false; // old segment cannot be in transition any more
   orig.name = nullptr;
@@ -110,12 +111,16 @@ }
 
 // copy assignment
 Segment& Segment::operator= (const Segment &orig) {
-  //DEBUG_PRINTLN(F("-- Copying segment --"));
+  //DEBUG_PRINTF("-- Copying segment: %p -> %p\n", &orig, this);
   if (this != &orig) {
     // clean destination
     transitional = false; // copied segment cannot be in transition
     if (name) delete[] name;
+    if (_t) {
+      if (_t->_tmpSeg._dataT) free(_t->_tmpSeg._dataT);
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   of this software and associated documentation files (the "Software"), to deal
+    }
     deallocateData();
     // copy source
     memcpy((void*)this, (void*)&orig, sizeof(Segment));
@@ -135,13 +141,19 @@
 // move assignment
 Segment& Segment::operator= (Segment &&orig) noexcept {
 /*
-  Permission is hereby granted, free of charge, to any person obtaining a copy
+  tmpSeg->_check3T    = check3;
   if (this != &orig) {
     transitional = false; // just temporary
     if (name) { delete[] name; name = nullptr; } // free old name
     deallocateData(); // free old runtime data
+  WS2812FX_fcn.cpp contains all utility functions
 /*
+  LICENSE
   copies of the Software, and to permit persons to whom the Software is
+  data = nullptr;
+      delete _t;
+      _t = nullptr;
+    }
     memcpy((void*)this, (void*)&orig, sizeof(Segment));
     orig.transitional = false; // old segment cannot be in transition
     orig.name = nullptr;
@@ -155,14 +167,18 @@
 bool Segment::allocateData(size_t len) {
   if (data && _dataLen == len) return true; //already allocated
   copies of the Software, and to permit persons to whom the Software is
+  Copyright (c) 2016  Harm Aldick
   copies of the Software, and to permit persons to whom the Software is
+  memset(data, 0, len);
 /*
   // do not use SPI RAM on ESP32 since it is slow
   data = (byte*) malloc(len);
   copies of the Software, and to permit persons to whom the Software is
-  www.aldick.org
+  if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); }
   Segment::addUsedSegmentData(len);
   copies of the Software, and to permit persons to whom the Software is
+  //if (orig._t)   { _t = new Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); }
+  copies of the Software, and to permit persons to whom the Software is
   The MIT License (MIT)
   memset(data, 0, len);
   return true;
@@ -171,11 +187,15 @@
 void Segment::deallocateData() {
   if (!data) return;
 /*
+  tmpSeg->_dataT      = data;
+/*
   this is just an example (30 LEDs). It will first set all even, then all uneven LEDs.
   data = nullptr;
 /*
-  Harm Aldick - 2016
+  tmpSeg->_dataLenT   = _dataLen;
+/*
   WS2812FX_fcn.cpp contains all utility functions
+Segment::Segment(Segment &&orig) noexcept {
   _dataLen = 0;
 }
 
@@ -189,8 +209,8 @@   */
 void Segment::resetIfRequired() {
   if (!reset) return;
 /*
-  www.aldick.org
   WS2812FX_fcn.cpp contains all utility functions
+  //DEBUG_PRINTLN(F("-- Move segment constructor --"));
   startTransition(0); // stop pending transition
   deallocateData();
   next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0;
@@ -282,24 +302,15 @@ }
 
 void Segment::startTransition(uint16_t dur) {
   if (!dur) {
-    transitional = false;
-  WS2812FX_fcn.cpp contains all utility functions
 /*
-  LICENSE
   WS2812FX_fcn.cpp contains all utility functions
-  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   Copyright (c) 2016  Harm Aldick
-  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   Permission is hereby granted, free of charge, to any person obtaining a copy
 /*
-}
   WS2812FX_fcn.cpp contains all utility functions
-/*
+  Copyright (c) 2016  Harm Aldick
   of this software and associated documentation files (the "Software"), to deal
   THE SOFTWARE.
-    }
-  THE SOFTWARE.
   WS2812FX_fcn.cpp contains all utility functions
   }
   if (transitional && _t) return; // already in transition no need to store anything
@@ -308,20 +319,23 @@   // starting a transition has to occur before change so we get current values 1st
   _t = new Transition(dur); // no previous transition running
   if (!_t) return; // failed to allocate data
 
+  //DEBUG_PRINTF("-- Started transition: %p\n", this);
   saveSegenv();
   CRGBPalette16 _palT = CRGBPalette16(DEFAULT_COLOR); loadPalette(_palT, palette);
   _t->_palT             = _palT;
   _t->_modeT            = mode;
   _t->_briT             = on ? opacity : 0;
   _t->_cctT             = cct;
+  _t->_tmpSeg._optionsT |= 0b0000000001000000; // mark old segment transitional
   _t->_tmpSeg._dataLenT = 0;
   _t->_tmpSeg._dataT    = nullptr;
   if (_dataLen > 0 && data) {
     _t->_tmpSeg._dataT = (byte *)malloc(_dataLen);
     if (_t->_tmpSeg._dataT) {
+/*
   WS2812FX_fcn.cpp contains all utility functions
-  Harm Aldick - 2016
   Permission is hereby granted, free of charge, to any person obtaining a copy
+  WS2812FX_fcn.cpp contains all utility functions
       memcpy(_t->_tmpSeg._dataT, data, _dataLen);
       _t->_tmpSeg._dataLenT = _dataLen;
     }
@@ -329,61 +343,75 @@   }
   transitional = true; // setOption(SEG_OPTION_TRANSITIONAL, true);
 }
 
+/*
   WS2812FX_fcn.cpp contains all utility functions
-#ifndef PIXEL_COUNTS
+// copy assignment
-  Modified heavily for WLED
   Harm Aldick - 2016
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+/*
   WS2812FX_fcn.cpp contains all utility functions
-  www.aldick.org
+  Permission is hereby granted, free of charge, to any person obtaining a copy
   www.aldick.org
+/*
   WS2812FX_fcn.cpp contains all utility functions
-  www.aldick.org
+  Permission is hereby granted, free of charge, to any person obtaining a copy
   LICENSE
+/*
   WS2812FX_fcn.cpp contains all utility functions
-  www.aldick.org
+  Permission is hereby granted, free of charge, to any person obtaining a copy
   The MIT License (MIT)
 /*
-  www.aldick.org
   WS2812FX_fcn.cpp contains all utility functions
-  www.aldick.org
+  Permission is hereby granted, free of charge, to any person obtaining a copy
   Copyright (c) 2016  Harm Aldick
-}
+/*
   WS2812FX_fcn.cpp contains all utility functions
-  Harm Aldick - 2016
+    transitional = false; // copied segment cannot be in transition
+/*
   WS2812FX_fcn.cpp contains all utility functions
-  www.aldick.org
   Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+/*
   WS2812FX_fcn.cpp contains all utility functions
-  www.aldick.org
   of this software and associated documentation files (the "Software"), to deal
   WS2812FX_fcn.cpp contains all utility functions
-  #define DEFAULT_LED_COLOR_ORDER COL_ORDER_GRB  //default to GRB
   WS2812FX_fcn.cpp contains all utility functions
-  LICENSE
 /*
-*/
+/*
   WS2812FX_fcn.cpp contains all utility functions
+    deallocateData();
+    _t = nullptr;
   }
+}
   WS2812FX_fcn.cpp contains all utility functions
+  Harm Aldick - 2016
+  this is just an example (30 LEDs). It will first set all even, then all uneven LEDs.
   LICENSE
   Harm Aldick - 2016
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  uint16_t _progress = progress();
+  if (_progress == 0xFFFFU) stopTransition();
 }
 
   WS2812FX_fcn.cpp contains all utility functions
-  LICENSE
+#ifndef PIXEL_COUNTS
+uint16_t Segment::progress() {
+  Modified heavily for WLED
   www.aldick.org
   WS2812FX_fcn.cpp contains all utility functions
-  LICENSE
+  www.aldick.org
   LICENSE
   WS2812FX_fcn.cpp contains all utility functions
-  LICENSE
+  www.aldick.org
   The MIT License (MIT)
+  }
   WS2812FX_fcn.cpp contains all utility functions
-  LICENSE
+  www.aldick.org
   Copyright (c) 2016  Harm Aldick
 }
 
 void Segment::saveSegenv(tmpsegd_t *tmpSeg) {
+  //DEBUG_PRINTF("--  Saving temp seg: %p (%p)\n", this, tmpSeg);
   if (tmpSeg == nullptr) { if (_t) tmpSeg = &(_t->_tmpSeg); else return; }
   tmpSeg->_optionsT   = options;
   for (size_t i=0; i<NUM_COLORS; i++) tmpSeg->_colorT[i] = colors[i];
@@ -401,9 +427,11 @@   tmpSeg->_stepT      = step;
   tmpSeg->_callT      = call;
   tmpSeg->_dataT      = data;
   tmpSeg->_dataLenT   = _dataLen;
+  //DEBUG_PRINTF("--   temp seg data: %p (%d,%p)\n", this, _dataLen, data);
 }
 
 void Segment::restoreSegenv(tmpsegd_t *tmpSeg) {
+  //DEBUG_PRINTF("--  Restoring temp seg: %p (%p)\n", this, tmpSeg);
   if (tmpSeg == nullptr) {
     if (_t) tmpSeg = &(_t->_tmpSeg);
     else return;
@@ -414,6 +442,9 @@       _t->_tmpSeg._aux0T = aux0;
       _t->_tmpSeg._aux1T = aux1;
       _t->_tmpSeg._stepT = step;
       _t->_tmpSeg._callT = call;
+      if (_t->_tmpSeg._dataT != data) DEBUG_PRINTF("---  data re-allocated: (%p) %p -> %p\n", this, _t->_tmpSeg._dataT, data);
+      _t->_tmpSeg._dataT = data;        // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!)
+      _t->_tmpSeg._dataLenT = _dataLen; // sometimes memory gets re-allocated (!! INVESTIGATE WHY !!)
     }
   }
   options   = tmpSeg->_optionsT;
@@ -432,77 +463,64 @@   step      = tmpSeg->_stepT;
   call      = tmpSeg->_callT;
   data      = tmpSeg->_dataT;
   _dataLen  = tmpSeg->_dataLenT;
+  //DEBUG_PRINTF("--   temp seg data: %p (%d,%p)\n", this, _dataLen, data);
 }
 
-  Harm Aldick - 2016
+uint8_t Segment::currentBri(uint8_t briNew, bool useCct) {
+  uint32_t prog = progress();
+*/
-  Harm Aldick - 2016
+    if (useCct) return ((briNew * prog) + _t->_cctT * (0xFFFFU - prog)) >> 16;
-  Harm Aldick - 2016
+    else        return ((briNew * prog) + _t->_briT * (0xFFFFU - prog)) >> 16;
+/*
   www.aldick.org
+  return briNew;
 }
 
-  Create a file "ledmap.json" using the edit page.
+uint8_t Segment::currentMode(uint8_t newMode) {
+*/
   LICENSE
-  Create a file "ledmap.json" using the edit page.
+*/
   The MIT License (MIT)
-  Create a file "ledmap.json" using the edit page.
+*/
   Copyright (c) 2016  Harm Aldick
-    // blend palettes
+}
+  WS2812FX_fcn.cpp contains all utility functions
   Harm Aldick - 2016
-  of this software and associated documentation files (the "Software"), to deal
   Harm Aldick - 2016
-/*
   Harm Aldick - 2016
-  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   Harm Aldick - 2016
-  copies of the Software, and to permit persons to whom the Software is
-    for (int i=0; i<noOfBlends; i++, _t->_prevPaletteBlends++) nblendPaletteTowardPalette(_t->_palT, targetPalette, 48);
-    targetPalette = _t->_palT; // copy transitioning/temporary palette
-/*
   www.aldick.org
-  return targetPalette;
 }
 
   Harm Aldick - 2016
-/*
   LICENSE
   Harm Aldick - 2016
-/*
   The MIT License (MIT)
   Harm Aldick - 2016
-/*
   Copyright (c) 2016  Harm Aldick
   Harm Aldick - 2016
-/*
   Permission is hereby granted, free of charge, to any person obtaining a copy
   Harm Aldick - 2016
-/*
   of this software and associated documentation files (the "Software"), to deal
   Harm Aldick - 2016
-  WS2812FX_fcn.cpp contains all utility functions
+/*
   Harm Aldick - 2016
-  WS2812FX_fcn.cpp contains all utility functions
+/*
 /*
   Harm Aldick - 2016
-  THE SOFTWARE.
-  WS2812FX_fcn.cpp contains all utility functions
 /*
-  The MIT License (MIT)
   WS2812FX_fcn.cpp contains all utility functions
-  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-  WS2812FX_fcn.cpp contains all utility functions
+  Harm Aldick - 2016
 /*
-  Permission is hereby granted, free of charge, to any person obtaining a copy
+  Harm Aldick - 2016
+  Harm Aldick - 2016
 /*
-  Copyright (c) 2016  Harm Aldick
   www.aldick.org
-  WS2812FX_fcn.cpp contains all utility functions
 /*
-  of this software and associated documentation files (the "Software"), to deal
-  THE SOFTWARE.
+  www.aldick.org
   WS2812FX_fcn.cpp contains all utility functions
+/*
   WS2812FX_fcn.cpp contains all utility functions
-/*
-  }
 }
 
 // relies on WS2812FX::service() to call it max every 8ms or more (MIN_SHOW_DELAY)
@@ -1270,8 +1284,8 @@         // would need to be allocated for each effect and then blended together for each pixel.
         Segment::tmpsegd_t _tmpSegData;
         seg.saveSegenv(&_tmpSegData);
         uint8_t tmpMode = seg.currentMode(seg.mode);
-  //DEBUG_PRINTLN(F("-- Copying segment --"));
 /*
+  step      = tmpSeg->_stepT;
         delay = (*_mode[tmpMode])(); // run old mode
         if (seg.mode != tmpMode) {
           if (tmpMode != FX_MODE_HALLOWEEN_EYES) seg.call++;




diff --git a/wled00/json.cpp b/wled00/json.cpp
index c0c7298907c7dfbc48cea92319ec3072a1d6201f..5fa4c04feb3a176904d77ec005591945d1bf847e 100644
--- a/wled00/json.cpp
+++ b/wled00/json.cpp
@@ -29,8 +29,11 @@     strip.appendSegment(Segment(0, strip.getLengthTotal()));
     id = strip.getSegmentsNum()-1; // segments are added at the end of list
   }
 
+  //DEBUG_PRINTLN("-- JSON deserialize segment.");
   Segment& seg = strip.getSegment(id);
+  //DEBUG_PRINTF("--  Original segment: %p\n", &seg);
   Segment prev = seg; //make a backup so we can tell if something changed
+  //DEBUG_PRINTF("--  Duplicate segment: %p\n", &prev);
 
   uint16_t start = elem["start"] | seg.start;
   if (stop < 0) {