Skip to content

New user effect: Firefly Garden 2D - #5974

Draft
sudo-kk wants to merge 6 commits into
wled:mainfrom
sudo-kk:feat/firefly-garden
Draft

sudo-kk wants to merge 6 commits into
wled:mainfrom
sudo-kk:feat/firefly-garden

Conversation

@sudo-kk

@sudo-kk sudo-kk commented Nov 14, 2026 •

Copy link
Copy Markdown

What it does

Adds an optional 2D “Firefly Garden” effect to the user_fx usermod. It displays glowing fireflies drifting around a matrix with adjustable speed, density, glow, and drift behavior.

Additional references and context

No related issue.

How it works

The effect uses a bounded pool of 24 particles stored in SEGENV.data. Each particle has a fixed-point position, velocity, color hue, brightness, and twinkle target. Motion is updated at a controlled interval, while brightness fades toward randomly changing targets. The effect uses the selected WLED palette and falls back to static output on non-2D setups.

Testing and Validation

  • Tested on actual hardware:
    • ESP32 with a 16x16 LED matrix.
    • Tested speed, density, glow, and drifting behavior.
  • Open ends, known limitations, breaking changes:
    • The effect is intended for 2D matrices and is provided through the optional user_fx usermod.
    • No core firmware APIs were changed.
  • Anything you couldn't quite solve?
    • No known issues on the tested hardware.

Checklist

  • I have personally reviewed all changes (incl. source code) before opening this PR.
  • I'm ready to explain my proposed source code changes in detail (without an AI agent answering for me).
  • I agree to follow this project's Code of Conduct

Summary by CodeRabbit

  • New Features
    • Added the Firefly Garden effect for 2D displays, featuring palette-colored glows that move and pulse in brightness.
    • Intensity adjusts the number of active fireflies, while speed controls movement and scale; additional settings adjust glow radius and direction changes.
    • The effect falls back to a static fill on unsupported displays, segments larger than 128 pixels, or when particle state cannot be allocated.

Copilot AI balanced review requested due to automatic review settings November 14, 2026 19:00

This comment was marked as outdated.

@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: wled/WLED
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 88a853b
  • Layers: Repository YAML (base), Repository UI (inherited), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 60a75d97-3307-435e-84c6-ea7d31882081
Commits

Reviewing files that changed from the base of the PR and between c4affcb and 7ea6c86.

Files selected for processing: 1

  • usermods/user_fx/user_fx.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Recent review details

Context from checks skipped due to timeout: 1

  • GitHub Check: CodeRabbit
Additional context used

Code guidelines: 3

docs/hardening.instructions.md — configured
docs/cpp.instructions.md — configured
AGENTS.md — configured
Path-based instructions: 6

For usermods, the untrusted ingress points are: readFromConfig(JsonObject& root) and calls to getJsonValue() readFromJsonState(JsonObject& obj) — JSON is parsed, but values are client-supplied onMqttMessage(char* topic, char* payload) — raw...

CodeRabbit configuration file

Files:

  • usermods/user_fx/user_fx.cpp
Follow the C++ coding conventions documented in docs/cpp.instructions.md and the general project guidelines in AGENTS.md and .github/copilot-instructions.md.

CodeRabbit configuration file

Files:

  • usermods/user_fx/user_fx.cpp
Usermods are community add-ons.

CodeRabbit configuration file

Files:

  • usermods/user_fx/user_fx.cpp
Applicability reference: AGENTS.md: # AGENTS.md — WLED AI Coding Agent & AI Code Review Reference See also: `.github/copilot-instructions.md`, `.github/agent-build.instructions.md`, `docs/cpp.instructions.md`, `docs/web.in...

CodeRabbit inference engine (docs/hardening.instructions.md)

Files:

  • usermods/user_fx/user_fx.cpp
Source excerpt: Follow the existing style in the file you are editing Source excerpt: Avoid unexplained "magic numbers".

CodeRabbit inference engine (AGENTS.md)

Files:

  • usermods/user_fx/user_fx.cpp
Source excerpt: **In-Tree Usermods** live in `usermods//` with a `.cpp`, optional `.h`, `library.json`, and `readme.md`.

CodeRabbit inference engine (AGENTS.md)

Files:

  • usermods/user_fx/user_fx.cpp
Additional comments: 1

usermods/user_fx/user_fx.cpp: 1

125-125: LGTM!

Also applies to: 141-141, 154-155, 173-178

Walkthrough

The pull request adds and registers the 2D-only Firefly Garden effect. It updates particle motion and brightness, renders additive palette-colored glows, and uses a static fallback when the display or particle allocation is unsupported.

Changes

Firefly Garden effect

