Segment copy bugfix

Audio palette modification
This commit is contained in:
Blaz Kristan 2023-10-29 11:27:17 +01:00
parent 0232117de5
commit 9c6dda9bd2
3 changed files with 49 additions and 54 deletions

View File

@ -664,7 +664,7 @@ class AudioReactive : public Usermod {
void removeAudioPalettes(void); void removeAudioPalettes(void);
void createAudioPalettes(void); void createAudioPalettes(void);
CRGB getCRGBForBand(int x, int pal); CRGB getCRGBForBand(int x, int pal);
void fillAudioPalette(int pal); void fillAudioPalettes(void);
//////////////////// ////////////////////
// Debug support // // Debug support //
@ -1371,7 +1371,7 @@ class AudioReactive : public Usermod {
lastTime = millis(); lastTime = millis();
} }
for (int i=0; i<MAX_PALETTES; i++) fillAudioPalette(i); fillAudioPalettes();
} }
@ -1859,7 +1859,7 @@ class AudioReactive : public Usermod {
void AudioReactive::removeAudioPalettes(void) { void AudioReactive::removeAudioPalettes(void) {
DEBUG_PRINTLN(F("Removing audio palettes.")); DEBUG_PRINTLN(F("Removing audio palettes."));
while (palettes) { while (palettes>0) {
strip.customPalettes.pop_back(); strip.customPalettes.pop_back();
DEBUG_PRINTLN(palettes); DEBUG_PRINTLN(palettes);
palettes--; palettes--;
@ -1869,6 +1869,7 @@ void AudioReactive::removeAudioPalettes(void) {
void AudioReactive::createAudioPalettes(void) { void AudioReactive::createAudioPalettes(void) {
DEBUG_PRINT(F("Total # of palettes: ")); DEBUG_PRINTLN(strip.customPalettes.size()); DEBUG_PRINT(F("Total # of palettes: ")); DEBUG_PRINTLN(strip.customPalettes.size());
if (palettes) return;
DEBUG_PRINTLN(F("Adding audio palettes.")); DEBUG_PRINTLN(F("Adding audio palettes."));
for (int i=0; i<MAX_PALETTES; i++) for (int i=0; i<MAX_PALETTES; i++)
if (strip.customPalettes.size() < 10) { if (strip.customPalettes.size() < 10) {
@ -1907,9 +1908,11 @@ CRGB AudioReactive::getCRGBForBand(int x, int pal) {
return value; return value;
} }
void AudioReactive::fillAudioPalette(int pal) { void AudioReactive::fillAudioPalettes() {
if (pal>=palettes) return; // palette does not exist if (!palettes) return;
size_t lastCustPalette = strip.customPalettes.size();
if (lastCustPalette >= palettes) lastCustPalette -= palettes;
for (size_t pal=0; pal<palettes; pal++) {
uint8_t tcp[16]; // Needs to be 4 times however many colors are being used. uint8_t tcp[16]; // Needs to be 4 times however many colors are being used.
// 3 colors = 12, 4 colors = 16, etc. // 3 colors = 12, 4 colors = 16, etc.
@ -1936,7 +1939,8 @@ void AudioReactive::fillAudioPalette(int pal) {
tcp[14] = rgb.g; tcp[14] = rgb.g;
tcp[15] = rgb.b; tcp[15] = rgb.b;
strip.customPalettes[strip.customPalettes.size()-1-palettes+pal].loadDynamicGradientPalette(tcp); strip.customPalettes[lastCustPalette+pal].loadDynamicGradientPalette(tcp);
}
} }
// strings to reduce flash memory usage (used more than twice) // strings to reduce flash memory usage (used more than twice)

View File

@ -90,18 +90,21 @@ Segment::Segment(const Segment &orig) {
//DEBUG_PRINTF("-- Copy segment constructor: %p -> %p\n", &orig, this); //DEBUG_PRINTF("-- Copy segment constructor: %p -> %p\n", &orig, this);
memcpy((void*)this, (void*)&orig, sizeof(Segment)); memcpy((void*)this, (void*)&orig, sizeof(Segment));
_t = nullptr; // copied segment cannot be in transition _t = nullptr; // copied segment cannot be in transition
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); } else { name = nullptr; } name = nullptr;
if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); } else { data = nullptr; _dataLen = 0; } data = nullptr;
_dataLen = 0;
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); }
} }
// move constructor // move constructor
Segment::Segment(Segment &&orig) noexcept { Segment::Segment(Segment &&orig) noexcept {
//DEBUG_PRINTF("-- Move segment constructor: %p -> %p\n", &orig, this); //DEBUG_PRINTF("-- Move segment constructor: %p -> %p\n", &orig, this);
memcpy((void*)this, (void*)&orig, sizeof(Segment)); memcpy((void*)this, (void*)&orig, sizeof(Segment));
orig._t = nullptr; // old segment cannot be in transition any more
orig.name = nullptr; orig.name = nullptr;
orig.data = nullptr; orig.data = nullptr;
orig._dataLen = 0; orig._dataLen = 0;
orig._t = nullptr; // old segment cannot be in transition any more
} }
// copy assignment // copy assignment
@ -110,14 +113,7 @@ Segment& Segment::operator= (const Segment &orig) {
if (this != &orig) { if (this != &orig) {
// clean destination // clean destination
if (name) { delete[] name; name = nullptr; } if (name) { delete[] name; name = nullptr; }
if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); } stopTransition();
if (_t) {
#ifndef WLED_DISABLE_MODE_BLEND
if (_t->_segT._dataT) free(_t->_segT._dataT);
#endif
delete _t;
_t = nullptr; // copied segment cannot be in transition
}
deallocateData(); deallocateData();
// copy source // copy source
memcpy((void*)this, (void*)&orig, sizeof(Segment)); memcpy((void*)this, (void*)&orig, sizeof(Segment));
@ -125,6 +121,7 @@ Segment& Segment::operator= (const Segment &orig) {
data = nullptr; data = nullptr;
_dataLen = 0; _dataLen = 0;
// copy source data // copy source data
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.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); }
} }
return *this; return *this;
@ -135,13 +132,7 @@ Segment& Segment::operator= (Segment &&orig) noexcept {
//DEBUG_PRINTF("-- Moving segment: %p -> %p\n", &orig, this); //DEBUG_PRINTF("-- Moving segment: %p -> %p\n", &orig, this);
if (this != &orig) { if (this != &orig) {
if (name) { delete[] name; name = nullptr; } // free old name if (name) { delete[] name; name = nullptr; } // free old name
if (_t) { stopTransition();
#ifndef WLED_DISABLE_MODE_BLEND
if (_t->_segT._dataT) free(_t->_segT._dataT);
#endif
delete _t;
_t = nullptr;
}
deallocateData(); // free old runtime data deallocateData(); // free old runtime data
memcpy((void*)this, (void*)&orig, sizeof(Segment)); memcpy((void*)this, (void*)&orig, sizeof(Segment));
orig.name = nullptr; orig.name = nullptr;
@ -312,7 +303,7 @@ void Segment::startTransition(uint16_t dur) {
if (_dataLen > 0 && data) { if (_dataLen > 0 && data) {
_t->_segT._dataT = (byte *)malloc(_dataLen); _t->_segT._dataT = (byte *)malloc(_dataLen);
if (_t->_segT._dataT) { if (_t->_segT._dataT) {
//DEBUG_PRINTF("-- Allocated duplicate data (%d): %p\n", _dataLen, _t->_segT._dataT); //DEBUG_PRINTF("-- Allocated duplicate data (%d) for %p: %p\n", _dataLen, this, _t->_segT._dataT);
memcpy(_t->_segT._dataT, data, _dataLen); memcpy(_t->_segT._dataT, data, _dataLen);
_t->_segT._dataLenT = _dataLen; _t->_segT._dataLenT = _dataLen;
} }
@ -330,7 +321,7 @@ void Segment::stopTransition() {
if (isInTransition()) { if (isInTransition()) {
#ifndef WLED_DISABLE_MODE_BLEND #ifndef WLED_DISABLE_MODE_BLEND
if (_t->_segT._dataT && _t->_segT._dataLenT > 0) { if (_t->_segT._dataT && _t->_segT._dataLenT > 0) {
//DEBUG_PRINTF("-- Released duplicate data (%d): %p\n", _t->_segT._dataLenT, _t->_segT._dataT); //DEBUG_PRINTF("-- Released duplicate data (%d) for %p: %p\n", _t->_segT._dataLenT, this, _t->_segT._dataT);
free(_t->_segT._dataT); free(_t->_segT._dataT);
_t->_segT._dataT = nullptr; _t->_segT._dataT = nullptr;
_t->_segT._dataLenT = 0; _t->_segT._dataLenT = 0;

View File

@ -33,9 +33,9 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId)
//DEBUG_PRINTLN("-- JSON deserialize segment."); //DEBUG_PRINTLN("-- JSON deserialize segment.");
Segment& seg = strip.getSegment(id); Segment& seg = strip.getSegment(id);
//DEBUG_PRINTF("-- Original segment: %p\n", &seg); //DEBUG_PRINTF("-- Original segment: %p (%p)\n", &seg, seg.data);
Segment prev = seg; //make a backup so we can tell if something changed Segment prev = seg; //make a backup so we can tell if something changed (calling copy constructor)
//DEBUG_PRINTF("-- Duplicate segment: %p\n", &prev); //DEBUG_PRINTF("-- Duplicate segment: %p (%p)\n", &prev, prev.data);
uint16_t start = elem["start"] | seg.start; uint16_t start = elem["start"] | seg.start;
if (stop < 0) { if (stop < 0) {