-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Added new effect usermod - Diffusion Fire #4667
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
Changes from 3 commits
14653f8
dc136bf
d54bb92
8f732c1
f101e4c
7abc5d2
9bc70b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "name:": "EXAMPLE", | ||
| "name": "EXAMPLE", | ||
| "dependencies": {} | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "name:": "sht", | ||
| "name": "sht", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert, it will be fixed with this pending PR: #4623 |
||
| "dependencies": { | ||
| "robtillaart/SHT85": "~0.3.3" | ||
| } | ||
| } | ||
| } | ||
| 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. | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some additional info as seen in other usermods. Also don't forget your contact information so users may ask for help. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "name:": "user_fx" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is what you want to fix |
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,102 @@ | ||||||||||||||||
| #include "user_fx.h" | ||||||||||||||||
|
|
||||||||||||||||
| #include <cstdint> | ||||||||||||||||
|
|
||||||||||||||||
| #define XY(_x, _y) (_x + SEG_W * (_y)) | ||||||||||||||||
|
|
||||||||||||||||
| 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; | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SEG_W and SEG_H are macros that obscure their meaning. It would be nice to have explanation of their meaning (in comment), i.e. "segment width in pixels" or something similar. |
||||||||||||||||
| 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 | ||||||||||||||||
|
|
||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid re-allocating the scratch buffer every frame
- if (!SEGENV.allocateData(dataSize))
+ if (SEGENV.data == nullptr && !SEGENV.allocateData(dataSize))
return mode_static(); // allocation failed📝 Committable suggestion
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is ok as allocateData() does its checks.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No description provided. |
||||||||||||||||
| if (SEGENV.call == 0) { | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add comment like "when |
||||||||||||||||
| SEGMENT.fill(BLACK); | ||||||||||||||||
| SEGENV.step = 0; | ||||||||||||||||
| call = 0; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if ((strip.now - SEGENV.step) >= refresh_ms) { | ||||||||||||||||
| // Reserve one extra byte and align to 2-byte boundary to avoid hard-faults | ||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you did not reserve the extra byte... |
||||||||||||||||
| uint8_t *bufStart = SEGMENT.data + SEGMENT.length(); | ||||||||||||||||
| uintptr_t aligned = (uintptr_t(bufStart) + 1u) & ~uintptr_t(0x1u); | ||||||||||||||||
| uint16_t *tmp_row = reinterpret_cast<uint16_t *>(aligned); | ||||||||||||||||
| SEGENV.step = strip.now; | ||||||||||||||||
| call++; | ||||||||||||||||
|
|
||||||||||||||||
| // scroll up | ||||||||||||||||
| for (unsigned y = 1; y < rows; y++) | ||||||||||||||||
| for (unsigned x = 0; x < cols; x++) { | ||||||||||||||||
| unsigned src = XY(x, y); | ||||||||||||||||
| unsigned dst = 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 = 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[XY(x, y)]; | ||||||||||||||||
| if (x > 0) { | ||||||||||||||||
| v += SEGMENT.data[XY(x - 1, y)]; | ||||||||||||||||
| } | ||||||||||||||||
| if (x < (cols - 1)) { | ||||||||||||||||
| v += SEGMENT.data[XY(x + 1, y)]; | ||||||||||||||||
| } | ||||||||||||||||
| tmp_row[x] = min(255, (int)(v * 100 / (300 + diffusion))); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| for (unsigned x = 0; x < cols; x++) { | ||||||||||||||||
| SEGMENT.data[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"; | ||||||||||||||||
|
|
||||||||||||||||
| void UserFxUsermod::setup() { | ||||||||||||||||
| strip.addEffect(255, &mode_diffusionfire, _data_FX_MODE_DIFFUSIONFIRE); | ||||||||||||||||
| } | ||||||||||||||||
| void UserFxUsermod::loop() {} | ||||||||||||||||
| uint16_t UserFxUsermod::getId() { return USERMOD_ID_USER_FX; } | ||||||||||||||||
|
|
||||||||||||||||
| static UserFxUsermod user_fx; | ||||||||||||||||
| REGISTER_USERMOD(user_fx); | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #pragma once | ||
| #include "wled.h" | ||
|
|
||
| class UserFxUsermod : public Usermod { | ||
| private: | ||
| public: | ||
| void setup(); | ||
|
|
||
| void loop(); | ||
|
|
||
| uint16_t getId(); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert, see #4623