Skip to content
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

Added new effect usermod - Diffusion Fire #4473

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions usermods/user_fx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Usermod user FX

This Usermod is a common place to put various user's LED effects.

96 changes: 96 additions & 0 deletions usermods/user_fx/usermod_user_fx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
static uint16_t mode_static(void) {
SEGMENT.fill(SEGCOLOR(0));
return strip.isOffRefreshRequired() ? FRAMETIME : 350;
}

static uint16_t mode_diffusionfire(void) {
static uint32_t call = 0;

if (!strip.isMatrix || !SEGMENT.is2D())
return mode_static(); // not a 2D set-up

const int cols = SEG_W;
const int rows = SEG_H;

const uint8_t refresh_hz = map(SEGMENT.speed, 0, 255, 20, 80);
const unsigned refresh_ms = 1000 / refresh_hz;
const int16_t diffusion = map(SEGMENT.custom1, 0, 255, 0, 100);
const uint8_t spark_rate = SEGMENT.intensity;
const uint8_t turbulence = SEGMENT.custom2;

unsigned dataSize = SEGMENT.length() + (cols * sizeof(uint16_t));
if (!SEGENV.allocateData(dataSize))
return mode_static(); // allocation failed

if (SEGENV.call == 0) {
SEGMENT.fill(BLACK);
SEGENV.step = 0;
call = 0;
}

if ((strip.now - SEGENV.step) >= refresh_ms) {
uint16_t *tmp_row = (uint16_t *)(SEGMENT.data + SEGMENT.length());
SEGENV.step = strip.now;
call++;

// scroll up
for (unsigned y = 1; y < rows; y++)
for (unsigned x = 0; x < cols; x++) {
unsigned src = SEGMENT.XY(x, y);
unsigned dst = SEGMENT.XY(x, y - 1);
SEGMENT.data[dst] = SEGMENT.data[src];
}

if (hw_random8() > turbulence) {
// create new sparks at bottom row
for (unsigned x = 0; x < cols; x++) {
uint8_t p = hw_random8();
if (p < spark_rate) {
unsigned dst = SEGMENT.XY(x, rows - 1);
SEGMENT.data[dst] = 255;
}
}
}

// diffuse
for (unsigned y = 0; y < rows; y++) {
for (unsigned x = 0; x < cols; x++) {
unsigned v = SEGMENT.data[SEGMENT.XY(x, y)];
if (x > 0) {
v += SEGMENT.data[SEGMENT.XY(x - 1, y)];
}
if (x < (cols - 1)) {
v += SEGMENT.data[SEGMENT.XY(x + 1, y)];
}
tmp_row[x] = min(255, (int)(v * 100 / (300 + diffusion)));
}

for (unsigned x = 0; x < cols; x++) {
SEGMENT.data[SEGMENT.XY(x, y)] = tmp_row[x];
if (SEGMENT.check1) {
CRGB color = ColorFromPalette(SEGPALETTE, tmp_row[x], 255, NOBLEND);
SEGMENT.setPixelColorXY(x, y, color);
} else {
uint32_t color = SEGCOLOR(0);
SEGMENT.setPixelColorXY(x, y, color_fade(color, tmp_row[x]));
}
}
}
}
return FRAMETIME;
}
static const char _data_FX_MODE_DIFFUSIONFIRE[] PROGMEM =
"Diffusion Fire@!,Spark rate,Diffusion Speed,Turbulence,,Use "
"palette;;Color;;2;pal=35";

class UserFxUsermod : public Usermod {
private:
public:
void setup() {
strip.addEffect(255, &mode_diffusionfire, _data_FX_MODE_DIFFUSIONFIRE);
}

void loop() {}

uint16_t getId() { return USERMOD_ID_USER_FX; }
};
2 changes: 2 additions & 0 deletions wled00/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@
#define USERMOD_ID_POV_DISPLAY 53 //Usermod "usermod_pov_display.h"
#define USERMOD_ID_PIXELS_DICE_TRAY 54 //Usermod "pixels_dice_tray.h"
#define USERMOD_ID_DEEP_SLEEP 55 //Usermod "usermod_deep_sleep.h"
#define USERMOD_ID_USER_FX 56 //Usermod "usermod_user_fx.h"


//Access point behavior
#define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot
Expand Down
9 changes: 8 additions & 1 deletion wled00/usermods_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,12 @@
#include "../usermods/LD2410_v2/usermod_ld2410.h"
#endif


#ifdef USERMOD_DEEP_SLEEP
#include "../usermods/deep_sleep/usermod_deep_sleep.h"
#endif

#ifdef USERMOD_USER_FX
#include "../usermods/user_fx/usermod_user_fx.h"
#endif

void registerUsermods()
Expand Down Expand Up @@ -479,4 +482,8 @@ void registerUsermods()
#ifdef USERMOD_DEEP_SLEEP
usermods.add(new DeepSleepUsermod());
#endif

#ifdef USERMOD_USER_FX
UsermodManager::add(new UserFxUsermod());
#endif
}