Skip to content
25 changes: 7 additions & 18 deletions usermods/user_fx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ The first line of the code imports the [wled.h](https://github.com/wled/WLED/blo
### Static Effect Definition
The next code block is the `mode_static` definition. This is usually left as `SEGMENT.fill(SEGCOLOR(0));` to leave all pixels off if the effect fails to load, but in theory one could use this as a 'fallback effect' to take on a different behavior, such as displaying some other color instead of leaving the pixels off.

`FX_FALLBACK_STATIC` is a macro that calls `mode_static()` and then returns.

### User Effect Definitions
Pre-loaded in this template is an example 2D Effect called "Diffusion Fire". (This is the name that would be shown in the UI once the binary is compiled and run on your device, as defined in the metadata string.)
The effect starts off by checking to see if the segment that the effect is being applied to is a 2D Matrix, and if it is not, then it returns the static effect which displays no pattern:
The effect starts off by checking to see if the segment that the effect is being applied to is a 2D Matrix, and if it is not, then it runs the static effect which displays no pattern:
```cpp
if (!strip.isMatrix || !SEGMENT.is2D())
return mode_static(); // not a 2D set-up
FX_FALLBACK_STATIC; // not a 2D set-up
```
Comment thread
coderabbitai[bot] marked this conversation as resolved.
The next code block contains several constant variable definitions which essentially serve to extract the dimensions of the user's 2D matrix and allow WLED to interpret the matrix as a 1D coordinate system (WLED must do this for all 2D animations):
```cpp
Expand Down Expand Up @@ -128,7 +130,7 @@ unsigned dataSize = cols * rows; // SEGLEN (virtual length) is equivalent to vW

```cpp
if (!SEGENV.allocateData(dataSize))
return mode_static(); // allocation failed
FX_FALLBACK_STATIC; // allocation failed
```
* Upon the first call, this section allocates a persistent data buffer tied to the segment environment (`SEGENV.data`). All subsequent calls simply ensure that the data is still valid.
* The syntax `SEGENV.allocateData(n)` requests a buffer of size n bytes (1 byte per pixel here).
Expand Down Expand Up @@ -250,20 +252,7 @@ After calculating tmp_row, we now handle rendering the pixels by updating the ac
* `SEGCOLOR(0)` gets the first user-selected color for the segment.
* The final line of code fades that base color according to the heat value (acts as brightness multiplier).

The final piece of this custom effect returns the frame time:
```cpp
}
return FRAMETIME;
}
```
* The first bracket closes the earlier `if ((strip.now - SEGENV.step) >= refresh_ms)` block.
* It ensures that the fire simulation (scrolling, sparking, diffusion, rendering) only runs when enough time has passed since the last update.
* returning the frame time tells WLED how soon this effect wants to be called again.
* `FRAMETIME` is a predefined macro in WLED, typically set to ~16ms, corresponding to ~60 FPS (frames per second).
* Even though the effect logic itself controls when to update based on refresh_ms, WLED will still call this function at roughly FRAMETIME intervals to check whether an update is needed.
* ⚠️ Important: Because the actual frame logic is gated by strip.now - SEGENV.step, returning FRAMETIME here doesn’t cause excessive updates — it just keeps the engine responsive. **Also note that an Effect should ALWAYS return FRAMETIME. Not doing so can cause glitches.**
* The final bracket closes the `mode_diffusionfire()` function itself.

* Even though the effect logic itself controls when to update based on refresh_ms, WLED will still call this function at roughly FRAMETIME intervals (the FPS limit set in config) to check whether an update is needed. If nothing needs to change, the frame still needs to be re-rendered so color or brightness transitions will be smooth.

### The Metadata String
At the end of every effect is an important line of code called the **metadata string**.
Expand Down Expand Up @@ -316,7 +305,7 @@ static uint16_t sinelon_base(bool dual, bool rainbow=false) {
* Notice that it has some optional flags; these parameters will allow us to easily define the effect in different ways in the UI.

```cpp
if (SEGLEN <= 1) return mode_static();
if (SEGLEN <= 1) FX_FALLBACK_STATIC;
```
* If segment length ≤ 1, there’s nothing to animate. Just show static mode.

Expand Down
12 changes: 6 additions & 6 deletions usermods/user_fx/user_fx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
// for information how FX metadata strings work see https://kno.wled.ge/interfaces/json-api/#effect-metadata

// static effect, used if an effect fails to initialize
static uint16_t mode_static(void) {
static void mode_static(void) {
SEGMENT.fill(SEGCOLOR(0));
return strip.isOffRefreshRequired() ? FRAMETIME : 350;
}

#define FX_FALLBACK_STATIC { mode_static(); return; }

/////////////////////////
// User FX functions //
/////////////////////////

// Diffusion Fire: fire effect intended for 2D setups smaller than 16x16
static uint16_t mode_diffusionfire(void) {
static void mode_diffusionfire(void) {
if (!strip.isMatrix || !SEGMENT.is2D())
return mode_static(); // not a 2D set-up
FX_FALLBACK_STATIC; // not a 2D set-up

const int cols = SEG_W;
const int rows = SEG_H;
Expand All @@ -29,7 +30,7 @@ static uint16_t mode_diffusionfire(void) {

unsigned dataSize = cols * rows; // SEGLEN (virtual length) is equivalent to vWidth()*vHeight() for 2D
if (!SEGENV.allocateData(dataSize))
return mode_static(); // allocation failed
FX_FALLBACK_STATIC; // allocation failed

if (SEGENV.call == 0) {
SEGMENT.fill(BLACK);
Expand Down Expand Up @@ -84,7 +85,6 @@ unsigned dataSize = cols * rows; // SEGLEN (virtual length) is equivalent to vW
}
}
}
return FRAMETIME;
}
static const char _data_FX_MODE_DIFFUSIONFIRE[] PROGMEM = "Diffusion Fire@!,Spark rate,Diffusion Speed,Turbulence,,Use palette;;Color;;2;pal=35";

Expand Down
Loading
Loading