Skip to content

Added Chase Race Effect to WLED #4566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

drdome
Copy link

@drdome drdome commented Feb 17, 2025

Added a new effect, "Chase Race," based on the existing Chase effect.

Chase Race is designed for automotive racing displays, simulating three colored "cars" chasing each other in LED sequences. Unlike Chase, it accepts three input colors, with a fixed black background. This can be integrated with Home Assistant, allowing real-time race data to dynamically adjust colors and sequencing. Initial testing has been conducted using F1 race data.

Summary by CodeRabbit

  • New Features
    • Introduced a new "Chase Race" LED effect mode, featuring three colored bands racing along the strip with black gaps in between. This effect is now available for selection among the lighting modes.

Copy link

coderabbitai bot commented Feb 17, 2025

Walkthrough

A new LED effect mode called "Chase Race" has been introduced. The core implementation is added in the main effects source file, where a function animates three colored bands racing along the LED strip with black gaps, handling different segment lengths and cycling through colors as needed. The effect is registered in the setup routine with its metadata. Additionally, the effect mode is defined in the header file with a unique identifier, and the total mode count is incremented to account for the new addition. No other logic or control flow is modified.

Changes

File(s) Change Summary
wled00/FX.cpp Added the chase_race() function implementing the "Chase Race" effect. Registered the new effect mode (FX_MODE_CHASE_RACE) with its metadata in the effect setup routine. Minor whitespace adjustment on one line.
wled00/FX.h Defined FX_MODE_CHASE_RACE with identifier 217 and incremented MODE_COUNT from 217 to 218.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ast-grep (0.31.1)
wled00/FX.cpp

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 37ce66e and f595010.

