Skip to content

Commit 14653f8

Browse files
committed
Added new effect usermod - Diffusion Fire
1 parent f721efc commit 14653f8

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

usermods/user_fx/README.md

+4
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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name:": "user_fx"
3+
}

usermods/user_fx/user_fx.cpp

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "user_fx.h"
2+
3+
#include <cstdint>
4+
5+
#define XY(_x, _y) (_x + SEG_W * (_y))
6+
7+
static uint16_t mode_static(void) {
8+
SEGMENT.fill(SEGCOLOR(0));
9+
return strip.isOffRefreshRequired() ? FRAMETIME : 350;
10+
}
11+
12+
static uint16_t mode_diffusionfire(void) {
13+
static uint32_t call = 0;
14+
15+
if (!strip.isMatrix || !SEGMENT.is2D())
16+
return mode_static(); // not a 2D set-up
17+
18+
const int cols = SEG_W;
19+
const int rows = SEG_H;
20+
21+
const uint8_t refresh_hz = map(SEGMENT.speed, 0, 255, 20, 80);
22+
const unsigned refresh_ms = 1000 / refresh_hz;
23+
const int16_t diffusion = map(SEGMENT.custom1, 0, 255, 0, 100);
24+
const uint8_t spark_rate = SEGMENT.intensity;
25+
const uint8_t turbulence = SEGMENT.custom2;
26+
27+
unsigned dataSize = SEGMENT.length() + (cols * sizeof(uint16_t));
28+
if (!SEGENV.allocateData(dataSize))
29+
return mode_static(); // allocation failed
30+
31+
if (SEGENV.call == 0) {
32+
SEGMENT.fill(BLACK);
33+
SEGENV.step = 0;
34+
call = 0;
35+
}
36+
37+
if ((strip.now - SEGENV.step) >= refresh_ms) {
38+
uint16_t *tmp_row = (uint16_t *)(SEGMENT.data + SEGMENT.length());
39+
SEGENV.step = strip.now;
40+
call++;
41+
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+
CRGB color = ColorFromPalette(SEGPALETTE, tmp_row[x], 255, NOBLEND);
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 =
89+
"Diffusion Fire@!,Spark rate,Diffusion Speed,Turbulence,,Use "
90+
"palette;;Color;;2;pal=35";
91+
92+
void UserFxUsermod::setup() {
93+
strip.addEffect(255, &mode_diffusionfire, _data_FX_MODE_DIFFUSIONFIRE);
94+
}
95+
void UserFxUsermod::loop() {}
96+
uint16_t UserFxUsermod::getId() { return USERMOD_ID_USER_FX; }
97+
98+
static UserFxUsermod user_fx;
99+
REGISTER_USERMOD(user_fx);

usermods/user_fx/user_fx.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include "wled.h"
3+
4+
class UserFxUsermod : public Usermod {
5+
private:
6+
public:
7+
void setup();
8+
9+
void loop();
10+
11+
uint16_t getId();
12+
};

wled00/const.h

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
#define USERMOD_ID_DEEP_SLEEP 55 //Usermod "usermod_deep_sleep.h"
211211
#define USERMOD_ID_RF433 56 //Usermod "usermod_v2_RF433.h"
212212
#define USERMOD_ID_BRIGHTNESS_FOLLOW_SUN 57 //Usermod "usermod_v2_brightness_follow_sun.h"
213+
#define USERMOD_ID_USER_FX 58 //Usermod "user_fx"
213214

214215
//Access point behavior
215216
#define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot

0 commit comments

Comments
 (0)