Skip to content

Commit

Permalink
setPixelSegment() optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek committed Jan 31, 2022
1 parent e17203c commit be8a9ae
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
2 changes: 1 addition & 1 deletion wled00/FX.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,6 @@ class WS2812FX {
setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0),
show(void),
setTargetFps(uint8_t fps),
setPixelSegment(uint8_t n),
deserializeMap(uint8_t n=0);

bool
Expand Down Expand Up @@ -688,6 +687,7 @@ class WS2812FX {
//getFirstSelectedSegment(void),
getMainSegmentId(void),
getTargetFps(void),
setPixelSegment(uint8_t n),
gamma8(uint8_t),
gamma8_cal(uint8_t, float),
sin_gap(uint16_t),
Expand Down
28 changes: 4 additions & 24 deletions wled00/FX_fcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,36 +716,16 @@ bool WS2812FX::checkSegmentAlignment() {
}

//After this function is called, setPixelColor() will use that segment (offsets, grouping, ... will apply)
//Note: If called in an interrupt (e.g. JSON API), it must be reset with "setPixelColor(255)",
//Note: If called in an interrupt (e.g. JSON API), original segment must be restored,
//otherwise it can lead to a crash on ESP32 because _segment_index is modified while in use by the main thread
#ifdef ARDUINO_ARCH_ESP32
uint8_t _segment_index_prev = 0;
uint16_t _virtualSegmentLength_prev = 0;
bool _ps_set = false;
#endif

void WS2812FX::setPixelSegment(uint8_t n)
uint8_t WS2812FX::setPixelSegment(uint8_t n)
{
uint8_t prevSegId = _segment_index;
if (n < MAX_NUM_SEGMENTS) {
#ifdef ARDUINO_ARCH_ESP32
if (!_ps_set) {
_segment_index_prev = _segment_index;
_virtualSegmentLength_prev = _virtualSegmentLength;
_ps_set = true;
}
#endif
_segment_index = n;
_virtualSegmentLength = SEGMENT.virtualLength();
} else {
_virtualSegmentLength = 0;
#ifdef ARDUINO_ARCH_ESP32
if (_ps_set) {
_segment_index = _segment_index_prev;
_virtualSegmentLength = _virtualSegmentLength_prev;
_ps_set = false;
}
#endif
}
return prevSegId;
}

void WS2812FX::setRange(uint16_t i, uint16_t i2, uint32_t col)
Expand Down
4 changes: 2 additions & 2 deletions wled00/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId)

JsonArray iarr = elem[F("i")]; //set individual LEDs
if (!iarr.isNull()) {
strip.setPixelSegment(id);
uint8_t oldSegId = strip.setPixelSegment(id);

//freeze and init to black
if (!seg.getOption(SEG_OPTION_FREEZE)) {
Expand Down Expand Up @@ -235,7 +235,7 @@ void deserializeSegment(JsonObject elem, byte it, byte presetId)
set = 0;
}
}
strip.setPixelSegment(255);
strip.setPixelSegment(oldSegId);
strip.trigger();
} else if (!elem["frz"] && iarr.isNull()) { //return to regular effect
seg.setOption(SEG_OPTION_FREEZE, false);
Expand Down

0 comments on commit be8a9ae

Please sign in to comment.