📒 Files selected for processing (2)
  • wled00/FX.cpp (5 hunks)
  • wled00/FX.h (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • wled00/FX.h
🚧 Files skipped from review as they are similar to previous changes (1)
  • wled00/FX.cpp

🪧 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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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

@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 (3)
wled00/FX.cpp (2)

932-995: Consider refactoring repeated logic and clarifying the do_palette parameter.

  1. The repeated sequence of for-loops filling color1, color2, and color3 could be more compact and easier to maintain if consolidated in a helper method or by iterating over an array of colors.
  2. The bool do_palette argument is never used in this function body. If palette-based color selection isn’t planned, removing this parameter reduces confusion and complexity. Otherwise, consider implementing palette-based logic.

Below is one possible approach to eliminate the unused parameter and move the color-filling logic into a simple loop:

-uint16_t chase_race(uint32_t color1, uint32_t color2, uint32_t color3, bool do_palette)
+uint16_t chase_race(uint32_t color1, uint32_t color2, uint32_t color3)
 {
   uint16_t counter = strip.now * ((SEGMENT.speed >> 2) + 1);
   // ... existing code to compute a, b, c, d, e, f

-  // if (do_palette) { ... } // <— not currently implemented
   SEGMENT.fill(0);

   // Example of refactoring repeated loops
   uint32_t colors[3] = {color1, color2, color3};
   uint16_t fillStarts[3] = {a, c, e};
   uint16_t fillEnds[3]   = {b, d, f};

   for (int idx = 0; idx < 3; idx++) {
     if (fillStarts[idx] < fillEnds[idx]) {
       for (unsigned i = fillStarts[idx]; i < fillEnds[idx]; i++) {
         SEGMENT.setPixelColor(i, colors[idx]);
       }
     } else {
       for (unsigned i = fillStarts[idx]; i < SEGLEN; i++) {
         SEGMENT.setPixelColor(i, colors[idx]);
       }
       for (unsigned i = 0; i < fillEnds[idx]; i++) {
         SEGMENT.setPixelColor(i, colors[idx]);
       }
     }
   }
   return FRAMETIME;
 }

1006-1008: Fix spelling in documentation.
The comment “Chace Race with 3 color strips” contains a minor spelling mistake. Consider changing “Chace” to “Chase” to maintain clarity.

-* Chace Race with 3 color strips 
+* Chase Race with 3 color strips
wled00/FX.h (1)

354-355: Consider adding documentation for the new effect mode.

Since this is a specialized effect for automotive racing displays, it would be helpful to add a comment describing its purpose and behavior, similar to how other effects are documented (e.g., line 179's comment for FX_MODE_TETRIX).

Add a comment like:

+// Chase Race effect simulates three colored "cars" chasing each other with a black background
 #define FX_MODE_CHASE_RACE             215
 #define MODE_COUNT                     216
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a2de3d3 and 32dc661.

📒 Files selected for processing (2)
  • wled00/FX.cpp (3 hunks)
  • wled00/FX.h (1 hunks)
🔇 Additional comments (6)
wled00/FX.cpp (2)

928-931: Good documentation on the feature intent.
This doc comment succinctly outlines the purpose of the new Chase Race function and how it operates with three separate color bands.


10272-10272: Verify the effect ID assignment.
Ensure that FX_MODE_CHASE_RACE does not conflict with existing effect IDs and that MODE_COUNT has been incremented accordingly in the header file. If the new effect ID is already in use, it would cause unexpected behavior.

wled00/FX.h (4)

354-355: LGTM! New effect mode added correctly.

The new Chase Race effect mode is properly defined with a unique identifier (215) and MODE_COUNT is correctly incremented to 216.


1023-1026: Verify vector capacity for new effect mode.

The _mode and _modeData vectors need to accommodate the new effect. The code already handles this in the constructor (lines 839-842) by reserving capacity and falling back to a single mode if allocation fails.


354-355:

✅ Verification successful

Verify effect mode number uniqueness.

Let's verify that no other effect is using the same mode number to prevent conflicts.


🏁 Script executed:

#!/bin/bash
# Search for any other occurrences of mode number 215
rg --type cpp "215" --no-filename | grep -v "FX_MODE_CHASE_RACE"

Length of output: 1231


Effect mode number 215 is confirmed unique.

The search output shows that the literal “215” appears in various parts of the code (comments, array parameters, EEPROM accesses, etc.), but among the effect mode definitions, it’s used only for FX_MODE_CHASE_RACE (with MODE_COUNT immediately following at 216). There are no other macro definitions assigning 215 to an effect mode.


75-87:

❓ Verification inconclusive

Verify memory constraints for new effect.

Since each segment uses significant memory (82 bytes) and there are platform-specific limits, let's ensure adding a new effect doesn't push memory usage beyond safe limits.


🏁 Script executed:

#!/bin/bash
# Look for memory-related issues or warnings in the codebase
rg -A 3 "insufficient memory|memory allocation failed|out of memory" --no-filename

Length of output: 1165


Ensure new effect respects existing memory constraints

  • The current macro definitions in wled00/FX.h set platform-specific limits (e.g., 16 segments for ESP8266, 20 for ESP32S2, 32 for others), with each segment consuming roughly 82 bytes.
  • Our grep search confirms that warnings (e.g., "insufficient memory", "out of memory") are already in place to catch memory allocation issues elsewhere in the code.
  • When integrating the new effect, please verify that its memory footprint does not exceed the limits implied by MAX_NUM_SEGMENTS and MAX_SEGMENT_DATA. Ensure that, on low-memory platforms, adding the effect won’t push memory usage into unsafe territory.
  • Further testing on the target hardware (especially on constrained devices like ESP8266) is recommended to confirm that memory remains within safe bounds.

@DedeHai
Copy link
Collaborator

DedeHai commented Feb 18, 2025

thanks for your contribution.
the code seems a bit unfinished...
there is the do_palette parameter that is unused, also making the effect a function and using another function to call it does only make sense if its used multiple times.
I used an AI to improve code size and adjusted parameters slightly. in your original version, there is overlapping if you set width to a high value.

consider this:


uint16_t chase_race(uint32_t color1, uint32_t color2, uint32_t color3) {
    uint16_t counter = strip.now * ((SEGMENT.speed >> 2) + 1);
    uint16_t a = (counter * SEGLEN) >> 16;
    
    // Use intensity setting to vary chase up to 1/2 string length
    unsigned size = 1 + ((SEGMENT.intensity * SEGLEN) >> 10);
    unsigned gap = 1 + size / 4;  // Smaller black band size as a fraction of color size
    
    // Calculate positions for each color band
    uint16_t positions[] = {
        a,                          // Start of color1
        (a + size) % SEGLEN,       // End of color1
        (a + size + gap) % SEGLEN, // Start of color2
        (a + size * 2 + gap) % SEGLEN, // End of color2
        (a + size * 2 + gap * 2) % SEGLEN, // Start of color3
        (a + size * 3 + gap * 2) % SEGLEN  // End of color3
    };
    
    // Define compact lambda for filling LED segments using modulo
    auto fillSegment = [](uint16_t start, uint16_t end, uint32_t color) {
        for (unsigned count = 0; count < SEGLEN && count < end - start + (end < start ? SEGLEN : 0); count++) {
            SEGMENT.setPixelColor((start + count) % SEGLEN, color);
        }
    };
    
    // Set background to black
    SEGMENT.fill(0);
    
    // Fill the three color segments
    fillSegment(positions[0], positions[1], color1);
    fillSegment(positions[2], positions[3], color2);
    fillSegment(positions[4], positions[5], color3);
    
    return FRAMETIME;
}

in my brief checking it looks almost the same but improved gaps, no overlapping and 40% less code size.

one more thing: the colors should be named 1, 2, 3 (see "Chase 3" effect) instead of Fx, Bg, Cs

an option to add a palette background with a checkmark would also be a nice addition

Copy link

@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 (4)
wled00/FX.cpp (3)

928-931: Clarify documentation to reflect absence of palette usage
The doc block states that three colored bands race along with black in between, but it does not mention that the background is forced to black and that the do_palette parameter remains unused. Consider elaborating on this to ensure users know the effect doesn’t honor palette-based backgrounds.


933-970: Validate arithmetic for wrapping and segment sizes
The arithmetic, especially around size and gap, correctly uses modular wrapping but can become large if SEGLEN and the intensity setting are near their maximum. Although the modulo math in fillSegment mitigates out-of-bounds writes, you may wish to clamp or explicitly validate sizes for extreme cases. Additionally, watch for scenarios where (end == start) can create unintended edge conditions.


980-987: Consider optional palette background support
This mode currently sets three colors and forces a black background. If you want to leverage existing palette features or add a user-selectable background, consider accepting a bool do_palette parameter or adopting a palette-based fill approach to improve consistency with other multi-color effects.

platformio.ini (1)

13-14: Default Environments Revision Impact

You've replaced the extensive list of default environments with a single target (esp32dev). This change streamlines the build process—likely to focus on testing the new Chase Race effect on a specific platform—but it reduces the diversity of environments tested by default. Please verify that this narrowing is intentional and that alternative configurations are tested elsewhere if multi-board compatibility remains a requirement.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 32dc661 and 6fc2495.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (2)
  • platformio.ini (1 hunks)
  • wled00/FX.cpp (3 hunks)
🔇 Additional comments (1)
wled00/FX.cpp (1)

10246-10246: Effect registration looks good
Registering the new “Chase Race” effect with addEffect() is straightforward and consistent with the approach used for other modes. No issues here.

@netmindz
Copy link
Member

This feels a bit niche to be a core effect and I think should be a usermod instead

@DedeHai
Copy link
Collaborator

DedeHai commented Feb 18, 2025

@netmindz if done right, its about the same as the other chase effects so I see no reason to have them but not this version.
I would like to see them unified in one FX though (currently working on unifying some other FX, PR coming soon)

@blazoncek
Copy link
Collaborator

@DedeHai cherry-pick from my fork. Already done that part.

@netmindz
Copy link
Member

netmindz commented Feb 22, 2025

Ah, so the code doesn't really do what the description says, it's only when you also basically provide the other half of the functionality with HA that it works. I should have read the code before commenting

I stick by my comment however that i'm sure most users will have no idea that the new effect was intended to be used in this way

wled00/FX.cpp Outdated
/*
* Chase Race with 3 color strips
*/
uint16_t mode_chase_race()
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove alias function, it serves no purpose.

platformio.ini Outdated
default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, nodemcuv2_160, esp8266_2m_160, esp01_1m_full_160, nodemcuv2_compat, esp8266_2m_compat, esp01_1m_full_compat, esp32dev, esp32dev_V4, esp32_eth, lolin_s2_mini, esp32c3dev, esp32s3dev_16MB_opi, esp32s3dev_8MB_opi, esp32s3_4M_qspi, esp32_wrover

#default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, nodemcuv2_160, esp8266_2m_160, esp01_1m_full_160, nodemcuv2_compat, esp8266_2m_compat, esp01_1m_full_compat, esp32dev, esp32dev_V4, esp32_eth, lolin_s2_mini, esp32c3dev, esp32s3dev_16MB_opi, esp32s3dev_8MB_opi, esp32s3_4M_qspi, esp32_wrover
default_envs = esp32dev
Copy link
Collaborator

Choose a reason for hiding this comment

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

revert any changes to platform.ini

scattervideo and others added 2 commits April 14, 2025 16:15
Changed platformoio.ini back to original for all platforms per DedeHai
@drdome
Copy link
Author

drdome commented Apr 14, 2025

Made/removed changes to platformio.ini file. Updated fx.cpp to remove unneeded alias per DedeHai

@drdome drdome closed this Apr 14, 2025
@drdome drdome reopened this Apr 14, 2025
wled00/FX.cpp Outdated
/*
* Chase Race with 3 color strips
*/
uint16_t chase_race()
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is the alias function, it does nothing but call the other one.

platformio.ini Outdated
@@ -11,7 +11,6 @@

# CI/release binaries
default_envs = nodemcuv2, esp8266_2m, esp01_1m_full, nodemcuv2_160, esp8266_2m_160, esp01_1m_full_160, nodemcuv2_compat, esp8266_2m_compat, esp01_1m_full_compat, esp32dev, esp32dev_V4, esp32_eth, lolin_s2_mini, esp32c3dev, esp32s3dev_16MB_opi, esp32s3dev_8MB_opi, esp32s3_4M_qspi, esp32_wrover, usermods

Copy link
Collaborator

Choose a reason for hiding this comment

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

remove change

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.

5 participants