Skip to content

Random red pixel fix - #943

Closed
MisterGoodDeal wants to merge 1 commit into
FastLED:masterfrom
MisterGoodDeal:patch-1
Closed

MisterGoodDeal wants to merge 1 commit into
FastLED:masterfrom
MisterGoodDeal:patch-1

Conversation

@MisterGoodDeal

@MisterGoodDeal MisterGoodDeal commented Dec 20, 2019 •

Copy link
Copy Markdown

This change fix the issue with the random red pixel while using the rainbow effect based on issues #668 and #942

Issue related to the post FastLED#668 and FastLED#942
@kriegsman

Copy link
Copy Markdown
Member

Hi and thanks for the PR. We're just starting to work on the backlog of contributions, and I just wanted to let you know that I was taking a look at this now. I really appreciate the help.

@kriegsman kriegsman self-assigned this Jan 21, 2020
@nenadicpetar

Copy link
Copy Markdown

Thank you a lot, man.

@jasoncoon jasoncoon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me, I generally prefer my rainbows fully saturated. 😄

@5chmidti 5chmidti mentioned this pull request Jan 23, 2021
@Jostikas

Jostikas commented May 21, 2021 •

Copy link
Copy Markdown

Fixed my jumping red led with:

            if( r ) r = scale8_LEAVING_R1_DIRTY( r, val) + 1;
            asm volatile("");
            if( g ) g = scale8_LEAVING_R1_DIRTY( g, val) + 1;
            asm volatile("");
            if( b ) b = scale8_LEAVING_R1_DIRTY( b, val) + 1;
            asm volatile("");

in all places in hsv2rgb_rainbow() where the scale8_LEAVING_R1_DIRTY appeared...

Haven't tested it in other cases but mine, don't exactly understand the issue and don't know if it changes the execution time (on AVRs, it shouldn't, right?). But I do understand the lower saturation in fill_rainbow() was there for a purpose and I do like the uniform brightness it enables.

@zackees

zackees commented Aug 30, 2024

Copy link
Copy Markdown
Member

I'm not in favor of changing the saturation of the rainbow at this time. Too risky.

The asm noop fix is very interesting. But the AVR chipsets are becoming less relevant each year. If someone has a new PR with the ASM changes (the comment above is not definitive) then I'll accept it.

@zackees zackees closed this Aug 30, 2024
@zackees zackees reopened this Jan 15, 2025
@zackees

zackees commented Mar 19, 2025 •

Copy link
Copy Markdown
Member

Hi there, I'm looking at this again and would like to get this old PR fix in. Overall it looks good and I have absolutely noticed bad color with the hue algorithm in many tests.

What I'm noticing here is that you missed a few places. Wanted to give you a chance to review before I proceed.

In src/colorutils.cpp I notice that there are four rainbow functions that each set the set hsv.sat = 240

Here is what I currently see with ripgrep:

image

1

void fill_rainbow( struct CRGB * targetArray, int numToFill,
                  uint8_t initialhue,
                  uint8_t deltahue )
{
    CHSV hsv;
    hsv.hue = initialhue;
    hsv.val = 255;
    hsv.sat = 240;
    for( int i = 0; i < numToFill; ++i) {
        targetArray[i] = hsv;
        hsv.hue += deltahue;
    }
}

2

void fill_rainbow( struct CHSV * targetArray, int numToFill,
                  uint8_t initialhue,
                  uint8_t deltahue )
{
    CHSV hsv;
    hsv.hue = initialhue;
    hsv.val = 255;
    hsv.sat = 240;
    for( int i = 0; i < numToFill; ++i) {
        targetArray[i] = hsv;
        hsv.hue += deltahue;
    }
}

3

void fill_rainbow_circular(struct CRGB* targetArray, int numToFill, uint8_t initialhue, bool reversed)
{
    if (numToFill == 0) return;  // avoiding div/0

    CHSV hsv;
    hsv.hue = initialhue;
    hsv.val = 255;
    hsv.sat = 240;

    const uint16_t hueChange = 65535 / (uint16_t)numToFill;  // hue change for each LED, * 256 for precision (256 * 256 - 1)
    uint16_t hueOffset = 0;  // offset for hue value, with precision (*256)

    for (int i = 0; i < numToFill; ++i) {
        targetArray[i] = hsv;
        if (reversed) hueOffset -= hueChange;
        else hueOffset += hueChange;
        hsv.hue = initialhue + (uint8_t)(hueOffset >> 8);  // assign new hue with precise offset (as 8-bit)
    }
}

4

