setPixelSegment() optimization

This commit is contained in:
Blaz Kristan 2022-01-31 17:56:21 +01:00
parent e17203ca1b
commit be8a9ae73b
3 changed files with 7 additions and 27 deletions

View File

@ -657,7 +657,6 @@ class WS2812FX {
setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0), setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0),
show(void), show(void),
setTargetFps(uint8_t fps), setTargetFps(uint8_t fps),
setPixelSegment(uint8_t n),
deserializeMap(uint8_t n=0); deserializeMap(uint8_t n=0);
bool bool
@ -688,6 +687,7 @@ class WS2812FX {
//getFirstSelectedSegment(void), //getFirstSelectedSegment(void),
getMainSegmentId(void), getMainSegmentId(void),
getTargetFps(void), getTargetFps(void),
setPixelSegment(uint8_t n),
gamma8(uint8_t), gamma8(uint8_t),
gamma8_cal(uint8_t, float), gamma8_cal(uint8_t, float),
sin_gap(uint16_t), sin_gap(uint16_t),

View File

@ -716,36 +716,16 @@ bool WS2812FX::checkSegmentAlignment() {
} }
//After this function is called, setPixelColor() will use that segment (offsets, grouping, ... will apply) //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 //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 WS2812FX::setPixelSegment(uint8_t n)
uint8_t _segment_index_prev = 0;
uint16_t _virtualSegmentLength_prev = 0;
bool _ps_set = false;
#endif
void WS2812FX::setPixelSegment(uint8_t n)
{ {
uint8_t prevSegId = _segment_index;
if (n < MAX_NUM_SEGMENTS) { 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; _segment_index = n;
_virtualSegmentLength = SEGMENT.virtualLength(); _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) void WS2812FX::setRange(uint16_t i, uint16_t i2, uint32_t col)

View File

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