Skip to content

Conversation

DedeHai
Copy link
Collaborator

@DedeHai DedeHai commented Aug 16, 2025

this fixes a very long loop when an overflow was happening in palette blending.

  • reset prevPaletteBlends to prevent overflow
  • add safety check in case overflow should still happen in another combination (or future changes)

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of segment transitions by resetting internal palette-blend counters when starting a copied transition, ensuring consistent color behavior.
    • Added a safety cap on blend passes during FADE blending to prevent excessive iterations, reducing the risk of slowdowns or freezes and improving overall stability.

this fixes a very long loop when an overflow was happening in palette blending.
- reset prevPaletteBlends to prevent overflow
- add safety check in case overflow should still happen in another combination
@DedeHai DedeHai requested a review from blazoncek August 16, 2025 05:52
Copy link
Contributor

coderabbitai bot commented Aug 16, 2025

Walkthrough

Updates in wled00/FX_fcn.cpp adjust transition handling and blending safety: resetting a per-transition palette-blend counter when copying an active transition, and capping FADE blending passes to a maximum of 255.

Changes

Cohort / File(s) Change summary
Transition and blending safety
wled00/FX_fcn.cpp
- In Segment::startTransition, reset _t->_prevPaletteBlends to 0 when copying an active transition.
- In Segment::beginDraw (FADE), clamp noOfBlends to a maximum of 255 before blending loop.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • blazoncek

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
wled00/FX_fcn.cpp (1)

371-375: Clamp is good; also guard against negative-diff underflow to avoid extra work

Capping noOfBlends at 255 prevents pathological runtimes. To fully eliminate underflow and avoid doing 255 blends when the target decreased (e.g., mid-transition restarts), compute the target first and only blend if it increased since the last run.

-    unsigned noOfBlends = ((255U * prog) / 0xFFFFU) - _t->_prevPaletteBlends;
-    if(noOfBlends > 255) noOfBlends = 255; // safety check
+    unsigned targetBlends = (255U * prog) / 0xFFFFU;
+    unsigned noOfBlends = 0;
+    if (targetBlends > _t->_prevPaletteBlends) {
+      noOfBlends = targetBlends - _t->_prevPaletteBlends;
+      if (noOfBlends > 255U) noOfBlends = 255U; // cap work per frame
+    }

This preserves the safety cap while ensuring we never over-blend due to unsigned wraparound when the target goes backwards.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c9c442a and b914ab3.

📒 Files selected for processing (1)
  • wled00/FX_fcn.cpp (2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: netmindz
PR: wled/WLED#4728
File: wled00/FX.h:378-378
Timestamp: 2025-06-15T09:59:52.720Z
Learning: In WLED's FX.h, MODE_COUNT represents the highest FX_MODE_ ID + 1, not the total count of FX_MODE_ definitions. This is because some effects may have been removed but their IDs are not reused, creating gaps in the numbering sequence. MODE_COUNT is used for array bounds and range checking.
Learnt from: netmindz
PR: wled/WLED#4728
File: wled00/FX.h:378-378
Timestamp: 2025-06-15T09:59:52.720Z
Learning: In WLED's FX.h, MODE_COUNT represents the highest FX_MODE_ ID + 1, not the total count of FX_MODE_ definitions. This is because some effects may have been removed but their IDs are not reused, creating gaps in the numbering sequence. MODE_COUNT is used for array bounds and range checking.
🔇 Additional comments (1)
wled00/FX_fcn.cpp (1)

282-291: Confirmed: _prevPaletteBlends and _palT declarations match build flags and our guard ensures safe compilation

Inspection of wled00/FX.h shows:

  • _prevPaletteBlends is declared unconditionally (line 512).
  • _palT is only declared under #ifndef WLED_SAVE_RAM (line 508).

Because our new loadPalette(_t->_palT, …) call is wrapped in the same #ifndef WLED_SAVE_RAM guard, it compiles correctly whether or not WLED_SAVE_RAM is defined. No further changes are needed.

Copy link
Collaborator

@blazoncek blazoncek left a comment

Choose a reason for hiding this comment

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

There is no need to do a check if noOfBlends is > 255 as prog <= 0xffff and _prevPaletteBlends <= 0xff.

Resetting _prevPaletteBlends when transition restarts is enough for correct fix.

@@ -368,6 +369,7 @@ void Segment::beginDraw(uint16_t prog) {
// minimum blend time is 100ms maximum is 65535ms
#ifndef WLED_SAVE_RAM
unsigned noOfBlends = ((255U * prog) / 0xFFFFU) - _t->_prevPaletteBlends;
if(noOfBlends > 255) noOfBlends = 255; // safety check
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need for this check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

are you 100% sure there are no possible combinations where transition time changes i.e. prog gets smaller and _prevPaletteBlends is not reset?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we trust in math then yes.
I.e. prog is a _t->_progress alias. _progress only increases with time. One exception is the one you found, when transition is restarted (_start and _dur change) to create a segment copy.

@DedeHai DedeHai merged commit cfad0b8 into wled:main Aug 28, 2025
19 of 21 checks passed
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.

2 participants