Layer / File(s) Summary
Implement and register Firefly Garden
usermods/user_fx/user_fx.cpp
Adds particle state and motion helpers, initializes and updates particles, and renders additive palette-colored glows. The effect uses a static fallback for non-2D segments, dimensions above 128 pixels, and particle allocation failures. It registers the effect with its metadata.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Suggested reviewers: bobloeffler68, dedehai

Merge Risk: Minimal · up to 7ea6c

This adds an optional 2D effect to the user_fx usermod and falls back to a static output on unsupported setups. No unresolved merge-blocking risk remains in the supplied review evidence.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title 'New user effect: Firefly Garden 2D' matches the main change. The raw summary says the PR adds a Firefly Garden 2D matrix effect registered in usermods/user_fx/user_fx.cpp, and the objective…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title 'New user effect: Firefly Garden 2D' matches the main change. The raw summary says the PR adds a Firefly Garden 2D matrix effect registered in usermods/user_fx/user_fx.cpp, and the objectives describe the same optional 2D effect. The title is short, specific, and free of file lists or vague terms.

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

coderabbitai[bot]

This comment was marked as resolved.

@sudo-kk sudo-kk left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed all four findings:

  • Firefly particles now initialize after allocation recovery.
  • Brightness comparisons use wider arithmetic to avoid uint8_t wraparound.
  • Palette hue mapping is disabled because hue is already a palette index.
  • Firefly Garden registration now appends after existing effects to preserve effect IDs.

@sudo-kk sudo-kk closed this Nov 14, 2026
@sudo-kk sudo-kk reopened this Nov 14, 2026
@DedeHai

DedeHai commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

looks good in general, a few notes:

  • no title, no description, no comments - please add some context to the code
  • test on smaller and larger (virtual) matrix, at least up to 64x64
  • the positions are 32bit, that is a bit over the top for particles that move in an 8bit grid so you could save RAM by optimizing that
  • I assume the "jerky movement", jumps between pixels and the diamond-shapes are intentional?

@sudo-kk

sudo-kk commented Nov 15, 2026

Copy link
Copy Markdown
Author

Thanks for the feedback. I added comments describing the effect controls, Q8.8 particle positions, SEGENV allocation, palette behavior, and 2D scope. I reduced position storage to int16_t; its range is sufficient for 64×64 coordinates, though I haven’t visually tested a 64×64 matrix.

The diamond shape was unintended. It came from Manhattan distance and is now replaced with a circular falloff. The particle positions retain fractional-pixel movement, but rendering on a 16×16 LED matrix still rounds to whole pixels, so some visible stepping remains.

I built the updated branch with esp32dev_user_fx and tested on my physical 16×16 matrix. I also tested 8×16 and 8×32 WLED layouts using the same hardware. I have not tested a physical 64×64 matrix.

@softhack007 softhack007 added effect usermod usermod related labels Nov 16, 2026
@softhack007

This comment was marked as outdated.

@coderabbitai

This comment was marked as outdated.

coderabbitai[bot]

This comment was marked as resolved.

@sudo-kk

sudo-kk commented Nov 16, 2026

Copy link
Copy Markdown
Author

Fixed: dimensions above 128 now fall back to static.
Fixed: Speed changes rescale velocities, even with Drift at zero.
Tested on 16×16

coderabbitai[bot]

This comment was marked as resolved.

@sudo-kk

sudo-kk commented Nov 16, 2026

Copy link
Copy Markdown
Author

Stored velocities are now speed-independent base values, with speed scaling applied during movement rather than rescaling in place. This preserves effect state and eliminates truncation decay across Speed changes when Drift=0

This comment was marked as low quality.

Comment thread usermods/user_fx/user_fx.cpp Resolved
@softhack007

This comment was marked as resolved.

@coderabbitai

This comment was marked as resolved.

@softhack007

Copy link
Copy Markdown
Member

@coderabbitai would visual quality be impacted if the effect uses Q12.4 (or Q10.6) instead of Q8.8?

@coderabbitai

coderabbitai Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

Analysis chain

Script executed:

#!/bin/bash
set -eu
file="usermods/user_fx/user_fx.cpp"
echo '=== Firefly outline ==='
ast-grep outline "$file" --items all --match '[Ff]irefly' --view expanded || true
echo '=== Firefly references ==='
rg -n -i -C 3 'firefly' "$file"
echo '=== Firefly implementation ==='
line=$(rg -n 'mode_2D[Ff]irefly|mode_[Ff]irefly' "$file" | head -n 1 | cut -d: -f1)
if [ -n "$line" ]; then
  start=$((line > 80 ? line - 80 : 1))
  end=$((line + 165))
  cat -n "$file" | sed -n "${start},${end}p"
fi