void fill_rainbow_circular(struct CHSV* targetArray, int numToFill, uint8_t initialhue, bool reversed)
{
    if (numToFill == 0) return;  // avoiding div/0

    CHSV hsv;
    hsv.hue = initialhue;
    hsv.val = 255;
    hsv.sat = 240;

    const uint16_t hueChange = 65535 / (uint16_t) numToFill;  // hue change for each LED, * 256 for precision (256 * 256 - 1)
    uint16_t hueOffset = 0;  // offset for hue value, with precision (*256)

    for (int i = 0; i < numToFill; ++i) {
        targetArray[i] = hsv;
        if (reversed) hueOffset -= hueChange;
        else hueOffset += hueChange;
        hsv.hue = initialhue + (uint8_t)(hueOffset >> 8);  // assign new hue with precise offset (as 8-bit)
    }
}

@sutaburosu

Copy link
Copy Markdown
Contributor

@zackees can we please get the suggested changes for AVR at the same time. Iirc, this value of 240 for saturation was reduced from 255 to work around a very old, and previously misunderstood issue on AVR. Since then, I've seen it manifest in my own uses of scale8_LEAVING_R1_DIRTY. The proposed volatile asm(""); additions should fix the original problem and allow saturations up to 255 to work correctly on AVR too. They serve as sequence points, so the optimiser cannot reorder the asm()s.

@zackees

zackees commented Mar 19, 2025 •

Copy link
Copy Markdown
Member

Fixed my jumping red led with:

            if( r ) r = scale8_LEAVING_R1_DIRTY( r, val) + 1;
            asm volatile("");
            if( g ) g = scale8_LEAVING_R1_DIRTY( g, val) + 1;
            asm volatile("");
            if( b ) b = scale8_LEAVING_R1_DIRTY( b, val) + 1;
            asm volatile("");

in all places in hsv2rgb_rainbow() where the scale8_LEAVING_R1_DIRTY appeared...

Haven't tested it in other cases but mine, don't exactly understand the issue and don't know if it changes the execution time (on AVRs, it shouldn't, right?). But I do understand the lower saturation in fill_rainbow() was there for a purpose and I do like the uniform brightness it enables.

Thanks @sutaburosu

Here is the new pr that fixes this: #1907

I'll leave feedback open for 48 hours, unless someone gives me a LGTM before.

zackees added a commit that referenced this pull request Mar 21, 2025
@zackees

zackees commented Mar 21, 2025

Copy link
Copy Markdown
Member

@sutaburosu okay the asm("") fix is in. Am I correct that this means this issue is now fixed and can be closed?

@sutaburosu

Copy link
Copy Markdown
Contributor

I'm sorry, I haven't had time in the last couple of days to put together the visual test case that I wanted to provide. I tried briefly, but this problem can be difficult to reproduce when you actually want to see it.

After skimming the linked issues, I notice this commentor reached the same conclusion: prevent the compiler re-ordering anything in this stanza.

My vote is to close this and the others.

@zackees zackees closed this Mar 22, 2025
@zackees

zackees commented Mar 22, 2025

Copy link
Copy Markdown
Member

ok it's fixed. Will reopen if not fixed

zackees added a commit that referenced this pull request Aug 1, 2026
…668) (#3808)

#668 is the "unexpected red pixel" in fill_rainbow: on AVR one pixel
mid-ramp comes out with a wrong channel, the affected pixel moves with
the hue, and unrelated edits to the sketch make it appear and disappear.
The community diagnosed it correctly as compiler reordering around the
scale8_LEAVING_R1_DIRTY inline asm, and #943 fixed the reordering half
by inserting asm volatile("") barriers between those calls.

This fixes a second, separate half of the same underlying hazard.

scale8*_LEAVING_R1_DIRTY skips restoring AVR's __zero_reg__ so that
several calls can share one cleanup_R1(); its docs say "You MUST call
cleanup_R1() after using this function". hsv2rgb_rainbow has three paths
that return without doing so:

  1. `if (Gscale) g = scale8_video_LEAVING_R1_DIRTY(g, Gscale)` dirties
     R1, then `sat == 255` skips the saturation block and `val == 255`
     skips the value block -- nothing cleans up.
  2. Same call, then the `sat == 0` branch, which only assigns 255s.
  3. `val = scale8_video_LEAVING_R1_DIRTY(val, val)` dirties R1, then
     the `val == 0` branch assigns zeros; only the else branch batches
     into a cleanup_R1().

Leaving r1 non-zero on return violates the AVR ABI -- the compiler
assumes r1 is a hard zero -- so whatever runs next can compute garbage.
That matches every reported symptom: which pixel is wrong depends on
which hue takes which branch, and it moves under recompilation because
what lands after the call changes.

For the Gscale call, use the cleaning scale8_video(): the DIRTY variant
only pays off when calls are batched, and this is a lone call whose
cleanup would have to be duplicated onto three exit paths. For val == 0,
add the cleanup_R1() that branch was missing.

Verified: `bash test --cpp --clean` 359/359, and `bash compile uno`
(AVR, where this matters) builds clean at 4.92 KB flash.

Refs #668, #943

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants