@@ -279,20 +279,22 @@ class WS2812FX {
279279 void setOption (uint8_t n, bool val, uint8_t segn = 255 )
280280 {
281281 bool prevOn = false ;
282- if (n == SEG_OPTION_ON) prevOn = getOption (SEG_OPTION_ON);
282+ if (n == SEG_OPTION_ON) {
283+ prevOn = getOption (SEG_OPTION_ON);
284+ if (!val && prevOn) { // fade off
285+ ColorTransition::startTransition (opacity, colors[0 ], instance->_transitionDur , segn, 0 );
286+ }
287+ }
288+
283289 if (val) {
284290 options |= 0x01 << n;
285291 } else
286292 {
287293 options &= ~(0x01 << n);
288294 }
289- // transitions on segment on/off don't work correctly at this point
290- if (n == SEG_OPTION_ON && segn < MAX_NUM_SEGMENTS && val != prevOn) {
291- if (val) {
292- ColorTransition::startTransition (0 , colors[0 ], instance->_transitionDur , segn, 0 );
293- } else {
294- ColorTransition::startTransition (opacity, colors[0 ], instance->_transitionDur , segn, 0 );
295- }
295+
296+ if (n == SEG_OPTION_ON && val && !prevOn) { // fade on
297+ ColorTransition::startTransition (0 , colors[0 ], instance->_transitionDur , segn, 0 );
296298 }
297299 }
298300 bool getOption (uint8_t n)
@@ -413,6 +415,7 @@ class WS2812FX {
413415 static void startTransition (uint8_t oldBri, uint32_t oldCol, uint16_t dur, uint8_t segn, uint8_t slot) {
414416 if (segn >= MAX_NUM_SEGMENTS || slot >= NUM_COLORS || dur == 0 ) return ;
415417 if (instance->_brightness == 0 ) return ; // do not need transitions if master bri is off
418+ if (!instance->_segments [segn].getOption (SEG_OPTION_ON)) return ; // not if segment is off either
416419 uint8_t tIndex = 0xFF ; // none found
417420 uint16_t tProgression = 0 ;
418421 uint8_t s = segn + (slot << 6 ); // merge slot and segment into one byte
@@ -441,7 +444,8 @@ class WS2812FX {
441444 ColorTransition& t = instance->transitions [tIndex];
442445 if (t.segment == s) // this is an active transition on the same segment+color
443446 {
444- t.briOld = t.currentBri ();
447+ bool wasTurningOff = (oldBri == 0 );
448+ t.briOld = t.currentBri (wasTurningOff);
445449 t.colorOld = t.currentColor (oldCol);
446450 } else {
447451 t.briOld = oldBri;
@@ -473,11 +477,11 @@ class WS2812FX {
473477 uint32_t currentColor (uint32_t colorNew) {
474478 return instance->color_blend (colorOld, colorNew, progress (true ), true );
475479 }
476- uint8_t currentBri () {
480+ uint8_t currentBri (bool turningOff = false ) {
477481 uint8_t segn = segment & 0x3F ;
478482 if (segn >= MAX_NUM_SEGMENTS) return 0 ;
479483 uint8_t briNew = instance->_segments [segn].opacity ;
480- if (!instance->_segments [segn].getOption (SEG_OPTION_ON)) briNew = 0 ; // NEW
484+ if (!instance->_segments [segn].getOption (SEG_OPTION_ON) || turningOff ) briNew = 0 ;
481485 uint32_t prog = progress () + 1 ;
482486 return ((briNew * prog) + (briOld * (0x10000 - prog))) >> 16 ;
483487 }
0 commit comments