|
| 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); |
0 commit comments