Skip to content

Commit fc7d4df

Browse files
authored
Added new "effect usermod"
- Added new effect - Diffusion Fire - UM is intended as a base / tutorial (work in progress)
1 parent 8b65d87 commit fc7d4df

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

usermods/user_fx/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Usermod user FX
2+
3+
This Usermod is a common place to put various user's LED effects.
4+

usermods/user_fx/library.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "user_fx"
3+
}

usermods/user_fx/user_fx.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include "wled.h"
2+
3+
// for information how FX metadata strings work see https://kno.wled.ge/interfaces/json-api/#effect-metadata
4+
5+
// static effect, used if an effect fails to initialize
6+
static uint16_t mode_static(void) {
7+
SEGMENT.fill(SEGCOLOR(0));
8+
return strip.isOffRefreshRequired() ? FRAMETIME : 350;
9+
}
10+
11+
/////////////////////////
12+
// User FX functions //
13+
/////////////////////////
14+
15+
// Diffusion Fire: fire effect intended for 2D setups smaller than 16x16
16+
static uint16_t mode_diffusionfire(void) {
17+
if (!strip.isMatrix || !SEGMENT.is2D())
18+
return mode_static(); // not a 2D set-up
19+
20+
const int cols = SEG_W;
21+
const int rows = SEG_H;
22+
const auto XY = [&](int x, int y) { return x + y * cols; };
23+
24+
const uint8_t refresh_hz = map(SEGMENT.speed, 0, 255, 20, 80);
25+
const unsigned refresh_ms = 1000 / refresh_hz;
26+
const int16_t diffusion = map(SEGMENT.custom1, 0, 255, 0, 100);
27+
const uint8_t spark_rate = SEGMENT.intensity;
28+
const uint8_t turbulence = SEGMENT.custom2;
29+
30+
unsigned dataSize = SEGMENT.length(); // allocate persistent data for heat value for each pixel
31+
if (!SEGENV.allocateData(dataSize))
32+
return mode_static(); // allocation failed
33+
34+
if (SEGENV.call == 0) {
35+
SEGMENT.fill(BLACK);
36+
SEGENV.step = 0;
37+
}
38+
39+
if ((strip.now - SEGENV.step) >= refresh_ms) {
40+
uint8_t tmp_row[cols];
41+
SEGENV.step = strip.now;
42+
// scroll up
43+
for (unsigned y = 1; y < rows; y++)
44+
for (unsigned x = 0; x < cols; x++) {
45+
unsigned src = XY(x, y);
46+
unsigned dst = XY(x, y - 1);
47+
SEGMENT.data[dst] = SEGMENT.data[src];
48+
}
49+
50+
if (hw_random8() > turbulence) {
51+
// create new sparks at bottom row
52+
for (unsigned x = 0; x < cols; x++) {
53+
uint8_t p = hw_random8();
54+
if (p < spark_rate) {
55+
unsigned dst = XY(x, rows - 1);
56+
SEGMENT.data[dst] = 255;
57+
}
58+
}
59+
}
60+
61+
// diffuse
62+
for (unsigned y = 0; y < rows; y++) {
63+
for (unsigned x = 0; x < cols; x++) {
64+
unsigned v = SEGMENT.data[XY(x, y)];
65+
if (x > 0) {
66+
v += SEGMENT.data[XY(x - 1, y)];
67+
}
68+
if (x < (cols - 1)) {
69+
v += SEGMENT.data[XY(x + 1, y)];
70+
}
71+
tmp_row[x] = min(255, (int)(v * 100 / (300 + diffusion)));
72+
}
73+
74+
for (unsigned x = 0; x < cols; x++) {
75+
SEGMENT.data[XY(x, y)] = tmp_row[x];
76+
if (SEGMENT.check1) {
77+
uint32_t color = ColorFromPalette(SEGPALETTE, tmp_row[x], 255, LINEARBLEND_NOWRAP);
78+
SEGMENT.setPixelColorXY(x, y, color);
79+
} else {
80+
uint32_t color = SEGCOLOR(0);
81+
SEGMENT.setPixelColorXY(x, y, color_fade(color, tmp_row[x]));
82+
}
83+
}
84+
}
85+
}
86+
return FRAMETIME;
87+
}
88+
static const char _data_FX_MODE_DIFFUSIONFIRE[] PROGMEM = "Diffusion Fire@!,Spark rate,Diffusion Speed,Turbulence,,Use palette;;Color;;2;pal=35";
89+
90+
91+
/////////////////////
92+
// UserMod Class //
93+
/////////////////////
94+
95+
class UserFxUsermod : public Usermod {
96+
private:
97+
public:
98+
void setup() override {
99+
strip.addEffect(255, &mode_diffusionfire, _data_FX_MODE_DIFFUSIONFIRE);
100+
101+
////////////////////////////////////////
102+
// add your effect function(s) here //
103+
////////////////////////////////////////
104+
105+
// use id=255 for all custom user FX (the final id is assigned when adding the effect)
106+
107+
// strip.addEffect(255, &mode_your_effect, _data_FX_MODE_YOUR_EFFECT);
108+
// strip.addEffect(255, &mode_your_effect2, _data_FX_MODE_YOUR_EFFECT2);
109+
// strip.addEffect(255, &mode_your_effect3, _data_FX_MODE_YOUR_EFFECT3);
110+
}
111+
void loop() override {} // nothing to do in the loop
112+
uint16_t getId() override { return USERMOD_ID_USER_FX; }
113+
};
114+
115+
static UserFxUsermod user_fx;
116+
REGISTER_USERMOD(user_fx);

wled00/const.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ static_assert(WLED_MAX_BUSSES <= 32, "WLED_MAX_BUSSES exceeds hard limit");
198198
#define USERMOD_ID_DEEP_SLEEP 55 //Usermod "usermod_deep_sleep.h"
199199
#define USERMOD_ID_RF433 56 //Usermod "usermod_v2_RF433.h"
200200
#define USERMOD_ID_BRIGHTNESS_FOLLOW_SUN 57 //Usermod "usermod_v2_brightness_follow_sun.h"
201+
#define USERMOD_ID_USER_FX 58 //Usermod "user_fx"
201202

202203
//Access point behavior
203204
#define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot

0 commit comments

Comments
 (0)