Length of output: 17810


@s​ofthack007 Yes, visual quality could change, mainly because lower precision changes particle movement. Q10.6 is likely to preserve it better than Q12.4, but I have not visually tested either format.

The effect currently draws each glow at a whole-pixel position (usermods/user_fx/user_fx.cpp, Lines 201–202). Neither format would remove the visible pixel stepping. The fractional bits instead control when a particle moves to the next pixel.

  • Q8.8 has increments of 1/256 pixel.
  • Q10.6 has increments of 1/64 pixel.
  • Q12.4 has increments of 1/16 pixel.

If each movement update is truncated to the position format, the effect can run slower than intended. For example, at minimum Speed, with an 80 ms update and a velocity component of 127, the current calculation advances that component by 25/256 pixel. Equivalent truncated updates would advance it by 6/64 in Q10.6 or 1/16 in Q12.4. The Q12.4 update is about 36% smaller in this example. Keeping fractional remainders between updates would reduce this error.

Both formats can store coordinates for a 256×256 matrix in signed int16_t, which meets your existing review guidance for 2D effects. A conversion must also update position initialization, boundary calculations, movement scaling, and rendering shifts.

I would try Q10.6 first, then compare movement at low and high Speed with Drift set to zero and nonzero. A 64×64 virtual matrix would also help check the larger layout requested in review.


Learnings used

Learnt from: softhack007
Repo: wled/WLED PR: 5908
File: :0-0
Timestamp: 2026-10-16T19:16:35.445Z
Learning: For WLED C++ PRs that touch 2D effects in wled00/FX.cpp or usermods, always check that effect math remains stable for matrix dimensions up to 256×256 pixels. Review arithmetic overflow, signedness, coordinate ranges, and pixel-count calculations. Typical WS2812B panel sizes are 24×16, 32×24, 32×32, and 48×40. HUB75 panels are typically 64×64, and up to four panels can be chained into a larger fixture.
Learnt from: softhack007
Repo: wled/WLED PR: 0
File: :0-0
Timestamp: 2026-10-03T14:59:43.661Z
Learning: In wled/WLED, softhack007 prefers contributors to answer maintainer questions directly with short explanations in their own words, even if the explanations are technically imprecise or the English is imperfect. Contributors should state uncertainty and actual test results rather than relay unverified AI-generated technical answers. AI may assist with translation or wording, but should not manufacture explanations that shift fact-checking and AI-session supervision onto maintainers.

You are interacting with an AI system.

for (int8_t offsetY = -glowRadius; offsetY <= glowRadius; offsetY++) {
for (int8_t offsetX = -glowRadius; offsetX <= glowRadius; offsetX++) {
const int distanceSquared = offsetX * offsetX + offsetY * offsetY;
const int glowRadiusSquared = glowRadius * glowRadius;

@softhack007 softhack007 Nov 17, 2026 •

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.

for code clarity (and maybe for a small speedup), you could move calculation of glowRadiusSqared (line 207) and fallOffRange (line 214) out of the pixel loop, i.e. before line 204. The values do not depend on offsetX or offsetY.

for (int8_t offsetY = -glowRadius; offsetY <= glowRadius; offsetY++) {
for (int8_t offsetX = -glowRadius; offsetX <= glowRadius; offsetX++) {
const int distanceSquared = offsetX * offsetX + offsetY * offsetY;
const int glowRadiusSquared = glowRadius * glowRadius;

@softhack007 softhack007 Nov 17, 2026 •

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.

🤔 at higher glow settings, your fireflies look more like diamonds/spiked balls. In case you want them to be soft-edge circles, try

     const int glowRadiusSquared = max(1, glowRadius * glowRadius + glowRadius -1); // (r+0.5)^2 = r*r + r + 0.25;

For unsigned integers, glowRadius * glowRadius + glowRadius is mathematicially equivalent to (glowRadius+0.5) ^2. It avoids the spikes and creates nicer circles. The -1 preserves the default look at glow=96.

@softhack007

softhack007 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Another small speedup would be to replace the loop variable types int8_t by int. Espressif chips are 32bit - not 8bit as the original arduino.
Using 8bit variables forces the compiler to insert additional instructions to emulate 8-bit overflow behaviour, which makes int8_t and int16_t slower than int.

You can keep 8bit for the persistent fireFly state, as it reduces RAM usage.

@softhack007 softhack007 modified the milestones: 17.0, 16.2 Nov 27, 2026
@softhack007 softhack007 added the needs_rework PR needs improvements before merging (RED FLAG) label Dec 2, 2026
@softhack007
softhack007 marked this pull request as draft December 2, 2026 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effect needs_rework PR needs improvements before merging (RED FLAG) usermod usermod related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants